cc.warlock.rcp.application.WarlockApplication.java Source code

Java tutorial

Introduction

Here is the source code for cc.warlock.rcp.application.WarlockApplication.java

Source

/**
 * Warlock, the open-source cross-platform game client
 *  
 * Copyright 2008, Warlock LLC, and individual contributors as indicated
 * by the @authors tag. 
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
/*
 * Created on Dec 30, 2004
 */
package cc.warlock.rcp.application;

import java.util.Map;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.osgi.service.datalocation.Location;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;

import cc.warlock.core.settings.WarlockPreferencesScope;

import com.martiansoftware.jsap.FlaggedOption;
import com.martiansoftware.jsap.JSAP;
import com.martiansoftware.jsap.JSAPException;
import com.martiansoftware.jsap.JSAPResult;

/**
 * @author Marshall
 */
public class WarlockApplication implements IApplication, IAdaptable {
    private String startWithProfile = null;
    private static WarlockApplication _instance;
    private WarlockWorkbenchAdvisor advisor;

    public WarlockApplication() {
        _instance = this;
    }

    public static WarlockApplication instance() {
        return _instance;
    }

    private void parseArguments(String[] arguments) {
        JSAP jsap = new JSAP();
        FlaggedOption profileOption = new FlaggedOption("profile", JSAP.STRING_PARSER, JSAP.NO_DEFAULT,
                JSAP.NOT_REQUIRED, 'p', "profile", "The profile to start Warlock with");

        try {
            jsap.registerParameter(profileOption);
        } catch (JSAPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        JSAPResult result = jsap.parse(arguments);
        if (result.contains("profile")) {
            startWithProfile = result.getString("profile");
        }
    }

    @Override
    public Object start(IApplicationContext context) throws Exception {
        @SuppressWarnings("rawtypes")
        Map args = context.getArguments();
        String arguments[] = (String[]) args.get(IApplicationContext.APPLICATION_ARGS);

        parseArguments(arguments);

        Display display = PlatformUI.createDisplay();

        Location instanceLocation = Platform.getInstanceLocation();

        if (!instanceLocation.lock()) {
            MessageDialog.openError(new Shell(display), "Warlock 2",
                    "Another instance of Warlock is currently running.");
            return EXIT_OK;
        }

        advisor = new WarlockWorkbenchAdvisor();
        int ret = PlatformUI.createAndRunWorkbench(display, advisor);

        //save configuration
        WarlockPreferencesScope.getInstance().flush();

        if (ret == PlatformUI.RETURN_RESTART)
            return EXIT_RESTART;

        return EXIT_OK;
    }

    @Override
    public void stop() {

    }

    public String getStartWithProfile() {
        return startWithProfile;
    }

    // This exists solely so that ScriptsWindowHandler can configure the
    // workbench window it creates.
    public IWorkbenchWindowConfigurer getWindowConfigurer(IWorkbenchWindow w) {
        return advisor.getWindowConfigurer(w);
    }

    // Generic getAdapter because this is used as a model for the CNF-based script view.
    @SuppressWarnings("unchecked")
    @Override
    public <T> T getAdapter(Class<T> adapter) {
        if (adapter.isInstance(this))
            return (T) this;
        else
            return null;
    }
}