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