1 package org
.argeo
.jcr
.ui
.explorer
.editors
;
3 import java
.text
.DateFormat
;
4 import java
.text
.SimpleDateFormat
;
5 import java
.util
.ArrayList
;
6 import java
.util
.Arrays
;
8 import java
.util
.ListIterator
;
10 import javax
.jcr
.Node
;
11 import javax
.jcr
.Property
;
12 import javax
.jcr
.PropertyIterator
;
13 import javax
.jcr
.PropertyType
;
14 import javax
.jcr
.RepositoryException
;
16 import org
.apache
.commons
.logging
.Log
;
17 import org
.apache
.commons
.logging
.LogFactory
;
18 import org
.argeo
.ArgeoException
;
19 import org
.argeo
.jcr
.JcrUtils
;
20 import org
.argeo
.jcr
.ui
.explorer
.JcrExplorerConstants
;
21 import org
.eclipse
.swt
.events
.ModifyEvent
;
22 import org
.eclipse
.swt
.events
.ModifyListener
;
23 import org
.eclipse
.swt
.layout
.GridData
;
24 import org
.eclipse
.swt
.layout
.GridLayout
;
25 import org
.eclipse
.swt
.widgets
.Composite
;
26 import org
.eclipse
.swt
.widgets
.Control
;
27 import org
.eclipse
.swt
.widgets
.Label
;
28 import org
.eclipse
.swt
.widgets
.Text
;
29 import org
.eclipse
.ui
.forms
.AbstractFormPart
;
30 import org
.eclipse
.ui
.forms
.IManagedForm
;
31 import org
.eclipse
.ui
.forms
.editor
.FormEditor
;
32 import org
.eclipse
.ui
.forms
.editor
.FormPage
;
33 import org
.eclipse
.ui
.forms
.widgets
.FormToolkit
;
34 import org
.eclipse
.ui
.forms
.widgets
.ScrolledForm
;
37 * Main node editor page. Lists all properties of the current node and enable
38 * access and editing for some of them.
41 public class GenericNodePage
extends FormPage
implements JcrExplorerConstants
{
42 private final static Log log
= LogFactory
.getLog(GenericNodePage
.class);
45 private final static String JCR_PROPERTY_NAME
= "jcr:name";
48 protected DateFormat timeFormatter
= new SimpleDateFormat(DATE_TIME_FORMAT
);
50 // Main business Objects
51 private Node currentNode
;
54 private FormToolkit tk
;
55 private List
<Control
> modifyableProperties
= new ArrayList
<Control
>();
57 public GenericNodePage(FormEditor editor
, String title
, Node currentNode
) {
58 super(editor
, "id", title
);
59 this.currentNode
= currentNode
;
62 protected void createFormContent(IManagedForm managedForm
) {
63 tk
= managedForm
.getToolkit();
64 ScrolledForm form
= managedForm
.getForm();
65 GridLayout twt
= new GridLayout(3, false);
66 twt
.marginWidth
= twt
.marginHeight
= 5;
68 form
.getBody().setLayout(twt
);
69 createPropertiesPart(form
.getBody());
72 private void createPropertiesPart(Composite parent
) {
75 PropertyIterator pi
= currentNode
.getProperties();
77 // Initializes form part
78 AbstractFormPart part
= new AbstractFormPart() {
79 public void commit(boolean onSave
) {
82 ListIterator
<Control
> it
= modifyableProperties
84 while (it
.hasNext()) {
85 // we only support Text controls for the time
87 Text curControl
= (Text
) it
.next();
88 String value
= curControl
.getText();
89 currentNode
.setProperty((String
) curControl
90 .getData(JCR_PROPERTY_NAME
), value
);
93 // We only commit when onSave = true,
94 // thus it is still possible to save after a tab
98 } catch (RepositoryException re
) {
99 throw new ArgeoException(
100 "Unexpected error while saving properties", re
);
105 while (pi
.hasNext()) {
106 Property prop
= pi
.nextProperty();
107 addPropertyLine(parent
, part
, prop
);
110 getManagedForm().addPart(part
);
111 } catch (RepositoryException re
) {
112 throw new ArgeoException(
113 "Error during creation of network details section", re
);
118 private void addPropertyLine(Composite parent
, AbstractFormPart part
,
121 Label lbl
= tk
.createLabel(parent
, prop
.getName());
122 lbl
= tk
.createLabel(parent
,
123 "[" + JcrUtils
.getPropertyDefinitionAsString(prop
) + "]");
125 if (prop
.getDefinition().isProtected()) {
126 lbl
= tk
.createLabel(parent
, formatReadOnlyPropertyValue(prop
));
128 addModifyableValueWidget(parent
, part
, prop
);
129 } catch (RepositoryException re
) {
130 throw new ArgeoException("Cannot get property " + prop
, re
);
134 private String
formatReadOnlyPropertyValue(Property prop
) {
138 if (prop
.getType() == PropertyType
.BINARY
)
139 strValue
= "<binary>";
140 else if (prop
.isMultiple())
141 strValue
= Arrays
.asList(prop
.getValues()).toString();
142 else if (prop
.getType() == PropertyType
.DATE
)
143 strValue
= timeFormatter
.format(prop
.getValue().getDate()
146 strValue
= prop
.getValue().getString();
149 } catch (RepositoryException re
) {
150 throw new ArgeoException(
151 "Unexpected error while formatting read only property value",
156 private Control
addModifyableValueWidget(Composite parent
,
157 AbstractFormPart part
, Property prop
) {
160 if (prop
.getType() == PropertyType
.STRING
) {
161 Text txt
= tk
.createText(parent
, prop
.getString());
162 gd
= new GridData(GridData
.FILL_HORIZONTAL
);
163 txt
.setLayoutData(gd
);
164 txt
.addModifyListener(new ModifiedFieldListener(part
));
165 txt
.setData(JCR_PROPERTY_NAME
, prop
.getName());
166 modifyableProperties
.add(txt
);
168 // unsupported property type for editing, we create a read only
171 .createLabel(parent
, formatReadOnlyPropertyValue(prop
));
174 } catch (RepositoryException re
) {
175 throw new ArgeoException(
176 "Unexpected error while formatting read only property value",
186 private class ModifiedFieldListener
implements ModifyListener
{
188 private AbstractFormPart formPart
;
190 public ModifiedFieldListener(AbstractFormPart generalPart
) {
191 this.formPart
= generalPart
;
194 public void modifyText(ModifyEvent e
) {
195 formPart
.markDirty();