org.gluster.storage.management.console.Application.java Source code

Java tutorial

Introduction

Here is the source code for org.gluster.storage.management.console.Application.java

Source

/*******************************************************************************
 * Copyright (c) 2006-2011 Gluster, Inc. <http://www.gluster.com>
 * This file is part of Gluster Management Console.
 *
 * Gluster Management Console 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, or (at your option) any later version.
 *
 * Gluster Management Console 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/>.
 *******************************************************************************/
package org.gluster.storage.management.console;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
import org.gluster.storage.management.console.dialogs.LoginDialog;
import org.gluster.storage.management.core.model.Entity;

/**
 * This class controls all aspects of the application's execution
 */
public class Application implements IApplication {

    public static final String PLUGIN_ID = "org.gluster.storage.management.console";
    private static Application instance;
    private List<IEntityListener> entityListeners = Collections.synchronizedList(new ArrayList<IEntityListener>());
    private IStatusLineManager statusLineManager;

    public Application() {
        instance = this;
    }

    public static Application getApplication() {
        return instance;
    }

    public IStatusLineManager getStatusLineManager() {
        return statusLineManager;
    }

    public void setStatusLineManager(IStatusLineManager statusLineManager) {
        this.statusLineManager = statusLineManager;
    }

    private boolean login() {
        LoginDialog loginDialog = new LoginDialog(new Shell(Display.getDefault()));
        return (loginDialog.open() == Window.OK);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
     */
    public Object start(IApplicationContext context) {
        setSystemProperties();

        Display display = PlatformUI.createDisplay();

        final boolean[] loginSuccess = new boolean[1];
        Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
            public void run() {
                loginSuccess[0] = login();
            }
        });

        if (!loginSuccess[0]) {
            return IApplication.EXIT_OK;
        }
        try {
            int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
            if (returnCode == PlatformUI.RETURN_RESTART) {
                return IApplication.EXIT_RESTART;
            }

            return IApplication.EXIT_OK;
        } finally {
            display.dispose();
        }
    }

    private void setSystemProperties() {
        // TODO: Trying this to avoid the webstart authentication dialog
        // to be tested, and removed if this doesn't work.
        System.setProperty("javaws.cfg.jauthenticator", "none");
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.equinox.app.IApplication#stop()
     */
    public void stop() {
        if (!PlatformUI.isWorkbenchRunning())
            return;
        final IWorkbench workbench = PlatformUI.getWorkbench();
        final Display display = workbench.getDisplay();
        display.syncExec(new Runnable() {
            public void run() {
                if (!display.isDisposed())
                    workbench.close();
            }
        });
    }

    public void addEntityListener(IEntityListener listener) {
        entityListeners.add(listener);
    }

    public void entityChanged(Entity entity, String[] paremeters) {
        for (IEntityListener listener : entityListeners) {
            listener.entityChanged(entity, paremeters);
        }
    }
}