From: Mathieu Baudier Date: Sun, 17 Jan 2021 11:52:14 +0000 (+0100) Subject: Improve form framework. X-Git-Tag: argeo-commons-2.1.91~25 X-Git-Url: https://git.argeo.org/?p=lgpl%2Fargeo-commons.git;a=commitdiff_plain;h=fb3182380d9688998a81d853a4eebcf87b6a48c7 Improve form framework. --- diff --git a/org.argeo.cms.ui/src/org/argeo/cms/ui/forms/EditablePropertyString.java b/org.argeo.cms.ui/src/org/argeo/cms/ui/forms/EditablePropertyString.java index 265621d32..52bb596ee 100644 --- a/org.argeo.cms.ui/src/org/argeo/cms/ui/forms/EditablePropertyString.java +++ b/org.argeo.cms.ui/src/org/argeo/cms/ui/forms/EditablePropertyString.java @@ -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(); } diff --git a/org.argeo.cms.ui/src/org/argeo/cms/ui/viewers/AbstractPageViewer.java b/org.argeo.cms.ui/src/org/argeo/cms/ui/viewers/AbstractPageViewer.java index 1445fed33..e23e3ba79 100644 --- a/org.argeo.cms.ui/src/org/argeo/cms/ui/viewers/AbstractPageViewer.java +++ b/org.argeo.cms.ui/src/org/argeo/cms/ui/viewers/AbstractPageViewer.java @@ -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. */ diff --git a/org.argeo.cms.ui/src/org/argeo/cms/ui/viewers/EditablePart.java b/org.argeo.cms.ui/src/org/argeo/cms/ui/viewers/EditablePart.java index 158beaf1e..3967c97fb 100644 --- a/org.argeo.cms.ui/src/org/argeo/cms/ui/viewers/EditablePart.java +++ b/org.argeo.cms.ui/src/org/argeo/cms/ui/viewers/EditablePart.java @@ -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(); diff --git a/org.argeo.cms.ui/src/org/argeo/cms/ui/widgets/EditableText.java b/org.argeo.cms.ui/src/org/argeo/cms/ui/widgets/EditableText.java index 21f48138c..1ae44d0f7 100644 --- a/org.argeo.cms.ui/src/org/argeo/cms/ui/widgets/EditableText.java +++ b/org.argeo.cms.ui/src/org/argeo/cms/ui/widgets/EditableText.java @@ -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; + } + } diff --git a/org.argeo.cms.ui/src/org/argeo/cms/ui/widgets/JcrComposite.java b/org.argeo.cms.ui/src/org/argeo/cms/ui/widgets/JcrComposite.java index a3cdb9827..0708365bd 100644 --- a/org.argeo.cms.ui/src/org/argeo/cms/ui/widgets/JcrComposite.java +++ b/org.argeo.cms.ui/src/org/argeo/cms/ui/widgets/JcrComposite.java @@ -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); diff --git a/org.argeo.jcr/src/org/argeo/jcr/JcrxApi.java b/org.argeo.jcr/src/org/argeo/jcr/JcrxApi.java index 0223b6944..8f1ee9fa9 100644 --- a/org.argeo.jcr/src/org/argeo/jcr/JcrxApi.java +++ b/org.argeo.jcr/src/org/argeo/jcr/JcrxApi.java @@ -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); } }