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