ingo@1: package de.intevation.flys.client.client.ui;
ingo@1:
ingo@1: import com.google.gwt.core.client.GWT;
ingo@210: import com.google.gwt.i18n.client.LocaleInfo;
ingo@210: import com.google.gwt.user.client.Window;
ingo@1:
ingo@28: import com.smartgwt.client.types.Alignment;
ingo@212: import com.smartgwt.client.util.BooleanCallback;
ingo@212: import com.smartgwt.client.util.SC;
ingo@1: import com.smartgwt.client.widgets.Label;
ingo@6: import com.smartgwt.client.widgets.events.ClickEvent;
ingo@6: import com.smartgwt.client.widgets.events.ClickHandler;
ingo@1: import com.smartgwt.client.widgets.layout.HLayout;
ingo@1:
ingo@6: import de.intevation.flys.client.client.FLYS;
ingo@211: import de.intevation.flys.client.client.FLYSConstants;
ingo@25: import de.intevation.flys.client.shared.model.User;
ingo@1:
ingo@1:
ingo@1: /**
ingo@1: * @author Ingo Weinzierl
ingo@1: */
ingo@1: public class MainMenu extends HLayout {
ingo@1:
ingo@1: /** The interface that provides i18n messages. */
ingo@211: private FLYSConstants messages = GWT.create(FLYSConstants.class);
ingo@1:
ingo@6: /** An instance to FLYS.*/
ingo@6: protected FLYS flys;
ingo@6:
ingo@2: /** The user that is currently logged in. */
ingo@2: protected User currentUser;
ingo@2:
ingo@2: /** The label that displays the current logged in user. */
ingo@2: protected Label userText;
ingo@2:
ingo@28: /** The button to log the current user out.*/
ingo@28: protected Label logout;
ingo@2:
ingo@6: /** The button to add new projects.*/
ingo@28: protected Label newProject;
ingo@28:
ingo@28: /** The button to open the project list.*/
ingo@28: protected Label projectList;
ingo@28:
ingo@28: /** The button to switch between the english and german version.*/
ingo@28: protected Label language;
ingo@28:
ingo@28: /** The button to open an info panel.*/
ingo@28: protected Label info;
ingo@6:
ingo@1: /**
ingo@1: * The default constructor for creating a new MainMenu.
ingo@1: */
ingo@6: public MainMenu(FLYS flys) {
ingo@6: this.flys = flys;
ingo@6:
ingo@102: String guest = messages.user() + " " + messages.guest();
ingo@102:
ingo@102: userText = new Label(guest);
ingo@28: newProject = new Label(messages.new_project());
ingo@28: projectList = new Label(messages.manage_projects());
ingo@28: logout = new Label(messages.logout());
ingo@28: language = new Label(messages.switch_language());
ingo@28: info = new Label(messages.info());
ingo@6:
ingo@28: newProject.addClickHandler(new ClickHandler() {
ingo@6: public void onClick(ClickEvent event) {
ingo@6: GWT.log("Clicked 'New Project' button.");
ingo@6: createNewProject();
ingo@6: }
ingo@6: });
ingo@2:
ingo@28: projectList.addClickHandler(new ClickHandler() {
ingo@28: public void onClick(ClickEvent event) {
ingo@28: GWT.log("Clicked 'Open ProjectList' button.");
raimund@88: ProjectList list = getFlys().getProjectList();
raimund@94: if (list.isVisible())
raimund@88: list.hide();
raimund@82: else
raimund@88: list.show();
ingo@28: }
ingo@28: });
ingo@28:
ingo@28: logout.addClickHandler(new ClickHandler() {
ingo@28: public void onClick(ClickEvent event) {
ingo@28: GWT.log("Clicked 'logout' button.");
ingo@28: GWT.log("IMPLEMENT the 'logout' function.");
ingo@28: }
ingo@28: });
ingo@28:
ingo@28: language.addClickHandler(new ClickHandler() {
ingo@28: public void onClick(ClickEvent event) {
ingo@212: LocaleInfo info = LocaleInfo.getCurrentLocale();
ingo@212: final String currentLocale = info.getLocaleName();
ingo@212: final String newLocale = currentLocale.equals("de")
ingo@212: ? "en"
ingo@212: : "de";
ingo@210:
ingo@212: SC.confirm(messages.warning(), messages.warning_language(),
ingo@212: new BooleanCallback() {
ingo@212: public void execute(Boolean value) {
ingo@212: if (value) {
ingo@212: switchLanguage(currentLocale, newLocale);
ingo@212: }
ingo@212: }
ingo@212: });
ingo@28: }
ingo@28: });
ingo@28:
ingo@28: info.addClickHandler(new ClickHandler() {
ingo@28: public void onClick(ClickEvent event) {
ingo@28: GWT.log("Clicked 'info' button.");
ingo@28: GWT.log("IMPLEMENT the 'open info panel' function.");
ingo@28: }
ingo@28: });
ingo@28:
ingo@1: init();
ingo@1: }
ingo@1:
ingo@1:
ingo@2: /**
ingo@2: * This method is called by the constructor after creating the necessary
ingo@2: * components. It initializes layout specific stuff like width, height,
ingo@2: * colors and so on and appends the components.
ingo@2: */
ingo@1: protected void init() {
raimund@66: setStyleName("bgBlueDark");
raimund@66: setHeight("25px");
ingo@28: setLayoutMargin(5);
ingo@6:
raimund@66: newProject.setStyleName("fontLightSmall");
raimund@66: projectList.setStyleName("fontLightSmall");
raimund@66: userText.setStyleName("fontLightSmall");
raimund@66: newProject.setStyleName("fontLightSmall");
raimund@66: logout.setStyleName("fontLightSmall");
raimund@66: language.setStyleName("fontLightSmall");
raimund@66: info.setStyleName("fontLightSmall");
raimund@66:
raimund@66: projectList.setWidth("140px");
raimund@66:
ingo@28: HLayout leftPanel = new HLayout();
ingo@28: leftPanel.setWidth("80%");
ingo@28: leftPanel.setMembersMargin(5);
ingo@28: leftPanel.addMember(newProject);
ingo@28: leftPanel.addMember(projectList);
ingo@6:
ingo@28: userText.setAlign(Alignment.RIGHT);
ingo@28: logout.setAlign(Alignment.RIGHT);
ingo@28: info.setAlign(Alignment.RIGHT);
ingo@28: language.setAlign(Alignment.RIGHT);
ingo@2:
ingo@28: userText.setWidth(200);
ingo@28: logout.setWidth(70);
ingo@28: info.setWidth(40);
ingo@28: language.setWidth(70);
ingo@28:
ingo@28: HLayout rightPanel = new HLayout();
ingo@28: rightPanel.setAlign(Alignment.RIGHT);
ingo@28: rightPanel.setMembersMargin(3);
ingo@28: rightPanel.setLayoutRightMargin(5);
ingo@28: rightPanel.addMember(userText);
ingo@28: rightPanel.addMember(logout);
ingo@28: rightPanel.addMember(language);
ingo@28: rightPanel.addMember(info);
ingo@28:
ingo@28: addMember(leftPanel);
ingo@28: addMember(rightPanel);
ingo@28: }
ingo@28:
ingo@28:
ingo@28: /**
ingo@28: * Returns the FLYS instance stored in this class.
ingo@28: *
ingo@28: * @return the flys instance.
ingo@28: */
ingo@28: protected FLYS getFlys() {
ingo@28: return flys;
ingo@2: }
ingo@2:
ingo@2:
ingo@2: /**
ingo@2: * Set the current {@link User} and call {@link updateCurrentUser()}
ingo@2: * afterwards.
ingo@2: *
ingo@2: * @param user the new user.
ingo@2: */
ingo@2: public void setCurrentUser(User currentUser) {
ingo@2: this.currentUser = currentUser;
ingo@2:
ingo@2: updateCurrentUser();
ingo@2: }
ingo@2:
ingo@2:
ingo@2: /**
ingo@2: * Update the text field that shows the current user. If no user is
ingo@211: * currently logged in, the text will display {@link FLYSConstants.guest()}.
ingo@2: */
ingo@2: public void updateCurrentUser() {
ingo@2: String name = currentUser != null
ingo@25: ? currentUser.getName()
ingo@2: : messages.guest();
ingo@2:
ingo@2: GWT.log("Update the current user: " + name);
ingo@102:
ingo@102: String username = messages.user() + " " + name;
ingo@102: userText.setContents(username);
ingo@1: }
ingo@6:
ingo@6:
ingo@6: /**
ingo@6: * Create a new project by calling FLYS.newProject().
ingo@6: */
ingo@6: protected void createNewProject() {
ingo@6: flys.newProject();
ingo@6: }
ingo@210:
ingo@210:
ingo@210: /**
ingo@210: * This method triggers the language switch between the currentLocale
ingo@210: * and the newLocale. The switch is done by replacing a "locale="
ingo@210: * parameter in the url of the application. We could use the GWT UrlBuilder
ingo@210: * class to create a new URL, but - in my eyes - this class is a bit
ingo@210: * inconsistens in its implementation.
ingo@210: *
ingo@210: * @param currentLocale The current locale string (e.g. "en").
ingo@210: * @param newLocale The new locale string (e.g. "de").
ingo@210: */
ingo@210: protected void switchLanguage(String currentLocale, String newLocale) {
ingo@210: String newLocation = Window.Location.getHref();
ingo@210:
ingo@212: String oldLocation = newLocation;
ingo@212:
ingo@212: if (newLocation.endsWith("/")) {
ingo@212: newLocation = newLocation.substring(0, newLocation.length()-1);
ingo@212: }
ingo@212:
ingo@212: String replace = null;
ingo@212: String replaceWith = null;
ingo@212:
ingo@210: if (newLocation.indexOf("&locale=") >= 0) {
ingo@212: replace = currentLocale.equals("de")
ingo@210: ? "&locale=de"
ingo@210: : "&locale=en";
ingo@210:
ingo@212: replaceWith = "&locale=" + newLocale;
ingo@210: }
ingo@210: else if (newLocation.indexOf("?locale=") >= 0) {
ingo@212: replace = currentLocale.equals("de")
ingo@210: ? "?locale=de"
ingo@210: : "?locale=en";
ingo@210:
ingo@212: replaceWith = "?locale=" + newLocale;
ingo@210: }
ingo@210: else {
ingo@210: newLocation += newLocation.indexOf("?") >= 0
ingo@210: ? "&locale=" + newLocale
ingo@210: : "?locale=" + newLocale;
ingo@210: }
ingo@210:
ingo@212: if (replace != null && replaceWith != null) {
ingo@212: newLocation = newLocation.replace(replace, replaceWith);
ingo@212: }
ingo@212:
ingo@210: Window.open(newLocation, "_self", "");
ingo@210: }
ingo@1: }
ingo@1: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :