]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui.workbench/src/org/argeo/cms/ui/workbench/internal/jcr/parts/GenericNodePage.java
Improve user admin perspective
[lgpl/argeo-commons.git] / org.argeo.cms.ui.workbench / src / org / argeo / cms / 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.cms.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.cms.ui.workbench.internal.WorkbenchConstants;
32 import org.argeo.eclipse.ui.EclipseUiException;
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 * Work-In-Progress Node editor page: provides edition feature on String
51 * properties for power users. TODO implement manual modification of all
52 * property types.
53 */
54
55 public class GenericNodePage extends FormPage implements WorkbenchConstants {
56 // private final static Log log = LogFactory.getLog(GenericNodePage.class);
57
58 // local constants
59 private final static String JCR_PROPERTY_NAME = "jcr:name";
60
61 // Utils
62 protected DateFormat timeFormatter = new SimpleDateFormat(DATE_TIME_FORMAT);
63
64 // Main business Objects
65 private Node currentNode;
66
67 // This page widgets
68 private FormToolkit tk;
69 private List<Control> modifyableProperties = new ArrayList<Control>();
70
71 public GenericNodePage(FormEditor editor, String title, Node currentNode) {
72 super(editor, "id", title);
73 this.currentNode = currentNode;
74 }
75
76 protected void createFormContent(IManagedForm managedForm) {
77 tk = managedForm.getToolkit();
78 ScrolledForm form = managedForm.getForm();
79 Composite innerBox = form.getBody();
80 // Composite innerBox = new Composite(form.getBody(), SWT.NO_FOCUS);
81 GridLayout twt = new GridLayout(3, false);
82 innerBox.setLayout(twt);
83 createPropertiesPart(innerBox);
84 }
85
86 private void createPropertiesPart(Composite parent) {
87 try {
88 AbstractFormPart part = new AbstractFormPart() {
89 public void commit(boolean onSave) {
90 try {
91 if (onSave) {
92 ListIterator<Control> it = modifyableProperties.listIterator();
93 while (it.hasNext()) {
94 // we only support Text controls
95 Text curControl = (Text) it.next();
96 String value = curControl.getText();
97 currentNode.setProperty((String) curControl.getData(JCR_PROPERTY_NAME), value);
98 }
99
100 // We only commit when onSave = true,
101 // thus it is still possible to save after a tab
102 // change.
103 if (currentNode.getSession().hasPendingChanges())
104 currentNode.getSession().save();
105 super.commit(onSave);
106 }
107 } catch (RepositoryException re) {
108 throw new EclipseUiException("Cannot save properties on " + currentNode, re);
109 }
110 }
111 };
112
113 PropertyIterator pi = currentNode.getProperties();
114 while (pi.hasNext()) {
115 Property prop = pi.nextProperty();
116 addPropertyLine(parent, part, prop);
117 }
118 getManagedForm().addPart(part);
119 } catch (RepositoryException re) {
120 throw new EclipseUiException("Cannot display properties for " + currentNode, re);
121 }
122 }
123
124 private void addPropertyLine(Composite parent, AbstractFormPart part, Property prop) {
125 try {
126 tk.createLabel(parent, prop.getName());
127 tk.createLabel(parent, "[" + JcrUtils.getPropertyDefinitionAsString(prop) + "]");
128
129 if (prop.getDefinition().isProtected()) {
130 tk.createLabel(parent, formatReadOnlyPropertyValue(prop));
131 } else
132 addModifyableValueWidget(parent, part, prop);
133 } catch (RepositoryException re) {
134 throw new EclipseUiException("Cannot display property " + prop, re);
135 }
136 }
137
138 private String formatReadOnlyPropertyValue(Property prop) throws RepositoryException {
139 String strValue;
140 if (prop.getType() == PropertyType.BINARY)
141 strValue = "<binary>";
142 else if (prop.isMultiple())
143 strValue = Arrays.asList(prop.getValues()).toString();
144 else if (prop.getType() == PropertyType.DATE)
145 strValue = timeFormatter.format(prop.getValue().getDate().getTime());
146 else
147 strValue = prop.getValue().getString();
148 return strValue;
149 }
150
151 private Control addModifyableValueWidget(Composite parent, AbstractFormPart part, Property prop)
152 throws RepositoryException {
153 GridData gd;
154 if (prop.getType() == PropertyType.STRING && !prop.isMultiple()) {
155 Text txt = tk.createText(parent, prop.getString(), SWT.WRAP | SWT.MULTI);
156 gd = new GridData(GridData.FILL_HORIZONTAL);
157 txt.setLayoutData(gd);
158 txt.addModifyListener(new ModifiedFieldListener(part));
159 txt.setData(JCR_PROPERTY_NAME, prop.getName());
160 modifyableProperties.add(txt);
161 } else {
162 // unsupported property type for editing, we create a read only
163 // label.
164 return tk.createLabel(parent, formatReadOnlyPropertyValue(prop));
165 }
166 return null;
167 }
168
169 private class ModifiedFieldListener implements ModifyListener {
170 private static final long serialVersionUID = 2117484480773434646L;
171 private AbstractFormPart formPart;
172
173 public ModifiedFieldListener(AbstractFormPart generalPart) {
174 this.formPart = generalPart;
175 }
176
177 public void modifyText(ModifyEvent e) {
178 formPart.markDirty();
179 }
180 }
181 }