teichmann@5861: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5861: * Software engineering by Intevation GmbH teichmann@5861: * teichmann@5993: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5861: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5993: * documentation coming with Dive4Elements River for details. teichmann@5861: */ teichmann@5861: teichmann@5835: package org.dive4elements.river.client.client.ui; ingo@27: ingo@27: import com.google.gwt.core.client.GWT; bjoern@4602: import com.google.gwt.i18n.client.LocaleInfo; bjoern@4602: import com.google.gwt.user.client.Window; bjoern@4602: import com.google.gwt.user.client.rpc.AsyncCallback; ingo@27: import com.smartgwt.client.types.Alignment; bjoern@4602: import com.smartgwt.client.types.VerticalAlignment; bjoern@4602: import com.smartgwt.client.util.BooleanCallback; bjoern@4602: import com.smartgwt.client.util.SC; bjoern@4602: import com.smartgwt.client.widgets.Button; ingo@27: import com.smartgwt.client.widgets.Img; ingo@27: import com.smartgwt.client.widgets.Label; bjoern@4602: import com.smartgwt.client.widgets.events.ClickEvent; bjoern@4602: import com.smartgwt.client.widgets.events.ClickHandler; christian@6546: import com.smartgwt.client.widgets.form.DynamicForm; christian@6437: import com.smartgwt.client.widgets.layout.HLayout; ingo@27: christian@6437: import org.dive4elements.river.client.client.Config; teichmann@5835: import org.dive4elements.river.client.client.FLYS; teichmann@5835: import org.dive4elements.river.client.client.FLYSConstants; teichmann@5835: import org.dive4elements.river.client.client.services.UserService; teichmann@5835: import org.dive4elements.river.client.client.services.UserServiceAsync; teichmann@5835: import org.dive4elements.river.client.shared.model.User; ingo@27: ingo@27: ingo@27: /** christian@6437: * Header of the FLYS webpage/app. christian@6437: * ingo@27: * @author Ingo Weinzierl ingo@27: */ ingo@27: public class FLYSHeader extends HLayout { ingo@27: ingo@27: /** The interface that provides the message resources. */ christian@6437: private final FLYSConstants MESSAGES = GWT.create(FLYSConstants.class); ingo@27: ingo@27: /** The height used for this header.*/ bjoern@4602: public static final int HEIGHT = 56; ingo@27: ingo@27: /** The height used for the images.*/ ingo@27: public static final int IMG_HEIGHT = 50; ingo@27: bjoern@4602: /** The user that is currently logged in. */ bjoern@4602: private User currentUser; ingo@27: bjoern@4602: /** The label that displays the current logged in user. */ christian@6437: private final Label userText; bjoern@4602: bjoern@4602: /** The button to log the current user out.*/ christian@6437: private final Button logout; bjoern@4602: bjoern@4602: /** The button to open the project list.*/ christian@6437: private final Button projectList; bjoern@4602: bjoern@4602: /** The button to switch between the english and german version.*/ christian@6437: private final Button language; bjoern@4602: bjoern@4602: /** The button to open an info panel.*/ christian@6437: private final Button info; bjoern@4602: christian@6437: private final UserServiceAsync userService = bjoern@4602: GWT.create(UserService.class); bjoern@4602: bjoern@4602: /** An instance to FLYS.*/ christian@6437: private final FLYS flys; bjoern@4602: bjoern@4602: bjoern@4602: public FLYSHeader(FLYS flys) { bjoern@4602: this.flys = flys; bjoern@4602: bjoern@4602: String guest = MESSAGES.user() + " " + MESSAGES.guest(); bjoern@4602: bjoern@4602: userText = new Label(guest); bjoern@4602: projectList = new Button(MESSAGES.manage_projects()); bjoern@4602: logout = new Button(MESSAGES.logout()); bjoern@4602: language = new Button(MESSAGES.switch_language()); bjoern@4602: info = new Button(MESSAGES.info()); bjoern@4602: bjoern@4602: projectList.addClickHandler(new ClickHandler() { bjoern@4602: @Override bjoern@4602: public void onClick(ClickEvent event) { bjoern@4602: GWT.log("Clicked 'Open ProjectList' button."); bjoern@4602: getFlys().openProjectList(); bjoern@4602: } bjoern@4602: }); bjoern@4602: bjoern@4602: logout.addClickHandler(new ClickHandler() { bjoern@4602: @Override bjoern@4602: public void onClick(ClickEvent event) { bjoern@4602: GWT.log("Clicked 'logout' button."); bjoern@4602: userService.logoutCurrentUser(new AsyncCallback() { christian@6437: @Override bjoern@4602: public void onFailure(Throwable caught) { bjoern@4602: } bjoern@4602: christian@6437: @Override bjoern@4602: public void onSuccess(Void result) { bjoern@4602: /* Just reload the page. GGInAFilter is goint to redirect bjoern@4602: * to the correct login page */ bjoern@4602: Window.Location.reload(); bjoern@4602: } bjoern@4602: }); bjoern@4602: bjoern@4602: } bjoern@4602: }); bjoern@4602: bjoern@4602: language.addClickHandler(new ClickHandler() { bjoern@4602: @Override bjoern@4602: public void onClick(ClickEvent event) { bjoern@4602: LocaleInfo info = LocaleInfo.getCurrentLocale(); bjoern@4602: final String currentLocale = info.getLocaleName(); bjoern@4602: final String newLocale = currentLocale.equals("de") bjoern@4602: ? "en" bjoern@4602: : "de"; bjoern@4602: bjoern@4602: SC.confirm(MESSAGES.warning(), MESSAGES.warning_language(), bjoern@4602: new BooleanCallback() { bjoern@4602: @Override bjoern@4602: public void execute(Boolean value) { bjoern@4602: if (value) { bjoern@4602: switchLanguage(currentLocale, newLocale); bjoern@4602: } bjoern@4602: } bjoern@4602: }); bjoern@4602: } bjoern@4602: }); bjoern@4602: bjoern@4602: info.addClickHandler(new ClickHandler() { bjoern@4602: @Override bjoern@4602: public void onClick(ClickEvent event) { bjoern@4602: GWT.log("Clicked 'info' button."); christian@6437: String wikiLink = Config.getInstance().getWikiUrl() + "/Info"; christian@6546: christian@6546: // Create a dynamic form which contains the SAML session christian@6546: // for the user which is currently logged in christian@6546: DynamicForm df = WikiLinks.dynamicForm(getFlys(), wikiLink); christian@6546: info.addChild(df); // add the form somewhere christian@6546: df.submitForm(); // submit the form to go to the wiki christian@6546: info.removeChild(df); // and remove the dynamic form bjoern@4602: } bjoern@4602: }); ingo@27: init(); ingo@27: } ingo@27: ingo@27: public void init() { bjoern@4602: setStyleName("header"); ingo@27: setWidth100(); ingo@27: setHeight(HEIGHT); bjoern@4602: setBackgroundColor("#a9c9e6"); ingo@27: setLayoutLeftMargin(5); ingo@27: setLayoutRightMargin(5); ingo@27: raimund@1399: String baseUrl = GWT.getHostPageBaseURL(); ingo@27: ingo@27: Img flys = new Img( raimund@1399: baseUrl + MESSAGES.flysLogo(), raimund@1399: 50, ingo@27: IMG_HEIGHT); ingo@27: bjoern@4602: Img bfg = new Img( bjoern@4602: baseUrl + MESSAGES.bfgLogoSmall(), bjoern@4602: 150, bjoern@4602: IMG_HEIGHT); ingo@27: ingo@27: Label fullname = new Label(MESSAGES.fullname()); ingo@27: fullname.setHeight(HEIGHT - IMG_HEIGHT); bjoern@4602: fullname.setStyleName("fontBlackMid"); ingo@27: bjoern@4602: HLayout left = new HLayout(); bjoern@4602: left.setDefaultLayoutAlign(VerticalAlignment.CENTER); bjoern@4602: left.setMembersMargin(3); ingo@27: left.addMember(flys); ingo@27: left.addMember(fullname); ingo@27: ingo@27: HLayout right = new HLayout(); ingo@27: right.setAlign(Alignment.RIGHT); bjoern@4602: right.setDefaultLayoutAlign(Alignment.RIGHT); bjoern@4602: right.setDefaultLayoutAlign(VerticalAlignment.CENTER); bjoern@4602: right.setMembersMargin(3); bjoern@4602: right.setLayoutRightMargin(5); bjoern@4602: bjoern@4602: projectList.setStyleName("manageProjects"); bjoern@4602: userText.setStyleName("fontBlackSmall"); bjoern@4602: logout.setStyleName("fontLightSmall"); bjoern@4602: language.setStyleName("fontLightSmall"); bjoern@4602: info.setStyleName("fontLightSmall"); bjoern@4602: bjoern@4602: userText.setAlign(Alignment.RIGHT); bjoern@4602: userText.setWidth(200); bjoern@4602: logout.setWidth(70); bjoern@4602: info.setWidth(40); bjoern@4602: language.setWidth(70); bjoern@4602: bjoern@4602: left.addMember(projectList); bjoern@4602: if (this.flys.isProjectListVisible()) { bjoern@4602: hideProjectButton(); bjoern@4602: } bjoern@4602: else { bjoern@4602: showProjectButton(); bjoern@4602: } bjoern@4602: bjoern@4602: right.addMember(userText); bjoern@4602: right.addMember(logout); bjoern@4602: right.addMember(language); felix@6558: // TEMPORARILY DEACTIVATED: The info button to link in wiki. felix@6558: // TODO (issue702) felix@6558: //right.addMember(info); ingo@27: right.addMember(bfg); ingo@27: ingo@27: addMember(left); ingo@27: addMember(right); ingo@27: } ingo@27: bjoern@4602: /** bjoern@4602: * Returns the FLYS instance stored in this class. bjoern@4602: * bjoern@4602: * @return the flys instance. bjoern@4602: */ bjoern@4602: private FLYS getFlys() { bjoern@4602: return flys; bjoern@4602: } ingo@27: ingo@27: /** bjoern@4602: * This method triggers the language switch between the currentLocale bjoern@4602: * and the newLocale. The switch is done by replacing a "locale=" bjoern@4602: * parameter in the url of the application. We could use the GWT UrlBuilder bjoern@4602: * class to create a new URL, but - in my eyes - this class is a bit bjoern@4602: * inconsistens in its implementation. ingo@27: * bjoern@4602: * @param currentLocale The current locale string (e.g. "en"). bjoern@4602: * @param newLocale The new locale string (e.g. "de"). ingo@27: */ bjoern@4602: private void switchLanguage(String currentLocale, String newLocale) { bjoern@4602: String newLocation = Window.Location.getHref(); ingo@27: bjoern@4602: if (newLocation.endsWith("/")) { bjoern@4602: newLocation = newLocation.substring(0, newLocation.length()-1); bjoern@4602: } ingo@27: bjoern@4602: String replace = null; bjoern@4602: String replaceWith = null; bjoern@4602: bjoern@4602: if (newLocation.indexOf("&locale=") >= 0) { bjoern@4602: replace = currentLocale.equals("de") bjoern@4602: ? "&locale=de" bjoern@4602: : "&locale=en"; bjoern@4602: bjoern@4602: replaceWith = "&locale=" + newLocale; bjoern@4602: } bjoern@4602: else if (newLocation.indexOf("?locale=") >= 0) { bjoern@4602: replace = currentLocale.equals("de") bjoern@4602: ? "?locale=de" bjoern@4602: : "?locale=en"; bjoern@4602: bjoern@4602: replaceWith = "?locale=" + newLocale; bjoern@4602: } bjoern@4602: else { bjoern@4602: newLocation += newLocation.indexOf("?") >= 0 bjoern@4602: ? "&locale=" + newLocale bjoern@4602: : "?locale=" + newLocale; bjoern@4602: } bjoern@4602: bjoern@4602: if (replace != null && replaceWith != null) { bjoern@4602: newLocation = newLocation.replace(replace, replaceWith); bjoern@4602: } bjoern@4602: bjoern@4602: Window.open(newLocation, "_self", ""); bjoern@4602: } bjoern@4602: bjoern@4602: /** bjoern@4602: * Update the text field that shows the current user. If no user is bjoern@4602: * currently logged in, the text will display {@link FLYSConstants.guest()}. bjoern@4602: */ bjoern@4602: private void updateCurrentUser() { bjoern@4602: String name = currentUser != null bjoern@4602: ? currentUser.getName() bjoern@4602: : MESSAGES.guest(); bjoern@4602: bjoern@4602: GWT.log("Update the current user: " + name); bjoern@4602: bjoern@4602: String username = MESSAGES.user() + " " + name; bjoern@4602: userText.setContents(username); bjoern@4602: } bjoern@4602: bjoern@4602: /** bjoern@4602: * Set the current {@link User} and call {@link updateCurrentUser()} bjoern@4602: * afterwards. bjoern@4602: * bjoern@4602: * @param user the new user. bjoern@4602: */ bjoern@4602: public void setCurrentUser(User currentUser) { bjoern@4602: this.currentUser = currentUser; bjoern@4602: bjoern@4602: updateCurrentUser(); bjoern@4602: } bjoern@4602: bjoern@4602: public void hideProjectButton() { bjoern@4602: this.projectList.hide(); bjoern@4602: } bjoern@4602: bjoern@4602: public void showProjectButton() { bjoern@4602: this.projectList.show(); ingo@27: } ingo@27: } ingo@27: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :