Improve form framework.
authorMathieu Baudier <mbaudier@argeo.org>
Sun, 17 Jan 2021 11:52:14 +0000 (12:52 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Sun, 17 Jan 2021 11:52:14 +0000 (12:52 +0100)
org.argeo.cms.ui/src/org/argeo/cms/ui/forms/EditablePropertyString.java
org.argeo.cms.ui/src/org/argeo/cms/ui/viewers/AbstractPageViewer.java
org.argeo.cms.ui/src/org/argeo/cms/ui/viewers/EditablePart.java
org.argeo.cms.ui/src/org/argeo/cms/ui/widgets/EditableText.java
org.argeo.cms.ui/src/org/argeo/cms/ui/widgets/JcrComposite.java
org.argeo.jcr/src/org/argeo/jcr/JcrxApi.java

index 265621d32c0b838bc3c7ae4562df763000a36c59..52bb596ee1fc44fe4fd222516adc9905ec638dec 100644 (file)
@@ -63,17 +63,14 @@ public class EditablePropertyString extends EditableText implements EditablePart
 
        public synchronized void startEditing() {
                CmsUiUtils.style(getControl(), FormStyle.propertyText);
-//             getControl().setData(STYLE, propertyText.style());
                super.startEditing();
        }
 
        public synchronized void stopEditing() {
                if (EclipseUiUtils.isEmpty(((Text) getControl()).getText()))
                        CmsUiUtils.style(getControl(), FormStyle.propertyMessage);
-//                     getControl().setData(STYLE, propertyMessage.style());
                else
                        CmsUiUtils.style(getControl(), FormStyle.propertyText);
-//                     getControl().setData(STYLE, propertyText.style());
                super.stopEditing();
        }
 
index 1445fed33a82caf7186c9b77f8a238ba5e5809c9..e23e3ba7957320a67cdd82cb0d5de5f18c4561be 100644 (file)
@@ -16,6 +16,7 @@ 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;
 import org.eclipse.jface.viewers.ContentViewer;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.StructuredSelection;
@@ -190,13 +191,25 @@ public abstract class AbstractPageViewer extends ContentViewer implements Observ
                        updateContent(part);
                        prepare(part, caretPosition);
                        edited = part;
+                       edited.getControl().addFocusListener(new FocusListener() {
+
+                               @Override
+                               public void focusLost(FocusEvent event) {
+                                       stopEditing(true);
+                               }
+
+                               @Override
+                               public void focusGained(FocusEvent event) {
+                               }
+                       });
+
                        layout(part.getControl());
                } catch (RepositoryException e) {
                        throw new CmsException("Cannot edit " + part, e);
                }
        }
 
-       private void stopEditing(Boolean save) throws RepositoryException {
+       protected void stopEditing(Boolean save) {
                if (edited instanceof Widget && ((Widget) edited).isDisposed()) {
                        edited = null;
                        return;
@@ -209,32 +222,29 @@ public abstract class AbstractPageViewer extends ContentViewer implements Observ
                        return;
                }
 
-               if (save)
-                       save(edited);
+               try {
+                       if (save)
+                               save(edited);
 
-               edited.stopEditing();
-               updateContent(edited);
-               layout(((EditablePart) edited).getControl());
-               edited = null;
+                       edited.stopEditing();
+                       updateContent(edited);
+                       layout(((EditablePart) edited).getControl());
+               } catch (RepositoryException e) {
+                       throw new JcrException("Cannot stop editing", e);
+               } finally {
+                       edited = null;
+               }
        }
 
        // METHODS AVAILABLE TO EXTENDING CLASSES
        protected void saveEdit() {
-               try {
-                       if (edited != null)
-                               stopEditing(true);
-               } catch (RepositoryException e) {
-                       throw new CmsException("Cannot stop editing", e);
-               }
+               if (edited != null)
+                       stopEditing(true);
        }
 
        protected void cancelEdit() {
-               try {
-                       if (edited != null)
-                               stopEditing(false);
-               } catch (RepositoryException e) {
-                       throw new CmsException("Cannot cancel editing", e);
-               }
+               if (edited != null)
+                       stopEditing(false);
        }
 
        /** Layout this controls from the related base page. */
index 158beaf1ea1e26502211a260bf5da30cae5ad921..3967c97fbe62c3c584d489cf65d650d3160842ac 100644 (file)
@@ -2,6 +2,7 @@ package org.argeo.cms.ui.viewers;
 
 import org.eclipse.swt.widgets.Control;
 
+/** Manages whether an editable or non editable control is shown. */
 public interface EditablePart {
        public void startEditing();
 
index 21f48138ce92155d0c6c4687f393250f2c3da1bf..1ae44d0f74147843243ddb54710fc45a7d819c1b 100644 (file)
@@ -5,6 +5,8 @@ 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.layout.GridData;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
@@ -15,26 +17,30 @@ 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;
+
+       public EditableText(Composite parent, int style) {
+               super(parent, style);
+               editable = !(SWT.READ_ONLY == (style & SWT.READ_ONLY));
        }
 
-       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));
        }
 
        @Override
        protected Control createControl(Composite box, String style) {
-               if (isEditing())
-                       return createText(box, style);
-               else
+               if (isEditing() && getEditable()) {
+                       return createText(box, style, true);
+               } else {
+//                     return createText(box, style, false);
                        return createLabel(box, style);
+               }
        }
 
        protected Label createLabel(Composite box, String style) {
@@ -47,8 +53,9 @@ public class EditableText extends StyledControl {
                return lbl;
        }
 
-       protected Text createText(Composite box, String style) {
+       protected Text createText(Composite box, String style, boolean editable) {
                final Text text = new Text(box, getStyle() | SWT.MULTI | SWT.WRAP);
+               text.setEditable(editable);
                GridData textLayoutData = CmsUiUtils.fillWidth();
                // textLayoutData.heightHint = preferredHeight;
                text.setLayoutData(textLayoutData);
@@ -73,4 +80,19 @@ 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());
+       }
+
+       public boolean getEditable() {
+               return editable;
+       }
+
 }
index a3cdb9827f31027c057b21350b9a503f1a8c1fd8..0708365bda6f33d25957251df1fafbc060fa0bfd 100644 (file)
@@ -37,11 +37,11 @@ public class JcrComposite extends Composite {
                if (item != null)
                        try {
                                this.session = item.getSession();
-                               if (!cacheImmediately && (SWT.READ_ONLY == (style & SWT.READ_ONLY))) {
-                                       // (useless?) optimization: we only save a pointer to the session,
-                                       // not even a reference to the item
-                                       this.nodeId = null;
-                               } else {
+//                             if (!cacheImmediately && (SWT.READ_ONLY == (style & SWT.READ_ONLY))) {
+//                                     // (useless?) optimization: we only save a pointer to the session,
+//                                     // not even a reference to the item
+//                                     this.nodeId = null;
+//                             } else {
                                        Node node;
                                        Property property = null;
                                        if (item instanceof Node) {
@@ -56,7 +56,7 @@ public class JcrComposite extends Composite {
                                        this.nodeId = node.getIdentifier();
                                        if (cacheImmediately)
                                                this.cache = node;
-                               }
+//                             }
                                setLayout(CmsUiUtils.noSpaceGridLayout());
                        } catch (RepositoryException e) {
                                throw new IllegalStateException("Cannot create composite from " + item, e);
index 0223b69444a94054564603947bc4e178e438ae93..8f1ee9fa9a4f67752dd1661c460f08cb662b72d3 100644 (file)
@@ -56,14 +56,22 @@ public class JcrxApi {
                try {
                        if (node.hasNode(name)) {
                                Node child = node.getNode(name);
-                               if (!child.hasNode(Jcr.JCR_XMLTEXT))
-                                       child.addNode(Jcr.JCR_XMLTEXT, JcrxType.JCRX_XMLTEXT);
-                               child.getNode(Jcr.JCR_XMLTEXT).setProperty(Jcr.JCR_XMLCHARACTERS, value);
+                               setXmlValue(node, child, value);
                        } else
                                node.addNode(name, JcrxType.JCRX_XMLVALUE).addNode(Jcr.JCR_XMLTEXT, JcrxType.JCRX_XMLTEXT)
                                                .setProperty(Jcr.JCR_XMLCHARACTERS, value);
                } catch (RepositoryException e) {
-                       throw new IllegalStateException("Cannot set " + name + " as XML text", e);
+                       throw new JcrException("Cannot set " + name + " as XML text", e);
+               }
+       }
+
+       public static void setXmlValue(Node node, Node child, String value) {
+               try {
+                       if (!child.hasNode(Jcr.JCR_XMLTEXT))
+                               child.addNode(Jcr.JCR_XMLTEXT, JcrxType.JCRX_XMLTEXT);
+                       child.getNode(Jcr.JCR_XMLTEXT).setProperty(Jcr.JCR_XMLCHARACTERS, value);
+               } catch (RepositoryException e) {
+                       throw new JcrException("Cannot set " + child + " as XML text", e);
                }
        }