Search in sources :

Example 1 with ITemplate

use of com.archimatetool.templates.model.ITemplate in project archi by archimatetool.

the class NewArchimateModelFromTemplateWizard method performFinish.

@Override
public boolean performFinish() {
    // Get template
    ITemplate template = fMainPage.getSelectedTemplate();
    if (template == null) {
        return false;
    }
    getContainer().getShell().setVisible(false);
    fErrorMessage = null;
    final File zipFile = template.getFile();
    if (zipFile != null && zipFile.exists()) {
        BusyIndicator.showWhile(Display.getCurrent(), new Runnable() {

            @Override
            public void run() {
                try {
                    // $NON-NLS-1$
                    File tmp = File.createTempFile("~architemplate", null);
                    tmp.deleteOnExit();
                    File file = ZipUtils.extractZipEntry(zipFile, TemplateManager.ZIP_ENTRY_MODEL, tmp);
                    if (file != null && file.exists()) {
                        IArchimateModel model = IEditorModelManager.INSTANCE.openModel(file);
                        if (model != null) {
                            // New name
                            // $NON-NLS-1$
                            model.setName(Messages.NewArchimateModelFromTemplateWizard_1 + " " + model.getName());
                            // Set latest model version (need to do this in case we immediately save as Template)
                            model.setVersion(ModelVersion.VERSION);
                            // Set file to null
                            model.setFile(null);
                            // New IDs
                            model.setId(model.getIDAdapter().getNewID());
                            for (Iterator<EObject> iter = model.eAllContents(); iter.hasNext(); ) {
                                EObject eObject = iter.next();
                                if (eObject instanceof IIdentifier) {
                                    ((IIdentifier) eObject).setId(model.getIDAdapter().getNewID());
                                }
                            }
                            // Edit in-place in Tree
                            UIRequestManager.INSTANCE.fireRequest(new TreeEditElementRequest(this, model));
                        } else {
                            fErrorMessage = Messages.NewArchimateModelFromTemplateWizard_2;
                        }
                    } else {
                        fErrorMessage = Messages.NewArchimateModelFromTemplateWizard_2;
                    }
                    tmp.delete();
                } catch (IOException ex) {
                    ex.printStackTrace();
                    fErrorMessage = ex.getMessage();
                }
            }
        });
    }
    if (fErrorMessage != null) {
        MessageDialog.openError(getShell(), Messages.NewArchimateModelFromTemplateWizard_3, fErrorMessage);
        getContainer().getShell().setVisible(true);
    }
    return fErrorMessage == null;
}
Also used : IIdentifier(com.archimatetool.model.IIdentifier) EObject(org.eclipse.emf.ecore.EObject) Iterator(java.util.Iterator) TreeEditElementRequest(com.archimatetool.editor.views.tree.TreeEditElementRequest) ITemplate(com.archimatetool.templates.model.ITemplate) IOException(java.io.IOException) File(java.io.File) IArchimateModel(com.archimatetool.model.IArchimateModel)

Example 2 with ITemplate

use of com.archimatetool.templates.model.ITemplate in project archi by archimatetool.

the class NewModelFromTemplateWizardPage method handleOpenAction.

/**
 * User wants to open Template from file
 */
protected void handleOpenAction() {
    getContainer().getShell().setVisible(false);
    FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);
    dialog.setText(Messages.NewModelFromTemplateWizardPage_4);
    // $NON-NLS-1$ //$NON-NLS-2$
    dialog.setFilterExtensions(new String[] { "*" + fTemplateManager.getTemplateFileExtension(), "*.*" });
    String path = dialog.open();
    if (path == null) {
        selectFirstTableItem();
        getContainer().getShell().setVisible(true);
        return;
    }
    final File file = new File(path);
    // Create template and Finish
    BusyIndicator.showWhile(null, new Runnable() {

        @Override
        public void run() {
            try {
                ITemplate template = fTemplateManager.createTemplate(file);
                template.setFile(file);
                fSelectedTemplate = template;
                ((ExtendedWizardDialog) getContainer()).finishPressed();
            } catch (IOException ex) {
                MessageDialog.openError(getShell(), Messages.NewModelFromTemplateWizardPage_5, ex.getMessage());
                selectFirstTableItem();
                getContainer().getShell().setVisible(true);
            }
        }
    });
}
Also used : ITemplate(com.archimatetool.templates.model.ITemplate) IOException(java.io.IOException) FileDialog(org.eclipse.swt.widgets.FileDialog) File(java.io.File)

Example 3 with ITemplate

use of com.archimatetool.templates.model.ITemplate in project archi by archimatetool.

the class NewModelFromTemplateWizardPage method createControl.

@Override
public void createControl(Composite parent) {
    Composite container = new Composite(parent, SWT.NULL);
    container.setLayout(new GridLayout());
    setControl(container);
    // Help
    PlatformUI.getWorkbench().getHelpSystem().setHelp(container, getHelpID());
    GridData gd;
    SashForm sash1 = new SashForm(container, SWT.HORIZONTAL);
    gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    gd.widthHint = 800;
    gd.heightHint = 500;
    sash1.setLayoutData(gd);
    Composite tableComposite = new Composite(sash1, SWT.BORDER);
    GridLayout layout = new GridLayout();
    layout.marginWidth = 0;
    layout.marginHeight = 0;
    layout.verticalSpacing = 0;
    tableComposite.setLayout(layout);
    // Inbuilt Templates
    gd = new GridData(SWT.FILL, SWT.FILL, true, false);
    fInbuiltTableViewer = createGroupsTableViewer(tableComposite, Messages.NewModelFromTemplateWizardPage_2, gd);
    fInbuiltTableViewer.setInput(new Object[] { fTemplateManager.getInbuiltTemplateGroup(), OPEN, MANAGE });
    // Mouse UP actions...
    fInbuiltTableViewer.getControl().addMouseListener(new MouseAdapter() {

        @Override
        public void mouseUp(MouseEvent e) {
            Object o = ((IStructuredSelection) fInbuiltTableViewer.getSelection()).getFirstElement();
            // Open...
            if (o == OPEN) {
                handleOpenAction();
            } else // Manage...
            if (o == MANAGE) {
                handleManageTemplatesAction();
            }
        }
    });
    // My Templates
    gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    fUserTableViewer = createGroupsTableViewer(tableComposite, Messages.NewModelFromTemplateWizardPage_3, gd);
    fUserTableViewer.setComparator(new ViewerComparator() {

        @Override
        public int category(Object element) {
            if (element == fTemplateManager.AllUserTemplatesGroup) {
                return 0;
            }
            return 1;
        }
    });
    fUserTableViewer.setInput(fTemplateManager);
    SashForm sash2 = new SashForm(sash1, SWT.VERTICAL);
    Composite galleryComposite = new Composite(sash2, SWT.NULL);
    layout = new GridLayout();
    layout.marginWidth = 0;
    layout.marginHeight = 0;
    layout.verticalSpacing = 0;
    galleryComposite.setLayout(layout);
    fGallery = new Gallery(galleryComposite, SWT.V_SCROLL | SWT.BORDER);
    fGallery.setLayoutData(new GridData(GridData.FILL_BOTH));
    // Renderers
    final NoGroupRenderer groupRenderer = new NoGroupRenderer();
    groupRenderer.setItemSize(DEFAULT_GALLERY_ITEM_SIZE, DEFAULT_GALLERY_ITEM_SIZE);
    groupRenderer.setAutoMargin(true);
    groupRenderer.setMinMargin(10);
    fGallery.setGroupRenderer(groupRenderer);
    final DefaultGalleryItemRenderer itemRenderer = new DefaultGalleryItemRenderer();
    itemRenderer.setDropShadows(true);
    itemRenderer.setDropShadowsSize(7);
    itemRenderer.setShowRoundedSelectionCorners(false);
    fGallery.setItemRenderer(itemRenderer);
    // Root Group
    fGalleryRoot = new GalleryItem(fGallery, SWT.NONE);
    // Slider
    final Scale scale = new Scale(galleryComposite, SWT.HORIZONTAL);
    gd = new GridData(SWT.END, SWT.NONE, false, false);
    gd.widthHint = 120;
    if (PlatformUtils.isMac()) {
        // Mac clips height of slider
        gd.heightHint = 20;
    }
    scale.setLayoutData(gd);
    scale.setMaximum(480);
    scale.setMinimum(64);
    scale.setIncrement(8);
    scale.setPageIncrement(64);
    scale.setSelection(DEFAULT_GALLERY_ITEM_SIZE);
    scale.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            int inc = scale.getSelection();
            itemRenderer.setDropShadows(inc >= DEFAULT_GALLERY_ITEM_SIZE);
            groupRenderer.setItemSize(inc, inc);
        }
    });
    // Description
    fDescriptionText = new StyledText(sash2, SWT.V_SCROLL | SWT.READ_ONLY | SWT.WRAP | SWT.BORDER);
    fGallery.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            if (e.item instanceof GalleryItem) {
                ITemplate template = (ITemplate) ((GalleryItem) e.item).getData();
                updateWizard(template);
            } else {
                updateWizard(null);
            }
        }
    });
    // Double-clicks
    fGallery.addListener(SWT.MouseDoubleClick, new Listener() {

        public void handleEvent(Event event) {
            GalleryItem item = fGallery.getItem(new Point(event.x, event.y));
            if (item != null) {
                ((ExtendedWizardDialog) getContainer()).finishPressed();
            }
        }
    });
    // Dispose of the images in TemplateManager here not in the main dispose() method because if the help system is showing then
    // the TrayDialog is resized and this control is asked to relayout.
    fGallery.addDisposeListener(new DisposeListener() {

        @Override
        public void widgetDisposed(DisposeEvent e) {
            fTemplateManager.dispose();
        }
    });
    // Mouse move shows thumbnails
    registerMouseMoveHandler();
    // Select first group on table
    selectFirstTableItem();
    sash1.setWeights(new int[] { 30, 70 });
    sash2.setWeights(new int[] { 70, 30 });
    // Yes it's OK
    setPageComplete(true);
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) MouseEvent(org.eclipse.swt.events.MouseEvent) StyledText(org.eclipse.swt.custom.StyledText) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) MouseMoveListener(org.eclipse.swt.events.MouseMoveListener) DisposeListener(org.eclipse.swt.events.DisposeListener) Listener(org.eclipse.swt.widgets.Listener) Composite(org.eclipse.swt.widgets.Composite) ViewerComparator(org.eclipse.jface.viewers.ViewerComparator) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) MouseAdapter(org.eclipse.swt.events.MouseAdapter) Scale(org.eclipse.swt.widgets.Scale) DefaultGalleryItemRenderer(org.eclipse.nebula.widgets.gallery.DefaultGalleryItemRenderer) Point(org.eclipse.swt.graphics.Point) GalleryItem(org.eclipse.nebula.widgets.gallery.GalleryItem) DisposeEvent(org.eclipse.swt.events.DisposeEvent) SashForm(org.eclipse.swt.custom.SashForm) GridLayout(org.eclipse.swt.layout.GridLayout) Gallery(org.eclipse.nebula.widgets.gallery.Gallery) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) DisposeEvent(org.eclipse.swt.events.DisposeEvent) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) Event(org.eclipse.swt.widgets.Event) MouseEvent(org.eclipse.swt.events.MouseEvent) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ITemplate(com.archimatetool.templates.model.ITemplate) NoGroupRenderer(org.eclipse.nebula.widgets.gallery.NoGroupRenderer)

Example 4 with ITemplate

use of com.archimatetool.templates.model.ITemplate in project archi by archimatetool.

the class NewModelFromTemplateWizardPage method updateGallery.

/**
 * Update the Gallery
 * @param group
 */
protected void updateGallery(ITemplateGroup group) {
    for (ITemplate template : group.getSortedTemplates()) {
        GalleryItem item = new GalleryItem(fGalleryRoot, SWT.NONE);
        item.setText(StringUtils.safeString(template.getName()));
        item.setImage(template.getKeyThumbnail());
        item.setData(template);
    }
    if (fGalleryRoot.getItem(0) != null) {
        fGallery.setSelection(new GalleryItem[] { fGalleryRoot.getItem(0) });
        updateWizard((ITemplate) fGalleryRoot.getItem(0).getData());
    } else {
        updateWizard(null);
    }
}
Also used : ITemplate(com.archimatetool.templates.model.ITemplate) GalleryItem(org.eclipse.nebula.widgets.gallery.GalleryItem)

Example 5 with ITemplate

use of com.archimatetool.templates.model.ITemplate in project archi by archimatetool.

the class TemplateManagerDialog method deleteSelectedObjects.

/**
 * Remove selected object
 */
protected void deleteSelectedObjects() {
    // Table
    if (fSelectedControl == fTableViewer) {
        for (Object o : ((IStructuredSelection) fTableViewer.getSelection()).toArray()) {
            if (o instanceof ITemplate) {
                fTemplateManager.getUserTemplates().remove(o);
                for (ITemplateGroup group : fTemplateManager.getUserTemplateGroups()) {
                    group.removeTemplate((ITemplate) o);
                }
            }
        }
        fTableViewer.refresh();
        fTreeViewer.refresh();
    } else // Tree
    if (fSelectedControl == fTreeViewer) {
        // Do it this way because we can't get template parents
        for (TreeItem item : fTreeViewer.getTree().getSelection()) {
            if (item.getData() instanceof ITemplate) {
                ITemplate template = (ITemplate) item.getData();
                TreeItem parent = item.getParentItem();
                if (parent.getData() instanceof ITemplateGroup) {
                    ((ITemplateGroup) parent.getData()).removeTemplate(template);
                }
            } else if (item.getData() instanceof ITemplateGroup) {
                fTemplateManager.getUserTemplateGroups().remove(item.getData());
            }
        }
        fTreeViewer.refresh();
    }
}
Also used : ITemplateGroup(com.archimatetool.templates.model.ITemplateGroup) TreeItem(org.eclipse.swt.widgets.TreeItem) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ITemplate(com.archimatetool.templates.model.ITemplate)

Aggregations

ITemplate (com.archimatetool.templates.model.ITemplate)10 File (java.io.File)6 IOException (java.io.IOException)4 ITemplateGroup (com.archimatetool.templates.model.ITemplateGroup)3 TemplateGroup (com.archimatetool.templates.model.TemplateGroup)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 GalleryItem (org.eclipse.nebula.widgets.gallery.GalleryItem)2 FileDialog (org.eclipse.swt.widgets.FileDialog)2 CompatibilityHandlerException (com.archimatetool.editor.model.compatibility.CompatibilityHandlerException)1 IncompatibleModelException (com.archimatetool.editor.model.compatibility.IncompatibleModelException)1 TreeEditElementRequest (com.archimatetool.editor.views.tree.TreeEditElementRequest)1 IArchimateModel (com.archimatetool.model.IArchimateModel)1 IIdentifier (com.archimatetool.model.IIdentifier)1 Iterator (java.util.Iterator)1 EObject (org.eclipse.emf.ecore.EObject)1 ISelectionChangedListener (org.eclipse.jface.viewers.ISelectionChangedListener)1 SelectionChangedEvent (org.eclipse.jface.viewers.SelectionChangedEvent)1 ViewerComparator (org.eclipse.jface.viewers.ViewerComparator)1 DefaultGalleryItemRenderer (org.eclipse.nebula.widgets.gallery.DefaultGalleryItemRenderer)1 Gallery (org.eclipse.nebula.widgets.gallery.Gallery)1