]> git.argeo.org Git - lgpl/argeo-commons.git/blob - EditablePropertyString.java
e7840396d81199f60b52cce0c4fbd429a1e18ab9
[lgpl/argeo-commons.git] / EditablePropertyString.java
1 package org.argeo.cms.forms;
2
3 import static org.argeo.cms.forms.FormStyle.propertyMessage;
4 import static org.argeo.cms.forms.FormStyle.propertyText;
5
6 import javax.jcr.Node;
7 import javax.jcr.RepositoryException;
8
9 import org.argeo.cms.viewers.EditablePart;
10 import org.argeo.cms.widgets.EditableText;
11 import org.argeo.eclipse.ui.EclipseUiUtils;
12 import org.eclipse.swt.widgets.Composite;
13 import org.eclipse.swt.widgets.Control;
14 import org.eclipse.swt.widgets.Label;
15 import org.eclipse.swt.widgets.Text;
16
17 /** Editable String in a CMS context */
18 public class EditablePropertyString extends EditableText implements
19 EditablePart {
20 private static final long serialVersionUID = 5055000749992803591L;
21
22 private String propertyName;
23 private String message;
24
25 public EditablePropertyString(Composite parent, int style, Node node,
26 String propertyName, String message) throws RepositoryException {
27 super(parent, style, node, true);
28
29 this.propertyName = propertyName;
30 this.message = message;
31
32 if (node.hasProperty(propertyName)) {
33 this.setStyle(propertyText.style());
34 this.setText(node.getProperty(propertyName).getString());
35 } else {
36 this.setStyle(propertyMessage.style());
37 this.setText(message + " ");
38 }
39 }
40
41 public void setText(String text) {
42 Control child = getControl();
43 if (child instanceof Label) {
44 Label lbl = (Label) child;
45 if (EclipseUiUtils.isEmpty(text))
46 lbl.setText(message + " ");
47 else
48 lbl.setText(text);
49 } else if (child instanceof Text) {
50 Text txt = (Text) child;
51 if (EclipseUiUtils.isEmpty(text)) {
52 txt.setText("");
53 txt.setMessage(message + " ");
54 } else
55 txt.setText(text.replaceAll("<br/>", "\n"));
56 }
57 }
58
59 public synchronized void startEditing() {
60 getControl().setData(STYLE, propertyText.style());
61 super.startEditing();
62 }
63
64 public synchronized void stopEditing() {
65 if (EclipseUiUtils.isEmpty(((Text) getControl()).getText()))
66 getControl().setData(STYLE, propertyMessage.style());
67 else
68 getControl().setData(STYLE, propertyText.style());
69 super.stopEditing();
70 }
71
72 public String getPropertyName() {
73 return propertyName;
74 }
75 }