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