]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/jcr/internal/parts/GenericNodePage.java
Enhance privilege display to ease JCR privilege management
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui.workbench / src / org / argeo / eclipse / ui / workbench / jcr / internal / parts / GenericNodePage.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.jcr.internal.parts;
17
18 import java.text.DateFormat;
19 import java.text.SimpleDateFormat;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.List;
23 import java.util.ListIterator;
24
25 import javax.jcr.Node;
26 import javax.jcr.Property;
27 import javax.jcr.PropertyIterator;
28 import javax.jcr.PropertyType;
29 import javax.jcr.RepositoryException;
30
31 import org.argeo.ArgeoException;
32 import org.argeo.eclipse.ui.workbench.WorkbenchConstants;
33 import org.argeo.jcr.JcrUtils;
34 import org.eclipse.swt.events.ModifyEvent;
35 import org.eclipse.swt.events.ModifyListener;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Control;
40 import org.eclipse.swt.widgets.Text;
41 import org.eclipse.ui.forms.AbstractFormPart;
42 import org.eclipse.ui.forms.IManagedForm;
43 import org.eclipse.ui.forms.editor.FormEditor;
44 import org.eclipse.ui.forms.editor.FormPage;
45 import org.eclipse.ui.forms.widgets.FormToolkit;
46 import org.eclipse.ui.forms.widgets.ScrolledForm;
47
48 /**
49 * Main node editor page. Lists all properties of the current node and enable
50 * access and editing for some of them.
51 */
52
53 public class GenericNodePage extends FormPage implements WorkbenchConstants {
54 // private final static Log log = LogFactory.getLog(GenericNodePage.class);
55
56 // local constants
57 private final static String JCR_PROPERTY_NAME = "jcr:name";
58
59 // Utils
60 protected DateFormat timeFormatter = new SimpleDateFormat(DATE_TIME_FORMAT);
61
62 // Main business Objects
63 private Node currentNode;
64
65 // This page widgets
66 private FormToolkit tk;
67 private List<Control> modifyableProperties = new ArrayList<Control>();
68
69 public GenericNodePage(FormEditor editor, String title, Node currentNode) {
70 super(editor, "id", title);
71 this.currentNode = currentNode;
72 }
73
74 protected void createFormContent(IManagedForm managedForm) {
75 tk = managedForm.getToolkit();
76 ScrolledForm form = managedForm.getForm();
77 GridLayout twt = new GridLayout(3, false);
78 twt.marginWidth = twt.marginHeight = 5;
79
80 form.getBody().setLayout(twt);
81 createPropertiesPart(form.getBody());
82 }
83
84 private void createPropertiesPart(Composite parent) {
85 try {
86
87 PropertyIterator pi = currentNode.getProperties();
88
89 // Initializes form part
90 AbstractFormPart part = new AbstractFormPart() {
91 public void commit(boolean onSave) {
92 try {
93 if (onSave) {
94 ListIterator<Control> it = modifyableProperties
95 .listIterator();
96 while (it.hasNext()) {
97 // we only support Text controls for the time
98 // being
99 Text curControl = (Text) it.next();
100 String value = curControl.getText();
101 currentNode.setProperty((String) curControl
102 .getData(JCR_PROPERTY_NAME), value);
103 }
104
105 // We only commit when onSave = true,
106 // thus it is still possible to save after a tab
107 // change.
108 super.commit(onSave);
109 }
110 } catch (RepositoryException re) {
111 throw new ArgeoException(
112 "Unexpected error while saving properties", re);
113 }
114 }
115 };
116
117 while (pi.hasNext()) {
118 Property prop = pi.nextProperty();
119 addPropertyLine(parent, part, prop);
120 }
121
122 getManagedForm().addPart(part);
123 } catch (RepositoryException re) {
124 throw new ArgeoException(
125 "Error during creation of network details section", re);
126 }
127
128 }
129
130 private void addPropertyLine(Composite parent, AbstractFormPart part,
131 Property prop) {
132 try {
133 tk.createLabel(parent, prop.getName());
134 tk.createLabel(parent,
135 "[" + JcrUtils.getPropertyDefinitionAsString(prop) + "]");
136
137 if (prop.getDefinition().isProtected()) {
138 tk.createLabel(parent, formatReadOnlyPropertyValue(prop));
139 } else
140 addModifyableValueWidget(parent, part, prop);
141 } catch (RepositoryException re) {
142 throw new ArgeoException("Cannot get property " + prop, re);
143 }
144 }
145
146 private String formatReadOnlyPropertyValue(Property prop) {
147 try {
148 String strValue;
149
150 if (prop.getType() == PropertyType.BINARY)
151 strValue = "<binary>";
152 else if (prop.isMultiple())
153 strValue = Arrays.asList(prop.getValues()).toString();
154 else if (prop.getType() == PropertyType.DATE)
155 strValue = timeFormatter.format(prop.getValue().getDate()
156 .getTime());
157 else
158 strValue = prop.getValue().getString();
159
160 return strValue;
161 } catch (RepositoryException re) {
162 throw new ArgeoException(
163 "Unexpected error while formatting read only property value",
164 re);
165 }
166 }
167
168 private Control addModifyableValueWidget(Composite parent,
169 AbstractFormPart part, Property prop) {
170 GridData gd;
171 try {
172 if (prop.getType() == PropertyType.STRING) {
173 Text txt = tk.createText(parent, prop.getString());
174 gd = new GridData(GridData.FILL_HORIZONTAL);
175 txt.setLayoutData(gd);
176 txt.addModifyListener(new ModifiedFieldListener(part));
177 txt.setData(JCR_PROPERTY_NAME, prop.getName());
178 modifyableProperties.add(txt);
179 } else {
180 // unsupported property type for editing, we create a read only
181 // label.
182 return tk
183 .createLabel(parent, formatReadOnlyPropertyValue(prop));
184 }
185 return null;
186 } catch (RepositoryException re) {
187 throw new ArgeoException(
188 "Unexpected error while formatting read only property value",
189 re);
190 }
191
192 }
193
194 private class ModifiedFieldListener implements ModifyListener {
195 private static final long serialVersionUID = 2117484480773434646L;
196 private AbstractFormPart formPart;
197
198 public ModifiedFieldListener(AbstractFormPart generalPart) {
199 this.formPart = generalPart;
200 }
201
202 public void modifyText(ModifyEvent e) {
203 formPart.markDirty();
204 }
205 }
206
207 }