changeset 4602:33bb8bf3899a

Merge the MainMenu into the FLYSHeader Move all layouts and functionality from MainMenu into the FLYSHeader. Also refactor the layout and use smaller BfG Logo.
author Björn Ricks <bjoern.ricks@intevation.de>
date Fri, 30 Nov 2012 09:57:56 +0100
parents 16c19d4f1833
children 7b8063c2d589
files flys-client/src/main/java/de/intevation/flys/client/client/ui/FLYSHeader.java
diffstat 1 files changed, 225 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/FLYSHeader.java	Fri Nov 30 09:56:10 2012 +0100
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/FLYSHeader.java	Fri Nov 30 09:57:56 2012 +0100
@@ -1,15 +1,26 @@
 package de.intevation.flys.client.client.ui;
 
 import com.google.gwt.core.client.GWT;
-import com.google.gwt.resources.client.ImageResource;
+import com.google.gwt.i18n.client.LocaleInfo;
+import com.google.gwt.user.client.Window;
+import com.google.gwt.user.client.rpc.AsyncCallback;
 
 import com.smartgwt.client.types.Alignment;
+import com.smartgwt.client.types.VerticalAlignment;
+import com.smartgwt.client.util.BooleanCallback;
+import com.smartgwt.client.util.SC;
+import com.smartgwt.client.widgets.Button;
 import com.smartgwt.client.widgets.Img;
 import com.smartgwt.client.widgets.Label;
 import com.smartgwt.client.widgets.layout.HLayout;
-import com.smartgwt.client.widgets.layout.VLayout;
+import com.smartgwt.client.widgets.events.ClickEvent;
+import com.smartgwt.client.widgets.events.ClickHandler;
 
+import de.intevation.flys.client.client.FLYS;
 import de.intevation.flys.client.client.FLYSConstants;
+import de.intevation.flys.client.client.services.UserService;
+import de.intevation.flys.client.client.services.UserServiceAsync;
+import de.intevation.flys.client.shared.model.User;
 
 
 /**
@@ -21,19 +32,109 @@
     private FLYSConstants MESSAGES = GWT.create(FLYSConstants.class);
 
     /** The height used for this header.*/
-    public static final int HEIGHT = 75;
+    public static final int HEIGHT = 56;
 
     /** The height used for the images.*/
     public static final int IMG_HEIGHT = 50;
 
+    /** The user that is currently logged in. */
+    private User currentUser;
 
-    public FLYSHeader() {
+    /** The label that displays the current logged in user. */
+    private Label userText;
+
+    /** The button to log the current user out.*/
+    private Button logout;
+
+    /** The button to open the project list.*/
+    private Button projectList;
+
+    /** The button to switch between the english and german version.*/
+    private Button language;
+
+    /** The button to open an info panel.*/
+    private Button info;
+
+    private UserServiceAsync userService =
+        GWT.create(UserService.class);
+
+    /** An instance to FLYS.*/
+    private FLYS flys;
+
+
+    public FLYSHeader(FLYS flys) {
+        this.flys     = flys;
+
+        String guest = MESSAGES.user() + " " + MESSAGES.guest();
+
+        userText    = new Label(guest);
+        projectList = new Button(MESSAGES.manage_projects());
+        logout      = new Button(MESSAGES.logout());
+        language    = new Button(MESSAGES.switch_language());
+        info        = new Button(MESSAGES.info());
+
+        projectList.addClickHandler(new ClickHandler() {
+            @Override
+            public void onClick(ClickEvent event) {
+                GWT.log("Clicked 'Open ProjectList' button.");
+                getFlys().openProjectList();
+            }
+        });
+
+        logout.addClickHandler(new ClickHandler() {
+            @Override
+            public void onClick(ClickEvent event) {
+                GWT.log("Clicked 'logout' button.");
+                userService.logoutCurrentUser(new AsyncCallback<Void>() {
+                    public void onFailure(Throwable caught) {
+                    }
+
+                    public void onSuccess(Void result) {
+                        /* Just reload the page. GGInAFilter is goint to redirect
+                         * to the correct login page */
+                        Window.Location.reload();
+                    }
+                });
+
+            }
+        });
+
+        language.addClickHandler(new ClickHandler() {
+            @Override
+            public void onClick(ClickEvent event) {
+                LocaleInfo info            = LocaleInfo.getCurrentLocale();
+                final String currentLocale = info.getLocaleName();
+                final String newLocale     = currentLocale.equals("de")
+                    ? "en"
+                    : "de";
+
+                SC.confirm(MESSAGES.warning(), MESSAGES.warning_language(),
+                    new BooleanCallback() {
+                        @Override
+                        public void execute(Boolean value) {
+                            if (value) {
+                                switchLanguage(currentLocale, newLocale);
+                            }
+                        }
+                    });
+            }
+        });
+
+        info.addClickHandler(new ClickHandler() {
+            @Override
+            public void onClick(ClickEvent event) {
+                GWT.log("Clicked 'info' button.");
+                GWT.log("IMPLEMENT the 'open info panel' function.");
+            }
+        });
         init();
     }
 
     public void init() {
+        setStyleName("header");
         setWidth100();
         setHeight(HEIGHT);
+        setBackgroundColor("#a9c9e6");
         setLayoutLeftMargin(5);
         setLayoutRightMargin(5);
 
@@ -44,45 +145,147 @@
             50,
             IMG_HEIGHT);
 
-        Img bfg  = new Img(
-            baseUrl + MESSAGES.bfgLogo(),
-            112,
-            HEIGHT);
+        Img bfg = new Img(
+            baseUrl + MESSAGES.bfgLogoSmall(),
+            150,
+            IMG_HEIGHT);
 
         Label fullname = new Label(MESSAGES.fullname());
         fullname.setHeight(HEIGHT - IMG_HEIGHT);
-        fullname.setStyleName ("fontNormalMid");
+        fullname.setStyleName("fontBlackMid");
 
-        VLayout left = new VLayout();
+        HLayout left = new HLayout();
+        left.setDefaultLayoutAlign(VerticalAlignment.CENTER);
+        left.setMembersMargin(3);
         left.addMember(flys);
         left.addMember(fullname);
 
         HLayout right = new HLayout();
         right.setAlign(Alignment.RIGHT);
+        right.setDefaultLayoutAlign(Alignment.RIGHT);
+        right.setDefaultLayoutAlign(VerticalAlignment.CENTER);
+        right.setMembersMargin(3);
+        right.setLayoutRightMargin(5);
+
+        projectList.setStyleName("manageProjects");
+        userText.setStyleName("fontBlackSmall");
+        logout.setStyleName("fontLightSmall");
+        language.setStyleName("fontLightSmall");
+        info.setStyleName("fontLightSmall");
+
+        userText.setAlign(Alignment.RIGHT);
+        userText.setWidth(200);
+        logout.setWidth(70);
+        info.setWidth(40);
+        language.setWidth(70);
+
+        left.addMember(projectList);
+        if (this.flys.isProjectListVisible()) {
+            hideProjectButton();
+        }
+        else {
+            showProjectButton();
+        }
+
+        right.addMember(userText);
+        right.addMember(logout);
+        right.addMember(language);
+        right.addMember(info);
         right.addMember(bfg);
 
         addMember(left);
         addMember(right);
     }
 
+    /**
+     * Returns the FLYS instance stored in this class.
+     *
+     * @return the flys instance.
+     */
+    private FLYS getFlys() {
+        return flys;
+    }
 
     /**
-     * This method calculates the wight of an image relative to the given
-     * height.
+     * This method triggers the language switch between the <i>currentLocale</i>
+     * and the <i>newLocale</i>. The switch is done by replacing a "locale="
+     * parameter in the url of the application. We could use the GWT UrlBuilder
+     * class to create a new URL, but - in my eyes - this class is a bit
+     * inconsistens in its implementation.
      *
-     * @param res The ImageResource that points to the image.
-     * @param height The pre-defined height.
-     *
-     * @return the calculated width that should be used for the image.
+     * @param currentLocale The current locale string (e.g. "en").
+     * @param newLocale The new locale string (e.g. "de").
      */
-    protected int calcWidth(ImageResource res, int height) {
-        int widthOrig  = res.getWidth();
-        int heightOrig = res.getHeight();
+    private void switchLanguage(String currentLocale, String newLocale) {
+        String newLocation = Window.Location.getHref();
 
-        double factor = (double)heightOrig / height;
-        double width  = (double)widthOrig / factor;
+        if (newLocation.endsWith("/")) {
+            newLocation = newLocation.substring(0, newLocation.length()-1);
+        }
 
-        return (int) width * 10 / 10;
+        String replace     = null;
+        String replaceWith = null;
+
+        if (newLocation.indexOf("&locale=") >= 0) {
+            replace = currentLocale.equals("de")
+                ? "&locale=de"
+                : "&locale=en";
+
+            replaceWith = "&locale=" + newLocale;
+        }
+        else if (newLocation.indexOf("?locale=") >= 0) {
+            replace = currentLocale.equals("de")
+                ? "?locale=de"
+                : "?locale=en";
+
+            replaceWith = "?locale=" + newLocale;
+        }
+        else {
+            newLocation += newLocation.indexOf("?") >= 0
+                ? "&locale=" + newLocale
+                : "?locale=" + newLocale;
+        }
+
+        if (replace != null && replaceWith != null) {
+            newLocation = newLocation.replace(replace, replaceWith);
+        }
+
+        Window.open(newLocation, "_self", "");
+    }
+
+    /**
+     * Update the text field that shows the current user. If no user is
+     * currently logged in, the text will display {@link FLYSConstants.guest()}.
+     */
+    private void updateCurrentUser() {
+        String name = currentUser != null
+            ? currentUser.getName()
+            : MESSAGES.guest();
+
+        GWT.log("Update the current user: " + name);
+
+        String username = MESSAGES.user() + " " + name;
+        userText.setContents(username);
+    }
+
+    /**
+     * Set the current {@link User} and call {@link updateCurrentUser()}
+     * afterwards.
+     *
+     * @param user the new user.
+     */
+    public void setCurrentUser(User currentUser) {
+        this.currentUser = currentUser;
+
+        updateCurrentUser();
+    }
+
+    public void hideProjectButton() {
+        this.projectList.hide();
+    }
+
+    public void showProjectButton() {
+        this.projectList.show();
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org