Massive Argeo APIs refactoring
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / ui / forms / FormPageViewer.java
index 3ae2036ae8e221849aff86c9929ddd943b24c394..cc732d49d5a0ff225baa36a7ae6943b9d6ce7932 100644 (file)
@@ -14,12 +14,11 @@ import javax.jcr.Session;
 import javax.jcr.Value;
 import javax.jcr.ValueFormatException;
 
-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.CmsImageManager;
-import org.argeo.cms.ui.util.CmsUiUtils;
+import org.argeo.api.cms.Cms2DSize;
+import org.argeo.api.cms.CmsEditable;
+import org.argeo.api.cms.CmsImageManager;
+import org.argeo.api.cms.CmsLog;
+import org.argeo.cms.swt.CmsSwtUtils;
 import org.argeo.cms.ui.viewers.AbstractPageViewer;
 import org.argeo.cms.ui.viewers.EditablePart;
 import org.argeo.cms.ui.viewers.Section;
@@ -28,6 +27,7 @@ import org.argeo.cms.ui.widgets.EditableImage;
 import org.argeo.cms.ui.widgets.Img;
 import org.argeo.cms.ui.widgets.StyledControl;
 import org.argeo.eclipse.ui.EclipseUiUtils;
+import org.argeo.jcr.JcrException;
 import org.argeo.jcr.JcrUtils;
 import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.rap.fileupload.FileDetails;
@@ -61,19 +61,19 @@ import org.eclipse.swt.widgets.Text;
 
 /** Manage life cycle of a form page that is linked to a given node */
 public class FormPageViewer extends AbstractPageViewer {
-       private final static Log log = LogFactory.getLog(FormPageViewer.class);
+       private final static CmsLog log = CmsLog.getLog(FormPageViewer.class);
        private static final long serialVersionUID = 5277789504209413500L;
 
        private final Section mainSection;
 
        // TODO manage within the CSS
-       private int labelColWidth = 150;
+       private Integer labelColWidth = null;
        private int rowLayoutHSpacing = 8;
 
        // Context cached in the viewer
        // The reference to translate from text to calendar and reverse
        private DateFormat dateFormat = new SimpleDateFormat(FormUtils.DEFAULT_SHORT_DATE_FORMAT);
-       private CmsImageManager imageManager;
+       private CmsImageManager<Control, Node> imageManager;
        private FileUploadListener fileUploadListener;
 
        public FormPageViewer(Section mainSection, int style, CmsEditable cmsEditable) throws RepositoryException {
@@ -142,7 +142,7 @@ public class FormPageViewer extends AbstractPageViewer {
                // TODO: make this configurable, sometimes we do not want to save the
                // current session at this stage
                if (node != null && node.getSession().hasPendingChanges()) {
-                       JcrUtils.updateLastModified(node);
+                       JcrUtils.updateLastModified(node, true);
                        node.getSession().save();
                }
        }
@@ -204,7 +204,7 @@ public class FormPageViewer extends AbstractPageViewer {
                }
 
                public void uploadFailed(FileUploadEvent event) {
-                       throw new CmsException("Upload failed " + event, event.getException());
+                       throw new IllegalStateException("Upload failed " + event, event.getException());
                }
 
                public void uploadFinished(FileUploadEvent event) {
@@ -303,9 +303,9 @@ public class FormPageViewer extends AbstractPageViewer {
                return mainSection;
        }
 
-       protected CmsImageManager imageManager() {
+       protected CmsImageManager<Control, Node> imageManager() {
                if (imageManager == null)
-                       imageManager = CmsUiUtils.getCmsView().getImageManager();
+                       imageManager = (CmsImageManager<Control, Node>) CmsSwtUtils.getCmsView(mainSection).getImageManager();
                return imageManager;
        }
 
@@ -314,8 +314,8 @@ public class FormPageViewer extends AbstractPageViewer {
                Section section = null;
                if (node != null) {
                        section = new Section(body, SWT.NO_FOCUS, node);
-                       section.setLayoutData(CmsUiUtils.fillWidth());
-                       section.setLayout(CmsUiUtils.noSpaceGridLayout());
+                       section.setLayoutData(CmsSwtUtils.fillWidth());
+                       section.setLayout(CmsSwtUtils.noSpaceGridLayout());
                }
                return section;
        }
@@ -327,7 +327,7 @@ public class FormPageViewer extends AbstractPageViewer {
                        EditablePropertyString eps = new EditablePropertyString(bodyRow, SWT.WRAP | SWT.LEFT, node, propName, msg);
                        eps.setMouseListener(getMouseListener());
                        eps.setFocusListener(getFocusListener());
-                       eps.setLayoutData(CmsUiUtils.fillWidth());
+                       eps.setLayoutData(CmsSwtUtils.fillWidth());
                }
        }
 
@@ -352,21 +352,22 @@ public class FormPageViewer extends AbstractPageViewer {
                        addListeners(emsp);
                        // emsp.setMouseListener(getMouseListener());
                        emsp.setStyle(FormStyle.propertyMessage.style());
-                       emsp.setLayoutData(CmsUiUtils.fillWidth());
+                       emsp.setLayoutData(CmsSwtUtils.fillWidth());
                }
        }
 
        protected Label createPropertyLbl(Composite parent, String value) {
-               return createPropertyLbl(parent, value, SWT.TOP);
+               return createPropertyLbl(parent, value, SWT.NONE);
        }
 
        protected Label createPropertyLbl(Composite parent, String value, int vAlign) {
-               boolean isSmall = CmsUiUtils.getCmsView().getUxContext().isSmall();
-               Label label = new Label(parent, isSmall ? SWT.LEFT : SWT.RIGHT | SWT.WRAP);
+               // boolean isSmall = CmsView.getCmsView(parent).getUxContext().isSmall();
+               Label label = new Label(parent, SWT.LEAD | SWT.WRAP);
                label.setText(value + " ");
-               CmsUiUtils.style(label, FormStyle.propertyLabel.style());
-               GridData gd = new GridData(isSmall ? SWT.LEFT : SWT.RIGHT, vAlign, false, false);
-               gd.widthHint = labelColWidth;
+               CmsSwtUtils.style(label, FormStyle.propertyLabel.style());
+               GridData gd = new GridData(SWT.LEAD, vAlign, false, false);
+               if (labelColWidth != null)
+                       gd.widthHint = labelColWidth;
                label.setLayoutData(gd);
                return label;
        }
@@ -374,13 +375,13 @@ public class FormPageViewer extends AbstractPageViewer {
        protected Label newStyledLabel(Composite parent, String style, String value) {
                Label label = new Label(parent, SWT.NONE);
                label.setText(value);
-               CmsUiUtils.style(label, style);
+               CmsSwtUtils.style(label, style);
                return label;
        }
 
        protected Composite createRowLayoutComposite(Composite parent) throws RepositoryException {
                Composite bodyRow = new Composite(parent, SWT.NO_FOCUS);
-               bodyRow.setLayoutData(CmsUiUtils.fillWidth());
+               bodyRow.setLayoutData(CmsSwtUtils.fillWidth());
                RowLayout rl = new RowLayout(SWT.WRAP);
                rl.type = SWT.HORIZONTAL;
                rl.spacing = rowLayoutHSpacing;
@@ -446,24 +447,20 @@ public class FormPageViewer extends AbstractPageViewer {
                        // for a while
                        cleanedName = System.currentTimeMillis() % 100000 + "_" + cleanedName;
 
-                       try {
-                               imageManager().uploadImage(context, cleanedName, stream);
-                               // TODO clean refresh strategy
-                               section.getDisplay().asyncExec(new Runnable() {
-                                       @Override
-                                       public void run() {
-                                               try {
-                                                       FormPageViewer.this.refresh(section);
-                                                       section.layout();
-                                                       section.getParent().layout();
-                                               } catch (RepositoryException re) {
-                                                       throw new CmsException("unable to refresh " + "image section for " + context);
-                                               }
+                       imageManager().uploadImage(context, context, cleanedName, stream, details.getContentType());
+                       // TODO clean refresh strategy
+                       section.getDisplay().asyncExec(new Runnable() {
+                               @Override
+                               public void run() {
+                                       try {
+                                               FormPageViewer.this.refresh(section);
+                                               section.layout();
+                                               section.getParent().layout();
+                                       } catch (RepositoryException re) {
+                                               throw new JcrException("Unable to refresh " + "image section for " + context, re);
                                        }
-                               });
-                       } catch (RepositoryException re) {
-                               throw new CmsException("unable to upload image " + name + " at " + context);
-                       }
+                               }
+                       });
                }
        }
 
@@ -473,20 +470,20 @@ public class FormPageViewer extends AbstractPageViewer {
        }
 
        protected Img createImgComposite(Composite parent, Node node, Point preferredSize) throws RepositoryException {
-               Img img = new Img(parent, SWT.NONE, node, preferredSize) {
+               Img img = new Img(parent, SWT.NONE, node, new Cms2DSize(preferredSize.x, preferredSize.y)) {
                        private static final long serialVersionUID = 1297900641952417540L;
 
                        @Override
                        protected void setContainerLayoutData(Composite composite) {
-                               composite.setLayoutData(CmsUiUtils.grabWidth(SWT.CENTER, SWT.DEFAULT));
+                               composite.setLayoutData(CmsSwtUtils.grabWidth(SWT.CENTER, SWT.DEFAULT));
                        }
 
                        @Override
                        protected void setControlLayoutData(Control control) {
-                               control.setLayoutData(CmsUiUtils.grabWidth(SWT.CENTER, SWT.DEFAULT));
+                               control.setLayoutData(CmsSwtUtils.grabWidth(SWT.CENTER, SWT.DEFAULT));
                        }
                };
-               img.setLayoutData(CmsUiUtils.grabWidth(SWT.CENTER, SWT.DEFAULT));
+               img.setLayoutData(CmsSwtUtils.grabWidth(SWT.CENTER, SWT.DEFAULT));
                updateContent(img);
                addListeners(img);
                return img;
@@ -495,7 +492,7 @@ public class FormPageViewer extends AbstractPageViewer {
        protected Composite addDeleteAbility(final Section section, final Node sessionNode, int topWeight,
                        int rightWeight) {
                Composite comp = new Composite(section, SWT.NONE);
-               comp.setLayoutData(CmsUiUtils.fillAll());
+               comp.setLayoutData(CmsSwtUtils.fillAll());
                comp.setLayout(new FormLayout());
 
                // The body to be populated
@@ -505,7 +502,7 @@ public class FormPageViewer extends AbstractPageViewer {
                if (getCmsEditable().canEdit()) {
                        // the delete button
                        Button deleteBtn = new Button(comp, SWT.FLAT);
-                       CmsUiUtils.style(deleteBtn, FormStyle.deleteOverlay.style());
+                       CmsSwtUtils.style(deleteBtn, FormStyle.deleteOverlay.style());
                        FormData formData = new FormData();
                        formData.right = new FormAttachment(rightWeight, 0);
                        formData.top = new FormAttachment(topWeight, 0);
@@ -529,7 +526,7 @@ public class FormPageViewer extends AbstractPageViewer {
                                                        refresh(parSection);
                                                        layout(parSection);
                                                } catch (RepositoryException re) {
-                                                       throw new CmsException("Unable to delete " + sessionNode, re);
+                                                       throw new JcrException("Unable to delete " + sessionNode, re);
                                                }
 
                                        }
@@ -577,7 +574,7 @@ public class FormPageViewer extends AbstractPageViewer {
                                                                edit(emsp, 0);
                                                                cancelEdit();
                                                        } catch (RepositoryException e1) {
-                                                               throw new CmsException("Unable to remove value " + obj, e1);
+                                                               throw new JcrException("Unable to remove value " + obj, e1);
                                                        }
                                                        layout(emsp);
                                                }