Introduce UserMenuLink
authorMathieu Baudier <mbaudier@argeo.org>
Thu, 14 May 2015 16:44:31 +0000 (16:44 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Thu, 14 May 2015 16:44:31 +0000 (16:44 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@8105 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

org.argeo.cms/src/org/argeo/cms/CmsStyles.java
org.argeo.cms/src/org/argeo/cms/util/CmsLink.java
org.argeo.cms/src/org/argeo/cms/util/UserMenu.java
org.argeo.cms/src/org/argeo/cms/util/UserMenuLink.java [new file with mode: 0644]

index 419fb553a6fa73c8d933603bd60285e5d041c25c..69ce8107d6ac74d04f42b34d40406c37eee6d927 100644 (file)
@@ -11,12 +11,12 @@ public interface CmsStyles {
        public final static String CMS_HEADER_LEAD = "cms_header-lead";
        public final static String CMS_HEADER_CENTER = "cms_header-center";
        public final static String CMS_HEADER_END = "cms_header-end";
-       public final static String CMS_LOGIN = "cms_login";
+       public final static String CMS_USER_MENU = "cms_user_menu";
+       public final static String CMS_USER_MENU_LINK = "cms_user_menu-link";
+       public final static String CMS_USER_MENU_ITEM = "cms_user_menu-item";
        public final static String CMS_LOGIN_DIALOG = "cms_login_dialog";
        public final static String CMS_LOGIN_DIALOG_USERNAME = "cms_login_dialog-username";
        public final static String CMS_LOGIN_DIALOG_PASSWORD = "cms_login_dialog-password";
-       public final static String CMS_USER_MENU = "cms_user_menu";
-       public final static String CMS_USER_MENU_ITEM = "cms_user_menu-item";
 
        // Body
        public final static String CMS_SCROLLED_AREA = "cms_scrolled_area";
index 674dc3084795118b1686ae17b420772c8bbf8fb8..14f3755ff9c4bbe4b1ca526860fb466bf5fbd5ba 100644 (file)
@@ -39,7 +39,7 @@ public class CmsLink implements CmsUiProvider, InitializingBean,
        private int verticalAlignment = SWT.CENTER;
 
        // internal
-       //private Boolean isUrl = false;
+       // private Boolean isUrl = false;
        private Integer imageWidth, imageHeight;
 
        private BundleContext bundleContext;
@@ -62,18 +62,18 @@ public class CmsLink implements CmsUiProvider, InitializingBean,
 
        @Override
        public void afterPropertiesSet() {
-//             if (target != null) {
-//                     if (target.startsWith("/")) {
-//                             isUrl = true;
-//                     } else {
-//                             try {
-//                                     new URL(target);
-//                                     isUrl = true;
-//                             } catch (MalformedURLException e1) {
-//                                     isUrl = false;
-//                             }
-//                     }
-//             }
+               // if (target != null) {
+               // if (target.startsWith("/")) {
+               // isUrl = true;
+               // } else {
+               // try {
+               // new URL(target);
+               // isUrl = true;
+               // } catch (MalformedURLException e1) {
+               // isUrl = false;
+               // }
+               // }
+               // }
 
                if (image != null) {
                        ImageData image = loadImage();
@@ -82,6 +82,7 @@ public class CmsLink implements CmsUiProvider, InitializingBean,
                }
        }
 
+       /** @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);
@@ -111,8 +112,8 @@ public class CmsLink implements CmsUiProvider, InitializingBean,
                if (target != null) {
                        labelText
                                        .append("<a style='color:inherit;text-decoration:inherit;' href=\"");
-//                     if (!isUrl)
-//                             labelText.append('#');
+                       // if (!isUrl)
+                       // labelText.append('#');
                        labelText.append(target);
                        labelText.append("\">");
                }
index 73567886b0f7986575f58f9c7626b68247a3233f..ba8e3d48764731c6cb01fe1aae78a7c4afcea393 100644 (file)
@@ -75,20 +75,23 @@ public class UserMenu extends Shell implements CmsStyles, CallbackHandler {
        }
 
        protected void userUi() {
-               setLayout(new GridLayout());
-
-               String username = SecurityContextHolder.getContext()
-                               .getAuthentication().getName();
+               setLayout(CmsUtils.noSpaceGridLayout());
+               Composite c = new Composite(this, SWT.NONE);
+               c.setLayout(new GridLayout());
+               c.setLayoutData(CmsUtils.fillAll());
 
-               Label l = new Label(this, SWT.NONE);
-               l.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU_ITEM);
-               l.setData(RWT.MARKUP_ENABLED, true);
-               l.setLayoutData(CmsUtils.fillWidth());
-               l.setText("<b>" + username + "</b>");
+               // String username = SecurityContextHolder.getContext()
+               // .getAuthentication().getName();
+               //
+               // Label l = new Label(c, SWT.NONE);
+               // l.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU_ITEM);
+               // l.setData(RWT.MARKUP_ENABLED, true);
+               // l.setLayoutData(CmsUtils.fillWidth());
+               // l.setText("<b>" + username + "</b>");
 
-               specificUserUi(this);
+               specificUserUi(c);
 
-               l = new Label(this, SWT.NONE);
+               Label l = new Label(c, SWT.NONE);
                l.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU_ITEM);
                l.setText(CmsMsg.logout.lead());
                GridData lData = CmsUtils.fillWidth();
@@ -123,7 +126,7 @@ public class UserMenu extends Shell implements CmsStyles, CallbackHandler {
                c.setLayout(new GridLayout());
                c.setLayoutData(CmsUtils.fillAll());
 
-               Integer textWidth = 150;
+               Integer textWidth = 120;
                setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU);
 
                // new Label(this, SWT.NONE).setText(CmsMsg.username.lead());
diff --git a/org.argeo.cms/src/org/argeo/cms/util/UserMenuLink.java b/org.argeo.cms/src/org/argeo/cms/util/UserMenuLink.java
new file mode 100644 (file)
index 0000000..6893181
--- /dev/null
@@ -0,0 +1,82 @@
+package org.argeo.cms.util;
+
+import javax.jcr.Node;
+
+import org.argeo.cms.CmsMsg;
+import org.argeo.cms.CmsStyles;
+import org.argeo.cms.KernelHeader;
+import org.eclipse.swt.events.DisposeEvent;
+import org.eclipse.swt.events.DisposeListener;
+import org.eclipse.swt.events.MouseEvent;
+import org.eclipse.swt.events.MouseListener;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Label;
+import org.springframework.security.core.context.SecurityContextHolder;
+
+/** Open the user menu when clicked */
+public class UserMenuLink extends MenuLink {
+
+       public UserMenuLink() {
+               setCustom(CmsStyles.CMS_USER_MENU_LINK);
+       }
+
+       @Override
+       public Control createUi(Composite parent, Node context) {
+               String username = SecurityContextHolder.getContext()
+                               .getAuthentication().getName();
+               if (username.equals(KernelHeader.USERNAME_ANONYMOUS))
+                       setLabel(CmsMsg.login.lead());
+               else
+                       setLabel(username);
+               Label link = (Label) ((Composite) super.createUi(parent, context))
+                               .getChildren()[0];
+               link.addMouseListener(new UserMenuLinkController());
+               return link.getParent();
+       }
+
+       protected UserMenu createUserMenu(Control source) {
+               return new UserMenu(source.getParent());
+       }
+
+       private class UserMenuLinkController implements MouseListener,
+                       DisposeListener {
+               private static final long serialVersionUID = 3634864186295639792L;
+
+               private UserMenu userMenu = null;
+               private long lastDisposeTS = 0l;
+
+               //
+               // MOUSE LISTENER
+               //
+               @Override
+               public void mouseDown(MouseEvent e) {
+                       if (e.button == 1) {
+                               Control source = (Control) e.getSource();
+                               if (userMenu == null) {
+                                       long durationSinceLastDispose = System.currentTimeMillis()
+                                                       - lastDisposeTS;
+                                       // avoid to reopen the menu, if one has clicked gain
+                                       if (durationSinceLastDispose > 200) {
+                                               userMenu = createUserMenu(source);
+                                               userMenu.addDisposeListener(this);
+                                       }
+                               }
+                       }
+               }
+
+               @Override
+               public void mouseDoubleClick(MouseEvent e) {
+               }
+
+               @Override
+               public void mouseUp(MouseEvent e) {
+               }
+
+               @Override
+               public void widgetDisposed(DisposeEvent event) {
+                       userMenu = null;
+                       lastDisposeTS = System.currentTimeMillis();
+               }
+       }
+}
\ No newline at end of file