2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.cms
.ui
.workbench
.internal
.jcr
.parts
;
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
;
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
;
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
;
50 * Main node editor page. Lists all properties of the current node and enable
51 * access and editing for some of them.
54 public class GenericNodePage
extends FormPage
implements WorkbenchConstants
{
55 // private final static Log log = LogFactory.getLog(GenericNodePage.class);
58 private final static String JCR_PROPERTY_NAME
= "jcr:name";
61 protected DateFormat timeFormatter
= new SimpleDateFormat(DATE_TIME_FORMAT
);
63 // Main business Objects
64 private Node currentNode
;
67 private FormToolkit tk
;
68 private List
<Control
> modifyableProperties
= new ArrayList
<Control
>();
70 public GenericNodePage(FormEditor editor
, String title
, Node currentNode
) {
71 super(editor
, "id", title
);
72 this.currentNode
= currentNode
;
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;
81 form
.getBody().setLayout(twt
);
82 createPropertiesPart(form
.getBody());
85 private void createPropertiesPart(Composite parent
) {
88 PropertyIterator pi
= currentNode
.getProperties();
90 // Initializes form part
91 AbstractFormPart part
= new AbstractFormPart() {
92 public void commit(boolean onSave
) {
95 ListIterator
<Control
> it
= modifyableProperties
97 while (it
.hasNext()) {
98 // we only support Text controls for the time
100 Text curControl
= (Text
) it
.next();
101 String value
= curControl
.getText();
102 currentNode
.setProperty((String
) curControl
103 .getData(JCR_PROPERTY_NAME
), value
);
106 // We only commit when onSave = true,
107 // thus it is still possible to save after a tab
109 super.commit(onSave
);
110 if (currentNode
.getSession().hasPendingChanges())
111 currentNode
.getSession().save();
113 } catch (RepositoryException re
) {
114 throw new EclipseUiException(
115 "Unexpected error while saving properties", re
);
120 while (pi
.hasNext()) {
121 Property prop
= pi
.nextProperty();
122 addPropertyLine(parent
, part
, prop
);
125 getManagedForm().addPart(part
);
126 } catch (RepositoryException re
) {
127 throw new EclipseUiException(
128 "Error during creation of network details section", re
);
133 private void addPropertyLine(Composite parent
, AbstractFormPart part
,
136 tk
.createLabel(parent
, prop
.getName());
137 tk
.createLabel(parent
,
138 "[" + JcrUtils
.getPropertyDefinitionAsString(prop
) + "]");
140 if (prop
.getDefinition().isProtected()) {
141 tk
.createLabel(parent
, formatReadOnlyPropertyValue(prop
));
143 addModifyableValueWidget(parent
, part
, prop
);
144 } catch (RepositoryException re
) {
145 throw new EclipseUiException("Cannot get property " + prop
, re
);
149 private String
formatReadOnlyPropertyValue(Property prop
) {
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()
161 strValue
= prop
.getValue().getString();
164 } catch (RepositoryException re
) {
165 throw new EclipseUiException(
166 "Unexpected error while formatting read only property value",
171 private Control
addModifyableValueWidget(Composite parent
,
172 AbstractFormPart part
, Property prop
) {
175 if (prop
.getType() == PropertyType
.STRING
&& !prop
.isMultiple()) {
176 Text txt
= tk
.createText(parent
, prop
.getString(), SWT
.WRAP
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
);
184 // unsupported property type for editing, we create a read only
187 .createLabel(parent
, formatReadOnlyPropertyValue(prop
));
190 } catch (RepositoryException re
) {
191 throw new EclipseUiException(
192 "Unexpected error while formatting read only property value",
198 private class ModifiedFieldListener
implements ModifyListener
{
199 private static final long serialVersionUID
= 2117484480773434646L;
200 private AbstractFormPart formPart
;
202 public ModifiedFieldListener(AbstractFormPart generalPart
) {
203 this.formPart
= generalPart
;
206 public void modifyText(ModifyEvent e
) {
207 formPart
.markDirty();