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