Search in sources :

Example 1 with AddNewMetaNodeCommand

use of org.knime.workbench.editor2.commands.AddNewMetaNodeCommand in project knime-core by knime.

the class AddMetaNodeWizard method createMetaNodeFromPorts.

private void createMetaNodeFromPorts(final PortType[] inPorts, final PortType[] outPorts, final String name) {
    Viewport viewPort = ((ScalableFreeformRootEditPart) m_wfEditor.getViewer().getRootEditPart()).getZoomManager().getViewport();
    // get the currently visible area
    Rectangle rect = viewPort.getClientArea();
    // translate it to absolute coordinates (with respect to the scrolled
    // editor viewer)
    viewPort.translateToAbsolute(rect.getLocation());
    // now we have an absolute point for the new metanode location
    org.eclipse.draw2d.geometry.Point location = new org.eclipse.draw2d.geometry.Point(rect.x + (rect.width / 2), rect.y + (rect.height / 2));
    // in order to get a local (relative position we have to substract the
    // invisible offset
    Point adaptedLoc = new Point(location.x - rect.x, location.y - rect.y);
    EditPart ep;
    // iterate until we have found a free place on the editor
    do {
        // we have to use the adaptedLoc (which is the relative position)
        ep = m_wfEditor.getViewer().findObjectAt(adaptedLoc);
        if (ep instanceof NodeContainerEditPart) {
            // offset the absolute location
            location.x += ((NodeContainerEditPart) ep).getFigure().getBounds().width;
            location.y += ((NodeContainerEditPart) ep).getFigure().getBounds().height;
            // offset the relative position
            adaptedLoc.x += ((NodeContainerEditPart) ep).getFigure().getBounds().width;
            adaptedLoc.y += ((NodeContainerEditPart) ep).getFigure().getBounds().height;
        }
    } while (ep instanceof NodeContainerEditPart);
    AddNewMetaNodeCommand cmd = new AddNewMetaNodeCommand(m_wfEditor.getWorkflowManager().get(), inPorts, outPorts, name, location);
    m_wfEditor.getViewer().getEditDomain().getCommandStack().execute(cmd);
}
Also used : NodeContainerEditPart(org.knime.workbench.editor2.editparts.NodeContainerEditPart) AddNewMetaNodeCommand(org.knime.workbench.editor2.commands.AddNewMetaNodeCommand) Point(org.eclipse.draw2d.geometry.Point) Viewport(org.eclipse.draw2d.Viewport) Rectangle(org.eclipse.draw2d.geometry.Rectangle) NodeContainerEditPart(org.knime.workbench.editor2.editparts.NodeContainerEditPart) ScalableFreeformRootEditPart(org.eclipse.gef.editparts.ScalableFreeformRootEditPart) EditPart(org.eclipse.gef.EditPart) Point(org.eclipse.draw2d.geometry.Point)

Aggregations

Viewport (org.eclipse.draw2d.Viewport)1 Point (org.eclipse.draw2d.geometry.Point)1 Rectangle (org.eclipse.draw2d.geometry.Rectangle)1 EditPart (org.eclipse.gef.EditPart)1 ScalableFreeformRootEditPart (org.eclipse.gef.editparts.ScalableFreeformRootEditPart)1 AddNewMetaNodeCommand (org.knime.workbench.editor2.commands.AddNewMetaNodeCommand)1 NodeContainerEditPart (org.knime.workbench.editor2.editparts.NodeContainerEditPart)1