Search in sources :

Example 1 with CompatibilityHandlerException

use of com.archimatetool.editor.model.compatibility.CompatibilityHandlerException in project archi by archimatetool.

the class EditorModelManager method loadModel.

@Override
public IArchimateModel loadModel(File file) {
    if (file == null || !file.exists()) {
        return null;
    }
    // If it is already loaded return it
    IArchimateModel model = locateLoadedModel(file);
    if (model != null) {
        return model;
    }
    // Ascertain if this is an archive file
    boolean useArchiveFormat = IArchiveManager.FACTORY.isArchiveFile(file);
    // Create the Resource
    Resource resource = ArchimateResourceFactory.createNewResource(useArchiveFormat ? IArchiveManager.FACTORY.createArchiveModelURI(file) : URI.createFileURI(file.getAbsolutePath()));
    // Check model compatibility
    ModelCompatibility modelCompatibility = new ModelCompatibility(resource);
    // Load the model file
    try {
        resource.load(null);
    } catch (IOException ex) {
        // Error occured loading model.
        try {
            modelCompatibility.checkErrors();
        } catch (IncompatibleModelException ex1) {
            // Was it a disaster?
            MessageDialog.openError(Display.getCurrent().getActiveShell(), Messages.EditorModelManager_2, NLS.bind(Messages.EditorModelManager_3, file) + "\n" + // $NON-NLS-1$
            ex1.getMessage());
            return null;
        }
    }
    model = (IArchimateModel) resource.getContents().get(0);
    // Once loaded - check for later model version
    boolean isLaterModelVersion = modelCompatibility.isLaterModelVersion(ModelVersion.VERSION);
    if (isLaterModelVersion) {
        boolean answer = MessageDialog.openQuestion(Display.getCurrent().getActiveShell(), Messages.EditorModelManager_4, NLS.bind(Messages.EditorModelManager_5, file, model.getVersion()));
        if (!answer) {
            return null;
        }
    } else // Check for unknown model features which might be OK to load
    {
        List<Diagnostic> exceptions = modelCompatibility.getAcceptableExceptions();
        if (!exceptions.isEmpty()) {
            // $NON-NLS-1$
            String message = "";
            for (int i = 0; i < exceptions.size(); i++) {
                if (i == 3) {
                    // $NON-NLS-1$
                    message += (exceptions.size() - 3) + " " + Messages.EditorModelManager_12;
                    break;
                }
                // $NON-NLS-1$
                message += exceptions.get(i).getMessage() + "\n";
            }
            boolean answer = MessageDialog.openQuestion(Display.getCurrent().getActiveShell(), Messages.EditorModelManager_4, NLS.bind(Messages.EditorModelManager_13, file) + "\n\n" + // $NON-NLS-1$
            message);
            if (!answer) {
                return null;
            }
        }
    }
    // And then fix any backward compatibility issues
    try {
        modelCompatibility.fixCompatibility();
    } catch (CompatibilityHandlerException ex) {
    }
    model.setFile(file);
    model.setDefaults();
    getModels().add(model);
    model.eAdapters().add(new ECoreAdapter());
    // New Command Stack
    createNewCommandStack(model);
    // New Archive Manager
    createNewArchiveManager(model);
    // Initiate all diagram models to be marked as "saved" - this is for the editor view persistence
    markDiagramModelsAsSaved(model);
    // This last
    firePropertyChange(this, PROPERTY_MODEL_LOADED, null, model);
    return model;
}
Also used : ModelCompatibility(com.archimatetool.editor.model.compatibility.ModelCompatibility) Resource(org.eclipse.emf.ecore.resource.Resource) Diagnostic(org.eclipse.emf.ecore.resource.Resource.Diagnostic) CompatibilityHandlerException(com.archimatetool.editor.model.compatibility.CompatibilityHandlerException) IOException(java.io.IOException) IArchimateModel(com.archimatetool.model.IArchimateModel) IncompatibleModelException(com.archimatetool.editor.model.compatibility.IncompatibleModelException)

Example 2 with CompatibilityHandlerException

use of com.archimatetool.editor.model.compatibility.CompatibilityHandlerException in project archi by archimatetool.

the class NewCanvasFromTemplateWizard method createNewCanvasFromTemplate.

private void createNewCanvasFromTemplate(File file) throws IncompatibleModelException, IOException {
    // Ascertain if this is a zip file
    boolean isArchiveFormat = IArchiveManager.FACTORY.isArchiveFile(file);
    Resource resource = ArchimateResourceFactory.createNewResource(isArchiveFormat ? IArchiveManager.FACTORY.createArchiveModelURI(file) : URI.createFileURI(file.getAbsolutePath()));
    // Check model compatibility
    ModelCompatibility modelCompatibility = new ModelCompatibility(resource);
    // Wrap in try/catch to load as much as possible
    try {
        resource.load(null);
    } catch (IOException ex) {
        // Error occured loading model. Was it a disaster?
        try {
            modelCompatibility.checkErrors();
        }// Incompatible
         catch (IncompatibleModelException ex1) {
            fErrorMessage = NLS.bind(Messages.NewCanvasFromTemplateWizard_4, file) + "\n" + // $NON-NLS-1$
            ex1.getMessage();
            throw ex1;
        }
    }
    // And then fix any backward compatibility issues
    try {
        modelCompatibility.fixCompatibility();
    } catch (CompatibilityHandlerException ex) {
    }
    // Pull out the Canvas model
    IArchimateModel templateModel = (IArchimateModel) resource.getContents().get(0);
    IFolder folderViews = templateModel.getFolder(FolderType.DIAGRAMS);
    ICanvasModel canvasModel = (ICanvasModel) folderViews.getElements().get(0);
    // Create New UUIDs for elements...
    TemplateUtils.generateNewUUIDs(canvasModel);
    // Load the images from the template model's file now
    if (isArchiveFormat) {
        IArchiveManager archiveManager = (IArchiveManager) fFolder.getAdapter(IArchiveManager.class);
        archiveManager.loadImagesFromModelFile(file);
    }
    Command cmd = new NewDiagramCommand(fFolder, canvasModel, Messages.NewCanvasFromTemplateWizard_5);
    CommandStack commandStack = (CommandStack) fFolder.getAdapter(CommandStack.class);
    commandStack.execute(cmd);
}
Also used : CommandStack(org.eclipse.gef.commands.CommandStack) Resource(org.eclipse.emf.ecore.resource.Resource) CompatibilityHandlerException(com.archimatetool.editor.model.compatibility.CompatibilityHandlerException) IOException(java.io.IOException) IArchiveManager(com.archimatetool.editor.model.IArchiveManager) IncompatibleModelException(com.archimatetool.editor.model.compatibility.IncompatibleModelException) NewDiagramCommand(com.archimatetool.editor.views.tree.commands.NewDiagramCommand) NewDiagramCommand(com.archimatetool.editor.views.tree.commands.NewDiagramCommand) Command(org.eclipse.gef.commands.Command) ModelCompatibility(com.archimatetool.editor.model.compatibility.ModelCompatibility) ICanvasModel(com.archimatetool.canvas.model.ICanvasModel) IArchimateModel(com.archimatetool.model.IArchimateModel) IFolder(com.archimatetool.model.IFolder)

Example 3 with CompatibilityHandlerException

use of com.archimatetool.editor.model.compatibility.CompatibilityHandlerException in project archi by archimatetool.

the class LoadModelFromFileProvider method loadModel.

static IArchimateModel loadModel(File file) throws IOException {
    if (file == null || !file.exists()) {
        return null;
    }
    // Ascertain if this is an archive file
    boolean useArchiveFormat = IArchiveManager.FACTORY.isArchiveFile(file);
    // Create the Resource
    Resource resource = ArchimateResourceFactory.createNewResource(useArchiveFormat ? IArchiveManager.FACTORY.createArchiveModelURI(file) : URI.createFileURI(file.getAbsolutePath()));
    // Check model compatibility
    ModelCompatibility modelCompatibility = new ModelCompatibility(resource);
    // Load model
    resource.load(null);
    IArchimateModel model = (IArchimateModel) resource.getContents().get(0);
    // And then fix any backward compatibility issues
    try {
        modelCompatibility.fixCompatibility();
    } catch (CompatibilityHandlerException ex) {
    }
    model.setFile(file);
    model.setDefaults();
    // Add an Archive Manager and load images
    IArchiveManager archiveManager = IArchiveManager.FACTORY.createArchiveManager(model);
    model.setAdapter(IArchiveManager.class, archiveManager);
    archiveManager.loadImages();
    // Add a Command Stack
    CommandStack cmdStack = new CommandStack();
    model.setAdapter(CommandStack.class, cmdStack);
    return model;
}
Also used : CommandStack(org.eclipse.gef.commands.CommandStack) ModelCompatibility(com.archimatetool.editor.model.compatibility.ModelCompatibility) Resource(org.eclipse.emf.ecore.resource.Resource) CompatibilityHandlerException(com.archimatetool.editor.model.compatibility.CompatibilityHandlerException) IArchiveManager(com.archimatetool.editor.model.IArchiveManager) IArchimateModel(com.archimatetool.model.IArchimateModel)

Aggregations

CompatibilityHandlerException (com.archimatetool.editor.model.compatibility.CompatibilityHandlerException)3 ModelCompatibility (com.archimatetool.editor.model.compatibility.ModelCompatibility)3 IArchimateModel (com.archimatetool.model.IArchimateModel)3 Resource (org.eclipse.emf.ecore.resource.Resource)3 IArchiveManager (com.archimatetool.editor.model.IArchiveManager)2 IncompatibleModelException (com.archimatetool.editor.model.compatibility.IncompatibleModelException)2 IOException (java.io.IOException)2 CommandStack (org.eclipse.gef.commands.CommandStack)2 ICanvasModel (com.archimatetool.canvas.model.ICanvasModel)1 NewDiagramCommand (com.archimatetool.editor.views.tree.commands.NewDiagramCommand)1 IFolder (com.archimatetool.model.IFolder)1 Diagnostic (org.eclipse.emf.ecore.resource.Resource.Diagnostic)1 Command (org.eclipse.gef.commands.Command)1