Adapt CMS Web to new assembly approach.
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / util / CmsLink.java
index 14f3755ff9c4bbe4b1ca526860fb466bf5fbd5ba..b18770f189f3959db89684938580aeb60da7009d 100644 (file)
@@ -5,14 +5,16 @@ import java.net.MalformedURLException;
 import java.net.URL;
 
 import javax.jcr.Node;
+import javax.jcr.RepositoryException;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.cms.CmsException;
-import org.argeo.cms.CmsStyles;
-import org.argeo.cms.CmsUiProvider;
-import org.eclipse.gemini.blueprint.context.BundleContextAware;
+import org.argeo.cms.auth.CurrentUser;
+import org.argeo.cms.ui.CmsStyles;
+import org.argeo.cms.ui.CmsUiProvider;
+import org.argeo.node.NodeUtils;
 import org.eclipse.rap.rwt.RWT;
 import org.eclipse.rap.rwt.service.ResourceManager;
 import org.eclipse.swt.SWT;
@@ -23,12 +25,11 @@ import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Label;
 import org.osgi.framework.BundleContext;
-import org.springframework.beans.factory.InitializingBean;
 
 /** A link to an internal or external location. */
-public class CmsLink implements CmsUiProvider, InitializingBean,
-               BundleContextAware {
+public class CmsLink implements CmsUiProvider {
        private final static Log log = LogFactory.getLog(CmsLink.class);
+       private BundleContext bundleContext;
 
        private String label;
        private String custom;
@@ -36,14 +37,16 @@ public class CmsLink implements CmsUiProvider, InitializingBean,
        private String image;
        private MouseListener mouseListener;
 
+       private int horizontalAlignment = SWT.CENTER;
        private int verticalAlignment = SWT.CENTER;
 
+       private String loggedInLabel = null;
+       private String loggedInTarget = null;
+
        // internal
        // private Boolean isUrl = false;
        private Integer imageWidth, imageHeight;
 
-       private BundleContext bundleContext;
-
        public CmsLink() {
                super();
        }
@@ -57,45 +60,43 @@ public class CmsLink implements CmsUiProvider, InitializingBean,
                this.label = label;
                this.target = target;
                this.custom = custom;
-               afterPropertiesSet();
+               init();
        }
 
-       @Override
-       public void afterPropertiesSet() {
-               // if (target != null) {
-               // if (target.startsWith("/")) {
-               // isUrl = true;
-               // } else {
-               // try {
-               // new URL(target);
-               // isUrl = true;
-               // } catch (MalformedURLException e1) {
-               // isUrl = false;
-               // }
-               // }
-               // }
-
+       public void init() {
                if (image != null) {
                        ImageData image = loadImage();
-                       imageWidth = image.width;
-                       imageHeight = image.height;
+                       if (imageHeight == null && imageWidth == null) {
+                               imageWidth = image.width;
+                               imageHeight = image.height;
+                       } else if (imageHeight == null) {
+                               imageHeight = (imageWidth * image.height) / image.width;
+                       } else if (imageWidth == null) {
+                               imageWidth = (imageHeight * image.width) / image.height;
+                       }
                }
        }
 
        /** @return {@link Composite} with a single {@link Label} child. */
        @Override
        public Control createUi(final Composite parent, Node context) {
-               Composite comp = new Composite(parent, SWT.BOTTOM);
+//             if (image != null && (imageWidth == null || imageHeight == null)) {
+//                     throw new CmsException("Image is not properly configured."
+//                                     + " Make sure bundleContext property is set and init() method has been called.");
+//             }
+
+               Composite comp = new Composite(parent, SWT.NONE);
                comp.setLayout(CmsUtils.noSpaceGridLayout());
 
                Label link = new Label(comp, SWT.NONE);
                link.setData(RWT.MARKUP_ENABLED, Boolean.TRUE);
-               GridData layoutData = new GridData(SWT.CENTER, verticalAlignment, true,
-                               true);
+               GridData layoutData = new GridData(horizontalAlignment, verticalAlignment, false, false);
                if (image != null) {
-                       layoutData.heightHint = imageHeight;
+                       if (imageHeight != null)
+                               layoutData.heightHint = imageHeight;
                        if (label == null)
-                               layoutData.widthHint = imageWidth;
+                               if (imageWidth != null)
+                                       layoutData.widthHint = imageWidth;
                }
 
                link.setLayoutData(layoutData);
@@ -109,39 +110,48 @@ public class CmsLink implements CmsUiProvider, InitializingBean,
 
                // label
                StringBuilder labelText = new StringBuilder();
-               if (target != null) {
-                       labelText
-                                       .append("<a style='color:inherit;text-decoration:inherit;' href=\"");
-                       // if (!isUrl)
-                       // labelText.append('#');
+               if (loggedInTarget != null && isLoggedIn()) {
+                       labelText.append("<a style='color:inherit;text-decoration:inherit;' href=\"");
+                       if (loggedInTarget.equals("")) {
+                               try {
+                                       Node homeNode = NodeUtils.getUserHome(context.getSession());
+                                       String homePath = homeNode.getPath();
+                                       labelText.append("/#" + homePath);
+                               } catch (RepositoryException e) {
+                                       throw new CmsException("Cannot get home path", e);
+                               }
+                       } else {
+                               labelText.append(loggedInTarget);
+                       }
+                       labelText.append("\">");
+               } else if (target != null) {
+                       labelText.append("<a style='color:inherit;text-decoration:inherit;' href=\"");
                        labelText.append(target);
                        labelText.append("\">");
                }
                if (image != null) {
                        registerImageIfNeeded();
                        String imageLocation = RWT.getResourceManager().getLocation(image);
-                       labelText.append("<img width='").append(imageWidth)
-                                       .append("' height='").append(imageHeight)
-                                       .append("' src=\"").append(imageLocation).append("\"/>");
+                       labelText.append("<img");
+                       if (imageWidth != null)
+                               labelText.append(" width='").append(imageWidth).append('\'');
+                       if (imageHeight != null)
+                               labelText.append(" height='").append(imageHeight).append('\'');
+                       labelText.append(" src=\"").append(imageLocation).append("\"/>");
 
-                       // final Image img = loadImage(parent.getDisplay());
-                       // link.setImage(img);
-                       // link.addDisposeListener(new DListener(img));
                }
 
-               if (label != null) {
-                       // link.setText(label);
+               if (loggedInLabel != null && isLoggedIn()) {
+                       labelText.append(' ').append(loggedInLabel);
+               } else if (label != null) {
                        labelText.append(' ').append(label);
                }
 
-               if (target != null)
+               if ((loggedInTarget != null && isLoggedIn()) || target != null)
                        labelText.append("</a>");
 
                link.setText(labelText.toString());
 
-               // link.setCursor(link.getDisplay().getSystemCursor(SWT.CURSOR_HAND));
-               // CmsSession cmsSession = (CmsSession) parent.getDisplay().getData(
-               // CmsSession.KEY);
                if (mouseListener != null)
                        link.addMouseListener(mouseListener);
 
@@ -190,9 +200,6 @@ public class CmsLink implements CmsUiProvider, InitializingBean,
                        // pure URL
                        url = new URL(image);
                } catch (MalformedURLException e1) {
-                       // in OSGi bundle
-                       if (bundleContext == null)
-                               throw new CmsException("No bundle context available");
                        url = bundleContext.getBundle().getResource(image);
                }
 
@@ -202,6 +209,10 @@ public class CmsLink implements CmsUiProvider, InitializingBean,
                return url;
        }
 
+       public void setBundleContext(BundleContext bundleContext) {
+               this.bundleContext = bundleContext;
+       }
+
        public void setLabel(String label) {
                this.label = label;
        }
@@ -224,9 +235,12 @@ public class CmsLink implements CmsUiProvider, InitializingBean,
                this.image = image;
        }
 
-       @Override
-       public void setBundleContext(BundleContext bundleContext) {
-               this.bundleContext = bundleContext;
+       public void setLoggedInLabel(String loggedInLabel) {
+               this.loggedInLabel = loggedInLabel;
+       }
+
+       public void setLoggedInTarget(String loggedInTarget) {
+               this.loggedInTarget = loggedInTarget;
        }
 
        public void setMouseListener(MouseListener mouseListener) {
@@ -241,34 +255,20 @@ public class CmsLink implements CmsUiProvider, InitializingBean,
                } else if ("center".equals(vAlign)) {
                        verticalAlignment = SWT.CENTER;
                } else {
-                       throw new CmsException("Unsupported vertical allignment " + vAlign
-                                       + " (must be: top, bottom or center)");
+                       throw new CmsException("Unsupported vertical allignment " + vAlign + " (must be: top, bottom or center)");
                }
        }
 
-       // private class MListener extends MouseAdapter {
-       // private static final long serialVersionUID = 3634864186295639792L;
-       //
-       // @Override
-       // public void mouseDown(MouseEvent e) {
-       // if (e.button == 1) {
-       // }
-       // }
-       // }
-       //
-       // private class DListener implements DisposeListener {
-       // private static final long serialVersionUID = -3808587499269394812L;
-       // private final Image img;
-       //
-       // public DListener(Image img) {
-       // super();
-       // this.img = img;
-       // }
-       //
-       // @Override
-       // public void widgetDisposed(DisposeEvent event) {
-       // img.dispose();
-       // }
-       //
-       // }
+       protected boolean isLoggedIn() {
+               return !CurrentUser.isAnonymous();
+       }
+
+       public void setImageWidth(Integer imageWidth) {
+               this.imageWidth = imageWidth;
+       }
+
+       public void setImageHeight(Integer imageHeight) {
+               this.imageHeight = imageHeight;
+       }
+
 }