use of com.archimatetool.editor.model.IArchiveManager 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.IArchiveManager in project archi by archimatetool.
the class SaveCanvasAsTemplateWizard method saveModelToTempFile.
private File saveModelToTempFile() throws IOException {
// $NON-NLS-1$
File tmpFile = File.createTempFile("architemplate", null);
tmpFile.deleteOnExit();
// Create a new container Archimate model
IArchimateModel tempModel = IArchimateFactory.eINSTANCE.createArchimateModel();
tempModel.setDefaults();
// Remove this after default folders have been added, as we'll generate our own IDs
tempModel.eAdapters().clear();
tempModel.setId(EcoreUtil.generateUUID());
tempModel.setFile(tmpFile);
tempModel.setVersion(ModelVersion.VERSION);
tempModel.setName(Messages.SaveCanvasAsTemplateWizard_4);
// Get the Canvas copy
ICanvasModel copyCanvas = EcoreUtil.copy(fCanvasModel);
// Remove any unsupported elements
for (Iterator<EObject> iter = copyCanvas.eAllContents(); iter.hasNext(); ) {
EObject eObject = iter.next();
if (eObject instanceof IDiagramModelReference) {
EcoreUtil.delete(eObject);
}
}
// Generate new IDs
TemplateUtils.generateNewUUIDs(copyCanvas);
// Add the canvas copy to a new Views folder
IFolder folder = tempModel.getDefaultFolderForObject(copyCanvas);
folder.getElements().add(copyCanvas);
// Use an Archive Manager to save it
IArchiveManager archiveManager = IArchiveManager.FACTORY.createArchiveManager(tempModel);
archiveManager.saveModel();
archiveManager.dispose();
return tmpFile;
}
use of com.archimatetool.editor.model.IArchiveManager in project archi by archimatetool.
the class CanvasDNDEditPolicy method getFileDropCommand.
/**
* @param request
* @return
*/
protected Command getFileDropCommand(DiagramDropRequest request) {
String[] files = (String[]) request.getData();
// XY drop point
Point pt = getDropLocation(request);
int origin = pt.x;
int x = pt.x;
int y = pt.y;
IArchiveManager archiveManager = (IArchiveManager) getTargetContainer().getAdapter(IArchiveManager.class);
CompoundCommand result = new CompoundCommand(Messages.CanvasDNDEditPolicy_0);
for (String s : files) {
File file = new File(s);
if (!file.canRead()) {
continue;
}
String name = file.getName().toLowerCase();
if (!(// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
name.endsWith(".png") || name.endsWith(".bmp") || name.endsWith(".gif") || name.endsWith(".jpg") || name.endsWith(".jpeg") || // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
name.endsWith(".tif") || name.endsWith(".tiff") || name.endsWith(".ico"))) {
// $NON-NLS-1$ //$NON-NLS-2$
continue;
}
ICanvasModelImage canvasModelImage = ICanvasFactory.eINSTANCE.createCanvasModelImage();
canvasModelImage.setName(Messages.CanvasDNDEditPolicy_1);
String pathName;
try {
pathName = archiveManager.addImageFromFile(file);
} catch (IOException ex) {
ex.printStackTrace();
continue;
}
canvasModelImage.setImagePath(pathName);
// Get width and height of the image
Image image = null;
try {
image = archiveManager.createImage(pathName);
} catch (Exception ex) {
ex.printStackTrace();
continue;
}
int image_width = image.getBounds().width;
int image_height = image.getBounds().height;
image.dispose();
canvasModelImage.setBounds(x, y, image_width, image_height);
result.add(new AddDiagramObjectCommand(getTargetContainer(), canvasModelImage));
// Increase x,y like a Carriage Return
x += image_width + 10;
if (x > origin + 1000) {
x = origin;
y += image_height + 10;
}
}
return result;
}
use of com.archimatetool.editor.model.IArchiveManager in project archi by archimatetool.
the class IconSection method refreshPreviewImage.
protected void refreshPreviewImage() {
disposeImage();
IIconic iconic = (IIconic) getFirstSelectedObject();
if (iconic.getImagePath() != null) {
IArchiveManager archiveManager = (IArchiveManager) iconic.getAdapter(IArchiveManager.class);
Image image = null;
try {
image = archiveManager.createImage(iconic.getImagePath());
} catch (Exception ex) {
ex.printStackTrace();
}
if (image != null) {
// If the image is bigger than the maximum allowed image then create a scaled image
if (image.getBounds().width > IIconic.MAX_IMAGE_SIZE || image.getBounds().height > IIconic.MAX_IMAGE_SIZE) {
fImage = ImageFactory.getScaledImage(image, IIconic.MAX_IMAGE_SIZE);
image.dispose();
} else // Else use original
{
fImage = image;
}
}
}
fCanvas.redraw();
}
use of com.archimatetool.editor.model.IArchiveManager in project archi by archimatetool.
the class CreateEmptyModelProvider method createEmptyModel.
private IArchimateModel createEmptyModel() {
IArchimateModel model = IArchimateFactory.eINSTANCE.createArchimateModel();
model.setDefaults();
// Add an Archive Manager
IArchiveManager archiveManager = IArchiveManager.FACTORY.createArchiveManager(model);
model.setAdapter(IArchiveManager.class, archiveManager);
// Add a Command Stack
CommandStack cmdStack = new CommandStack();
model.setAdapter(CommandStack.class, cmdStack);
logMessage(Messages.CreateEmptyModelProvider_7);
return model;
}
Aggregations