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