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
.jcr
.ui
.explorer
.editors
;
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
.ArgeoException
;
32 import org
.argeo
.jcr
.JcrUtils
;
33 import org
.argeo
.jcr
.ui
.explorer
.JcrExplorerConstants
;
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
;
49 * Main node editor page. Lists all properties of the current node and enable
50 * access and editing for some of them.
53 public class GenericNodePage
extends FormPage
implements JcrExplorerConstants
{
54 // private final static Log log = LogFactory.getLog(GenericNodePage.class);
57 private final static String JCR_PROPERTY_NAME
= "jcr:name";
60 protected DateFormat timeFormatter
= new SimpleDateFormat(DATE_TIME_FORMAT
);
62 // Main business Objects
63 private Node currentNode
;
66 private FormToolkit tk
;
67 private List
<Control
> modifyableProperties
= new ArrayList
<Control
>();
69 public GenericNodePage(FormEditor editor
, String title
, Node currentNode
) {
70 super(editor
, "id", title
);
71 this.currentNode
= currentNode
;
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;
80 form
.getBody().setLayout(twt
);
81 createPropertiesPart(form
.getBody());
84 private void createPropertiesPart(Composite parent
) {
87 PropertyIterator pi
= currentNode
.getProperties();
89 // Initializes form part
90 AbstractFormPart part
= new AbstractFormPart() {
91 public void commit(boolean onSave
) {
94 ListIterator
<Control
> it
= modifyableProperties
96 while (it
.hasNext()) {
97 // we only support Text controls for the time
99 Text curControl
= (Text
) it
.next();
100 String value
= curControl
.getText();
101 currentNode
.setProperty((String
) curControl
102 .getData(JCR_PROPERTY_NAME
), value
);
105 // We only commit when onSave = true,
106 // thus it is still possible to save after a tab
108 super.commit(onSave
);
110 } catch (RepositoryException re
) {
111 throw new ArgeoException(
112 "Unexpected error while saving properties", re
);
117 while (pi
.hasNext()) {
118 Property prop
= pi
.nextProperty();
119 addPropertyLine(parent
, part
, prop
);
122 getManagedForm().addPart(part
);
123 } catch (RepositoryException re
) {
124 throw new ArgeoException(
125 "Error during creation of network details section", re
);
130 private void addPropertyLine(Composite parent
, AbstractFormPart part
,
133 tk
.createLabel(parent
, prop
.getName());
134 tk
.createLabel(parent
,
135 "[" + JcrUtils
.getPropertyDefinitionAsString(prop
) + "]");
137 if (prop
.getDefinition().isProtected()) {
138 tk
.createLabel(parent
, formatReadOnlyPropertyValue(prop
));
140 addModifyableValueWidget(parent
, part
, prop
);
141 } catch (RepositoryException re
) {
142 throw new ArgeoException("Cannot get property " + prop
, re
);
146 private String
formatReadOnlyPropertyValue(Property prop
) {
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()
158 strValue
= prop
.getValue().getString();
161 } catch (RepositoryException re
) {
162 throw new ArgeoException(
163 "Unexpected error while formatting read only property value",
168 private Control
addModifyableValueWidget(Composite parent
,
169 AbstractFormPart part
, Property prop
) {
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
);
180 // unsupported property type for editing, we create a read only
183 .createLabel(parent
, formatReadOnlyPropertyValue(prop
));
186 } catch (RepositoryException re
) {
187 throw new ArgeoException(
188 "Unexpected error while formatting read only property value",
198 private class ModifiedFieldListener
implements ModifyListener
{
200 private AbstractFormPart formPart
;
202 public ModifiedFieldListener(AbstractFormPart generalPart
) {
203 this.formPart
= generalPart
;
206 public void modifyText(ModifyEvent e
) {
207 formPart
.markDirty();