use of com.archimatetool.editor.model.compatibility.ModelCompatibility 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;
}
use of com.archimatetool.editor.model.compatibility.ModelCompatibility 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);
}
use of com.archimatetool.editor.model.compatibility.ModelCompatibility 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;
}
Aggregations