/*******************************************************************************
 * 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.actions;

import java.io.File;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;

import uk.ac.bolton.archimate.editor.model.IEditorModelManager;
import uk.ac.bolton.archimate.editor.ui.IArchimateImages;

/**
 * Open Model Action
 * 
 * @author Phillip Beauvoir
 */
public class OpenModelAction
extends Action
implements IWorkbenchAction
{
    
    public OpenModelAction(IWorkbenchWindow window) {
        setText("&Open...");
        setToolTipText("Open Model");
        setId("uk.ac.bolton.archimate.editor.action.openModel");
        setActionDefinitionId(getId()); // register key binding
    }
    
    @Override
    public void run() {
        FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.OPEN);
        dialog.setFilterExtensions(new String[] { IEditorModelManager.ARCHIMATE_FILE_WILDCARD, "*.xml", "*.*" } );
        String path = dialog.open();
        if(path != null) {
            File file = new File(path);
            
            // Check it's not already open
            if(IEditorModelManager.INSTANCE.isModelLoaded(file)) {
                MessageDialog.openInformation(Display.getCurrent().getActiveShell(), "Open Model", "'" +
                                                file.getName() + "' " +
                                                "is already open.");
                return;
            }
            
            IEditorModelManager.INSTANCE.openModel(file);
        }
    }
    
    @Override
    public ImageDescriptor getImageDescriptor() {
        return IArchimateImages.ImageFactory.getImageDescriptor(IArchimateImages.ICON_OPEN_16);
    }

    public void dispose() {
    } 
}
@@ -950,16 +950,70 @@
 Images;%0D
+%0Aimport uk.ac.bolton.archimate.model.IArchimateModel;%0D
 %0A%0D%0A/**%0D%0A
@@ -1856,59 +1856,77 @@
     
-if(IEditorModelManager.INSTANCE.isModelLoaded
+IArchimateModel model = getModel
 (file)
+;%0D%0A            if(model != null
 ) %7B%0D
@@ -2010,16 +2010,65 @@
 Shell(),
+%0D%0A                                               
  %22Open M
@@ -2077,14 +2077,8 @@
 el%22,
- %22'%22 +
 %0D%0A  
@@ -2122,16 +2122,27 @@
         
+ %22File '%22 +
  file.ge
@@ -2225,16 +2225,115 @@
 y open.%22
+ +%0D%0A                                                %22 Its model name is '%22 + model.getName() + %22'.%22
 );%0D%0A    
@@ -2634,16 +2634,415 @@
 %0D%0A    %7D%0D
+%0A    %0D%0A    /**%0D%0A     * Get model if it is already open%0D%0A     */%0D%0A    private IArchimateModel getModel(File file) %7B%0D%0A        if(file != null) %7B%0D%0A            for(IArchimateModel model : IEditorModelManager.INSTANCE.getModels()) %7B%0D%0A                if(file.equals(model.getFile())) %7B%0D%0A                    return model;%0D%0A                %7D%0D%0A            %7D%0D%0A        %7D%0D%0A        %0D%0A        return null;%0D%0A    %7D%0D
 %0A%0D%0A    p

Make meaning of dialog message clearer