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 * Work-In-Progress Node editor page: provides edition feature on String
51 * properties for power users. TODO implement manual modification of all
55 public class GenericNodePage
extends FormPage
implements WorkbenchConstants
{
56 // private final static Log log = LogFactory.getLog(GenericNodePage.class);
59 private final static String JCR_PROPERTY_NAME
= "jcr:name";
62 protected DateFormat timeFormatter
= new SimpleDateFormat(DATE_TIME_FORMAT
);
64 // Main business Objects
65 private Node currentNode
;
68 private FormToolkit tk
;
69 private List
<Control
> modifyableProperties
= new ArrayList
<Control
>();
71 public GenericNodePage(FormEditor editor
, String title
, Node currentNode
) {
72 super(editor
, "id", title
);
73 this.currentNode
= currentNode
;
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
);
86 private void createPropertiesPart(Composite parent
) {
88 AbstractFormPart part
= new AbstractFormPart() {
89 public void commit(boolean 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
);
100 // We only commit when onSave = true,
101 // thus it is still possible to save after a tab
103 if (currentNode
.getSession().hasPendingChanges())
104 currentNode
.getSession().save();
105 super.commit(onSave
);
107 } catch (RepositoryException re
) {
108 throw new EclipseUiException("Cannot save properties on " + currentNode
, re
);
113 PropertyIterator pi
= currentNode
.getProperties();
114 while (pi
.hasNext()) {
115 Property prop
= pi
.nextProperty();
116 addPropertyLine(parent
, part
, prop
);
118 getManagedForm().addPart(part
);
119 } catch (RepositoryException re
) {
120 throw new EclipseUiException("Cannot display properties for " + currentNode
, re
);
124 private void addPropertyLine(Composite parent
, AbstractFormPart part
, Property prop
) {
126 tk
.createLabel(parent
, prop
.getName());
127 tk
.createLabel(parent
, "[" + JcrUtils
.getPropertyDefinitionAsString(prop
) + "]");
129 if (prop
.getDefinition().isProtected()) {
130 tk
.createLabel(parent
, formatReadOnlyPropertyValue(prop
));
132 addModifyableValueWidget(parent
, part
, prop
);
133 } catch (RepositoryException re
) {
134 throw new EclipseUiException("Cannot display property " + prop
, re
);
138 private String
formatReadOnlyPropertyValue(Property prop
) throws RepositoryException
{
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());
147 strValue
= prop
.getValue().getString();
151 private Control
addModifyableValueWidget(Composite parent
, AbstractFormPart part
, Property prop
)
152 throws RepositoryException
{
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
);
162 // unsupported property type for editing, we create a read only
164 return tk
.createLabel(parent
, formatReadOnlyPropertyValue(prop
));
169 private class ModifiedFieldListener
implements ModifyListener
{
170 private static final long serialVersionUID
= 2117484480773434646L;
171 private AbstractFormPart formPart
;
173 public ModifiedFieldListener(AbstractFormPart generalPart
) {
174 this.formPart
= generalPart
;
177 public void modifyText(ModifyEvent e
) {
178 formPart
.markDirty();