]> git.argeo.org Git - lgpl/argeo-commons.git/blob - swt/org.argeo.cms.swt/src/org/argeo/cms/swt/widgets/EditableText.java
Extract log method from asynchronous publisher
[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 /**
25 * Message to display if there is not value. Only used with SWT.FLAT (label
26 * displayed with a {@link Text})
27 *
28 * @see Text#setMessage(String)
29 */
30 private String message;
31
32 public EditableText(Composite parent, int style) {
33 super(parent, style);
34 editable = !(SWT.READ_ONLY == (style & SWT.READ_ONLY));
35 multiLine = !(SWT.SINGLE == (style & SWT.SINGLE));
36 highlightColor = parent.getDisplay().getSystemColor(SWT.COLOR_GRAY);
37 useTextAsLabel = SWT.FLAT == (style & SWT.FLAT);
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 if (useTextAsLabel) {
46 return createTextLabel(box, style);
47 } else {
48 return createLabel(box, style);
49 }
50 }
51 }
52
53 protected Label createLabel(Composite box, String style) {
54 Label lbl = new Label(box, getStyle() | SWT.WRAP);
55 lbl.setLayoutData(CmsSwtUtils.fillWidth());
56 if (style != null)
57 CmsSwtUtils.style(lbl, style);
58 CmsSwtUtils.markup(lbl);
59 if (mouseListener != null)
60 lbl.addMouseListener(mouseListener);
61 return lbl;
62 }
63
64 protected Text createTextLabel(Composite box, String style) {
65 Text lbl = new Text(box, getStyle() | (multiLine ? SWT.MULTI | SWT.WRAP : SWT.SINGLE));
66 lbl.setEditable(false);
67 if (message != null)
68 lbl.setMessage(message);
69 lbl.setLayoutData(multiLine ? CmsSwtUtils.fillAll() : CmsSwtUtils.fillWidth());
70 if (style != null)
71 CmsSwtUtils.style(lbl, style);
72 CmsSwtUtils.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, multiLine);
82 highlightGd.widthHint = 5;
83 if (!multiLine)
84 highlightGd.heightHint = 3;
85 highlight.setLayoutData(highlightGd);
86
87 final Text text = new Text(box, getStyle() | (multiLine ? SWT.MULTI : SWT.SINGLE) | SWT.WRAP);
88 text.setEditable(editable);
89 GridData textLayoutData = multiLine ? CmsSwtUtils.fillAll() : CmsSwtUtils.fillWidth();
90 // textLayoutData.heightHint = preferredHeight;
91 text.setLayoutData(textLayoutData);
92 if (style != null)
93 CmsSwtUtils.style(text, style);
94 text.setFocus();
95 return text;
96 }
97
98 @Override
99 protected void clear(boolean deep) {
100 if (highlight != null)
101 highlight.dispose();
102 super.clear(deep);
103 }
104
105 public void setText(String text) {
106 Control child = getControl();
107 if (child instanceof Label)
108 ((Label) child).setText(text);
109 else if (child instanceof Text)
110 ((Text) child).setText(text);
111 }
112
113 public Text getAsText() {
114 return (Text) getControl();
115 }
116
117 public Label getAsLabel() {
118 return (Label) getControl();
119 }
120
121 public String getText() {
122 Control child = getControl();
123
124 if (child instanceof Label)
125 return ((Label) child).getText();
126 else if (child instanceof Text)
127 return ((Text) child).getText();
128 else
129 throw new IllegalStateException("Unsupported control " + child.getClass());
130 }
131
132 /** @deprecated Use {@link #isEditable()} instead. */
133 @Deprecated
134 public boolean getEditable() {
135 return isEditable();
136 }
137
138 public boolean isEditable() {
139 return editable;
140 }
141
142 public void setUseTextAsLabel(boolean useTextAsLabel) {
143 this.useTextAsLabel = useTextAsLabel;
144 }
145
146 public String getMessage() {
147 return message;
148 }
149
150 public void setMessage(String message) {
151 this.message = message;
152 Control control = getControl();
153 if (control != null && control instanceof Text txt)
154 txt.setMessage(this.message);
155 }
156
157 @Override
158 protected void setContainerLayoutData(Composite composite) {
159 if (multiLine)
160 composite.setLayoutData(CmsSwtUtils.fillAll());
161 else
162 super.setContainerLayoutData(composite);
163 }
164
165 @Override
166 protected void setControlLayoutData(Control control) {
167 // if (multiLine)
168 // control.setLayoutData(CmsSwtUtils.fillAll());
169 // else
170 super.setControlLayoutData(control);
171 }
172
173 }