]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - swt/org.argeo.cms.swt/src/org/argeo/cms/swt/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;
24 public EditableText(Composite parent
, int 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
);
33 protected Control
createControl(Composite box
, String style
) {
34 if (isEditing() && getEditable()) {
35 return createText(box
, style
, true);
38 return createTextLabel(box
, style
);
40 return createLabel(box
, style
);
45 protected Label
createLabel(Composite box
, String style
) {
46 Label lbl
= new Label(box
, getStyle() | SWT
.WRAP
);
47 lbl
.setLayoutData(CmsSwtUtils
.fillWidth());
49 CmsSwtUtils
.style(lbl
, style
);
50 CmsSwtUtils
.markup(lbl
);
51 if (mouseListener
!= null)
52 lbl
.addMouseListener(mouseListener
);
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());
61 CmsSwtUtils
.style(lbl
, style
);
62 CmsSwtUtils
.markup(lbl
);
63 if (mouseListener
!= null)
64 lbl
.addMouseListener(mouseListener
);
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
);
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
);
82 CmsSwtUtils
.style(text
, style
);
88 protected void clear(boolean deep
) {
89 if (highlight
!= null)
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
);
102 public Text
getAsText() {
103 return (Text
) getControl();
106 public Label
getAsLabel() {
107 return (Label
) getControl();
110 public String
getText() {
111 Control child
= getControl();
113 if (child
instanceof Label
)
114 return ((Label
) child
).getText();
115 else if (child
instanceof Text
)
116 return ((Text
) child
).getText();
118 throw new IllegalStateException("Unsupported control " + child
.getClass());
121 /** @deprecated Use {@link #isEditable()} instead. */
123 public boolean getEditable() {
127 public boolean isEditable() {
131 public void setUseTextAsLabel(boolean useTextAsLabel
) {
132 this.useTextAsLabel
= useTextAsLabel
;