Introduce context overlay.
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / ui / widgets / EditableText.java
index 21f48138ce92155d0c6c4687f393250f2c3da1bf..27b7c9b105ed4ad5e66790bd9f8cfe2fad0f0679 100644 (file)
@@ -5,6 +5,7 @@ import javax.jcr.RepositoryException;
 
 import org.argeo.cms.ui.util.CmsUiUtils;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.graphics.Color;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
@@ -15,48 +16,91 @@ import org.eclipse.swt.widgets.Text;
 public class EditableText extends StyledControl {
        private static final long serialVersionUID = -6372283442330912755L;
 
-       public EditableText(Composite parent, int swtStyle) {
-               super(parent, swtStyle);
+       private boolean editable = true;
+
+       private Color highlightColor;
+       private Composite highlight;
+
+       private boolean useTextAsLabel = false;
+
+       public EditableText(Composite parent, int style) {
+               super(parent, style);
+               editable = !(SWT.READ_ONLY == (style & SWT.READ_ONLY));
+               highlightColor = parent.getDisplay().getSystemColor(SWT.COLOR_GRAY);
        }
 
-       public EditableText(Composite parent, int style, Item item)
-                       throws RepositoryException {
+       public EditableText(Composite parent, int style, Item item) throws RepositoryException {
                this(parent, style, item, false);
        }
 
-       public EditableText(Composite parent, int style, Item item,
-                       boolean cacheImmediately) throws RepositoryException {
+       public EditableText(Composite parent, int style, Item item, boolean cacheImmediately) throws RepositoryException {
                super(parent, style, item, cacheImmediately);
+               editable = !(SWT.READ_ONLY == (style & SWT.READ_ONLY));
+               highlightColor = parent.getDisplay().getSystemColor(SWT.COLOR_GRAY);
        }
 
        @Override
        protected Control createControl(Composite box, String style) {
-               if (isEditing())
-                       return createText(box, style);
-               else
-                       return createLabel(box, style);
+               if (isEditing() && getEditable()) {
+                       return createText(box, style, true);
+               } else {
+                       if (useTextAsLabel) {
+                               return createTextLabel(box, style);
+                       } else {
+                               return createLabel(box, style);
+                       }
+               }
        }
 
        protected Label createLabel(Composite box, String style) {
                Label lbl = new Label(box, getStyle() | SWT.WRAP);
                lbl.setLayoutData(CmsUiUtils.fillWidth());
-               CmsUiUtils.style(lbl, style);
+               if (style != null)
+                       CmsUiUtils.style(lbl, style);
+               CmsUiUtils.markup(lbl);
+               if (mouseListener != null)
+                       lbl.addMouseListener(mouseListener);
+               return lbl;
+       }
+
+       protected Text createTextLabel(Composite box, String style) {
+               Text lbl = new Text(box, getStyle() | SWT.MULTI);
+               lbl.setEditable(false);
+               lbl.setLayoutData(CmsUiUtils.fillWidth());
+               if (style != null)
+                       CmsUiUtils.style(lbl, style);
                CmsUiUtils.markup(lbl);
                if (mouseListener != null)
                        lbl.addMouseListener(mouseListener);
                return lbl;
        }
 
-       protected Text createText(Composite box, String style) {
+       protected Text createText(Composite box, String style, boolean editable) {
+               highlight = new Composite(box, SWT.NONE);
+               highlight.setBackground(highlightColor);
+               GridData highlightGd = new GridData(SWT.FILL, SWT.FILL, false, false);
+               highlightGd.widthHint = 5;
+               highlightGd.heightHint = 3;
+               highlight.setLayoutData(highlightGd);
+
                final Text text = new Text(box, getStyle() | SWT.MULTI | SWT.WRAP);
+               text.setEditable(editable);
                GridData textLayoutData = CmsUiUtils.fillWidth();
                // textLayoutData.heightHint = preferredHeight;
                text.setLayoutData(textLayoutData);
-               CmsUiUtils.style(text, style);
+               if (style != null)
+                       CmsUiUtils.style(text, style);
                text.setFocus();
                return text;
        }
 
+       @Override
+       protected void clear(boolean deep) {
+               if (highlight != null)
+                       highlight.dispose();
+               super.clear(deep);
+       }
+
        public void setText(String text) {
                Control child = getControl();
                if (child instanceof Label)
@@ -73,4 +117,29 @@ public class EditableText extends StyledControl {
                return (Label) getControl();
        }
 
+       public String getText() {
+               Control child = getControl();
+
+               if (child instanceof Label)
+                       return ((Label) child).getText();
+               else if (child instanceof Text)
+                       return ((Text) child).getText();
+               else
+                       throw new IllegalStateException("Unsupported control " + child.getClass());
+       }
+
+       /** @deprecated Use {@link #isEditable()} instead. */
+       @Deprecated
+       public boolean getEditable() {
+               return isEditable();
+       }
+
+       public boolean isEditable() {
+               return editable;
+       }
+
+       public void setUseTextAsLabel(boolean useTextAsLabel) {
+               this.useTextAsLabel = useTextAsLabel;
+       }
+
 }