Improve generic editable components.
authorMathieu Baudier <mbaudier@argeo.org>
Mon, 18 Jan 2021 10:38:16 +0000 (11:38 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Mon, 18 Jan 2021 10:38:16 +0000 (11:38 +0100)
org.argeo.cms.ui/src/org/argeo/cms/ui/viewers/AbstractPageViewer.java
org.argeo.cms.ui/src/org/argeo/cms/ui/widgets/EditableText.java
org.argeo.cms.ui/src/org/argeo/cms/ui/widgets/StyledControl.java

index e23e3ba7957320a67cdd82cb0d5de5f18c4561be..004bd943531aa9854c318ac184cecd7a03c3f776 100644 (file)
@@ -13,7 +13,6 @@ import javax.security.auth.Subject;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.argeo.cms.CmsException;
 import org.argeo.cms.ui.CmsEditable;
 import org.argeo.cms.ui.widgets.ScrolledPage;
 import org.argeo.jcr.JcrException;
@@ -75,8 +74,8 @@ public abstract class AbstractPageViewer extends ContentViewer implements Observ
                                        initModel(node);
                                        node.getSession().save();
                                }
-               } catch (Exception e) {
-                       throw new CmsException("Cannot initialize model", e);
+               } catch (RepositoryException e) {
+                       throw new JcrException("Cannot initialize model", e);
                }
        }
 
@@ -121,6 +120,16 @@ public abstract class AbstractPageViewer extends ContentViewer implements Observ
                }
        }
 
+       protected void layoutPage() {
+               if (page != null)
+                       page.layout(true, true);
+       }
+
+       protected void showControl(Control control) {
+               if (page != null && (page instanceof ScrolledPage))
+                       ((ScrolledPage) page).showControl(control);
+       }
+
        @Override
        public void update(Observable o, Object arg) {
                if (o == cmsEditable)
@@ -154,9 +163,10 @@ public abstract class AbstractPageViewer extends ContentViewer implements Observ
                                else
                                        mouseListener = null;
                                refresh(getControl());
-                               layout(getControl());
+                               // layout(getControl());
+                               layoutPage();
                        } catch (RepositoryException e) {
-                               throw new CmsException("Cannot refresh", e);
+                               throw new JcrException("Cannot refresh", e);
                        }
                        return null;
                });
@@ -192,6 +202,7 @@ public abstract class AbstractPageViewer extends ContentViewer implements Observ
                        prepare(part, caretPosition);
                        edited = part;
                        edited.getControl().addFocusListener(new FocusListener() {
+                               private static final long serialVersionUID = 6883521812717097017L;
 
                                @Override
                                public void focusLost(FocusEvent event) {
@@ -204,8 +215,9 @@ public abstract class AbstractPageViewer extends ContentViewer implements Observ
                        });
 
                        layout(part.getControl());
+                       showControl(part.getControl());
                } catch (RepositoryException e) {
-                       throw new CmsException("Cannot edit " + part, e);
+                       throw new JcrException("Cannot edit " + part, e);
                }
        }
 
@@ -262,14 +274,14 @@ public abstract class AbstractPageViewer extends ContentViewer implements Observ
                if (parent.getParent() != null)
                        return findDataParent(parent.getParent());
                else
-                       throw new CmsException("No data parent found");
+                       throw new IllegalStateException("No data parent found");
        }
 
        // UTILITIES
        /** Check whether the edited part is in a proper state */
        protected void checkEdited() {
                if (edited == null || (edited instanceof Widget) && ((Widget) edited).isDisposed())
-                       throw new CmsException("Edited should not be null or disposed at this stage");
+                       throw new IllegalStateException("Edited should not be null or disposed at this stage");
        }
 
        /** Persist all changes. */
@@ -302,7 +314,7 @@ public abstract class AbstractPageViewer extends ContentViewer implements Observ
                        res = Subject.getSubject(accessControlContext);
                }
                if (res == null)
-                       throw new CmsException("No subject associated with this viewer");
+                       throw new IllegalStateException("No subject associated with this viewer");
                return res;
        }
 
index 1ae44d0f74147843243ddb54710fc45a7d819c1b..5fadbc07ba436b0a0a867f4c602e3b68d3ba51df 100644 (file)
@@ -5,8 +5,7 @@ import javax.jcr.RepositoryException;
 
 import org.argeo.cms.ui.util.CmsUiUtils;
 import org.eclipse.swt.SWT;
-import org.eclipse.swt.events.FocusEvent;
-import org.eclipse.swt.events.FocusListener;
+import org.eclipse.swt.graphics.Color;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
@@ -19,9 +18,13 @@ public class EditableText extends StyledControl {
 
        private boolean editable = true;
 
+       private Color highlightColor;
+       private Composite highlight;
+
        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 {
@@ -31,6 +34,7 @@ public class EditableText extends StyledControl {
        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
@@ -46,7 +50,8 @@ public class EditableText extends StyledControl {
        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);
@@ -54,16 +59,31 @@ public class EditableText extends StyledControl {
        }
 
        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)
@@ -82,7 +102,7 @@ public class EditableText extends StyledControl {
 
        public String getText() {
                Control child = getControl();
-               
+
                if (child instanceof Label)
                        return ((Label) child).getText();
                else if (child instanceof Text)
index 0455d2cdc9e1f0669b8e5eecd521f1a5c53c83d3..1814131f4b39d5f8bc32dc73a572512ec2d405fb 100644 (file)
@@ -39,11 +39,17 @@ public abstract class StyledControl extends JcrComposite implements CmsConstants
 
        protected abstract Control createControl(Composite box, String style);
 
-       protected Composite createBox(Composite parent) {
-               Composite box = new Composite(parent, SWT.INHERIT_DEFAULT);
+       protected Composite createBox() {
+               Composite box = new Composite(container, SWT.INHERIT_DEFAULT);
+               setContainerLayoutData(box);
+               box.setLayout(CmsUiUtils.noSpaceGridLayout(3));
+               return box;
+       }
+
+       protected Composite createContainer() {
+               Composite box = new Composite(this, SWT.INHERIT_DEFAULT);
                setContainerLayoutData(box);
                box.setLayout(CmsUiUtils.noSpaceGridLayout());
-               // new Label(box, SWT.NONE).setText("BOX");
                return box;
        }
 
@@ -91,10 +97,10 @@ public abstract class StyledControl extends JcrComposite implements CmsConstants
                control = createControl(box, style);
                setControlLayoutData(control);
 
-//             control.getParent().setData(STYLE, style + "_box");
-               EclipseUiSpecificUtils.setStyleData(control.getParent(), style + "_box");
-//             control.getParent().getParent().setData(STYLE, style + "_container");
-               EclipseUiSpecificUtils.setStyleData(control.getParent().getParent(), style + "_container");
+               if (style != null) {
+                       CmsUiUtils.style(box, style + "_box");
+                       CmsUiUtils.style(container, style + "_container");
+               }
        }
 
        /** To be overridden */
@@ -111,8 +117,8 @@ public abstract class StyledControl extends JcrComposite implements CmsConstants
                if (deep) {
                        for (Control control : getChildren())
                                control.dispose();
-                       container = createBox(this);
-                       box = createBox(container);
+                       container = createContainer();
+                       box = createBox();
                } else {
                        control.dispose();
                }