]> git.argeo.org Git - lgpl/argeo-commons.git/blob - ui/widgets/EditableText.java
Prepare next development cycle
[lgpl/argeo-commons.git] / ui / widgets / EditableText.java
1 package org.argeo.cms.ui.widgets;
2
3 import javax.jcr.Item;
4 import javax.jcr.RepositoryException;
5
6 import org.argeo.cms.ui.util.CmsUiUtils;
7 import org.eclipse.swt.SWT;
8 import org.eclipse.swt.events.FocusEvent;
9 import org.eclipse.swt.events.FocusListener;
10 import org.eclipse.swt.layout.GridData;
11 import org.eclipse.swt.widgets.Composite;
12 import org.eclipse.swt.widgets.Control;
13 import org.eclipse.swt.widgets.Label;
14 import org.eclipse.swt.widgets.Text;
15
16 /** Editable text part displaying styled text. */
17 public class EditableText extends StyledControl {
18 private static final long serialVersionUID = -6372283442330912755L;
19
20 private boolean editable = true;
21
22 public EditableText(Composite parent, int style) {
23 super(parent, style);
24 editable = !(SWT.READ_ONLY == (style & SWT.READ_ONLY));
25 }
26
27 public EditableText(Composite parent, int style, Item item) throws RepositoryException {
28 this(parent, style, item, false);
29 }
30
31 public EditableText(Composite parent, int style, Item item, boolean cacheImmediately) throws RepositoryException {
32 super(parent, style, item, cacheImmediately);
33 editable = !(SWT.READ_ONLY == (style & SWT.READ_ONLY));
34 }
35
36 @Override
37 protected Control createControl(Composite box, String style) {
38 if (isEditing() && getEditable()) {
39 return createText(box, style, true);
40 } else {
41 // return createText(box, style, false);
42 return createLabel(box, style);
43 }
44 }
45
46 protected Label createLabel(Composite box, String style) {
47 Label lbl = new Label(box, getStyle() | SWT.WRAP);
48 lbl.setLayoutData(CmsUiUtils.fillWidth());
49 CmsUiUtils.style(lbl, style);
50 CmsUiUtils.markup(lbl);
51 if (mouseListener != null)
52 lbl.addMouseListener(mouseListener);
53 return lbl;
54 }
55
56 protected Text createText(Composite box, String style, boolean editable) {
57 final Text text = new Text(box, getStyle() | SWT.MULTI | SWT.WRAP);
58 text.setEditable(editable);
59 GridData textLayoutData = CmsUiUtils.fillWidth();
60 // textLayoutData.heightHint = preferredHeight;
61 text.setLayoutData(textLayoutData);
62 CmsUiUtils.style(text, style);
63 text.setFocus();
64 return text;
65 }
66
67 public void setText(String text) {
68 Control child = getControl();
69 if (child instanceof Label)
70 ((Label) child).setText(text);
71 else if (child instanceof Text)
72 ((Text) child).setText(text);
73 }
74
75 public Text getAsText() {
76 return (Text) getControl();
77 }
78
79 public Label getAsLabel() {
80 return (Label) getControl();
81 }
82
83 public String getText() {
84 Control child = getControl();
85
86 if (child instanceof Label)
87 return ((Label) child).getText();
88 else if (child instanceof Text)
89 return ((Text) child).getText();
90 else
91 throw new IllegalStateException("Unsupported control " + child.getClass());
92 }
93
94 public boolean getEditable() {
95 return editable;
96 }
97
98 }