Improve forms
authorMathieu Baudier <mbaudier@argeo.org>
Thu, 22 Jun 2023 09:49:05 +0000 (11:49 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Thu, 22 Jun 2023 09:49:05 +0000 (11:49 +0200)
swt/org.argeo.app.swt/src/org/argeo/app/swt/terms/AbstractTermsPart.java
swt/org.argeo.app.swt/src/org/argeo/app/swt/terms/MultiTermsPart.java
swt/org.argeo.app.swt/src/org/argeo/app/swt/terms/SingleTermPart.java
swt/org.argeo.app.swt/src/org/argeo/app/swt/ux/SuiteSwtUtils.java

index e288143bdd8ade19e8d8005601ce351e87fcbce8..2ca2a57a00e34debe48be2b8b22f4fd460b36775 100644 (file)
@@ -17,7 +17,6 @@ import org.eclipse.swt.graphics.Color;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
-import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Text;
 import org.eclipse.swt.widgets.ToolItem;
 
@@ -40,6 +39,10 @@ public abstract class AbstractTermsPart extends StyledControl implements SwtEdit
 
        protected final CmsSwtTheme theme;
 
+       private int iconsSize = 12;
+
+       private String message;
+
        @SuppressWarnings("rawtypes")
        private Class<? extends Enum> localized;
 
@@ -90,7 +93,7 @@ public abstract class AbstractTermsPart extends StyledControl implements SwtEdit
                return true;
        }
 
-       protected void processTermListLabel(Term term, Label label) {
+       protected void processTermListLabel(Term term, Text label) {
 
        }
 
@@ -129,17 +132,22 @@ public abstract class AbstractTermsPart extends StyledControl implements SwtEdit
        }
 
        protected void styleCancel(ToolItem cancelItem) {
-               if (cancelIcon != null)
-                       cancelItem.setImage(theme.getSmallIcon(cancelIcon));
-               else
+               if (cancelIcon != null) {
+                       // cancelItem.setImage(theme.getSmallIcon(cancelIcon));
+                       cancelItem.setImage(theme.getIcon(cancelIcon.name(), iconsSize));
+
+               } else {
                        cancelItem.setText("X");
+               }
        }
 
        protected void styleAdd(ToolItem addItem) {
-               if (addIcon != null)
-                       addItem.setImage(theme.getSmallIcon(addIcon));
-               else
+               if (addIcon != null) {
+//                     addItem.setImage(theme.getSmallIcon(addIcon));
+                       addItem.setImage(theme.getIcon(addIcon.name(), iconsSize));
+               } else {
                        addItem.setText("+");
+               }
        }
 
        @Override
@@ -160,4 +168,12 @@ public abstract class AbstractTermsPart extends StyledControl implements SwtEdit
                this.localized = localized;
        }
 
+       public String getMessage() {
+               return message;
+       }
+
+       public void setMessage(String message) {
+               this.message = message;
+       }
+
 }
index 16f2413e510eef035a80cc3eea8389c25a260b5c..80b6f721e41b3b8fd74c71182c607ccbaaa30887 100644 (file)
@@ -8,7 +8,7 @@ import org.argeo.api.acr.NamespaceUtils;
 import org.argeo.api.cms.CmsLog;
 import org.argeo.app.api.Term;
 import org.argeo.app.api.TermsManager;
-import org.argeo.app.swt.forms.FormStyle;
+import org.argeo.app.ux.SuiteStyle;
 import org.argeo.cms.swt.CmsSwtUtils;
 import org.argeo.cms.swt.MouseDoubleClick;
 import org.argeo.cms.swt.MouseDown;
@@ -22,7 +22,6 @@ import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.layout.RowLayout;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
-import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Text;
 import org.eclipse.swt.widgets.ToolBar;
 import org.eclipse.swt.widgets.ToolItem;
@@ -51,10 +50,11 @@ public class MultiTermsPart extends AbstractTermsPart {
                        for (Term value : currentValue) {
                                Composite block = new Composite(placeholder, SWT.NONE);
                                block.setLayout(CmsSwtUtils.noSpaceGridLayout(3));
-                               Label lbl = new Label(block, SWT.NONE);
+                               Text lbl = new Text(block, SWT.NONE);
+                               lbl.setEditable(false);
                                String display = getTermLabel(value);
                                lbl.setText(display);
-                               CmsSwtUtils.style(lbl, style == null ? FormStyle.propertyText.style() : style);
+                               CmsSwtUtils.style(lbl, style == null ? SuiteStyle.simpleInput.style() : style);
                                processTermListLabel(value, lbl);
                                if (isEditable())
                                        lbl.addMouseListener((MouseDoubleClick) (e) -> {
@@ -100,7 +100,7 @@ public class MultiTermsPart extends AbstractTermsPart {
                        txt.setLayoutData(CmsSwtUtils.fillWidth());
 //                     txt.setMessage("[new]");
 
-                       CmsSwtUtils.style(txt, style == null ? FormStyle.propertyText.style() : style);
+                       CmsSwtUtils.style(txt, style == null ? SuiteStyle.simpleInput.style() : style);
 
                        ToolBar toolBar = new ToolBar(block, SWT.HORIZONTAL);
                        ToolItem cancelItem = new ToolItem(toolBar, SWT.FLAT);
@@ -158,7 +158,8 @@ public class MultiTermsPart extends AbstractTermsPart {
                        String display = getTermLabel(term);
                        if (filter != null && !display.toLowerCase().contains(filter))
                                continue terms;
-                       Label termL = new Label(contextArea, SWT.WRAP);
+                       Text termL = new Text(contextArea, SWT.WRAP);
+                       termL.setEditable(false);
                        termL.setText(display);
                        processTermListLabel(term, termL);
                        if (isTermSelectable(term))
index c918c25d1d098aa811723a035dcaa63d70466ad4..0a1abda3744636e659248f725b3f6e8118b4e025 100644 (file)
@@ -5,7 +5,7 @@ import java.util.List;
 import org.argeo.api.acr.Content;
 import org.argeo.app.api.Term;
 import org.argeo.app.api.TermsManager;
-import org.argeo.app.swt.forms.FormStyle;
+import org.argeo.app.ux.SuiteStyle;
 import org.argeo.cms.swt.CmsSwtUtils;
 import org.argeo.cms.swt.MouseDoubleClick;
 import org.argeo.cms.swt.MouseDown;
@@ -18,7 +18,6 @@ import org.eclipse.swt.events.FocusListener;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
-import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Text;
 import org.eclipse.swt.widgets.ToolBar;
 import org.eclipse.swt.widgets.ToolItem;
@@ -39,8 +38,8 @@ public class SingleTermPart extends AbstractTermsPart {
 
                        createHighlight(block);
 
-                       Text txt = new Text(block, SWT.SINGLE | SWT.BORDER);
-                       CmsSwtUtils.style(txt, style == null ? FormStyle.propertyText.style() : style);
+                       Text txt = new Text(block, SWT.SINGLE);
+                       CmsSwtUtils.style(txt, style == null ? SuiteStyle.simpleInput.style() : style);
 
                        ToolBar toolBar = new ToolBar(block, SWT.HORIZONTAL);
                        if (isCanDelete()) {
@@ -97,10 +96,11 @@ public class SingleTermPart extends AbstractTermsPart {
                        block.setLayout(CmsSwtUtils.noSpaceGridLayout(2));
                        Term currentValue = getValue();
                        if (currentValue != null) {
-                               Label lbl = new Label(block, SWT.SINGLE);
+                               Text lbl = new Text(block, SWT.SINGLE);
+                               lbl.setEditable(false);
                                String display = getTermLabel(currentValue);
                                lbl.setText(display);
-                               CmsSwtUtils.style(lbl, style == null ? FormStyle.propertyText.style() : style);
+                               CmsSwtUtils.style(lbl, style == null ? SuiteStyle.simpleInput.style() : style);
                                processTermListLabel(currentValue, lbl);
                                if (isEditable()) {
                                        lbl.addMouseListener((MouseDoubleClick) (e) -> {
@@ -109,6 +109,7 @@ public class SingleTermPart extends AbstractTermsPart {
                                }
                        } else {
                                if (isEditable()) {
+
                                        ToolBar toolBar = new ToolBar(block, SWT.HORIZONTAL);
                                        ToolItem addItem = new ToolItem(toolBar, SWT.FLAT);
                                        styleAdd(addItem);
@@ -116,6 +117,13 @@ public class SingleTermPart extends AbstractTermsPart {
                                                startEditing();
                                        });
                                }
+                               // add dummy text so that height wont's move afterwards
+                               Text lbl = new Text(block, SWT.SINGLE);
+                               lbl.setEditable(false);
+                               if (!isEditable()) {// empty, non editable
+                                       if (getMessage() != null)
+                                               lbl.setMessage(getMessage());
+                               }
                        }
                        return block;
                }
@@ -129,7 +137,8 @@ public class SingleTermPart extends AbstractTermsPart {
                        String display = getTermLabel(term);
                        if (filter != null && !display.toLowerCase().contains(filter))
                                continue terms;
-                       Label termL = new Label(contextArea, SWT.WRAP);
+                       Text termL = new Text(contextArea, SWT.WRAP);
+                       termL.setEditable(false);
                        termL.setText(display);
                        processTermListLabel(term, termL);
                        if (isTermSelectable(term))
index cfd758fae82e12b3f456a9dc045c3b8b3d2c1ddd..36e018ea4411785dfa818aecefe653664c354c33 100644 (file)
@@ -1,5 +1,7 @@
 package org.argeo.app.swt.ux;
 
+import java.util.function.Predicate;
+
 import javax.xml.namespace.QName;
 
 import org.argeo.api.acr.Content;
@@ -8,8 +10,16 @@ import org.argeo.api.cms.ux.CmsEditable;
 import org.argeo.app.ux.SuiteStyle;
 import org.argeo.cms.Localized;
 import org.argeo.cms.swt.CmsSwtUtils;
+import org.argeo.cms.swt.dialogs.CmsFeedback;
+import org.argeo.cms.swt.widgets.EditableText;
 import org.argeo.eclipse.ui.EclipseUiUtils;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.FocusEvent;
+import org.eclipse.swt.events.FocusListener;
+import org.eclipse.swt.events.MouseAdapter;
+import org.eclipse.swt.events.MouseEvent;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Composite;
@@ -48,7 +58,7 @@ public class SuiteSwtUtils {
        public static Label addFormLabel(Composite parent, String label) {
                Label lbl = new Label(parent, SWT.WRAP);
                lbl.setText(label);
-               // lbl.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, true, true));
+               lbl.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
                CmsSwtUtils.style(lbl, SuiteStyle.simpleLabel);
                return lbl;
        }
@@ -63,8 +73,8 @@ public class SuiteSwtUtils {
                        txt.setText(text);
                if (message != null)
                        txt.setMessage(message);
-               txt.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, true, true));
-               CmsSwtUtils.style(txt, SuiteStyle.simpleText);
+               txt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
+               CmsSwtUtils.style(txt, SuiteStyle.simpleInput);
                return txt;
        }
 
@@ -72,22 +82,23 @@ public class SuiteSwtUtils {
                return addFormTextField(parent, text, message, SWT.NONE);
        }
 
-       public static Text addFormInputField(Composite parent, String placeholder) {
-               Text txt = new Text(parent, SWT.BORDER);
-
-               GridData gridData = CmsSwtUtils.fillWidth();
-               txt.setLayoutData(gridData);
-
-               if (placeholder != null)
-                       txt.setText(placeholder);
-
-               CmsSwtUtils.style(txt, SuiteStyle.simpleInput);
-               return txt;
-       }
+//     public static Text addFormInputField(Composite parent, String placeholder) {
+//             Text txt = new Text(parent, SWT.BORDER);
+//
+//             GridData gridData = CmsSwtUtils.fillWidth();
+//             txt.setLayoutData(gridData);
+//
+//             if (placeholder != null)
+//                     txt.setText(placeholder);
+//
+//             CmsSwtUtils.style(txt, SuiteStyle.simpleInput);
+//             return txt;
+//     }
 
        public static Composite addLineComposite(Composite parent, int columns) {
                Composite lineComposite = new Composite(parent, SWT.NONE);
-               lineComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
+               GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
+               lineComposite.setLayoutData(gd);
                lineComposite.setLayout(new GridLayout(columns, false));
                CmsSwtUtils.style(lineComposite, SuiteStyle.formLine);
                return lineComposite;
@@ -109,13 +120,13 @@ public class SuiteSwtUtils {
                return txt;
        }
 
-       public static Text addFormInput(Composite parent, String label, String placeholder) {
-               Composite lineComposite = addLineComposite(parent, 2);
-               addFormLabel(lineComposite, label);
-               Text txt = addFormInputField(lineComposite, placeholder);
-               txt.setLayoutData(CmsSwtUtils.fillWidth());
-               return txt;
-       }
+//     public static Text addFormInput(Composite parent, String label, String placeholder) {
+//             Composite lineComposite = addLineComposite(parent, 2);
+//             addFormLabel(lineComposite, label);
+//             Text txt = addFormInputField(lineComposite, placeholder);
+//             txt.setLayoutData(CmsSwtUtils.fillWidth());
+//             return txt;
+//     }
 
        /**
         * creates a single horizontal-block composite for key:value display, with
@@ -190,6 +201,84 @@ public class SuiteSwtUtils {
                return addFormLine(parent, label.lead(), content, property.qName(), cmsEditable);
        }
 
+       public static EditableText addTextLine(Composite parent, int style, Localized msg, Content content, QNamed attr,
+                       CmsEditable cmsEditable, boolean line, Predicate<String> validator) {
+               Composite parentToUse = line ? SuiteSwtUtils.addLineComposite(parent, 2) : parent;
+               SuiteSwtUtils.addFormLabel(parentToUse, msg.lead());
+               EditableText text = createFormText(parentToUse, style, msg, content, attr, cmsEditable, validator);
+               return text;
+       }
+
+       public static EditableText createFormText(Composite parent, int style, Localized msg, Content content, QNamed attr,
+                       CmsEditable cmsEditable, Predicate<String> validator) {
+               EditableText text = new EditableText(parent, style | SWT.FLAT | (cmsEditable.isEditing() ? 0 : SWT.READ_ONLY));
+               text.setMessage("-");
+               text.setLayoutData(CmsSwtUtils.fillWidth());
+               text.setStyle(SuiteStyle.simpleInput);
+               String txt = content.attr(attr);
+               if (txt == null)
+                       txt = "";
+               text.setText(txt);
+               if (cmsEditable.isEditing())
+                       text.setMouseListener(new MouseAdapter() {
+
+                               private static final long serialVersionUID = 1L;
+
+                               @Override
+                               public void mouseDoubleClick(MouseEvent e) {
+                                       String currentTxt = text.getText();
+                                       text.startEditing();
+                                       text.setText(currentTxt);
+
+                                       Runnable save = () -> {
+                                               String editedTxt = text.getText();
+                                               if (validator != null) {
+                                                       if (!validator.test(editedTxt)) {
+                                                               text.stopEditing();
+                                                               text.setText(currentTxt);
+                                                               CmsFeedback.show(editedTxt + " is not properly formatted");
+                                                               return;
+                                                               // throw new IllegalArgumentException(editedTxt + " is not properly formatted");
+                                                       }
+                                               }
+                                               content.put(attr, editedTxt);
+                                               text.stopEditing();
+                                               text.setText(editedTxt);
+                                               text.getParent().layout(new Control[] { text.getControl() });
+                                       };
+                                       ((Text) text.getControl()).addSelectionListener(new SelectionListener() {
+
+                                               private static final long serialVersionUID = 1L;
+
+                                               @Override
+                                               public void widgetSelected(SelectionEvent e) {
+                                               }
+
+                                               @Override
+                                               public void widgetDefaultSelected(SelectionEvent e) {
+                                                       save.run();
+                                               }
+                                       });
+                                       ((Text) text.getControl()).addFocusListener(new FocusListener() {
+
+                                               private static final long serialVersionUID = 333838002411959302L;
+
+                                               @Override
+                                               public void focusLost(FocusEvent event) {
+                                                       save.run();
+                                               }
+
+                                               @Override
+                                               public void focusGained(FocusEvent event) {
+                                               }
+                                       });
+
+                               }
+
+                       });
+               return text;
+       }
+
        public static Text addFormLine(Composite parent, String label, Content content, QName property,
                        CmsEditable cmsEditable) {
                Composite lineComposite = SuiteSwtUtils.addLineComposite(parent, 2);
@@ -216,7 +305,7 @@ public class SuiteSwtUtils {
                        CmsEditable cmsEditable) {
                SuiteSwtUtils.addFormLabel(parent, label);
                String text = content.attr(property);
-               Text txt = SuiteSwtUtils.addFormTextField(parent, text, null, SWT.WRAP);
+               Text txt = SuiteSwtUtils.addFormTextField(parent, text, null, 0);
                if (cmsEditable != null && cmsEditable.isEditing()) {
                        txt.addModifyListener((e) -> {
                                content.put(property, txt.getText());