]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/editors/GenericNodePage.java
Deprecate TreeObject (not used anywhere anymore)
[lgpl/argeo-commons.git] / server / plugins / org.argeo.jcr.ui.explorer / src / main / java / org / argeo / jcr / ui / explorer / editors / GenericNodePage.java
1 package org.argeo.jcr.ui.explorer.editors;
2
3 import java.text.DateFormat;
4 import java.text.SimpleDateFormat;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.List;
8 import java.util.ListIterator;
9
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;
15
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;
32
33 /**
34 * Main node editor page. Lists all properties of the current node and enable
35 * access and editing for some of them.
36 */
37
38 public class GenericNodePage extends FormPage implements JcrExplorerConstants {
39 // private final static Log log = LogFactory.getLog(GenericNodePage.class);
40
41 // local constants
42 private final static String JCR_PROPERTY_NAME = "jcr:name";
43
44 // Utils
45 protected DateFormat timeFormatter = new SimpleDateFormat(DATE_TIME_FORMAT);
46
47 // Main business Objects
48 private Node currentNode;
49
50 // This page widgets
51 private FormToolkit tk;
52 private List<Control> modifyableProperties = new ArrayList<Control>();
53
54 public GenericNodePage(FormEditor editor, String title, Node currentNode) {
55 super(editor, "id", title);
56 this.currentNode = currentNode;
57 }
58
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;
64
65 form.getBody().setLayout(twt);
66 createPropertiesPart(form.getBody());
67 }
68
69 private void createPropertiesPart(Composite parent) {
70 try {
71
72 PropertyIterator pi = currentNode.getProperties();
73
74 // Initializes form part
75 AbstractFormPart part = new AbstractFormPart() {
76 public void commit(boolean onSave) {
77 try {
78 if (onSave) {
79 ListIterator<Control> it = modifyableProperties
80 .listIterator();
81 while (it.hasNext()) {
82 // we only support Text controls for the time
83 // being
84 Text curControl = (Text) it.next();
85 String value = curControl.getText();
86 currentNode.setProperty((String) curControl
87 .getData(JCR_PROPERTY_NAME), value);
88 }
89
90 // We only commit when onSave = true,
91 // thus it is still possible to save after a tab
92 // change.
93 super.commit(onSave);
94 }
95 } catch (RepositoryException re) {
96 throw new ArgeoException(
97 "Unexpected error while saving properties", re);
98 }
99 }
100 };
101
102 while (pi.hasNext()) {
103 Property prop = pi.nextProperty();
104 addPropertyLine(parent, part, prop);
105 }
106
107 getManagedForm().addPart(part);
108 } catch (RepositoryException re) {
109 throw new ArgeoException(
110 "Error during creation of network details section", re);
111 }
112
113 }
114
115 private void addPropertyLine(Composite parent, AbstractFormPart part,
116 Property prop) {
117 try {
118 tk.createLabel(parent, prop.getName());
119 tk.createLabel(parent,
120 "[" + JcrUtils.getPropertyDefinitionAsString(prop) + "]");
121
122 if (prop.getDefinition().isProtected()) {
123 tk.createLabel(parent, formatReadOnlyPropertyValue(prop));
124 } else
125 addModifyableValueWidget(parent, part, prop);
126 } catch (RepositoryException re) {
127 throw new ArgeoException("Cannot get property " + prop, re);
128 }
129 }
130
131 private String formatReadOnlyPropertyValue(Property prop) {
132 try {
133 String strValue;
134
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()
141 .getTime());
142 else
143 strValue = prop.getValue().getString();
144
145 return strValue;
146 } catch (RepositoryException re) {
147 throw new ArgeoException(
148 "Unexpected error while formatting read only property value",
149 re);
150 }
151 }
152
153 private Control addModifyableValueWidget(Composite parent,
154 AbstractFormPart part, Property prop) {
155 GridData gd;
156 try {
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);
164 } else {
165 // unsupported property type for editing, we create a read only
166 // label.
167 return tk
168 .createLabel(parent, formatReadOnlyPropertyValue(prop));
169 }
170 return null;
171 } catch (RepositoryException re) {
172 throw new ArgeoException(
173 "Unexpected error while formatting read only property value",
174 re);
175 }
176
177 }
178
179 //
180 // LISTENERS
181 //
182
183 private class ModifiedFieldListener implements ModifyListener {
184
185 private AbstractFormPart formPart;
186
187 public ModifiedFieldListener(AbstractFormPart generalPart) {
188 this.formPart = generalPart;
189 }
190
191 public void modifyText(ModifyEvent e) {
192 formPart.markDirty();
193 }
194 }
195
196 }