]> 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/GenericPropertyPage.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 / GenericPropertyPage.java
1 package org.argeo.jcr.ui.explorer.editors;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.jcr.Node;
7 import javax.jcr.Property;
8 import javax.jcr.PropertyIterator;
9 import javax.jcr.RepositoryException;
10 import javax.jcr.Value;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.argeo.ArgeoException;
15 import org.argeo.jcr.ui.explorer.JcrExplorerConstants;
16 import org.argeo.jcr.ui.explorer.JcrExplorerPlugin;
17 import org.argeo.jcr.ui.explorer.providers.PropertyLabelProvider;
18 import org.eclipse.jface.viewers.ITreeContentProvider;
19 import org.eclipse.jface.viewers.TreeViewer;
20 import org.eclipse.jface.viewers.Viewer;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.layout.FillLayout;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Tree;
25 import org.eclipse.swt.widgets.TreeColumn;
26 import org.eclipse.ui.forms.IManagedForm;
27 import org.eclipse.ui.forms.editor.FormEditor;
28 import org.eclipse.ui.forms.editor.FormPage;
29 import org.eclipse.ui.forms.widgets.FormToolkit;
30 import org.eclipse.ui.forms.widgets.ScrolledForm;
31
32 /**
33 * Generic editor property page. Lists all properties of current node as a
34 * complex tree. TODO: enable editing
35 */
36
37 public class GenericPropertyPage extends FormPage implements
38 JcrExplorerConstants {
39 private final static Log log = LogFactory.getLog(GenericPropertyPage.class);
40
41 // local constants
42 private final static String JCR_PROPERTY_NAME = "jcr:name";
43
44 // Main business Objects
45 private Node currentNode;
46
47 // This page widgets
48 private FormToolkit tk;
49 private TreeViewer complexTree;
50
51 public GenericPropertyPage(FormEditor editor, String title, Node currentNode) {
52 super(editor, "id", title);
53 this.currentNode = currentNode;
54 }
55
56 protected void createFormContent(IManagedForm managedForm) {
57 tk = managedForm.getToolkit();
58 ScrolledForm form = managedForm.getForm();
59 form.setText(JcrExplorerPlugin
60 .getMessage("genericNodePageTitle"));
61 FillLayout layout = new FillLayout();
62 layout.marginHeight = 5;
63 layout.marginWidth = 5;
64 form.getBody().setLayout(layout);
65
66 complexTree = createComplexTree(form.getBody());
67
68 // TODO remove following
69 // createPropertiesPart(form.getBody());
70 }
71
72 private TreeViewer createComplexTree(Composite parent) {
73 int style = SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION;
74 Tree tree = new Tree(parent, style);
75 createColumn(tree, "Property", SWT.LEFT, 200);
76 createColumn(tree, "Value(s)", SWT.LEFT, 300);
77 createColumn(tree, "Attributes", SWT.LEFT, 65);
78 tree.setLinesVisible(true);
79 tree.setHeaderVisible(true);
80
81 TreeViewer result = new TreeViewer(tree);
82 result.setContentProvider(new TreeContentProvider());
83 result.setLabelProvider(new PropertyLabelProvider());
84 result.setInput(currentNode);
85 result.expandAll();
86 return result;
87 }
88
89 private static TreeColumn createColumn(Tree parent, String name, int style,
90 int width) {
91 TreeColumn result = new TreeColumn(parent, style);
92 result.setText(name);
93 result.setWidth(width);
94 result.setMoveable(true);
95 result.setResizable(true);
96 return result;
97 }
98
99 //
100 // private void createPropertiesPart(Composite parent) {
101 // try {
102 //
103 // PropertyIterator pi = currentNode.getProperties();
104 //
105 // // Initializes form part
106 // AbstractFormPart part = new AbstractFormPart() {
107 // public void commit(boolean onSave) {
108 // try {
109 // if (onSave) {
110 // ListIterator<Control> it = modifyableProperties
111 // .listIterator();
112 // while (it.hasNext()) {
113 // // we only support Text controls for the time
114 // // being
115 // Text curControl = (Text) it.next();
116 // String value = curControl.getText();
117 // currentNode.setProperty((String) curControl
118 // .getData(JCR_PROPERTY_NAME), value);
119 // }
120 //
121 // // We only commit when onSave = true,
122 // // thus it is still possible to save after a tab
123 // // change.
124 // super.commit(onSave);
125 // }
126 // } catch (RepositoryException re) {
127 // throw new ArgeoException(
128 // "Unexpected error while saving properties", re);
129 // }
130 // }
131 // };
132 //
133 // while (pi.hasNext()) {
134 // Property prop = pi.nextProperty();
135 // addPropertyLine(parent, part, prop);
136 // }
137 //
138 // getManagedForm().addPart(part);
139 // } catch (RepositoryException re) {
140 // throw new ArgeoException(
141 // "Error during creation of network details section", re);
142 // }
143 //
144 // }
145 //
146 // private void addPropertyLine(Composite parent, AbstractFormPart part,
147 // Property prop) {
148 // try {
149 // tk.createLabel(parent, prop.getName());
150 // tk.createLabel(parent,
151 // "[" + JcrUtils.getPropertyDefinitionAsString(prop) + "]");
152 //
153 // if (prop.getDefinition().isProtected()) {
154 // tk.createLabel(parent, formatReadOnlyPropertyValue(prop));
155 // } else
156 // addModifyableValueWidget(parent, part, prop);
157 // } catch (RepositoryException re) {
158 // throw new ArgeoException("Cannot get property " + prop, re);
159 // }
160 // }
161 //
162 // private String formatReadOnlyPropertyValue(Property prop) {
163 // try {
164 // String strValue;
165 //
166 // if (prop.getType() == PropertyType.BINARY)
167 // strValue = "<binary>";
168 // else if (prop.isMultiple())
169 // strValue = Arrays.asList(prop.getValues()).toString();
170 // else if (prop.getType() == PropertyType.DATE)
171 // strValue = timeFormatter.format(prop.getValue().getDate()
172 // .getTime());
173 // else
174 // strValue = prop.getValue().getString();
175 //
176 // return strValue;
177 // } catch (RepositoryException re) {
178 // throw new ArgeoException(
179 // "Unexpected error while formatting read only property value",
180 // re);
181 // }
182 // }
183 //
184 // private Control addModifyableValueWidget(Composite parent,
185 // AbstractFormPart part, Property prop) {
186 // GridData gd;
187 // try {
188 // if (prop.getType() == PropertyType.STRING) {
189 // Text txt = tk.createText(parent, prop.getString());
190 // gd = new GridData(GridData.FILL_HORIZONTAL);
191 // txt.setLayoutData(gd);
192 // txt.addModifyListener(new ModifiedFieldListener(part));
193 // txt.setData(JCR_PROPERTY_NAME, prop.getName());
194 // modifyableProperties.add(txt);
195 // } else {
196 // // unsupported property type for editing, we create a read only
197 // // label.
198 // return tk
199 // .createLabel(parent, formatReadOnlyPropertyValue(prop));
200 // }
201 // return null;
202 // } catch (RepositoryException re) {
203 // throw new ArgeoException(
204 // "Unexpected error while formatting read only property value",
205 // re);
206 // }
207 //
208 // }
209
210 // Multiple Value Model
211 // protected class MultipleValueItem {
212 // private int index;
213 // private Value value;
214 //
215 // public MultipleValueItem(int index, Value value) {
216 // this.index = index;
217 // this.value = value;
218 // }
219 //
220 // public int getIndex() {
221 // return index;
222 // }
223 //
224 // public Object getValue() {
225 // return value;
226 // }
227 // }
228
229 private class TreeContentProvider implements ITreeContentProvider {
230 public Object[] getElements(Object parent) {
231 Object[] props = null;
232 try {
233
234 if (parent instanceof Node) {
235 Node node = (Node) parent;
236 PropertyIterator pi;
237 pi = node.getProperties();
238 List<Property> propList = new ArrayList<Property>();
239 while (pi.hasNext()) {
240 propList.add(pi.nextProperty());
241 }
242 props = propList.toArray();
243 }
244 } catch (RepositoryException e) {
245 throw new ArgeoException(
246 "Unexpected exception while listing node properties", e);
247 }
248 return props;
249 }
250
251 public Object getParent(Object child) {
252 return null;
253 }
254
255 public Object[] getChildren(Object parent) {
256 Object[] result = null;
257 if (parent instanceof Property) {
258 Property prop = (Property) parent;
259 try {
260
261 if (prop.isMultiple()) {
262 Value[] values = prop.getValues();
263 // List<MultipleValueItem> list = new
264 // ArrayList<MultipleValueItem>();
265 // for (int i = 0; i < values.length; i++) {
266 // MultipleValueItem mvi = new MultipleValueItem(i,
267 // values[i]);
268 // list.add(mvi);
269 // }
270
271 return values;
272 }
273 } catch (RepositoryException e) {
274 throw new ArgeoException(
275 "Unexpected error getting multiple values property.",
276 e);
277 }
278 }
279 return result;
280 }
281
282 public boolean hasChildren(Object parent) {
283 try {
284 if (parent instanceof Property
285 && ((Property) parent).isMultiple()) {
286 return true;
287 }
288 } catch (RepositoryException e) {
289 throw new ArgeoException(
290 "Unexpected exception while checking if property is multiple",
291 e);
292 }
293 return false;
294 }
295
296 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
297 }
298
299 public void dispose() {
300 }
301 }
302 }