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