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