Search in sources :

Example 16 with IArchiveManager

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);
}
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 17 with IArchiveManager

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;
}
Also used : IDiagramModelReference(com.archimatetool.model.IDiagramModelReference) EObject(org.eclipse.emf.ecore.EObject) ICanvasModel(com.archimatetool.canvas.model.ICanvasModel) IArchiveManager(com.archimatetool.editor.model.IArchiveManager) File(java.io.File) IArchimateModel(com.archimatetool.model.IArchimateModel) IFolder(com.archimatetool.model.IFolder)

Example 18 with IArchiveManager

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;
}
Also used : AddDiagramObjectCommand(com.archimatetool.editor.diagram.commands.AddDiagramObjectCommand) Point(org.eclipse.draw2d.geometry.Point) IOException(java.io.IOException) IArchiveManager(com.archimatetool.editor.model.IArchiveManager) ICanvasModelImage(com.archimatetool.canvas.model.ICanvasModelImage) ICanvasModelImage(com.archimatetool.canvas.model.ICanvasModelImage) Image(org.eclipse.swt.graphics.Image) File(java.io.File) Point(org.eclipse.draw2d.geometry.Point) IOException(java.io.IOException) CompoundCommand(org.eclipse.gef.commands.CompoundCommand)

Example 19 with IArchiveManager

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();
}
Also used : IIconic(com.archimatetool.canvas.model.IIconic) IArchiveManager(com.archimatetool.editor.model.IArchiveManager) Image(org.eclipse.swt.graphics.Image)

Example 20 with IArchiveManager

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;
}
Also used : CommandStack(org.eclipse.gef.commands.CommandStack) IArchiveManager(com.archimatetool.editor.model.IArchiveManager) IArchimateModel(com.archimatetool.model.IArchimateModel)

Aggregations

IArchiveManager (com.archimatetool.editor.model.IArchiveManager)23 IArchimateModel (com.archimatetool.model.IArchimateModel)11 CommandStack (org.eclipse.gef.commands.CommandStack)9 File (java.io.File)7 IOException (java.io.IOException)5 Image (org.eclipse.swt.graphics.Image)4 EObject (org.eclipse.emf.ecore.EObject)3 Test (org.junit.Test)3 ICanvasModel (com.archimatetool.canvas.model.ICanvasModel)2 CompatibilityHandlerException (com.archimatetool.editor.model.compatibility.CompatibilityHandlerException)2 ModelCompatibility (com.archimatetool.editor.model.compatibility.ModelCompatibility)2 IFolder (com.archimatetool.model.IFolder)2 Resource (org.eclipse.emf.ecore.resource.Resource)2 Command (org.eclipse.gef.commands.Command)2 CompoundCommand (org.eclipse.gef.commands.CompoundCommand)2 ICanvasModelImage (com.archimatetool.canvas.model.ICanvasModelImage)1 IIconic (com.archimatetool.canvas.model.IIconic)1 AddDiagramObjectCommand (com.archimatetool.editor.diagram.commands.AddDiagramObjectCommand)1 ModelChecker (com.archimatetool.editor.model.ModelChecker)1 EObjectFeatureCommand (com.archimatetool.editor.model.commands.EObjectFeatureCommand)1