Search in sources :

Example 1 with NonNotifyingCompoundCommand

use of com.archimatetool.editor.model.commands.NonNotifyingCompoundCommand in project archi by archimatetool.

the class DuplicateCommandHandler method getCompoundCommand.

/**
 * Get, and if need be create, a CompoundCommand to which to add the object to be duplicated command
 */
private CompoundCommand getCompoundCommand(IAdapter object) {
    // Get the Command Stack registered to the object
    CommandStack stack = (CommandStack) object.getAdapter(CommandStack.class);
    if (stack == null) {
        // $NON-NLS-1$
        System.err.println("CommandStack was null in " + getClass());
        return null;
    }
    // Now get or create a Compound Command
    CompoundCommand compoundCommand = fCommandMap.get(stack);
    if (compoundCommand == null) {
        compoundCommand = new NonNotifyingCompoundCommand(Messages.DuplicateCommandHandler_1);
        fCommandMap.put(stack, compoundCommand);
    }
    return compoundCommand;
}
Also used : CommandStack(org.eclipse.gef.commands.CommandStack) NonNotifyingCompoundCommand(com.archimatetool.editor.model.commands.NonNotifyingCompoundCommand) CompoundCommand(org.eclipse.gef.commands.CompoundCommand) NonNotifyingCompoundCommand(com.archimatetool.editor.model.commands.NonNotifyingCompoundCommand)

Example 2 with NonNotifyingCompoundCommand

use of com.archimatetool.editor.model.commands.NonNotifyingCompoundCommand in project archi by archimatetool.

the class DeleteFromModelAction method run.

@Override
public void run() {
    List<?> selection = getSelectedObjects();
    List<IArchimateConcept> archimateConcepts = new ArrayList<IArchimateConcept>();
    List<IDiagramModelComponent> diagramObjects = new ArrayList<IDiagramModelComponent>();
    // Gather Model elements, relations
    for (Object object : selection) {
        if (object instanceof EditPart) {
            Object model = ((EditPart) object).getModel();
            if (model instanceof IDiagramModelArchimateObject) {
                IArchimateElement element = ((IDiagramModelArchimateObject) model).getArchimateElement();
                if (!archimateConcepts.contains(element)) {
                    archimateConcepts.add(element);
                }
                // Element's relationships
                for (IArchimateRelationship relation : ArchimateModelUtils.getAllRelationshipsForConcept(element)) {
                    if (!archimateConcepts.contains(relation)) {
                        archimateConcepts.add(relation);
                    }
                    // Relation's relationships
                    for (IArchimateRelationship r : ArchimateModelUtils.getAllRelationshipsForConcept(relation)) {
                        if (!archimateConcepts.contains(r)) {
                            archimateConcepts.add(r);
                        }
                    }
                }
            } else if (model instanceof IDiagramModelArchimateConnection) {
                IArchimateRelationship relation = ((IDiagramModelArchimateConnection) model).getArchimateRelationship();
                if (!archimateConcepts.contains(relation)) {
                    archimateConcepts.add(relation);
                }
                // Relation's relationships
                for (IArchimateRelationship r : ArchimateModelUtils.getAllRelationshipsForConcept(relation)) {
                    if (!archimateConcepts.contains(r)) {
                        archimateConcepts.add(r);
                    }
                }
            }
        }
    }
    // Gather referenced diagram objects
    for (IArchimateConcept archimateConcept : archimateConcepts) {
        for (IDiagramModel diagramModel : archimateConcept.getArchimateModel().getDiagramModels()) {
            for (IDiagramModelComponent dc : DiagramModelUtils.findDiagramModelComponentsForArchimateConcept(diagramModel, archimateConcept)) {
                diagramObjects.add(dc);
            }
        }
    }
    // Check whether any of these concepts are referenced in other diagrams
    if (hasMoreThanOneReference(archimateConcepts, diagramObjects)) {
        if (!MessageDialog.openQuestion(Display.getDefault().getActiveShell(), Messages.DeleteFromModelAction_0, Messages.DeleteFromModelAction_1 + // $NON-NLS-1$
        "\n\n" + Messages.DeleteFromModelAction_2)) {
            return;
        }
    }
    // Create commands
    CompoundCommand compoundCommand = new NonNotifyingCompoundCommand(TEXT);
    for (IArchimateConcept archimateConcept : archimateConcepts) {
        if (archimateConcept instanceof IArchimateElement) {
            Command cmd = new DeleteArchimateElementCommand((IArchimateElement) archimateConcept);
            compoundCommand.add(cmd);
        } else if (archimateConcept instanceof IArchimateRelationship) {
            Command cmd = new DeleteArchimateRelationshipCommand((IArchimateRelationship) archimateConcept);
            compoundCommand.add(cmd);
        }
    }
    for (IDiagramModelComponent dc : diagramObjects) {
        if (dc instanceof IDiagramModelObject) {
            Command cmd = DiagramCommandFactory.createDeleteDiagramObjectCommand((IDiagramModelObject) dc);
            compoundCommand.add(cmd);
        } else if (dc instanceof IDiagramModelConnection) {
            Command cmd = DiagramCommandFactory.createDeleteDiagramConnectionCommand((IDiagramModelConnection) dc);
            compoundCommand.add(cmd);
        }
    }
    execute(compoundCommand);
}
Also used : NonNotifyingCompoundCommand(com.archimatetool.editor.model.commands.NonNotifyingCompoundCommand) IDiagramModelComponent(com.archimatetool.model.IDiagramModelComponent) DeleteArchimateElementCommand(com.archimatetool.editor.model.commands.DeleteArchimateElementCommand) IDiagramModelArchimateConnection(com.archimatetool.model.IDiagramModelArchimateConnection) ArrayList(java.util.ArrayList) EditPart(org.eclipse.gef.EditPart) DeleteArchimateRelationshipCommand(com.archimatetool.editor.model.commands.DeleteArchimateRelationshipCommand) IDiagramModelConnection(com.archimatetool.model.IDiagramModelConnection) IDiagramModelObject(com.archimatetool.model.IDiagramModelObject) CompoundCommand(org.eclipse.gef.commands.CompoundCommand) NonNotifyingCompoundCommand(com.archimatetool.editor.model.commands.NonNotifyingCompoundCommand) IDiagramModel(com.archimatetool.model.IDiagramModel) DeleteArchimateRelationshipCommand(com.archimatetool.editor.model.commands.DeleteArchimateRelationshipCommand) DeleteArchimateElementCommand(com.archimatetool.editor.model.commands.DeleteArchimateElementCommand) CompoundCommand(org.eclipse.gef.commands.CompoundCommand) NonNotifyingCompoundCommand(com.archimatetool.editor.model.commands.NonNotifyingCompoundCommand) Command(org.eclipse.gef.commands.Command) IArchimateConcept(com.archimatetool.model.IArchimateConcept) IArchimateElement(com.archimatetool.model.IArchimateElement) IDiagramModelArchimateObject(com.archimatetool.model.IDiagramModelArchimateObject) IDiagramModelObject(com.archimatetool.model.IDiagramModelObject) IArchimateRelationship(com.archimatetool.model.IArchimateRelationship) IDiagramModelArchimateObject(com.archimatetool.model.IDiagramModelArchimateObject)

Example 3 with NonNotifyingCompoundCommand

use of com.archimatetool.editor.model.commands.NonNotifyingCompoundCommand in project archi by archimatetool.

the class DiagramEditorFindReplaceProvider method doRenameCommands.

void doRenameCommands(List<EditPart> editParts, List<String> newNames) {
    // Must match sizes
    if (editParts.size() != newNames.size() || editParts.isEmpty()) {
        return;
    }
    CompoundCommand compoundCommand = new NonNotifyingCompoundCommand(Messages.DiagramEditorFindReplaceProvider_1);
    for (int i = 0; i < editParts.size(); i++) {
        INameable object = (INameable) editParts.get(i).getModel();
        String newName = newNames.get(i);
        compoundCommand.add(new EObjectFeatureCommand(NLS.bind(Messages.DiagramEditorFindReplaceProvider_0, object.getName()), object, IArchimatePackage.Literals.NAMEABLE__NAME, newName));
    }
    CommandStack stack = (CommandStack) ((IAdapter) editParts.get(0).getModel()).getAdapter(CommandStack.class);
    if (stack != null) {
        stack.execute(compoundCommand.unwrap());
    }
}
Also used : NonNotifyingCompoundCommand(com.archimatetool.editor.model.commands.NonNotifyingCompoundCommand) CommandStack(org.eclipse.gef.commands.CommandStack) EObjectFeatureCommand(com.archimatetool.editor.model.commands.EObjectFeatureCommand) INameable(com.archimatetool.model.INameable) CompoundCommand(org.eclipse.gef.commands.CompoundCommand) NonNotifyingCompoundCommand(com.archimatetool.editor.model.commands.NonNotifyingCompoundCommand)

Example 4 with NonNotifyingCompoundCommand

use of com.archimatetool.editor.model.commands.NonNotifyingCompoundCommand in project archi by archimatetool.

the class ArchimateDNDEditPolicy method getDropCommand.

@Override
protected Command getDropCommand(DiagramDropRequest request) {
    if (!(request.getData() instanceof IStructuredSelection)) {
        return null;
    }
    // XY drop point
    Point pt = getDropLocation(request);
    int origin = pt.x;
    int x = pt.x;
    int y = pt.y;
    fElementsToAdd = new ArrayList<IArchimateElement>();
    fRelationsToAdd = new ArrayList<IArchimateRelationship>();
    fDiagramRefsToAdd = new ArrayList<IDiagramModel>();
    // Gather an actual list of elements dragged onto the container, omitting duplicates and anything already on the diagram
    Object[] objects = ((IStructuredSelection) request.getData()).toArray();
    getElementsToAdd(objects);
    // Store the Diagram Model Components that will be added in this list
    List<IDiagramModelArchimateComponent> diagramComponentsThatWereAdded = new ArrayList<IDiagramModelArchimateComponent>();
    // Create a Compound Command - it has to be Non-Notifying or it's too slow (tested with Bill's UoB model!)
    CompoundCommand result = new NonNotifyingCompoundCommand(Messages.ArchimateDNDEditPolicy_0);
    // Add the Commands adding the Elements first
    for (IArchimateElement element : fElementsToAdd) {
        // Add Diagram object
        IDiagramModelArchimateObject dmo = ArchimateDiagramModelFactory.createDiagramModelArchimateObject(element);
        // Set location
        dmo.getBounds().setLocation(x, y);
        // Store it
        diagramComponentsThatWereAdded.add(dmo);
        // Add Command
        result.add(new AddDiagramObjectCommand(getTargetContainer(), dmo));
        // Increase x,y
        x += 150;
        if (x > origin + 400) {
            x = origin;
            y += 100;
        }
    }
    // Then any Diagram Model Ref Commands
    for (IDiagramModel diagramModel : fDiagramRefsToAdd) {
        result.add(new AddDiagramModelReferenceCommand(getTargetContainer(), diagramModel, x, y));
        x += 150;
        if (x > origin + 400) {
            x = origin;
            y += 100;
        }
    }
    // Add selected Relations to create connections to those elements on the diagram that don't already have them
    for (IArchimateRelationship relation : fRelationsToAdd) {
        // Find existing source & target components on the diagram that the new connection will link to
        List<IDiagramModelArchimateComponent> sources = DiagramModelUtils.findDiagramModelComponentsForArchimateConcept(getTargetDiagramModel(), relation.getSource());
        List<IDiagramModelArchimateComponent> targets = DiagramModelUtils.findDiagramModelComponentsForArchimateConcept(getTargetDiagramModel(), relation.getTarget());
        for (IDiagramModelComponent dcSource : sources) {
            for (IDiagramModelComponent dcTarget : targets) {
                if (dcSource instanceof IConnectable && dcTarget instanceof IConnectable) {
                    // Add a new connection between dcSource & dcTarget if there isn't already one on the diagram
                    if (dcTarget != dcSource && !DiagramModelUtils.hasDiagramModelArchimateConnection((IConnectable) dcSource, (IConnectable) dcTarget, relation)) {
                        // Check that source or target is not a hiden connection
                        if (!((dcSource instanceof IDiagramModelArchimateConnection && DiagramModelUtils.shouldBeHiddenConnection((IDiagramModelArchimateConnection) dcSource)) || (dcTarget instanceof IDiagramModelArchimateConnection && DiagramModelUtils.shouldBeHiddenConnection((IDiagramModelArchimateConnection) dcTarget)))) {
                            AddDiagramArchimateConnectionCommand cmd = new AddDiagramArchimateConnectionCommand((IConnectable) dcSource, (IConnectable) dcTarget, relation);
                            result.add(cmd);
                            // Store it
                            diagramComponentsThatWereAdded.add(cmd.getConnection());
                        }
                    }
                }
            }
        }
    }
    // Whether to add connections to elements
    Boolean value = (Boolean) request.getExtendedData().get(ArchimateDiagramTransferDropTargetListener.ADD_ELEMENT_CONNECTIONS);
    boolean addConnectionsToElements = value != null && value.booleanValue();
    // Newly added concepts will need new connections to both existing and newly added concepts
    for (IDiagramModelArchimateComponent dmComponent : diagramComponentsThatWereAdded) {
        IArchimateConcept archimateConcept = dmComponent.getArchimateConcept();
        for (IArchimateRelationship relation : ArchimateModelUtils.getAllRelationshipsForConcept(archimateConcept)) {
            /*
                 * If the user holds down the Copy key (Ctrl on win/lnx, Alt on Mac) then linked connections
                 * are not added on drag and drop. However, any selected relations' linked objects are added.
                 */
            if (!addConnectionsToElements && !fRelationsToAdd.contains(relation)) {
                continue;
            }
            // Find existing objects
            List<IDiagramModelArchimateComponent> sources = DiagramModelUtils.findDiagramModelComponentsForArchimateConcept(getTargetDiagramModel(), relation.getSource());
            List<IDiagramModelArchimateComponent> targets = DiagramModelUtils.findDiagramModelComponentsForArchimateConcept(getTargetDiagramModel(), relation.getTarget());
            // Add new ones too
            for (IDiagramModelArchimateComponent dmComponent2 : diagramComponentsThatWereAdded) {
                if (dmComponent != dmComponent2) {
                    IArchimateConcept archimateConcept2 = dmComponent2.getArchimateConcept();
                    if (archimateConcept2 == relation.getSource()) {
                        // Only need to add sources, not targets
                        sources.add(dmComponent2);
                    }
                }
            }
            // Make the Commands...
            for (IDiagramModelComponent dcSource : sources) {
                if (dcSource instanceof IConnectable && archimateConcept == relation.getTarget()) {
                    result.add(new AddDiagramArchimateConnectionCommand((IConnectable) dcSource, (IConnectable) dmComponent, relation));
                }
            }
            for (IDiagramModelComponent dcTarget : targets) {
                if (dcTarget instanceof IConnectable && archimateConcept == relation.getSource()) {
                    result.add(new AddDiagramArchimateConnectionCommand((IConnectable) dmComponent, (IConnectable) dcTarget, relation));
                }
            }
        }
    }
    // Then, if adding to an Archimate container type to create nesting, ask whether to add new relations if none exist...
    if (ConnectionPreferences.createRelationWhenAddingModelTreeElement() && getTargetContainer() instanceof IDiagramModelArchimateObject) {
        List<IDiagramModelArchimateObject> diagramObjectsThatWereAdded = new ArrayList<IDiagramModelArchimateObject>();
        for (IDiagramModelArchimateComponent dmc : diagramComponentsThatWereAdded) {
            if (dmc instanceof IDiagramModelArchimateObject) {
                diagramObjectsThatWereAdded.add((IDiagramModelArchimateObject) dmc);
            }
        }
        Command cmd = new CreateNestedArchimateConnectionsWithDialogCommand((IDiagramModelArchimateObject) getTargetContainer(), diagramObjectsThatWereAdded);
        result.add(cmd);
    }
    // return the full compound command
    return result;
}
Also used : CreateNestedArchimateConnectionsWithDialogCommand(com.archimatetool.editor.diagram.commands.CreateNestedArchimateConnectionsWithDialogCommand) ArrayList(java.util.ArrayList) AddDiagramObjectCommand(com.archimatetool.editor.diagram.commands.AddDiagramObjectCommand) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) AddDiagramModelReferenceCommand(com.archimatetool.editor.diagram.commands.AddDiagramModelReferenceCommand) CompoundCommand(org.eclipse.gef.commands.CompoundCommand) NonNotifyingCompoundCommand(com.archimatetool.editor.model.commands.NonNotifyingCompoundCommand) IDiagramModel(com.archimatetool.model.IDiagramModel) IArchimateElement(com.archimatetool.model.IArchimateElement) IArchimateConcept(com.archimatetool.model.IArchimateConcept) IArchimateRelationship(com.archimatetool.model.IArchimateRelationship) NonNotifyingCompoundCommand(com.archimatetool.editor.model.commands.NonNotifyingCompoundCommand) IConnectable(com.archimatetool.model.IConnectable) IDiagramModelComponent(com.archimatetool.model.IDiagramModelComponent) IDiagramModelArchimateConnection(com.archimatetool.model.IDiagramModelArchimateConnection) Point(org.eclipse.draw2d.geometry.Point) Point(org.eclipse.draw2d.geometry.Point) AddDiagramModelReferenceCommand(com.archimatetool.editor.diagram.commands.AddDiagramModelReferenceCommand) AddDiagramObjectCommand(com.archimatetool.editor.diagram.commands.AddDiagramObjectCommand) CreateNestedArchimateConnectionsWithDialogCommand(com.archimatetool.editor.diagram.commands.CreateNestedArchimateConnectionsWithDialogCommand) CompoundCommand(org.eclipse.gef.commands.CompoundCommand) NonNotifyingCompoundCommand(com.archimatetool.editor.model.commands.NonNotifyingCompoundCommand) Command(org.eclipse.gef.commands.Command) IDiagramModelArchimateComponent(com.archimatetool.model.IDiagramModelArchimateComponent) IDiagramModelArchimateObject(com.archimatetool.model.IDiagramModelArchimateObject) IArchimateModelObject(com.archimatetool.model.IArchimateModelObject) EObject(org.eclipse.emf.ecore.EObject) IDiagramModelArchimateObject(com.archimatetool.model.IDiagramModelArchimateObject)

Example 5 with NonNotifyingCompoundCommand

use of com.archimatetool.editor.model.commands.NonNotifyingCompoundCommand in project archi by archimatetool.

the class TreeModelViewerDragDropHandler method moveTreeObjects.

/**
 * Move Tree Objects
 */
void moveTreeObjects(IFolder newParent, Object[] objects) {
    final CompoundCommand compoundCommand = new NonNotifyingCompoundCommand() {

        @Override
        public String getLabel() {
            return getCommands().size() > 1 ? Messages.TreeModelViewerDragDropHandler_0 : super.getLabel();
        }
    };
    for (Object object : objects) {
        if (object instanceof IFolder) {
            // This first - folders go in folders
            if (!newParent.getFolders().contains(object)) {
                compoundCommand.add(new MoveFolderCommand(newParent, (IFolder) object));
            }
        } else if (object instanceof IArchimateModelObject) {
            if (!newParent.getElements().contains(object)) {
                compoundCommand.add(new MoveObjectCommand(newParent, (IArchimateModelObject) object));
            }
        }
    }
    CommandStack stack = (CommandStack) newParent.getAdapter(CommandStack.class);
    stack.execute(compoundCommand);
}
Also used : NonNotifyingCompoundCommand(com.archimatetool.editor.model.commands.NonNotifyingCompoundCommand) CommandStack(org.eclipse.gef.commands.CommandStack) IArchimateModelObject(com.archimatetool.model.IArchimateModelObject) MoveFolderCommand(com.archimatetool.editor.views.tree.commands.MoveFolderCommand) IArchimateModelObject(com.archimatetool.model.IArchimateModelObject) EObject(org.eclipse.emf.ecore.EObject) CompoundCommand(org.eclipse.gef.commands.CompoundCommand) NonNotifyingCompoundCommand(com.archimatetool.editor.model.commands.NonNotifyingCompoundCommand) IFolder(com.archimatetool.model.IFolder) MoveObjectCommand(com.archimatetool.editor.views.tree.commands.MoveObjectCommand)

Aggregations

NonNotifyingCompoundCommand (com.archimatetool.editor.model.commands.NonNotifyingCompoundCommand)6 CompoundCommand (org.eclipse.gef.commands.CompoundCommand)6 CommandStack (org.eclipse.gef.commands.CommandStack)4 IArchimateConcept (com.archimatetool.model.IArchimateConcept)2 IArchimateElement (com.archimatetool.model.IArchimateElement)2 IArchimateModelObject (com.archimatetool.model.IArchimateModelObject)2 IArchimateRelationship (com.archimatetool.model.IArchimateRelationship)2 IDiagramModel (com.archimatetool.model.IDiagramModel)2 IDiagramModelArchimateConnection (com.archimatetool.model.IDiagramModelArchimateConnection)2 IDiagramModelArchimateObject (com.archimatetool.model.IDiagramModelArchimateObject)2 IDiagramModelComponent (com.archimatetool.model.IDiagramModelComponent)2 ArrayList (java.util.ArrayList)2 EObject (org.eclipse.emf.ecore.EObject)2 Command (org.eclipse.gef.commands.Command)2 AddDiagramModelReferenceCommand (com.archimatetool.editor.diagram.commands.AddDiagramModelReferenceCommand)1 AddDiagramObjectCommand (com.archimatetool.editor.diagram.commands.AddDiagramObjectCommand)1 CreateNestedArchimateConnectionsWithDialogCommand (com.archimatetool.editor.diagram.commands.CreateNestedArchimateConnectionsWithDialogCommand)1 DeleteArchimateElementCommand (com.archimatetool.editor.model.commands.DeleteArchimateElementCommand)1 DeleteArchimateRelationshipCommand (com.archimatetool.editor.model.commands.DeleteArchimateRelationshipCommand)1 EObjectFeatureCommand (com.archimatetool.editor.model.commands.EObjectFeatureCommand)1