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
.argeo
.ArgeoException
;
17 import org
.argeo
.jcr
.JcrUtils
;
18 import org
.argeo
.jcr
.ui
.explorer
.JcrExplorerConstants
;
19 import org
.eclipse
.swt
.events
.ModifyEvent
;
20 import org
.eclipse
.swt
.events
.ModifyListener
;
21 import org
.eclipse
.swt
.layout
.GridData
;
22 import org
.eclipse
.swt
.layout
.GridLayout
;
23 import org
.eclipse
.swt
.widgets
.Composite
;
24 import org
.eclipse
.swt
.widgets
.Control
;
25 import org
.eclipse
.swt
.widgets
.Text
;
26 import org
.eclipse
.ui
.forms
.AbstractFormPart
;
27 import org
.eclipse
.ui
.forms
.IManagedForm
;
28 import org
.eclipse
.ui
.forms
.editor
.FormEditor
;
29 import org
.eclipse
.ui
.forms
.editor
.FormPage
;
30 import org
.eclipse
.ui
.forms
.widgets
.FormToolkit
;
31 import org
.eclipse
.ui
.forms
.widgets
.ScrolledForm
;
34 * Main node editor page. Lists all properties of the current node and enable
35 * access and editing for some of them.
38 public class GenericNodePage
extends FormPage
implements JcrExplorerConstants
{
39 // private final static Log log = LogFactory.getLog(GenericNodePage.class);
42 private final static String JCR_PROPERTY_NAME
= "jcr:name";
45 protected DateFormat timeFormatter
= new SimpleDateFormat(DATE_TIME_FORMAT
);
47 // Main business Objects
48 private Node currentNode
;
51 private FormToolkit tk
;
52 private List
<Control
> modifyableProperties
= new ArrayList
<Control
>();
54 public GenericNodePage(FormEditor editor
, String title
, Node currentNode
) {
55 super(editor
, "id", title
);
56 this.currentNode
= currentNode
;
59 protected void createFormContent(IManagedForm managedForm
) {
60 tk
= managedForm
.getToolkit();
61 ScrolledForm form
= managedForm
.getForm();
62 GridLayout twt
= new GridLayout(3, false);
63 twt
.marginWidth
= twt
.marginHeight
= 5;
65 form
.getBody().setLayout(twt
);
66 createPropertiesPart(form
.getBody());
69 private void createPropertiesPart(Composite parent
) {
72 PropertyIterator pi
= currentNode
.getProperties();
74 // Initializes form part
75 AbstractFormPart part
= new AbstractFormPart() {
76 public void commit(boolean onSave
) {
79 ListIterator
<Control
> it
= modifyableProperties
81 while (it
.hasNext()) {
82 // we only support Text controls for the time
84 Text curControl
= (Text
) it
.next();
85 String value
= curControl
.getText();
86 currentNode
.setProperty((String
) curControl
87 .getData(JCR_PROPERTY_NAME
), value
);
90 // We only commit when onSave = true,
91 // thus it is still possible to save after a tab
95 } catch (RepositoryException re
) {
96 throw new ArgeoException(
97 "Unexpected error while saving properties", re
);
102 while (pi
.hasNext()) {
103 Property prop
= pi
.nextProperty();
104 addPropertyLine(parent
, part
, prop
);
107 getManagedForm().addPart(part
);
108 } catch (RepositoryException re
) {
109 throw new ArgeoException(
110 "Error during creation of network details section", re
);
115 private void addPropertyLine(Composite parent
, AbstractFormPart part
,
118 tk
.createLabel(parent
, prop
.getName());
119 tk
.createLabel(parent
,
120 "[" + JcrUtils
.getPropertyDefinitionAsString(prop
) + "]");
122 if (prop
.getDefinition().isProtected()) {
123 tk
.createLabel(parent
, formatReadOnlyPropertyValue(prop
));
125 addModifyableValueWidget(parent
, part
, prop
);
126 } catch (RepositoryException re
) {
127 throw new ArgeoException("Cannot get property " + prop
, re
);
131 private String
formatReadOnlyPropertyValue(Property prop
) {
135 if (prop
.getType() == PropertyType
.BINARY
)
136 strValue
= "<binary>";
137 else if (prop
.isMultiple())
138 strValue
= Arrays
.asList(prop
.getValues()).toString();
139 else if (prop
.getType() == PropertyType
.DATE
)
140 strValue
= timeFormatter
.format(prop
.getValue().getDate()
143 strValue
= prop
.getValue().getString();
146 } catch (RepositoryException re
) {
147 throw new ArgeoException(
148 "Unexpected error while formatting read only property value",
153 private Control
addModifyableValueWidget(Composite parent
,
154 AbstractFormPart part
, Property prop
) {
157 if (prop
.getType() == PropertyType
.STRING
) {
158 Text txt
= tk
.createText(parent
, prop
.getString());
159 gd
= new GridData(GridData
.FILL_HORIZONTAL
);
160 txt
.setLayoutData(gd
);
161 txt
.addModifyListener(new ModifiedFieldListener(part
));
162 txt
.setData(JCR_PROPERTY_NAME
, prop
.getName());
163 modifyableProperties
.add(txt
);
165 // unsupported property type for editing, we create a read only
168 .createLabel(parent
, formatReadOnlyPropertyValue(prop
));
171 } catch (RepositoryException re
) {
172 throw new ArgeoException(
173 "Unexpected error while formatting read only property value",
183 private class ModifiedFieldListener
implements ModifyListener
{
185 private AbstractFormPart formPart
;
187 public ModifiedFieldListener(AbstractFormPart generalPart
) {
188 this.formPart
= generalPart
;
191 public void modifyText(ModifyEvent e
) {
192 formPart
.markDirty();