/**
 * 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 com.archimatetool.editor;

import java.io.File;
import java.io.IOException;

import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.osgi.service.datalocation.Location;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;

import com.archimatetool.editor.utils.FileUtils;


/**
 * Cleans and resets the Workbnech state
 * 
 * @author Phillip Beauvoir
 */
public class WorkbenchCleaner {
    
    static final String RESET_FILE = ".reset"; //$NON-NLS-1$
    static final String METADATA_FOLDER = ".metadata"; //$NON-NLS-1$
    static final String CONFIG_FOLDER = ".config"; //$NON-NLS-1$

    /**
     * Ask user if they want to reset the workbench.
     * If they do, create a marker file and restart.
     */
    public static void reset() {
        boolean result = MessageDialog.openQuestion(Display.getCurrent().getActiveShell(),
                Messages.WorkbenchCleaner_0,
                Messages.WorkbenchCleaner_1);
        if(!result) {
            return;
        }
        
        Location instanceLoc = Platform.getInstanceLocation();
        if(instanceLoc != null) {
            try {
                if(PlatformUI.getWorkbench().restart()) { // User might have cancelled on a dirty editor
                    File resetFile = new File(instanceLoc.getURL().getPath(), RESET_FILE);
                    resetFile.createNewFile();
                }
            }
            catch(Exception ex) {
                MessageDialog.openError(Display.getCurrent().getActiveShell(), Messages.WorkbenchCleaner_2, ex.getMessage());
                ex.printStackTrace();
            }
        }
    }
    
    /**
     * Check if the marker file exists to clean.
     * If it does, delete the marker file, the .metadata folder and the .config folder
     * @throws IOException
     */
    public static void checkForReset() throws IOException {
        Location instanceLoc = Platform.getInstanceLocation();
        if(instanceLoc != null) {
            File resetFile = new File(instanceLoc.getURL().getPath(), RESET_FILE);
            if(resetFile.exists()) {
                resetFile.delete();
                FileUtils.deleteFolder(new File(instanceLoc.getURL().getPath(), METADATA_FOLDER));
                FileUtils.deleteFolder(new File(instanceLoc.getURL().getPath(), CONFIG_FOLDER));
            }
        }
    }
}

@@ -771,73 +771,8 @@
 S-1$
-%0A    static final String CONFIG_FOLDER = %22.config%22; //$NON-NLS-1$
 %0A%0A  
@@ -2266,127 +2266,400 @@
     
-resetFile.delete();%0A                FileUtils.deleteFolder(new File(instanceLoc.getURL().getPath(), METADATA_FOLDER));%0A
+// delete reset file%0A                resetFile.delete();%0A                %0A                // delete .metadata folder%0A                FileUtils.deleteFolder(new File(instanceLoc.getURL().getPath(), METADATA_FOLDER));%0A                %0A                // delete .config folder%0A                Location configLoc = Platform.getConfigurationLocation();%0A                if(configLoc != null) %7B%0A    
     
@@ -2698,32 +2698,30 @@
 er(new File(
-instance
+config
 Loc.getURL()
@@ -2734,26 +2734,29 @@
 th()
-, CONFIG_FOLDER));
+));%0A                %7D
 %0A   

Use Platform.getConfigurationLocation()