]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - widgets/EditableText.java
1 package org
.argeo
.cms
.swt
.widgets
;
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
;
12 /** Editable text part displaying styled text. */
13 public class EditableText
extends StyledControl
{
14 private static final long serialVersionUID
= -6372283442330912755L;
16 private boolean editable
= true;
17 private boolean multiLine
= true;
19 private Color highlightColor
;
20 private Composite highlight
;
22 private boolean useTextAsLabel
= false;
25 * Message to display if there is not value. Only used with SWT.FLAT (label
26 * displayed with a {@link Text})
28 * @see Text#setMessage(String)
30 private String message
;
32 public EditableText(Composite parent
, int 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
);
41 protected Control
createControl(Composite box
, String style
) {
42 if (isEditing() && getEditable()) {
43 return createText(box
, style
, true);
46 return createTextLabel(box
, style
);
48 return createLabel(box
, style
);
53 protected Label
createLabel(Composite box
, String style
) {
54 Label lbl
= new Label(box
, getStyle() | SWT
.WRAP
);
55 lbl
.setLayoutData(CmsSwtUtils
.fillWidth());
57 CmsSwtUtils
.style(lbl
, style
);
58 CmsSwtUtils
.markup(lbl
);
59 if (mouseListener
!= null)
60 lbl
.addMouseListener(mouseListener
);
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);
68 lbl
.setMessage(message
);
69 lbl
.setLayoutData(multiLine ? CmsSwtUtils
.fillAll() : CmsSwtUtils
.fillWidth());
71 CmsSwtUtils
.style(lbl
, style
);
72 CmsSwtUtils
.markup(lbl
);
73 if (mouseListener
!= null)
74 lbl
.addMouseListener(mouseListener
);
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;
84 highlightGd
.heightHint
= 3;
85 highlight
.setLayoutData(highlightGd
);
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
);
93 CmsSwtUtils
.style(text
, style
);
99 protected void clear(boolean deep
) {
100 if (highlight
!= null)
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
);
113 public Text
getAsText() {
114 return (Text
) getControl();
117 public Label
getAsLabel() {
118 return (Label
) getControl();
121 public String
getText() {
122 Control child
= getControl();
124 if (child
instanceof Label
)
125 return ((Label
) child
).getText();
126 else if (child
instanceof Text
)
127 return ((Text
) child
).getText();
129 throw new IllegalStateException("Unsupported control " + child
.getClass());
132 /** @deprecated Use {@link #isEditable()} instead. */
134 public boolean getEditable() {
138 public boolean isEditable() {
142 public void setUseTextAsLabel(boolean useTextAsLabel
) {
143 this.useTextAsLabel
= useTextAsLabel
;
146 public String
getMessage() {
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
);
158 protected void setContainerLayoutData(Composite composite
) {
160 composite
.setLayoutData(CmsSwtUtils
.fillAll());
162 super.setContainerLayoutData(composite
);
166 protected void setControlLayoutData(Control control
) {
168 // control.setLayoutData(CmsSwtUtils.fillAll());
170 super.setControlLayoutData(control
);