]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - ui/widgets/EditableText.java
1 package org
.argeo
.cms
.ui
.widgets
;
4 import javax
.jcr
.RepositoryException
;
6 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
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
;
15 /** Editable text part displaying styled text. */
16 public class EditableText
extends StyledControl
{
17 private static final long serialVersionUID
= -6372283442330912755L;
19 private boolean editable
= true;
21 private Color highlightColor
;
22 private Composite highlight
;
24 private boolean useTextAsLabel
= false;
26 public EditableText(Composite parent
, int style
) {
28 editable
= !(SWT
.READ_ONLY
== (style
& SWT
.READ_ONLY
));
29 highlightColor
= parent
.getDisplay().getSystemColor(SWT
.COLOR_GRAY
);
32 public EditableText(Composite parent
, int style
, Item item
) throws RepositoryException
{
33 this(parent
, style
, item
, false);
36 public EditableText(Composite parent
, int style
, Item item
, boolean cacheImmediately
) throws RepositoryException
{
37 super(parent
, style
, item
, cacheImmediately
);
38 editable
= !(SWT
.READ_ONLY
== (style
& SWT
.READ_ONLY
));
39 highlightColor
= parent
.getDisplay().getSystemColor(SWT
.COLOR_GRAY
);
43 protected Control
createControl(Composite box
, String style
) {
44 if (isEditing() && getEditable()) {
45 return createText(box
, style
, true);
48 return createTextLabel(box
, style
);
50 return createLabel(box
, style
);
55 protected Label
createLabel(Composite box
, String style
) {
56 Label lbl
= new Label(box
, getStyle() | SWT
.WRAP
);
57 lbl
.setLayoutData(CmsSwtUtils
.fillWidth());
59 CmsSwtUtils
.style(lbl
, style
);
60 CmsSwtUtils
.markup(lbl
);
61 if (mouseListener
!= null)
62 lbl
.addMouseListener(mouseListener
);
66 protected Text
createTextLabel(Composite box
, String style
) {
67 Text lbl
= new Text(box
, getStyle() | SWT
.MULTI
);
68 lbl
.setEditable(false);
69 lbl
.setLayoutData(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, false);
82 highlightGd
.widthHint
= 5;
83 highlightGd
.heightHint
= 3;
84 highlight
.setLayoutData(highlightGd
);
86 final Text text
= new Text(box
, getStyle() | SWT
.MULTI
| SWT
.WRAP
);
87 text
.setEditable(editable
);
88 GridData textLayoutData
= CmsSwtUtils
.fillWidth();
89 // textLayoutData.heightHint = preferredHeight;
90 text
.setLayoutData(textLayoutData
);
92 CmsSwtUtils
.style(text
, style
);
98 protected void clear(boolean deep
) {
99 if (highlight
!= null)
104 public void setText(String text
) {
105 Control child
= getControl();
106 if (child
instanceof Label
)
107 ((Label
) child
).setText(text
);
108 else if (child
instanceof Text
)
109 ((Text
) child
).setText(text
);
112 public Text
getAsText() {
113 return (Text
) getControl();
116 public Label
getAsLabel() {
117 return (Label
) getControl();
120 public String
getText() {
121 Control child
= getControl();
123 if (child
instanceof Label
)
124 return ((Label
) child
).getText();
125 else if (child
instanceof Text
)
126 return ((Text
) child
).getText();
128 throw new IllegalStateException("Unsupported control " + child
.getClass());
131 /** @deprecated Use {@link #isEditable()} instead. */
133 public boolean getEditable() {
137 public boolean isEditable() {
141 public void setUseTextAsLabel(boolean useTextAsLabel
) {
142 this.useTextAsLabel
= useTextAsLabel
;