]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/viewers/Section.java
Adapt to changes in Argeo.
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / viewers / Section.java
1 package org.argeo.cms.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.CmsException;
11 import org.argeo.cms.util.CmsUtils;
12 import org.argeo.cms.widgets.JcrComposite;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.swt.widgets.Control;
16
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) throws RepositoryException {
25 this(parent, findSection(parent), style, node);
26 }
27
28 public Section(Section section, int style, Node node) throws RepositoryException {
29 this(section, section, style, node);
30 }
31
32 protected Section(Composite parent, Section parentSection, int style, Node node) throws RepositoryException {
33 super(parent, style, node);
34 this.parentSection = parentSection;
35 if (parentSection != null) {
36 relativeDepth = getNode().getDepth() - parentSection.getNode().getDepth();
37 } else {
38 relativeDepth = 0;
39 }
40 setLayout(CmsUtils.noSpaceGridLayout());
41 }
42
43 public Map<String, Section> getSubSections() throws RepositoryException {
44 LinkedHashMap<String, Section> result = new LinkedHashMap<String, Section>();
45 for (Control child : getChildren()) {
46 if (child instanceof Composite) {
47 collectDirectSubSections((Composite) child, result);
48 }
49 }
50 return Collections.unmodifiableMap(result);
51 }
52
53 private void collectDirectSubSections(Composite composite, LinkedHashMap<String, Section> subSections)
54 throws RepositoryException {
55 if (composite == sectionHeader || composite instanceof EditablePart)
56 return;
57 if (composite instanceof Section) {
58 Section section = (Section) composite;
59 subSections.put(section.getNodeId(), section);
60 return;
61 }
62
63 for (Control child : composite.getChildren())
64 if (child instanceof Composite)
65 collectDirectSubSections((Composite) child, subSections);
66 }
67
68 public void createHeader() {
69 if (sectionHeader != null)
70 throw new CmsException("Section header was already created");
71
72 sectionHeader = new Composite(this, SWT.NONE);
73 sectionHeader.setLayoutData(CmsUtils.fillWidth());
74 sectionHeader.setLayout(CmsUtils.noSpaceGridLayout());
75 // sectionHeader.moveAbove(null);
76 // layout();
77 }
78
79 public Composite getHeader() {
80 if (sectionHeader != null && sectionHeader.isDisposed())
81 sectionHeader = null;
82 return sectionHeader;
83 }
84
85 // SECTION PARTS
86 public SectionPart getSectionPart(String partId) {
87 for (Control child : getChildren()) {
88 if (child instanceof SectionPart) {
89 SectionPart paragraph = (SectionPart) child;
90 if (paragraph.getPartId().equals(partId))
91 return paragraph;
92 }
93 }
94 return null;
95 }
96
97 public SectionPart nextSectionPart(SectionPart sectionPart) {
98 Control[] children = getChildren();
99 for (int i = 0; i < children.length; i++) {
100 if (sectionPart == children[i])
101 if (i + 1 < children.length) {
102 Composite next = (Composite) children[i + 1];
103 return (SectionPart) next;
104 } else {
105 // next section
106 }
107 }
108 return null;
109 }
110
111 public SectionPart previousSectionPart(SectionPart sectionPart) {
112 Control[] children = getChildren();
113 for (int i = 0; i < children.length; i++) {
114 if (sectionPart == children[i])
115 if (i != 0) {
116 Composite previous = (Composite) children[i - 1];
117 return (SectionPart) previous;
118 } else {
119 // previous section
120 }
121 }
122 return null;
123 }
124
125 @Override
126 public String toString() {
127 if (parentSection == null)
128 return "Main section " + getNode();
129 return "Section " + getNode();
130 }
131
132 public Section getParentSection() {
133 return parentSection;
134 }
135
136 public Integer getRelativeDepth() {
137 return relativeDepth;
138 }
139
140 /** Recursively finds the related section in the parents (can be itself) */
141 public static Section findSection(Control control) {
142 if (control == null)
143 return null;
144 if (control instanceof Section)
145 return (Section) control;
146 else
147 return findSection(control.getParent());
148 }
149 }