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