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; bjoern@4215: import com.google.gwt.user.client.rpc.AsyncCallback; 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@532: import com.smartgwt.client.widgets.Button; 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; bjoern@4215: import de.intevation.flys.client.client.services.UserService; bjoern@4215: import de.intevation.flys.client.client.services.UserServiceAsync; 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. */ christian@2983: private final 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@532: protected Button logout; ingo@2: ingo@6: /** The button to add new projects.*/ ingo@532: protected Button newProject; ingo@28: ingo@28: /** The button to open the project list.*/ ingo@532: protected Button projectList; ingo@28: ingo@28: /** The button to switch between the english and german version.*/ ingo@532: protected Button language; ingo@28: ingo@28: /** The button to open an info panel.*/ ingo@532: protected Button info; ingo@6: bjoern@4215: protected UserServiceAsync userService = bjoern@4215: GWT.create(UserService.class); bjoern@4215: 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@532: newProject = new Button(messages.new_project()); ingo@532: projectList = new Button(messages.manage_projects()); ingo@532: logout = new Button(messages.logout()); ingo@532: language = new Button(messages.switch_language()); ingo@532: info = new Button(messages.info()); ingo@6: ingo@28: newProject.addClickHandler(new ClickHandler() { christian@2983: @Override 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() { christian@2983: @Override 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() { christian@2983: @Override ingo@28: public void onClick(ClickEvent event) { ingo@28: GWT.log("Clicked 'logout' button."); bjoern@4215: userService.logoutCurrentUser(new AsyncCallback() { bjoern@4215: public void onFailure(Throwable caught) { bjoern@4215: } bjoern@4215: bjoern@4215: public void onSuccess(Void result) { bjoern@4215: Window.Location.replace("/login.jsp"); bjoern@4215: } bjoern@4215: }); bjoern@4215: ingo@28: } ingo@28: }); ingo@28: ingo@28: language.addClickHandler(new ClickHandler() { christian@2983: @Override 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() { christian@2983: @Override 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() { christian@2983: @Override 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: 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: 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 :