Search in sources :

Example 1 with AbstractTransactionalCommand

use of org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand in project tdi-studio-se by Talend.

the class BusinessNewDiagramFileWizard method performFinish.

/**
     * @generated
     */
public boolean performFinish() {
    IFile diagramFile = myFileCreationPage.createNewFile();
    try {
        //$NON-NLS-1$
        diagramFile.setCharset("UTF-8", new NullProgressMonitor());
    } catch (CoreException e) {
        //$NON-NLS-1$
        BusinessDiagramEditorPlugin.getInstance().logError("Unable to set charset for diagram file", e);
    }
    ResourceSet resourceSet = myEditingDomain.getResourceSet();
    final Resource diagramResource = resourceSet.createResource(URI.createPlatformResourceURI(diagramFile.getFullPath().toString()));
    List affectedFiles = new LinkedList();
    affectedFiles.add(mySelectedModelFile);
    affectedFiles.add(diagramFile);
    AbstractTransactionalCommand command = new AbstractTransactionalCommand(myEditingDomain, //$NON-NLS-1$
    "Initializing diagram contents", //$NON-NLS-1$
    affectedFiles) {

        protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
            int diagramVID = BusinessVisualIDRegistry.getDiagramVisualID(myDiagramRoot);
            if (diagramVID != BusinessProcessEditPart.VISUAL_ID) {
                //$NON-NLS-1$
                return CommandResult.newErrorCommandResult("Incorrect model object stored as a root resource object");
            }
            Diagram diagram = ViewService.createDiagram(myDiagramRoot, BusinessProcessEditPart.MODEL_ID, BusinessDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
            diagramResource.getContents().add(diagram);
            diagramResource.getContents().add(diagram.getElement());
            return CommandResult.newOKCommandResult();
        }
    };
    try {
        OperationHistoryFactory.getOperationHistory().execute(command, new NullProgressMonitor(), null);
        diagramResource.save(Collections.EMPTY_MAP);
        IDE.openEditor(myWorkbenchPage, diagramFile);
    } catch (ExecutionException e) {
        //$NON-NLS-1$
        BusinessDiagramEditorPlugin.getInstance().logError("Unable to create model and diagram", e);
    } catch (IOException ex) {
        BusinessDiagramEditorPlugin.getInstance().logError("Save operation failed for: " + diagramFile.getFullPath().toString(), //$NON-NLS-1$
        ex);
    } catch (PartInitException ex) {
        //$NON-NLS-1$
        BusinessDiagramEditorPlugin.getInstance().logError("Unable to open editor", ex);
    }
    return true;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IAdaptable(org.eclipse.core.runtime.IAdaptable) IFile(org.eclipse.core.resources.IFile) Resource(org.eclipse.emf.ecore.resource.Resource) AbstractTransactionalCommand(org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IOException(java.io.IOException) LinkedList(java.util.LinkedList) Diagram(org.eclipse.gmf.runtime.notation.Diagram) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) LinkedList(java.util.LinkedList) List(java.util.List) PartInitException(org.eclipse.ui.PartInitException) ExecutionException(org.eclipse.core.commands.ExecutionException)

Example 2 with AbstractTransactionalCommand

use of org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand in project tdi-studio-se by Talend.

the class BusinessDiagramEditorUtil method createNewDiagramFile.

/**
     * <p>
     * This method should be called within a workspace modify operation since it creates resources.
     * </p>
     * 
     * @generated
     * @return the created file resource, or <code>null</code> if the file was not created
     */
public static final IFile createNewDiagramFile(DiagramFileCreator diagramFileCreator, IPath containerFullPath, String fileName, InputStream initialContents, String kind, Shell shell, IProgressMonitor progressMonitor) {
    TransactionalEditingDomain editingDomain = GMFEditingDomainFactory.INSTANCE.createEditingDomain();
    ResourceSet resourceSet = editingDomain.getResourceSet();
    //$NON-NLS-1$
    progressMonitor.beginTask("Creating diagram and model files", 4);
    final IProgressMonitor subProgressMonitor = new SubProgressMonitor(progressMonitor, 1);
    final IFile diagramFile = diagramFileCreator.createNewFile(containerFullPath, fileName, initialContents, shell, new IRunnableContext() {

        public void run(boolean fork, boolean cancelable, IRunnableWithProgress runnable) throws InvocationTargetException, InterruptedException {
            runnable.run(subProgressMonitor);
        }
    });
    final Resource diagramResource = resourceSet.createResource(URI.createPlatformResourceURI(diagramFile.getFullPath().toString()));
    List affectedFiles = new ArrayList();
    affectedFiles.add(diagramFile);
    final String kindParam = kind;
    AbstractTransactionalCommand command = new AbstractTransactionalCommand(editingDomain, //$NON-NLS-1$
    "Creating diagram and model", //$NON-NLS-1$
    affectedFiles) {

        protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
            BusinessProcess model = createInitialModel();
            diagramResource.getContents().add(model);
            Diagram diagram = ViewService.createDiagram(model, kindParam, BusinessDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
            if (diagram != null) {
                diagramResource.getContents().add(diagram);
                diagram.setName(diagramFile.getName());
                diagram.setElement(model);
            }
            try {
                diagramResource.save(Collections.EMPTY_MAP);
            } catch (IOException e) {
                BusinessDiagramEditorPlugin.getInstance().logError("Unable to store model and diagram resources", //$NON-NLS-1$
                e);
            }
            return CommandResult.newOKCommandResult();
        }
    };
    try {
        OperationHistoryFactory.getOperationHistory().execute(command, new SubProgressMonitor(progressMonitor, 1), null);
    } catch (ExecutionException e) {
        //$NON-NLS-1$
        BusinessDiagramEditorPlugin.getInstance().logError("Unable to create model and diagram", e);
    }
    try {
        //$NON-NLS-1$
        diagramFile.setCharset("UTF-8", new SubProgressMonitor(progressMonitor, 1));
    } catch (CoreException e) {
        //$NON-NLS-1$
        BusinessDiagramEditorPlugin.getInstance().logError("Unable to set charset for diagram file", e);
    }
    return diagramFile;
}
Also used : IRunnableContext(org.eclipse.jface.operation.IRunnableContext) IAdaptable(org.eclipse.core.runtime.IAdaptable) IFile(org.eclipse.core.resources.IFile) Resource(org.eclipse.emf.ecore.resource.Resource) ArrayList(java.util.ArrayList) AbstractTransactionalCommand(org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IOException(java.io.IOException) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) Diagram(org.eclipse.gmf.runtime.notation.Diagram) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) TransactionalEditingDomain(org.eclipse.emf.transaction.TransactionalEditingDomain) CoreException(org.eclipse.core.runtime.CoreException) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionException(org.eclipse.core.commands.ExecutionException) BusinessProcess(org.talend.designer.business.model.business.BusinessProcess)

Example 3 with AbstractTransactionalCommand

use of org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand in project statecharts by Yakindu.

the class SimulationImageRenderer method highlightElements.

public void highlightElements(final List<? extends EObject> objects, final Diagram diagram) {
    AbstractTransactionalCommand cmd = new AbstractTransactionalCommand(TransactionUtil.getEditingDomain(diagram), "", null) {

        @Override
        protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
            // Init colors from preferences
            RGB rgbForeGround = PreferenceConverter.getColor(SimulationActivator.getDefault().getPreferenceStore(), SimulationPreferenceConstants.STATE_FOREGROUND_HIGHLIGHTING_COLOR);
            RGB rgbBackGround = PreferenceConverter.getColor(SimulationActivator.getDefault().getPreferenceStore(), SimulationPreferenceConstants.STATE_BACKGROUND_HIGHLIGHTING_COLOR);
            RGB rgbTransitionActive = PreferenceConverter.getColor(SimulationActivator.getDefault().getPreferenceStore(), SimulationPreferenceConstants.TRANSITION_HIGHLIGHTING_COLOR);
            Color color = new Color(Display.getDefault(), rgbForeGround);
            Integer foreGround = FigureUtilities.colorToInteger(color);
            color.dispose();
            color = new Color(Display.getDefault(), rgbBackGround);
            Integer background = FigureUtilities.colorToInteger(color);
            color.dispose();
            color = new Color(Display.getDefault(), rgbTransitionActive);
            Integer transitionActive = FigureUtilities.colorToInteger(color);
            color.dispose();
            // Set styling
            TreeIterator<EObject> eAllContents = diagram.eAllContents();
            while (eAllContents.hasNext()) {
                EObject next = eAllContents.next();
                if (next instanceof View) {
                    for (EObject elementToHighlight : objects) {
                        EObject element = null;
                        if (next instanceof Node) {
                            element = ((Node) next).getElement();
                        } else if (next instanceof Edge) {
                            element = ((Edge) next).getElement();
                        }
                        if (element == null) {
                            // next instanceof BasicDecorationNode || next instanceof Shape
                            continue;
                        }
                        if (EcoreUtil.getURI(elementToHighlight).equals(EcoreUtil.getURI(element))) {
                            if (next instanceof Node) {
                                ShapeStyle style = (ShapeStyle) ((Node) next).getStyle(NotationPackage.Literals.SHAPE_STYLE);
                                if (style != null) {
                                    style.setFillColor(background);
                                    style.setLineColor(foreGround);
                                }
                            } else if (next instanceof Edge) {
                                ConnectorStyle style = (ConnectorStyle) ((View) next).getStyle(NotationPackage.Literals.CONNECTOR_STYLE);
                                if (style != null) {
                                    style.setLineColor(transitionActive);
                                }
                            }
                        }
                    }
                }
            }
            return CommandResult.newOKCommandResult();
        }
    };
    try {
        cmd.execute(new NullProgressMonitor(), null);
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) Color(org.eclipse.swt.graphics.Color) Node(org.eclipse.gmf.runtime.notation.Node) AbstractTransactionalCommand(org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand) RGB(org.eclipse.swt.graphics.RGB) View(org.eclipse.gmf.runtime.notation.View) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ConnectorStyle(org.eclipse.gmf.runtime.notation.ConnectorStyle) EObject(org.eclipse.emf.ecore.EObject) ShapeStyle(org.eclipse.gmf.runtime.notation.ShapeStyle) ExecutionException(org.eclipse.core.commands.ExecutionException) Edge(org.eclipse.gmf.runtime.notation.Edge)

Example 4 with AbstractTransactionalCommand

use of org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand in project statecharts by Yakindu.

the class AbstractSemanticModification method modify.

/**
 * Executes the modification in a transactional command.
 */
public void modify() {
    if (!isApplicable())
        throw new IllegalStateException("Modification " + getClass().getSimpleName() + " is not executable.");
    final EObject semanticObject = getTargetView().getElement();
    AbstractTransactionalCommand refactoringCommand = new AbstractTransactionalCommand(TransactionUtil.getEditingDomain(semanticObject), getClass().getName(), Collections.EMPTY_LIST) {

        @Override
        protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
            try {
                AbstractSemanticModification.this.execute(semanticObject, getTargetView());
            } catch (Exception ex) {
                ex.printStackTrace();
                return CommandResult.newErrorCommandResult(ex);
            }
            return CommandResult.newOKCommandResult();
        }
    };
    executeCommand(refactoringCommand, semanticObject.eResource());
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) EObject(org.eclipse.emf.ecore.EObject) AbstractTransactionalCommand(org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand) ExecutionException(org.eclipse.core.commands.ExecutionException)

Example 5 with AbstractTransactionalCommand

use of org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand in project statecharts by Yakindu.

the class CreationWizard method createDiagram.

protected Resource createDiagram(final DiagramCreationDesccription create, IProgressMonitor progressMonitor) {
    TransactionalEditingDomain editingDomain = GMFEditingDomainFactory.INSTANCE.createEditingDomain();
    progressMonitor.beginTask("Creating diagram file ...", 3);
    final Resource resource = editingDomain.getResourceSet().createResource(create.getModelURI());
    AbstractTransactionalCommand command = new AbstractTransactionalCommand(editingDomain, "Creating diagram file ...", Collections.EMPTY_LIST) {

        @Override
        protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
            FactoryUtils.createStatechartModel(resource, preferencesHint);
            Statechart statechart = (Statechart) EcoreUtil.getObjectByType(resource.getContents(), SGraphPackage.Literals.STATECHART);
            statechart.setDomainID(create.getDomainID());
            try {
                resource.save(getSaveOptions());
            } catch (IOException e) {
                e.printStackTrace();
            }
            return CommandResult.newOKCommandResult();
        }
    };
    try {
        command.execute(progressMonitor, null);
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
    setCharset(WorkspaceSynchronizer.getFile(resource));
    editingDomain.dispose();
    return resource;
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) TransactionalEditingDomain(org.eclipse.emf.transaction.TransactionalEditingDomain) Resource(org.eclipse.emf.ecore.resource.Resource) XMLResource(org.eclipse.emf.ecore.xmi.XMLResource) IResource(org.eclipse.core.resources.IResource) AbstractTransactionalCommand(org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand) Statechart(org.yakindu.sct.model.sgraph.Statechart) IOException(java.io.IOException) ExecutionException(org.eclipse.core.commands.ExecutionException)

Aggregations

IAdaptable (org.eclipse.core.runtime.IAdaptable)9 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)9 AbstractTransactionalCommand (org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand)9 ExecutionException (org.eclipse.core.commands.ExecutionException)7 IOException (java.io.IOException)4 IFile (org.eclipse.core.resources.IFile)3 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)3 Resource (org.eclipse.emf.ecore.resource.Resource)3 TransactionalEditingDomain (org.eclipse.emf.transaction.TransactionalEditingDomain)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CoreException (org.eclipse.core.runtime.CoreException)2 EObject (org.eclipse.emf.ecore.EObject)2 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)2 XMLResource (org.eclipse.emf.ecore.xmi.XMLResource)2 Diagram (org.eclipse.gmf.runtime.notation.Diagram)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 LinkedList (java.util.LinkedList)1