]> git.argeo.org Git - gpl/argeo-jcr.git/blob - swt/org.argeo.cms.jcr.ui/src/org/argeo/cms/ui/viewers/Section.java
Use simple versioning
[gpl/argeo-jcr.git] / swt / org.argeo.cms.jcr.ui / src / org / argeo / cms / ui / viewers / Section.java
1 package org.argeo.cms.ui.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.swt.CmsSwtUtils;
11 import org.argeo.cms.swt.SwtEditablePart;
12 import org.argeo.cms.ui.widgets.JcrComposite;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.swt.widgets.Control;
16
17 /** A structured UI related to a JCR context. */
18 public class Section extends JcrComposite {
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 this(parent, findSection(parent), style, node);
27 }
28
29 public Section(Section section, int style, Node node) {
30 this(section, section, style, node);
31 }
32
33 protected Section(Composite parent, Section parentSection, int style, Node node) {
34 super(parent, style, node);
35 try {
36 this.parentSection = parentSection;
37 if (parentSection != null) {
38 relativeDepth = getNode().getDepth() - parentSection.getNode().getDepth();
39 } else {
40 relativeDepth = 0;
41 }
42 setLayout(CmsSwtUtils.noSpaceGridLayout());
43 } catch (RepositoryException e) {
44 throw new IllegalStateException("Cannot create section from " + node, e);
45 }
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, LinkedHashMap<String, Section> subSections)
59 throws RepositoryException {
60 if (composite == sectionHeader || composite instanceof SwtEditablePart)
61 return;
62 if (composite instanceof Section) {
63 Section section = (Section) composite;
64 subSections.put(section.getNodeId(), section);
65 return;
66 }
67
68 for (Control child : composite.getChildren())
69 if (child instanceof Composite)
70 collectDirectSubSections((Composite) child, subSections);
71 }
72
73 public Composite createHeader() {
74 return createHeader(this);
75 }
76
77 public Composite createHeader(Composite parent) {
78 if (sectionHeader != null)
79 sectionHeader.dispose();
80
81 sectionHeader = new Composite(parent, SWT.NONE);
82 sectionHeader.setLayoutData(CmsSwtUtils.fillWidth());
83 sectionHeader.setLayout(CmsSwtUtils.noSpaceGridLayout());
84 // sectionHeader.moveAbove(null);
85 // layout();
86 return sectionHeader;
87 }
88
89 public Composite getHeader() {
90 if (sectionHeader != null && sectionHeader.isDisposed())
91 sectionHeader = null;
92 return sectionHeader;
93 }
94
95 // SECTION PARTS
96 public SectionPart getSectionPart(String partId) {
97 for (Control child : getChildren()) {
98 if (child instanceof SectionPart) {
99 SectionPart sectionPart = (SectionPart) child;
100 if (sectionPart.getPartId().equals(partId))
101 return sectionPart;
102 }
103 }
104 return null;
105 }
106
107 public SectionPart nextSectionPart(SectionPart sectionPart) {
108 Control[] children = getChildren();
109 for (int i = 0; i < children.length; i++) {
110 if (sectionPart == children[i]) {
111 for (int j = i + 1; j < children.length; j++) {
112 if (children[i + 1] instanceof SectionPart) {
113 return (SectionPart) children[i + 1];
114 }
115 }
116
117 // if (i + 1 < children.length) {
118 // Composite next = (Composite) children[i + 1];
119 // return (SectionPart) next;
120 // } else {
121 // // next section
122 // }
123 }
124 }
125 return null;
126 }
127
128 public SectionPart previousSectionPart(SectionPart sectionPart) {
129 Control[] children = getChildren();
130 for (int i = 0; i < children.length; i++) {
131 if (sectionPart == children[i])
132 if (i != 0) {
133 Composite previous = (Composite) children[i - 1];
134 return (SectionPart) previous;
135 } else {
136 // previous section
137 }
138 }
139 return null;
140 }
141
142 @Override
143 public String toString() {
144 if (parentSection == null)
145 return "Main section " + getNode();
146 return "Section " + getNode();
147 }
148
149 public Section getParentSection() {
150 return parentSection;
151 }
152
153 public Integer getRelativeDepth() {
154 return relativeDepth;
155 }
156
157 /** Recursively finds the related section in the parents (can be itself) */
158 public static Section findSection(Control control) {
159 if (control == null)
160 return null;
161 if (control instanceof Section)
162 return (Section) control;
163 else
164 return findSection(control.getParent());
165 }
166 }