com.dimad.map.executor.client.Main.java Source code

Java tutorial

Introduction

Here is the source code for com.dimad.map.executor.client.Main.java

Source

/*
 * Copyright 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package com.dimad.map.executor.client;

import java.util.LinkedHashMap;
import java.util.Map;

import com.dimad.map.executor.client.MainConstants;
import com.dimad.map.executor.client.services.ILoginServiceAsync;
import com.dimad.gwt.utils.utils.IRedraw;
import com.dimad.map.executor.client.beans.custom.SessionInfo;
import com.dimad.map.executor.client.content.ContentPanel;
import com.dimad.map.executor.client.login.LoginDialog;
import com.dimad.map.executor.client.services.ILoginService;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.UrlBuilder;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.ClosingEvent;
import com.google.gwt.user.client.Window.ClosingHandler;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.UIObject;
import com.google.gwt.user.client.ui.Widget;

/**
 * HelloWorld application.
 */
public class Main implements EntryPoint, IRedraw {

    private Map<String, UIObject> elements = new LinkedHashMap<String, UIObject>();

    private MainPanel mainPanel = null;

    private MainConstants constants = null;

    public SessionInfo sessionInfo = null;

    private ILoginServiceAsync loginService = null;

    public Command login = new Command() {
        @Override
        public void execute() {
            LoginDialog dialog = LoginDialog.getInstance(Main.this);
            dialog.center();
        }
    };

    public Command logout = new Command() {
        @Override
        public void execute() {
            loginService.logout(new AsyncCallback<Void>() {
                @Override
                public void onSuccess(Void result) {
                    locate();
                }

                @Override
                public void onFailure(Throwable caught) {
                    locate();
                }

                private void locate() {
                    String currentLocale = LocaleInfo.getCurrentLocale().getLocaleName();
                    UrlBuilder builder = Location.createUrlBuilder().setParameter("locale", currentLocale);
                    Window.Location.replace(builder.buildString());
                }
            });
        }
    };

    public Main() {
        super();
        try {
            constants = GWT.create(MainConstants.class);
            loginService = GWT.create(ILoginService.class);
            GWT.log("Gwt sanatel started");
        } catch (Throwable e) {
            GWT.log("Could not start application", e);
            Window.alert(e.getMessage());
        }
    }

    @Override
    public void onModuleLoad() {
        try {
            GWT.log("Loading module ...");
            mainPanel = new MainPanel(this).buildPanel();

            Window.enableScrolling(true);
            Window.setMargin("0px");
            Window.setTitle(constants.applicationTitle());

            RootPanel.get().add(mainPanel);

            redraw();
            //disable();
            GWT.log("Module loaded");
        } catch (Throwable e) {
            GWT.log("Could not start application", e);
            Window.alert(e.getMessage());
        }
    }

    @Override
    public void enable() {
        DOM.setStyleAttribute(RootPanel.get().getElement(), "cursor", "default");
        for (String key : elements.keySet()) {
            UIObject uiObject = elements.get(key);
            if (uiObject != null && uiObject instanceof IRedraw) {
                ((IRedraw) uiObject).enable();
            }
        }
    }

    @Override
    public void disable() {
        DOM.setStyleAttribute(RootPanel.get().getElement(), "cursor", "wait");
        for (String key : elements.keySet()) {
            UIObject uiObject = elements.get(key);
            if (uiObject != null && uiObject instanceof IRedraw) {
                ((IRedraw) uiObject).disable();
            }
        }
    }

    @Override
    public void invertState() {
        try {
            DOM.setStyleAttribute(RootPanel.get().getElement(), "cursor", "wait");
            for (String key : elements.keySet()) {
                UIObject uiObject = elements.get(key);
                if (uiObject != null && uiObject instanceof IRedraw) {
                    ((IRedraw) uiObject).invertState();
                }
            }
            DOM.setStyleAttribute(RootPanel.get().getElement(), "cursor", "default");
        } catch (Exception e) {
            Window.alert("Main.redraw " + e.getMessage());
        }
    }

    @Override
    public void redraw() {
        try {
            DOM.setStyleAttribute(RootPanel.get().getElement(), "cursor", "wait");
            for (String key : elements.keySet()) {
                UIObject uiObject = elements.get(key);
                if (uiObject != null && uiObject instanceof IRedraw) {
                    ((IRedraw) uiObject).redraw();
                }
            }
            DOM.setStyleAttribute(RootPanel.get().getElement(), "cursor", "default");
        } catch (Exception e) {
            Window.alert("Main.redraw " + e.getMessage());
        }
    }

    public ContentPanel getContentPanel() {
        return mainPanel.contentPanell;
    }

    public void setContentPanel(Widget contentPanel) {
        mainPanel.setContentPanel(contentPanel);
    }

    public ButtonPanel getButtonPanel() {
        return mainPanel.buttonPanell;
    }

    public MenuPanel getMenuPanel() {
        return mainPanel.menuPanell;
    }

    public <T extends UIObject> T getElement(String name) {
        synchronized (elements) {
            UIObject object = elements.get(name);
            return (T) object;
        }
    }

    public <T extends UIObject> T getElement(Class<T> clazz) {
        return getElement(clazz.getName());
    }

    public void addElement(String name, UIObject object) {
        synchronized (elements) {
            if (elements.get(name) == null) {
                elements.put(name, object);
            }
        }
    }

    public void addElement(Class clazz, UIObject object) {
        synchronized (elements) {
            if (elements.get(clazz.getName()) == null) {
                elements.put(clazz.getName(), object);
            }
        }
    }

    public void removeElement(String name) {
        synchronized (elements) {
            elements.remove(name);
        }
    }

    public <T extends UIObject> void removeElement(Class<T> clazz) {
        synchronized (elements) {
            elements.remove(clazz.getName());
        }
    }

    public void replaceElement(String name, UIObject object) {
        synchronized (elements) {
            elements.put(name, object);
        }
    }

    public void replaceElement(Class clazz, UIObject object) {
        synchronized (elements) {
            elements.put(clazz.getName(), object);
        }
    }

    public static String getContentWidgetToken(Class cwClass) {
        String className = cwClass.getName();
        className = className.substring(className.lastIndexOf('.') + 1);
        return "!" + className;
    }

    public SessionInfo getSessionInfo() {
        return sessionInfo;
    }

    public void setSessionInfo(SessionInfo sessionInfo) {
        this.sessionInfo = sessionInfo;
    }

    private class WindowClosingHandler implements ClosingHandler {
        @Override
        public void onWindowClosing(ClosingEvent event) {
        }
    }

}