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