Java tutorial
package net.meddeb.md.core.presentation; /*-------------------------------------------------------------------- mdCore module. The boot module for Merged directory system. Copyright (C) 2015, Abdelhamid MEDDEB (abdelhamid@meddeb.net) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. ---------------------------------------------------------------------*/ import net.meddeb.md.core.shared.Logininfo; import com.google.gwt.core.client.GWT; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.user.client.Cookies; import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.DecoratorPanel; import com.google.gwt.user.client.ui.DialogBox; import com.google.gwt.user.client.ui.Grid; import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.HasHorizontalAlignment; import com.google.gwt.user.client.ui.HasVerticalAlignment; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.PasswordTextBox; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.TextBox; import com.google.gwt.user.client.ui.VerticalPanel; public class MenuLauncher { public static final String MENUTAG = "MDCOREMENU"; public static final String SESSIONTAG = "MDCORESESSION"; public static final String MODULEID = "mdcore"; private final static String VERSION = "Version 0.1.0 "; private MainMsg mainMsg = GWT.create(MainMsg.class); private MenuManager menuManager = null; private MenuMessaging menuMessaging = null; private final Label lblVersion = new Label(); private final Label lblHelp = new Label(); private final Button connectButton = new Button(mainMsg.connect()); private final Label connectLabel = new Label(); // -- connect button handlers -- // logout callback AsyncCallback<Boolean> logoutCallback = new AsyncCallback<Boolean>() { @Override public void onFailure(Throwable e) { showWarning("Server error: " + e.getMessage()); } @Override public void onSuccess(Boolean success) { if (success) { setLoggedoutStatus(); } else showWarning("Cannot logged out from server"); } }; // logout click handler private ClickHandler logoutClickHandler = new ClickHandler() { @Override public void onClick(ClickEvent evnt) { if (isConnected()) Login.Access.getInstance().logout(logoutCallback); } }; // -- private boolean isCurrent(MDCoreMenu menu) { String strMenu = Cookies.getCookie(MENUTAG); MDCoreMenu currMenu = MDCoreMenu.fromName(strMenu); return ((currMenu != null) && (currMenu == menu)); } private void storeMenu(MDCoreMenu menu) { String moduleTag = "/" + MODULEID + "/"; Cookies.setCookie(MENUTAG, menu.toString(), null, null, moduleTag, false); } private void clearMenu() { String moduleTag = "/" + MODULEID + "/"; Cookies.removeCookie(MENUTAG, moduleTag); } private void setConnected(String sessionID) { String moduleTag = "/" + MODULEID + "/"; Cookies.setCookie(SESSIONTAG, sessionID, null, null, moduleTag, false); } private void setLoggedout() { String moduleTag = "/" + MODULEID + "/"; Cookies.removeCookie(SESSIONTAG, moduleTag); } private boolean isConnected() { String sessionID = Cookies.getCookie(SESSIONTAG); return ((sessionID != null) && (!sessionID.isEmpty())); } private void setLoggedoutStatus() { setLoggedout(); menuManager.setLoggedoutStatus(); RootPanel.get("main").clear(); clearMenu(); showConnectstatus(null); } private void doShowError(String msg) { final DialogBox errorBox = new DialogBox(); msg = "<strong>" + mainMsg.loginError() + "</strong><p>" + msg + "</p>"; final HTML helpMessage = new HTML(msg); VerticalPanel helpPanel = new VerticalPanel(); final Button closeButton = new Button(mainMsg.close()); closeButton.getElement().setId("closeButton"); closeButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { errorBox.hide(); } }); errorBox.setText(mainMsg.errorTitle()); helpPanel.addStyleName("dialogPanel"); helpPanel.add(helpMessage); helpPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT); helpPanel.add(closeButton); errorBox.setWidget(helpPanel); errorBox.center(); closeButton.setFocus(true); } private void doHelpLink() { final DialogBox helpBox = new DialogBox(); String msg = mainMsg.help1(); msg = msg + "<p><a href=\"" + mainMsg.help2() + " \"target=\"_blank\">" + mainMsg.help2() + "</a></p>"; final HTML helpMessage = new HTML(msg); VerticalPanel helpPanel = new VerticalPanel(); final Button closeButton = new Button(mainMsg.close()); closeButton.getElement().setId("closeButton"); closeButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { helpBox.hide(); } }); helpBox.setText(mainMsg.helpLink()); helpPanel.addStyleName("dialogPanel"); helpPanel.add(helpMessage); helpPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT); helpPanel.add(closeButton); helpBox.setWidget(helpPanel); helpBox.center(); closeButton.setFocus(true); } private void doHelpAbout() { final DialogBox aboutBox = new DialogBox(); String msg = "<b>mdCore</b>, " + mainMsg.appTitle(); msg = msg + "<p>" + VERSION; msg = msg + "<br/>Copyright (C) 2015 Abdelhamid MEDDEB <abdelhamid@meddeb.net></p>"; msg = msg + "<p>" + mainMsg.gnuLicense() + "</p>"; msg = msg + "<p>" + mainMsg.noWarranty() + "</p>"; final HTML aboutMessage = new HTML(msg); VerticalPanel aboutPanel = new VerticalPanel(); final Button closeButton = new Button(mainMsg.close()); closeButton.getElement().setId("closeButton"); closeButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { aboutBox.hide(); } }); aboutBox.setText(mainMsg.helpAbout()); aboutPanel.addStyleName("dialogPanel"); aboutPanel.add(aboutMessage); aboutPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT); aboutPanel.add(closeButton); aboutBox.setWidget(aboutPanel); aboutBox.center(); closeButton.setFocus(true); } private void doMessaging() { menuMessaging = new MenuMessaging(this); menuMessaging.show(); } private void doLoginAsRoot() { lblHelp.setText(mainMsg.toRootConnect()); final Grid grid = new Grid(4, 2); grid.setWidth("100%"); grid.setCellSpacing(5); grid.getColumnFormatter().setWidth(0, "30%"); grid.getColumnFormatter().setWidth(1, "70%"); grid.getCellFormatter().getElement(0, 0).setAttribute("colSpan", "2"); grid.getCellFormatter().setAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE); grid.getCellFormatter().setAlignment(3, 1, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE); final Label lblTitle = new Label(mainMsg.rootConnectTitle()); lblTitle.setStyleName("edittitle"); final Label lblLogin = new Label(mainMsg.lblLogin()); final Label lblPassword = new Label(mainMsg.lblPassword()); final TextBox edtLogin = new TextBox(); edtLogin.setWidth("100%"); final PasswordTextBox edtPassword = new PasswordTextBox(); edtPassword.setWidth("100%"); final Button btnConnect = new Button(mainMsg.connect()); btnConnect.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent arg0) { Login.Access.getInstance().getRootLoginfromServer(edtLogin.getText().trim(), edtPassword.getText().trim(), new AsyncCallback<Logininfo>() { @Override public void onSuccess(Logininfo logInfo) { showConnectstatus(logInfo); if (logInfo == null) { lblHelp.setText(mainMsg.connectErr()); doShowError(mainMsg.connectErr()); } else { RootPanel.get("main").clear(); clearMenu(); setConnected(logInfo.getSessionID()); lblHelp.setText(mainMsg.connected()); menuManager.setRootLoggedinStatus(); } } @Override public void onFailure(Throwable e) { lblHelp.setText(mainMsg.connectErr()); } }); } }); grid.setWidget(0, 0, lblTitle); grid.setWidget(1, 0, lblLogin); grid.setWidget(2, 0, lblPassword); grid.setWidget(1, 1, edtLogin); grid.setWidget(2, 1, edtPassword); grid.setWidget(3, 1, btnConnect); DecoratorPanel mainPanel = new DecoratorPanel(); mainPanel.addStyleName("decPanels"); mainPanel.setWidth("300px"); mainPanel.add(grid); final VerticalPanel basePanel = new VerticalPanel(); basePanel.setWidth("100%"); basePanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER); basePanel.add(mainPanel); RootPanel.get("main").clear(); RootPanel.get("main").add(basePanel); } private void showConnectstatus(Logininfo logInfo) { if (logInfo == null) { connectButton.setText(mainMsg.connect()); connectLabel.setText(""); lblHelp.setText(mainMsg.toConnect()); } else { connectButton.setText(mainMsg.logout()); connectLabel.setText(logInfo.getDisplayName()); } } private void initMain() { lblVersion.setText(VERSION); connectButton.addStyleName("connectButton"); connectButton.addClickHandler(logoutClickHandler); RootPanel.get("connectBtn").add(connectButton); RootPanel.get("connectLbl").add(connectLabel); RootPanel.get("version").add(lblVersion); RootPanel.get("help").add(lblHelp); } public MenuLauncher(MenuManager menuMgr) { initMain(); menuManager = menuMgr; } public void showWarning(String message) { final DialogBox warningBox = new DialogBox(); String msg = "<b>Warning !</b>"; msg = msg + "<p>" + message + "</p>"; final HTML warningMessage = new HTML(msg); VerticalPanel warningPanel = new VerticalPanel(); final Button closeButton = new Button(mainMsg.close()); closeButton.getElement().setId("closeButton"); closeButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { warningBox.hide(); } }); warningBox.setText(mainMsg.helpAbout()); warningPanel.addStyleName("dialogPanel"); warningPanel.add(warningMessage); warningPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT); warningPanel.add(closeButton); warningBox.setWidget(warningPanel); warningBox.center(); closeButton.setFocus(true); } public void showHelp(String message) { lblHelp.setText(message); } public void initConnectStatus() { Login.Access.getInstance().getLoginfromSession(new AsyncCallback<Logininfo>() { @Override public void onFailure(Throwable e) { showConnectstatus(null); } @Override public void onSuccess(Logininfo logInfo) { showConnectstatus(logInfo); if (logInfo != null) { if (logInfo.isRoot()) menuManager.setRootLoggedinStatus(); lblHelp.setText(mainMsg.connected()); } } }); } public void launch(MDCoreMenu menu) { launch(menu, false); } public void launch(MDCoreMenu menu, boolean reload) { if (menu == null) { RootPanel.get("main").clear(); return; } if ((isCurrent(menu)) && (!reload)) return; switch (menu) { case MESSAGING: doMessaging(); break; case ROOTCONNECT: doLoginAsRoot(); break; case HELPABOUT: doHelpAbout(); break; case HELPLINK: doHelpLink(); break; default: } if ((menu != MDCoreMenu.HELPABOUT) && (menu != MDCoreMenu.HELPLINK)) { storeMenu(menu); } } }