Java Code Examples for org.eclipse.emf.common.command.CompoundCommand#canExecute()

The following examples show how to use org.eclipse.emf.common.command.CompoundCommand#canExecute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Commands.java    From graph-editor with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Clears everything in the given model.
 *
 * @param model the {@link GModel} to be cleared
 */
public static void clear(final GModel model) {

    final EditingDomain editingDomain = getEditingDomain(model);

    if (editingDomain != null) {
        final CompoundCommand command = new CompoundCommand();

        command.append(RemoveCommand.create(editingDomain, model, CONNECTIONS, model.getConnections()));
        command.append(RemoveCommand.create(editingDomain, model, NODES, model.getNodes()));

        if (command.canExecute()) {
            editingDomain.getCommandStack().execute(command);
        }
    }
}
 
Example 2
Source File: JointCommands.java    From graph-editor with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Removes any existing joints from the connection and creates a new set of joints at the given positions.
 *
 * <p>
 * This is executed as a single compound command and is therefore a single element in the undo-redo stack.
 * </p>
 *
 * @param positions a list of {@link Point2D} instances speciying the x and y positions of the new joints
 * @param connection the connection in which the joints will be set
 */
public static void setNewJoints(final List<Point2D> positions, final GConnection connection) {

    final EditingDomain editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(connection);
    final CompoundCommand command = new CompoundCommand();

    command.append(RemoveCommand.create(editingDomain, connection, JOINTS, connection.getJoints()));

    for (final Point2D position : positions) {

        final GJoint newJoint = GraphFactory.eINSTANCE.createGJoint();
        newJoint.setX(position.getX());
        newJoint.setY(position.getY());

        command.append(AddCommand.create(editingDomain, connection, JOINTS, newJoint));
    }

    if (command.canExecute()) {
        editingDomain.getCommandStack().execute(command);
    }
}
 
Example 3
Source File: ConnectionCommands.java    From graph-editor with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Removes a connection from the model.
 *
 * @param model the {@link GModel} from which the connection should be removed
 * @param connection the {@link GConnection} to be removed
 * @return the newly-executed {@link CompoundCommand} that removed the connection
 */
public static CompoundCommand removeConnection(final GModel model, final GConnection connection) {

    final EditingDomain editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(model);

    if (editingDomain != null) {
        final CompoundCommand command = new CompoundCommand();

        final GConnector source = connection.getSource();
        final GConnector target = connection.getTarget();

        command.append(RemoveCommand.create(editingDomain, model, CONNECTIONS, connection));
        command.append(RemoveCommand.create(editingDomain, source, CONNECTOR_CONNECTIONS, connection));
        command.append(RemoveCommand.create(editingDomain, target, CONNECTOR_CONNECTIONS, connection));

        if (command.canExecute()) {
            editingDomain.getCommandStack().execute(command);
        }
        return command;

    } else {
        return null;
    }
}
 
Example 4
Source File: SelectionCopier.java    From graph-editor with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Adds the pasted elements to the graph editor via a single EMF command.
 *
 * @param pastedNodes the pasted nodes to be added
 * @param pastedConnections the pasted connections to be added
 * @param consumer a consumer to allow custom commands to be appended to the paste command
 */
private void addPastedElements(final List<GNode> pastedNodes, final List<GConnection> pastedConnections,
        final BiConsumer<List<GNode>, CompoundCommand> consumer) {

    final EditingDomain editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(model);
    final CompoundCommand command = new CompoundCommand();

    for (final GNode pastedNode : pastedNodes) {
        command.append(AddCommand.create(editingDomain, model, NODES, pastedNode));
    }

    for (final GConnection pastedConnection : pastedConnections) {
        command.append(AddCommand.create(editingDomain, model, CONNECTIONS, pastedConnection));
    }

    if (command.canExecute()) {
        editingDomain.getCommandStack().execute(command);
    }

    if (consumer != null) {
        consumer.accept(pastedNodes, command);
    }

}
 
Example 5
Source File: DefaultSkinController.java    From graph-editor with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Adds a connector of the given type to all nodes that are currently selected.
 *
 * @param position the position of the new connector
 * @param input {@code true} for input, {@code false} for output
 */
@Override
public void addConnector(final Side position, final boolean input) {

    final String type = getType(position, input);

    final GModel model = graphEditor.getModel();
    final SkinLookup skinLookup = graphEditor.getSkinLookup();
    final CompoundCommand command = new CompoundCommand();
    final EditingDomain editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(model);

    for (final GNode node : model.getNodes()) {

        if (skinLookup.lookupNode(node).isSelected()) {
            if (countConnectors(node, position) < MAX_CONNECTOR_COUNT) {

                final GConnector connector = GraphFactory.eINSTANCE.createGConnector();
                connector.setType(type);

                final EReference connectors = GraphPackage.Literals.GCONNECTABLE__CONNECTORS;
                command.append(AddCommand.create(editingDomain, node, connectors, connector));
            }
        }
    }

    if (command.canExecute()) {
        editingDomain.getCommandStack().execute(command);
    }
}
 
Example 6
Source File: ConnectionCommands.java    From graph-editor with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Adds a connection to the model.
 *
 * @param model the {@link GModel} to which the connection should be added
 * @param source the source {@link GConnector} of the new connection
 * @param target the target {@link GConnector} of the new connection
 * @param type the type attribute for the new connection
 * @param joints the list of {@link GJoint} instances to be added inside the new connection
 * @return the newly-executed {@link CompoundCommand} that added the connection
 */
public static CompoundCommand addConnection(final GModel model, final GConnector source, final GConnector target,
        final String type, final List<GJoint> joints) {

    final EditingDomain editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(model);

    if (editingDomain != null) {
        final CompoundCommand command = new CompoundCommand();

        final GConnection connection = GraphFactory.eINSTANCE.createGConnection();

        command.append(AddCommand.create(editingDomain, model, CONNECTIONS, connection));

        if (type != null) {
            command.append(SetCommand.create(editingDomain, connection, CONNECTION_TYPE, type));
        }

        command.append(SetCommand.create(editingDomain, connection, SOURCE, source));
        command.append(SetCommand.create(editingDomain, connection, TARGET, target));
        command.append(AddCommand.create(editingDomain, source, CONNECTOR_CONNECTIONS, connection));
        command.append(AddCommand.create(editingDomain, target, CONNECTOR_CONNECTIONS, connection));

        for (final GJoint joint : joints) {
            command.append(AddCommand.create(editingDomain, connection, JOINTS, joint));
        }

        if (command.canExecute()) {
            editingDomain.getCommandStack().execute(command);
        }
        return command;

    } else {
        return null;
    }
}
 
Example 7
Source File: ModelEditingManager.java    From graph-editor with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Removes all specified nodes and connections from the model in a single compound command.
 *
 * <p>
 * All references to the removed elements are also removed.
 * </p>
 *
 * @param nodesToRemove the nodes to be removed
 * @param connectionsToRemove the connections to be removed
 */
public CompoundCommand remove(final List<GNode> nodesToRemove, final List<GConnection> connectionsToRemove) {

    final CompoundCommand command = new CompoundCommand();

    for (final GNode node : nodesToRemove) {
        command.append(RemoveCommand.create(editingDomain, model, NODES, node));
    }

    for (final GConnection connection : connectionsToRemove) {
        command.append(RemoveCommand.create(editingDomain, model, CONNECTIONS, connection));

        final GConnector source = connection.getSource();
        final GConnector target = connection.getTarget();

        if (!nodesToRemove.contains(source.getParent())) {
            command.append(RemoveCommand.create(editingDomain, source, CONNECTOR_CONNECTIONS, connection));
        }

        if (!nodesToRemove.contains(target.getParent())) {
            command.append(RemoveCommand.create(editingDomain, target, CONNECTOR_CONNECTIONS, connection));
        }
    }

    if (command.canExecute()) {
        editingDomain.getCommandStack().execute(command);
    }

    return command;
}
 
Example 8
Source File: TreeNodeSkin.java    From graph-editor with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Adds a child node with one input and one output connector, placed directly underneath its parent.
 */
private void addChildNode() {

    final GNode childNode = GraphFactory.eINSTANCE.createGNode();

    childNode.setType(TreeSkinConstants.TREE_NODE);
    childNode.setX(getNode().getX() + (getNode().getWidth() - childNode.getWidth()) / 2);
    childNode.setY(getNode().getY() + getNode().getHeight() + CHILD_Y_OFFSET);

    final GModel model = getGraphEditor().getModel();
    final double maxAllowedY = model.getContentHeight() - VIEW_PADDING;

    if (childNode.getY() + childNode.getHeight() > maxAllowedY) {
        childNode.setY(maxAllowedY - childNode.getHeight());
    }

    final GConnector input = GraphFactory.eINSTANCE.createGConnector();
    final GConnector output = GraphFactory.eINSTANCE.createGConnector();

    input.setType(TreeSkinConstants.TREE_INPUT_CONNECTOR);
    output.setType(TreeSkinConstants.TREE_OUTPUT_CONNECTOR);

    childNode.getConnectors().add(input);
    childNode.getConnectors().add(output);

    // This allows multiple connections to be created from the output.
    output.setConnectionDetachedOnDrag(false);

    final GConnector parentOutput = findOutput();
    final GConnection connection = GraphFactory.eINSTANCE.createGConnection();

    connection.setType(TreeSkinConstants.TREE_CONNECTION);
    connection.setSource(parentOutput);
    connection.setTarget(input);

    input.getConnections().add(connection);

    // Set the rest of the values via EMF commands because they touch the currently-edited model.
    final EditingDomain editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(model);
    final CompoundCommand command = new CompoundCommand();

    command.append(AddCommand.create(editingDomain, model, NODES, childNode));
    command.append(AddCommand.create(editingDomain, model, CONNECTIONS, connection));
    command.append(AddCommand.create(editingDomain, parentOutput, CONNECTOR_CONNECTIONS, connection));

    if (command.canExecute()) {
        editingDomain.getCommandStack().execute(command);
    }
}
 
Example 9
Source File: Commands.java    From graph-editor with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Removes all connectors from the given nodes, and all connections attached to them.
 * 
 * @param model the {@link GModel} being edited
 * @param nodes a list of {@link GNode} instances whose connectors should be removed
 */
public static void clearConnectors(final GModel model, final List<GNode> nodes) {

    final EditingDomain editingDomain = getEditingDomain(model);

    if (editingDomain != null) {

        final CompoundCommand command = new CompoundCommand();

        final Set<GConnection> connectionsToRemove = new HashSet<>();
        final Set<GConnector> connectorsToRemove = new HashSet<>();

        for (final GNode node : nodes) {

            command.append(RemoveCommand.create(editingDomain, node, NODE_CONNECTORS, node.getConnectors()));
            connectorsToRemove.addAll(node.getConnectors());

            for (final GConnector connector : node.getConnectors()) {
                connectionsToRemove.addAll(connector.getConnections());
            }
        }

        for (final GConnection connection : connectionsToRemove) {

            final GConnector source = connection.getSource();
            final GConnector target = connection.getTarget();

            if (!connectorsToRemove.contains(source)) {
                command.append(RemoveCommand.create(editingDomain, source, CONNECTOR_CONNECTIONS, connection));
            }

            if (!connectorsToRemove.contains(target)) {
                command.append(RemoveCommand.create(editingDomain, target, CONNECTOR_CONNECTIONS, connection));
            }
        }

        command.append(RemoveCommand.create(editingDomain, model, CONNECTIONS, connectionsToRemove));

        if (command.canExecute()) {
            editingDomain.getCommandStack().execute(command);
        }
    }
}
 
Example 10
Source File: JointCleaner.java    From graph-editor with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Adds handlers to remove all joints that are on top of each other after a mouse-released gesture.
 *
 * @param jointSkins the connection's joint skins
 */
public void addCleaningHandlers(final List<GJointSkin> jointSkins) {

    final Map<GJoint, EventHandler<MouseEvent>> oldCleaningHandlers = new HashMap<>(cleaningHandlers);

    cleaningHandlers.clear();

    for (final GJointSkin jointSkin : jointSkins) {

        final GJoint joint = jointSkin.getJoint();
        final Region jointRegion = jointSkin.getRoot();
        final EventHandler<MouseEvent> oldHandler = oldCleaningHandlers.get(joint);

        if (oldHandler != null) {
            jointRegion.removeEventHandler(MouseEvent.MOUSE_RELEASED, oldHandler);
        }

        final EventHandler<MouseEvent> newHandler = event -> {

            final Parent parent = jointRegion.getParent();

            if (jointSkins.size() == 2 || !event.getButton().equals(MouseButton.PRIMARY)) {
                return;
            }

            final List<Point2D> jointPositions = GeometryUtils.getJointPositions(jointSkins);
            final Set<Integer> jointsToCleanUp = findJointsToCleanUp(jointPositions);

            if (!jointsToCleanUp.isEmpty()) {

                final EditingDomain editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(connection);
                final CompoundCommand command = new CompoundCommand();

                final GModel model = graphEditor.getModel();
                final SkinLookup skinLookup = graphEditor.getSkinLookup();

                JointCommands.removeJoints(command, jointsToCleanUp, connection);
                Commands.updateLayoutValues(command, model, skinLookup);

                if (command.canExecute()) {
                    editingDomain.getCommandStack().execute(command);
                }
            }

            parent.layout();
        };

        jointRegion.addEventHandler(MouseEvent.MOUSE_RELEASED, newHandler);
        cleaningHandlers.put(joint, newHandler);
    }
}
 
Example 11
Source File: ModelEditingManager.java    From graph-editor with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Silently updates the model's layout values to match those in the skin instances.
 *
 * @param skinLookup the {@link SkinLookup} used to lookup skin instances
 */
public void updateLayoutValues(final SkinLookup skinLookup) {

    final CompoundCommand command = new CompoundCommand();

    Commands.updateLayoutValues(command, model, skinLookup);

    editingDomain.getCommandStack().removeCommandStackListener(commandStackListener);

    if (command.canExecute()) {
        editingDomain.getCommandStack().execute(command);
    }

    editingDomain.getCommandStack().addCommandStackListener(commandStackListener);
}