ingo@27: package de.intevation.flys.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:
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;
ingo@27: import com.smartgwt.client.widgets.layout.HLayout;
bjoern@4602: import com.smartgwt.client.widgets.events.ClickEvent;
bjoern@4602: import com.smartgwt.client.widgets.events.ClickHandler;
ingo@27:
bjoern@4602: import de.intevation.flys.client.client.FLYS;
ingo@211: import de.intevation.flys.client.client.FLYSConstants;
bjoern@4602: import de.intevation.flys.client.client.services.UserService;
bjoern@4602: import de.intevation.flys.client.client.services.UserServiceAsync;
bjoern@4602: import de.intevation.flys.client.shared.model.User;
ingo@27:
ingo@27:
ingo@27: /**
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. */
ingo@211: private 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. */
bjoern@4602: private Label userText;
bjoern@4602:
bjoern@4602: /** The button to log the current user out.*/
bjoern@4602: private Button logout;
bjoern@4602:
bjoern@4602: /** The button to open the project list.*/
bjoern@4602: private Button projectList;
bjoern@4602:
bjoern@4602: /** The button to switch between the english and german version.*/
bjoern@4602: private Button language;
bjoern@4602:
bjoern@4602: /** The button to open an info panel.*/
bjoern@4602: private Button info;
bjoern@4602:
bjoern@4602: private UserServiceAsync userService =
bjoern@4602: GWT.create(UserService.class);
bjoern@4602:
bjoern@4602: /** An instance to FLYS.*/
bjoern@4602: private 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() {
bjoern@4602: public void onFailure(Throwable caught) {
bjoern@4602: }
bjoern@4602:
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.");
bjoern@4602: GWT.log("IMPLEMENT the 'open info panel' function.");
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);
bjoern@4602: 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 :