Make picture visualisation more robust.
[gpl/argeo-suite.git] / org.argeo.suite.ui / src / org / argeo / suite / ui / SuiteUiUtils.java
index ace189c2bef68b0dea152f4f5b50632cebef22c4..be14a823d11b0fefd0c7328430fb921da8c4121f 100644 (file)
@@ -7,25 +7,27 @@ import javax.jcr.Node;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
-import org.argeo.api.NodeConstants;
 import org.argeo.cms.Localized;
-import org.argeo.cms.ui.dialogs.CmsWizardDialog;
+import org.argeo.cms.ui.CmsEditable;
+import org.argeo.cms.ui.dialogs.LightweightDialog;
 import org.argeo.cms.ui.util.CmsUiUtils;
 import org.argeo.eclipse.ui.EclipseUiUtils;
 import org.argeo.entity.EntityNames;
 import org.argeo.entity.EntityType;
-import org.argeo.jcr.JcrException;
+import org.argeo.jcr.Jcr;
 import org.argeo.jcr.JcrUtils;
-import org.eclipse.jface.window.Window;
-import org.eclipse.jface.wizard.Wizard;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.ScrolledComposite;
+import org.eclipse.swt.events.MouseEvent;
+import org.eclipse.swt.events.MouseListener;
 import org.eclipse.swt.graphics.ImageData;
+import org.eclipse.swt.graphics.Point;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Label;
-import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.Text;
 
 /** UI utilities related to the APAF project. */
@@ -58,13 +60,17 @@ public class SuiteUiUtils {
        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.LEAD, SWT.CENTER, true, true));
                CmsUiUtils.style(lbl, SuiteStyle.simpleLabel);
                return lbl;
        }
 
        public static Text addFormTextField(Composite parent, String text, String message) {
-               Text txt = new Text(parent, SWT.WRAP);
+               return addFormTextField(parent, text, message, SWT.NONE);
+       }
+
+       public static Text addFormTextField(Composite parent, String text, String message, int style) {
+               Text txt = new Text(parent, style);
                if (text != null)
                        txt.setText(text);
                if (message != null)
@@ -95,6 +101,29 @@ public class SuiteUiUtils {
                CmsUiUtils.style(lineComposite, SuiteStyle.formLine);
                addFormLabel(lineComposite, label);
                Text txt = addFormTextField(lineComposite, text, null);
+               txt.setEditable(false);
+               txt.setLayoutData(CmsUiUtils.fillWidth());
+               return txt;
+       }
+
+       public static Text addFormLine(Composite parent, String label, Node node, String property,
+                       CmsEditable cmsEditable) {
+               Composite lineComposite = new Composite(parent, SWT.NONE);
+               lineComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
+               lineComposite.setLayout(new GridLayout(2, false));
+               CmsUiUtils.style(lineComposite, SuiteStyle.formLine);
+               addFormLabel(lineComposite, label);
+               String text = Jcr.get(node, property);
+//             int style = cmsEditable.isEditing() ? SWT.WRAP : SWT.WRAP;
+               Text txt = addFormTextField(lineComposite, text, null, SWT.WRAP);
+               if (cmsEditable != null && cmsEditable.isEditing()) {
+                       txt.addModifyListener((e) -> {
+                               Jcr.set(node, property, txt.getText());
+                               Jcr.save(node);
+                       });
+               } else {
+                       txt.setEditable(false);
+               }
                txt.setLayoutData(CmsUiUtils.fillWidth());
                return txt;
        }
@@ -131,11 +160,33 @@ public class SuiteUiUtils {
 
        /** creates a single vertical-block composite for key:value display */
        public static Text addFormColumn(Composite parent, String label, String text) {
-               Composite columnComposite = new Composite(parent, SWT.NONE);
-               columnComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
-               columnComposite.setLayout(new GridLayout(1, false));
-               addFormLabel(columnComposite, label);
-               Text txt = addFormTextField(columnComposite, text, null);
+//             Composite columnComposite = new Composite(parent, SWT.NONE);
+//             columnComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
+//             columnComposite.setLayout(new GridLayout(1, false));
+               addFormLabel(parent, label);
+               Text txt = addFormTextField(parent, text, null);
+               txt.setEditable(false);
+               txt.setLayoutData(CmsUiUtils.fillWidth());
+               return txt;
+       }
+
+       public static Text addFormColumn(Composite parent, String label, Node node, String property,
+                       CmsEditable cmsEditable) {
+//             Composite columnComposite = new Composite(parent, SWT.NONE);
+//             columnComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
+//             columnComposite.setLayout(new GridLayout(1, false));
+               addFormLabel(parent, label);
+               String text = Jcr.get(node, property);
+//             int style = cmsEditable.isEditing() ? SWT.WRAP : SWT.WRAP;
+               Text txt = addFormTextField(parent, text, null, SWT.WRAP);
+               if (cmsEditable != null && cmsEditable.isEditing()) {
+                       txt.addModifyListener((e) -> {
+                               Jcr.set(node, property, txt.getText());
+                               Jcr.save(node);
+                       });
+               } else {
+                       txt.setEditable(false);
+               }
                txt.setLayoutData(CmsUiUtils.fillWidth());
                return txt;
        }
@@ -166,19 +217,35 @@ public class SuiteUiUtils {
                Node content = fileNode.getNode(Node.JCR_CONTENT);
                // TODO move it deeper in the middleware.
                if (!content.isNodeType(EntityType.box.get())) {
+                       if (content.getSession().hasPermission(content.getPath(), Session.ACTION_SET_PROPERTY)) {
+                               try (InputStream in = JcrUtils.getFileAsStream(fileNode)) {
+                                       ImageData imageData = new ImageData(in);
+                                       content.addMixin(EntityType.box.get());
+                                       content.setProperty(EntityNames.SVG_WIDTH, imageData.width);
+                                       content.setProperty(EntityNames.SVG_HEIGHT, imageData.height);
+                                       content.getSession().save();
+                               } catch (IOException e) {
+                                       throw new RuntimeException(e);
+                               }
+                       }
+               }
+
+               // TODO optimise
+               Long width;
+               Long height;
+               if (content.isNodeType(EntityType.box.get())) {
+                       width = content.getProperty(EntityNames.SVG_WIDTH).getLong();
+                       height = content.getProperty(EntityNames.SVG_HEIGHT).getLong();
+               } else {
                        try (InputStream in = JcrUtils.getFileAsStream(fileNode)) {
                                ImageData imageData = new ImageData(in);
-                               content.addMixin(EntityType.box.get());
-                               content.setProperty(EntityNames.SVG_WIDTH, imageData.width);
-                               content.setProperty(EntityNames.SVG_HEIGHT, imageData.height);
-                               content.getSession().save();
+                               width = Long.valueOf(imageData.width);
+                               height = Long.valueOf(imageData.height);
                        } catch (IOException e) {
-                               throw new IllegalStateException(e);
+                               throw new RuntimeException(e);
                        }
                }
 
-               Long width = content.getProperty(EntityNames.SVG_WIDTH).getLong();
-               Long height = content.getProperty(EntityNames.SVG_HEIGHT).getLong();
                if (maxWidth != null && width > maxWidth) {
                        Double ratio = maxWidth.doubleValue() / width.doubleValue();
                        width = maxWidth.longValue();
@@ -187,44 +254,95 @@ public class SuiteUiUtils {
                Label img = new Label(parent, SWT.NONE);
                CmsUiUtils.markup(img);
                img.setText(CmsUiUtils.img(fileNode, width.toString(), height.toString()));
-               if (parent.getLayout() instanceof GridLayout)
-                       img.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
-               return img;
-       }
+               if (parent.getLayout() instanceof GridLayout) {
+                       GridData gd = new GridData(SWT.CENTER, SWT.CENTER, false, false);
+                       gd.widthHint = width.intValue();
+                       gd.heightHint = height.intValue();
+                       img.setLayoutData(gd);
+               }
+               img.addMouseListener(new MouseListener() {
+                       private static final long serialVersionUID = -1362242049325206168L;
 
-       public static String createAndConfigureEntity(Shell shell, Session referenceSession, String mainMixin,
-                       String... additionnalProps) {
+                       @Override
+                       public void mouseUp(MouseEvent e) {
+                       }
 
-               Session tmpSession = null;
-               Session mainSession = null;
-               try {
-                       // FIXME would not work if home is another physical workspace
-                       tmpSession = referenceSession.getRepository().login(NodeConstants.HOME_WORKSPACE);
-                       Node draftNode = null;
-                       for (int i = 0; i < additionnalProps.length - 1; i += 2) {
-                               draftNode.setProperty(additionnalProps[i], additionnalProps[i + 1]);
+                       @Override
+                       public void mouseDown(MouseEvent e) {
                        }
-                       Wizard wizard = null;
-                       CmsWizardDialog dialog = new CmsWizardDialog(shell, wizard);
-                       // WizardDialog dialog = new WizardDialog(shell, wizard);
-                       if (dialog.open() == Window.OK) {
-                               String parentPath = null;// "/" + appService.getBaseRelPath(mainMixin);
-                               // FIXME it should be possible to specify the workspace
-                               mainSession = referenceSession.getRepository().login();
-                               Node parent = mainSession.getNode(parentPath);
-                               Node task = null;// appService.publishEntity(parent, mainMixin, draftNode);
-//                             task = appService.saveEntity(task, false);
-                               referenceSession.refresh(true);
-                               return task.getPath();
+
+                       @Override
+                       public void mouseDoubleClick(MouseEvent e) {
+                               LightweightDialog dialog = new LightweightDialog(img.getShell()) {
+
+                                       @Override
+                                       protected Control createDialogArea(Composite parent) {
+                                               parent.setLayout(new GridLayout());
+                                               ScrolledComposite scroll = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
+                                               scroll.setLayoutData(CmsUiUtils.fillAll());
+                                               scroll.setLayout(CmsUiUtils.noSpaceGridLayout());
+                                               scroll.setExpandHorizontal(true);
+                                               scroll.setExpandVertical(true);
+                                               // scroll.setAlwaysShowScrollBars(true);
+
+                                               Composite c = new Composite(scroll, SWT.NONE);
+                                               scroll.setContent(c);
+                                               c.setLayout(new GridLayout());
+                                               c.setLayoutData(CmsUiUtils.fillAll());
+                                               Label bigImg = new Label(c, SWT.NONE);
+                                               CmsUiUtils.markup(bigImg);
+                                               bigImg.setText(CmsUiUtils.img(fileNode, Jcr.get(content, EntityNames.SVG_WIDTH),
+                                                               Jcr.get(content, EntityNames.SVG_HEIGHT)));
+                                               bigImg.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
+                                               return bigImg;
+                                       }
+
+                                       @Override
+                                       protected Point getInitialSize() {
+                                               Point shellSize = img.getShell().getSize();
+                                               return new Point(shellSize.x - 100, shellSize.y - 100);
+                                       }
+
+                               };
+                               dialog.open();
                        }
-                       return null;
-               } catch (RepositoryException e1) {
-                       throw new JcrException(
-                                       "Unable to create " + mainMixin + " entity with session " + referenceSession.toString(), e1);
-               } finally {
-                       JcrUtils.logoutQuietly(tmpSession);
-                       JcrUtils.logoutQuietly(mainSession);
-               }
+               });
+               return img;
        }
 
+//     public static String createAndConfigureEntity(Shell shell, Session referenceSession, String mainMixin,
+//                     String... additionnalProps) {
+//
+//             Session tmpSession = null;
+//             Session mainSession = null;
+//             try {
+//                     // FIXME would not work if home is another physical workspace
+//                     tmpSession = referenceSession.getRepository().login(NodeConstants.HOME_WORKSPACE);
+//                     Node draftNode = null;
+//                     for (int i = 0; i < additionnalProps.length - 1; i += 2) {
+//                             draftNode.setProperty(additionnalProps[i], additionnalProps[i + 1]);
+//                     }
+//                     Wizard wizard = null;
+//                     CmsWizardDialog dialog = new CmsWizardDialog(shell, wizard);
+//                     // WizardDialog dialog = new WizardDialog(shell, wizard);
+//                     if (dialog.open() == Window.OK) {
+//                             String parentPath = null;// "/" + appService.getBaseRelPath(mainMixin);
+//                             // FIXME it should be possible to specify the workspace
+//                             mainSession = referenceSession.getRepository().login();
+//                             Node parent = mainSession.getNode(parentPath);
+//                             Node task = null;// appService.publishEntity(parent, mainMixin, draftNode);
+////                           task = appService.saveEntity(task, false);
+//                             referenceSession.refresh(true);
+//                             return task.getPath();
+//                     }
+//                     return null;
+//             } catch (RepositoryException e1) {
+//                     throw new JcrException(
+//                                     "Unable to create " + mainMixin + " entity with session " + referenceSession.toString(), e1);
+//             } finally {
+//                     JcrUtils.logoutQuietly(tmpSession);
+//                     JcrUtils.logoutQuietly(mainSession);
+//             }
+//     }
+
 }