]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/ui/widgets/EditableText.java
5fadbc07ba436b0a0a867f4c602e3b68d3ba51df
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / 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 public EditableText(Composite parent, int style) {
25 super(parent, style);
26 editable = !(SWT.READ_ONLY == (style & SWT.READ_ONLY));
27 highlightColor = parent.getDisplay().getSystemColor(SWT.COLOR_GRAY);
28 }
29
30 public EditableText(Composite parent, int style, Item item) throws RepositoryException {
31 this(parent, style, item, false);
32 }
33
34 public EditableText(Composite parent, int style, Item item, boolean cacheImmediately) throws RepositoryException {
35 super(parent, style, item, cacheImmediately);
36 editable = !(SWT.READ_ONLY == (style & SWT.READ_ONLY));
37 highlightColor = parent.getDisplay().getSystemColor(SWT.COLOR_GRAY);
38 }
39
40 @Override
41 protected Control createControl(Composite box, String style) {
42 if (isEditing() && getEditable()) {
43 return createText(box, style, true);
44 } else {
45 // return createText(box, style, false);
46 return createLabel(box, style);
47 }
48 }
49
50 protected Label createLabel(Composite box, String style) {
51 Label lbl = new Label(box, getStyle() | SWT.WRAP);
52 lbl.setLayoutData(CmsUiUtils.fillWidth());
53 if (style != null)
54 CmsUiUtils.style(lbl, style);
55 CmsUiUtils.markup(lbl);
56 if (mouseListener != null)
57 lbl.addMouseListener(mouseListener);
58 return lbl;
59 }
60
61 protected Text createText(Composite box, String style, boolean editable) {
62 highlight = new Composite(box, SWT.NONE);
63 highlight.setBackground(highlightColor);
64 GridData highlightGd = new GridData(SWT.FILL, SWT.FILL, false, false);
65 highlightGd.widthHint = 5;
66 highlightGd.heightHint = 3;
67 highlight.setLayoutData(highlightGd);
68
69 final Text text = new Text(box, getStyle() | SWT.MULTI | SWT.WRAP);
70 text.setEditable(editable);
71 GridData textLayoutData = CmsUiUtils.fillWidth();
72 // textLayoutData.heightHint = preferredHeight;
73 text.setLayoutData(textLayoutData);
74 if (style != null)
75 CmsUiUtils.style(text, style);
76 text.setFocus();
77 return text;
78 }
79
80 @Override
81 protected void clear(boolean deep) {
82 if (highlight != null)
83 highlight.dispose();
84 super.clear(deep);
85 }
86
87 public void setText(String text) {
88 Control child = getControl();
89 if (child instanceof Label)
90 ((Label) child).setText(text);
91 else if (child instanceof Text)
92 ((Text) child).setText(text);
93 }
94
95 public Text getAsText() {
96 return (Text) getControl();
97 }
98
99 public Label getAsLabel() {
100 return (Label) getControl();
101 }
102
103 public String getText() {
104 Control child = getControl();
105
106 if (child instanceof Label)
107 return ((Label) child).getText();
108 else if (child instanceof Text)
109 return ((Text) child).getText();
110 else
111 throw new IllegalStateException("Unsupported control " + child.getClass());
112 }
113
114 public boolean getEditable() {
115 return editable;
116 }
117
118 }