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