]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/viewers/Section.java
[maven-release-plugin] prepare release argeo-commons-2.1.84
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / viewers / Section.java
1 package org.argeo.cms.viewers;
2
3 import java.util.Collections;
4 import java.util.LinkedHashMap;
5 import java.util.Map;
6
7 import javax.jcr.Node;
8 import javax.jcr.RepositoryException;
9
10 import org.argeo.cms.CmsException;
11 import org.argeo.cms.CmsNames;
12 import org.argeo.cms.util.CmsUtils;
13 import org.argeo.cms.widgets.JcrComposite;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Control;
17
18 public class Section extends JcrComposite implements CmsNames {
19 private static final long serialVersionUID = -5933796173755739207L;
20
21 private final Section parentSection;
22 private Composite sectionHeader;
23 private final Integer relativeDepth;
24
25 public Section(Composite parent, int style, Node node)
26 throws RepositoryException {
27 this(parent, findSection(parent), style, node);
28 }
29
30 public Section(Section section, int style, Node node)
31 throws RepositoryException {
32 this(section, section, style, node);
33 }
34
35 protected Section(Composite parent, Section parentSection, int style,
36 Node node) throws RepositoryException {
37 super(parent, style, node);
38 this.parentSection = parentSection;
39 if (parentSection != null) {
40 relativeDepth = getNode().getDepth()
41 - parentSection.getNode().getDepth();
42 } else {
43 relativeDepth = 0;
44 }
45 setLayout(CmsUtils.noSpaceGridLayout());
46 }
47
48 public Map<String, Section> getSubSections() throws RepositoryException {
49 LinkedHashMap<String, Section> result = new LinkedHashMap<String, Section>();
50 for (Control child : getChildren()) {
51 if (child instanceof Composite) {
52 collectDirectSubSections((Composite) child, result);
53 }
54 }
55 return Collections.unmodifiableMap(result);
56 }
57
58 private void collectDirectSubSections(Composite composite,
59 LinkedHashMap<String, Section> subSections)
60 throws RepositoryException {
61 if (composite == sectionHeader || composite instanceof EditablePart)
62 return;
63 if (composite instanceof Section) {
64 Section section = (Section) composite;
65 subSections.put(section.getNodeId(), section);
66 return;
67 }
68
69 for (Control child : composite.getChildren())
70 if (child instanceof Composite)
71 collectDirectSubSections((Composite) child, subSections);
72 }
73
74 public void createHeader() {
75 if (sectionHeader != null)
76 throw new CmsException("Section header was already created");
77
78 sectionHeader = new Composite(this, SWT.NONE);
79 sectionHeader.setLayoutData(CmsUtils.fillWidth());
80 sectionHeader.setLayout(CmsUtils.noSpaceGridLayout());
81 // sectionHeader.moveAbove(null);
82 // layout();
83 }
84
85 public Composite getHeader() {
86 if (sectionHeader != null && sectionHeader.isDisposed())
87 sectionHeader = null;
88 return sectionHeader;
89 }
90
91 // SECTION PARTS
92 public SectionPart getSectionPart(String partId) {
93 for (Control child : getChildren()) {
94 if (child instanceof SectionPart) {
95 SectionPart paragraph = (SectionPart) child;
96 if (paragraph.getPartId().equals(partId))
97 return paragraph;
98 }
99 }
100 return null;
101 }
102
103 public SectionPart nextSectionPart(SectionPart sectionPart) {
104 Control[] children = getChildren();
105 for (int i = 0; i < children.length; i++) {
106 if (sectionPart == children[i])
107 if (i + 1 < children.length) {
108 Composite next = (Composite) children[i + 1];
109 return (SectionPart) next;
110 } else {
111 // next section
112 }
113 }
114 return null;
115 }
116
117 public SectionPart previousSectionPart(SectionPart sectionPart) {
118 Control[] children = getChildren();
119 for (int i = 0; i < children.length; i++) {
120 if (sectionPart == children[i])
121 if (i != 0) {
122 Composite previous = (Composite) children[i - 1];
123 return (SectionPart) previous;
124 } else {
125 // previous section
126 }
127 }
128 return null;
129 }
130
131 @Override
132 public String toString() {
133 if (parentSection == null)
134 return "Main section " + getNode();
135 return "Section " + getNode();
136 }
137
138 public Section getParentSection() {
139 return parentSection;
140 }
141
142 public Integer getRelativeDepth() {
143 return relativeDepth;
144 }
145
146 /** Recursively finds the related section in the parents (can be itself) */
147 public static Section findSection(Control control) {
148 if (control == null)
149 return null;
150 if (control instanceof Section)
151 return (Section) control;
152 else
153 return findSection(control.getParent());
154 }
155 }