/*******************************************************************************
 * Copyright (c) 2010 Bolton University, UK.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the License
 * which accompanies this distribution in the file LICENSE.txt
 *******************************************************************************/
package uk.ac.bolton.archimate.editor;

import java.io.File;
import java.io.IOException;
import java.net.URL;

import org.eclipse.core.runtime.Platform;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.osgi.service.datalocation.Location;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;

import uk.ac.bolton.archimate.editor.utils.PlatformUtils;



/**
 * The main application class for standalone RCP operation.
 * 
 * @author Phillip Beauvoir
 */
public class Application
implements IApplication {

    /**
     * ID of the Application
     */
    public static String ID = ArchimateEditorPlugin.PLUGIN_ID + ".app"; //$NON-NLS-1$
    
    public static File WORKSPACE_FOLDER = new File(PlatformUtils.getApplicationDataFolder(),
                                                   "ArchiMate Modeller");

	/**
	 * Constructor
	 */
	public Application() {
	}
	
	public Object start(IApplicationContext context) throws Exception {
	    /*
	     * Platform specific startup if user launches app twice or from .archimate file
	     */
	    IPlatformLauncher launcher = ArchimateEditorPlugin.INSTANCE.getPlatformLauncher();
	    if(launcher != null) {
	        launcher.startup();
	        
            /*
             * If the application is already open, exit
             */
	        if(launcher.shouldApplicationExitEarly()) {
                return EXIT_OK;
            }
	    }
	    
	    Display display = PlatformUI.createDisplay();
	    
	    try {
	        int code = PlatformUI.createAndRunWorkbench(display, new ArchimateEditorWorkbenchAdvisor());
	        // Exit the application with an appropriate return code
	        return code == PlatformUI.RETURN_RESTART ? EXIT_RESTART : EXIT_OK;
	    }
	    finally {
	        if(display != null) {
	            display.dispose();
	        }
	    }
	}
	
	
    public void stop() {
    
    }

    /**
	 * Set the file location of the data store.<p>
	 * We want to do this for an RCP standalone app but not when this is running as a plugin
	 * becauae the location of the Workbench instance will be set already.
	 * This has to be done before the Workbench starts.
	 * Note that the launch configuration in the Eclipse IDE should have "@noDefault"
     * as the workspace for the launch configuration.
	 * 
	 * @param url A url to a folder
     * @throws IOException 
     * @throws IllegalStateException 
	 */
	@SuppressWarnings("unused")
    private void setWorkbenchDataLocation(URL url) throws IllegalStateException, IOException {
        /*
         * Ascertain the existing location. If it is null, we can set it.
         * When run from the Eclipse IDE, setting the workspace location in the launch configuration
         * to "@noDefault" will set it as null.
         */
        Location instanceLoc = Platform.getInstanceLocation();
        if(instanceLoc == null) {
            Logger.logError(
                    "Instance Location is null, cannot set it in setWorkbenchDataLocation(URL)", //$NON-NLS-1$
                    null);
        }
        else if(!instanceLoc.isSet()) {
            instanceLoc.release();
            /*
             * If this is set to true, you can't run another instance
             * of the app with this workspace open
             */ 
            instanceLoc.set(url, false);  
        }
	}
}

@@ -437,30 +437,8 @@
 %0D%0A%0D%0A
-import java.io.File;%0D%0A
 impo
@@ -458,24 +458,24 @@
 Exception;%0D%0A
+
 import java.
@@ -766,69 +766,8 @@
 %0D%0A%0D%0A
-import uk.ac.bolton.archimate.editor.utils.PlatformUtils;%0D%0A%0D%0A
 %0D%0A%0D%0A
@@ -1065,185 +1065,14 @@
 1$%0D%0A
+
     %0D%0A
-    public static File WORKSPACE_FOLDER = new File(PlatformUtils.getApplicationDataFolder(),%0D%0A                                                   %22ArchiMate Modeller%22);%0D%0A%0D%0A
 %09/**

Remove spurious constant WORKSPACE_FOLDER