view flys-client/src/main/java/de/intevation/flys/client/client/ui/MainMenu.java @ 4573:b87073a05f9d

flys-client: Patch to render combobox options as clickable links. The way of passing data arguments to the links and further to the Artifact feeding service is somewhat hacked and should be refactored (later...).
author Christian Lins <christian.lins@intevation.de>
date Tue, 27 Nov 2012 12:50:10 +0100
parents 9d7497f5be05
children
line wrap: on
line source
package de.intevation.flys.client.client.ui;

import com.google.gwt.core.client.GWT;
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.util.BooleanCallback;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;

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;


/**
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class MainMenu extends HLayout {

    /** The interface that provides i18n messages. */
    private final FLYSConstants messages = GWT.create(FLYSConstants.class);

    /** An instance to FLYS.*/
    protected FLYS flys;

    /** The user that is currently logged in. */
    protected User currentUser;

    /** The label that displays the current logged in user. */
    protected Label userText;

    /** The button to log the current user out.*/
    protected Button logout;

    /** The button to open the project list.*/
    protected Button projectList;

    /** The button to switch between the english and german version.*/
    protected Button language;

    /** The button to open an info panel.*/
    protected Button info;

    protected UserServiceAsync userService =
        GWT.create(UserService.class);

    /**
     * The default constructor for creating a new MainMenu.
     */
    public MainMenu(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.");
                ProjectList list = getFlys().getProjectList();
                if (list.isVisible())
                    list.hide();
                else
                    list.show();
            }
        });

        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();
    }


    /**
     * This method is called by the constructor after creating the necessary
     * components. It initializes layout specific stuff like width, height,
     * colors and so on and appends the components.
     */
    protected void init() {
        setStyleName("bgBlueDark");
        setHeight("25px");
        setLayoutMargin(5);

        projectList.setStyleName("fontLightSmall");
        userText.setStyleName("fontLightSmall");
        logout.setStyleName("fontLightSmall");
        language.setStyleName("fontLightSmall");
        info.setStyleName("fontLightSmall");

        projectList.setWidth("140px");

        HLayout leftPanel = new HLayout();
        leftPanel.setWidth("80%");
        leftPanel.setMembersMargin(5);
        leftPanel.addMember(projectList);

        userText.setAlign(Alignment.RIGHT);
        userText.setWidth(200);
        logout.setWidth(70);
        info.setWidth(40);
        language.setWidth(70);

        HLayout rightPanel = new HLayout();
        rightPanel.setAlign(Alignment.RIGHT);
        rightPanel.setMembersMargin(3);
        rightPanel.setLayoutRightMargin(5);
        rightPanel.addMember(userText);
        rightPanel.addMember(logout);
        rightPanel.addMember(language);
        rightPanel.addMember(info);

        addMember(leftPanel);
        addMember(rightPanel);
    }


    /**
     * Returns the FLYS instance stored in this class.
     *
     * @return the flys instance.
     */
    protected FLYS getFlys() {
        return flys;
    }


    /**
     * Set the current {@link User} and call {@link updateCurrentUser()}
     * afterwards.
     *
     * @param user the new user.
     */
    public void setCurrentUser(User currentUser) {
        this.currentUser = currentUser;

        updateCurrentUser();
    }


    /**
     * Update the text field that shows the current user. If no user is
     * currently logged in, the text will display {@link FLYSConstants.guest()}.
     */
    public 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);
    }


    /**
     * Create a new project by calling FLYS.newProject().
     */
    protected void createNewProject() {
        flys.newProject();
    }


    /**
     * 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 currentLocale The current locale string (e.g. "en").
     * @param newLocale The new locale string (e.g. "de").
     */
    protected void switchLanguage(String currentLocale, String newLocale) {
        String newLocation = Window.Location.getHref();

        if (newLocation.endsWith("/")) {
            newLocation = newLocation.substring(0, newLocation.length()-1);
        }

        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", "");
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org