]> git.argeo.org Git - lgpl/argeo-commons.git/blob - cms/ui/widgets/EditableText.java
Prepare next development cycle
[lgpl/argeo-commons.git] / cms / ui / widgets / EditableText.java
1 package org.argeo.cms.ui.widgets;
2
3 import javax.jcr.Item;
4 import javax.jcr.RepositoryException;
5
6 import org.argeo.cms.ui.util.CmsUiUtils;
7 import org.eclipse.swt.SWT;
8 import org.eclipse.swt.graphics.Color;
9 import org.eclipse.swt.layout.GridData;
10 import org.eclipse.swt.widgets.Composite;
11 import org.eclipse.swt.widgets.Control;
12 import org.eclipse.swt.widgets.Label;
13 import org.eclipse.swt.widgets.Text;
14
15 /** Editable text part displaying styled text. */
16 public class EditableText extends StyledControl {
17 private static final long serialVersionUID = -6372283442330912755L;
18
19 private boolean editable = true;
20
21 private Color highlightColor;
22 private Composite highlight;
23
24 private boolean useTextAsLabel = false;
25
26 public EditableText(Composite parent, int style) {
27 super(parent, style);
28 editable = !(SWT.READ_ONLY == (style & SWT.READ_ONLY));
29 highlightColor = parent.getDisplay().getSystemColor(SWT.COLOR_GRAY);
30 }
31
32 public EditableText(Composite parent, int style, Item item) throws RepositoryException {
33 this(parent, style, item, false);
34 }
35
36 public EditableText(Composite parent, int style, Item item, boolean cacheImmediately) throws RepositoryException {
37 super(parent, style, item, cacheImmediately);
38 editable = !(SWT.READ_ONLY == (style & SWT.READ_ONLY));
39 highlightColor = parent.getDisplay().getSystemColor(SWT.COLOR_GRAY);
40 }
41
42 @Override
43 protected Control createControl(Composite box, String style) {
44 if (isEditing() && getEditable()) {
45 return createText(box, style, true);
46 } else {
47 if (useTextAsLabel) {
48 return createTextLabel(box, style);
49 } else {
50 return createLabel(box, style);
51 }
52 }
53 }
54
55 protected Label createLabel(Composite box, String style) {
56 Label lbl = new Label(box, getStyle() | SWT.WRAP);
57 lbl.setLayoutData(CmsUiUtils.fillWidth());
58 if (style != null)
59 CmsUiUtils.style(lbl, style);
60 CmsUiUtils.markup(lbl);
61 if (mouseListener != null)
62 lbl.addMouseListener(mouseListener);
63 return lbl;
64 }
65
66 protected Text createTextLabel(Composite box, String style) {
67 Text lbl = new Text(box, getStyle() | SWT.MULTI);
68 lbl.setEditable(false);
69 lbl.setLayoutData(CmsUiUtils.fillWidth());
70 if (style != null)
71 CmsUiUtils.style(lbl, style);
72 CmsUiUtils.markup(lbl);
73 if (mouseListener != null)
74 lbl.addMouseListener(mouseListener);
75 return lbl;
76 }
77
78 protected Text createText(Composite box, String style, boolean editable) {
79 highlight = new Composite(box, SWT.NONE);
80 highlight.setBackground(highlightColor);
81 GridData highlightGd = new GridData(SWT.FILL, SWT.FILL, false, false);
82 highlightGd.widthHint = 5;
83 highlightGd.heightHint = 3;
84 highlight.setLayoutData(highlightGd);
85
86 final Text text = new Text(box, getStyle() | SWT.MULTI | SWT.WRAP);
87 text.setEditable(editable);
88 GridData textLayoutData = CmsUiUtils.fillWidth();
89 // textLayoutData.heightHint = preferredHeight;
90 text.setLayoutData(textLayoutData);
91 if (style != null)
92 CmsUiUtils.style(text, style);
93 text.setFocus();
94 return text;
95 }
96
97 @Override
98 protected void clear(boolean deep) {
99 if (highlight != null)
100 highlight.dispose();
101 super.clear(deep);
102 }
103
104 public void setText(String text) {
105 Control child = getControl();
106 if (child instanceof Label)
107 ((Label) child).setText(text);
108 else if (child instanceof Text)
109 ((Text) child).setText(text);
110 }
111
112 public Text getAsText() {
113 return (Text) getControl();
114 }
115
116 public Label getAsLabel() {
117 return (Label) getControl();
118 }
119
120 public String getText() {
121 Control child = getControl();
122
123 if (child instanceof Label)
124 return ((Label) child).getText();
125 else if (child instanceof Text)
126 return ((Text) child).getText();
127 else
128 throw new IllegalStateException("Unsupported control " + child.getClass());
129 }
130
131 /** @deprecated Use {@link #isEditable()} instead. */
132 @Deprecated
133 public boolean getEditable() {
134 return isEditable();
135 }
136
137 public boolean isEditable() {
138 return editable;
139 }
140
141 public void setUseTextAsLabel(boolean useTextAsLabel) {
142 this.useTextAsLabel = useTextAsLabel;
143 }
144
145 }