]> git.argeo.org Git - lgpl/argeo-commons.git/blob - ui/viewers/Section.java
Prepare next development cycle
[lgpl/argeo-commons.git] / 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.ui.util.CmsUiUtils;
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(CmsUiUtils.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(CmsUiUtils.fillWidth());
82 sectionHeader.setLayout(CmsUiUtils.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 if (i + 1 < children.length) {
111 Composite next = (Composite) children[i + 1];
112 return (SectionPart) next;
113 } else {
114 // next section
115 }
116 }
117 return null;
118 }
119
120 public SectionPart previousSectionPart(SectionPart 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 (SectionPart) 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 " + getNode();
138 return "Section " + getNode();
139 }
140
141 public Section 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 Section findSection(Control control) {
151 if (control == null)
152 return null;
153 if (control instanceof Section)
154 return (Section) control;
155 else
156 return findSection(control.getParent());
157 }
158 }