]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - argeo/cms/ui/viewers/Section.java
1 package org
.argeo
.cms
.ui
.viewers
;
3 import java
.util
.Collections
;
4 import java
.util
.LinkedHashMap
;
8 import javax
.jcr
.RepositoryException
;
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
;
16 public class Section
extends JcrComposite
{
17 private static final long serialVersionUID
= -5933796173755739207L;
19 private final Section parentSection
;
20 private Composite sectionHeader
;
21 private final Integer relativeDepth
;
23 public Section(Composite parent
, int style
, Node node
) {
24 this(parent
, findSection(parent
), style
, node
);
27 public Section(Section section
, int style
, Node node
) {
28 this(section
, section
, style
, node
);
31 protected Section(Composite parent
, Section parentSection
, int style
, Node node
) {
32 super(parent
, style
, node
);
34 this.parentSection
= parentSection
;
35 if (parentSection
!= null) {
36 relativeDepth
= getNode().getDepth() - parentSection
.getNode().getDepth();
40 setLayout(CmsUiUtils
.noSpaceGridLayout());
41 } catch (RepositoryException e
) {
42 throw new IllegalStateException("Cannot create section from " + node
, e
);
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
);
53 return Collections
.unmodifiableMap(result
);
56 private void collectDirectSubSections(Composite composite
, LinkedHashMap
<String
, Section
> subSections
)
57 throws RepositoryException
{
58 if (composite
== sectionHeader
|| composite
instanceof EditablePart
)
60 if (composite
instanceof Section
) {
61 Section section
= (Section
) composite
;
62 subSections
.put(section
.getNodeId(), section
);
66 for (Control child
: composite
.getChildren())
67 if (child
instanceof Composite
)
68 collectDirectSubSections((Composite
) child
, subSections
);
71 public Composite
createHeader() {
72 return createHeader(this);
75 public Composite
createHeader(Composite parent
) {
76 if (sectionHeader
!= null)
77 sectionHeader
.dispose();
79 sectionHeader
= new Composite(parent
, SWT
.NONE
);
80 sectionHeader
.setLayoutData(CmsUiUtils
.fillWidth());
81 sectionHeader
.setLayout(CmsUiUtils
.noSpaceGridLayout());
82 // sectionHeader.moveAbove(null);
87 public Composite
getHeader() {
88 if (sectionHeader
!= null && sectionHeader
.isDisposed())
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
))
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
;
119 public SectionPart
previousSectionPart(SectionPart sectionPart
) {
120 Control
[] children
= getChildren();
121 for (int i
= 0; i
< children
.length
; i
++) {
122 if (sectionPart
== children
[i
])
124 Composite previous
= (Composite
) children
[i
- 1];
125 return (SectionPart
) previous
;
134 public String
toString() {
135 if (parentSection
== null)
136 return "Main section " + getNode();
137 return "Section " + getNode();
140 public Section
getParentSection() {
141 return parentSection
;
144 public Integer
getRelativeDepth() {
145 return relativeDepth
;
148 /** Recursively finds the related section in the parents (can be itself) */
149 public static Section
findSection(Control control
) {
152 if (control
instanceof Section
)
153 return (Section
) control
;
155 return findSection(control
.getParent());