use of org.knime.workbench.editor2.editparts.NodeContainerEditPart in project knime-core by knime.
the class WorkflowEditor method addNode.
/**
* Listener interface method of the {@link NodeProvider}. Called when other
* instances want to add a node to the workflow in the editor. <br>
* The implementation only adds it if the editor is active and one node is
* selected in the editor, to which the new node will then be connected to.
* {@inheritDoc}
*/
@Override
public boolean addNode(final NodeFactory<? extends NodeModel> nodeFactory) {
if (!isEditorActive()) {
return false;
}
if (!getWorkflowManager().isPresent()) {
return false;
}
NodeContainerEditPart preNode = getTheOneSelectedNode();
Point nodeLoc = null;
Command newNodeCmd = null;
if (preNode == null) {
nodeLoc = getViewportCenterLocation();
// this command accepts/requires relative coordinates
newNodeCmd = new CreateNodeCommand(unwrapWFM(m_manager), nodeFactory, nodeLoc, getEditorSnapToGrid());
} else {
nodeLoc = getLocationRightOf(preNode);
newNodeCmd = new CreateNewConnectedNodeCommand(getViewer(), unwrapWFM(m_manager), nodeFactory, nodeLoc, preNode.getNodeContainer().getID());
}
getCommandStack().execute(newNodeCmd);
// after adding a node the editor should get the focus
// this is issued asynchronously, in order to avoid bug #3029
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
setFocus();
}
});
return true;
}
use of org.knime.workbench.editor2.editparts.NodeContainerEditPart in project knime-core by knime.
the class WorkflowEditor method isNodeAtRel.
private boolean isNodeAtRel(final Point relativeLoc) {
EditPart ep = getViewer().findObjectAt(relativeLoc);
if (ep == null) {
return false;
}
while (!(ep instanceof RootEditPart)) {
if (ep instanceof NodeContainerEditPart) {
return true;
}
EditPart parent = ep.getParent();
// avoid endless loops
if (parent == null || parent.equals(ep)) {
return false;
}
ep = parent;
}
return false;
}
use of org.knime.workbench.editor2.editparts.NodeContainerEditPart in project knime-core by knime.
the class WorkflowEditorDropTargetListener method getInsertLocation.
/**
* Computes the location of the new node and also determines if some node should be moved.
*
* A successor node is selected for the move action if the distance between this node and the successor is
* smaller than the {@link WorkflowEditorDropTargetListener#MINIMUM_NODE_DISTANCE} and if they overlap vertically.
*
* @return point where the new node should be inserted and sets
* {@link WorkflowEditorDropTargetListener#m_distanceToMoveTarget}
*/
private Point getInsertLocation() {
ZoomManager zoomManager = (ZoomManager) getViewer().getProperty(ZoomManager.class.toString());
double zoom = zoomManager.getZoom();
int adjustedMinimumNodeDistance = (int) (MINIMUM_NODE_DISTANCE * zoom);
double adjustedMinimumNodeDistanceBeforeInsertion = MINIMUM_NODE_DISTANCE_BEFORE_INSERTION * zoom;
Point dropLocation = getDropLocation();
Point insertLocation = dropLocation;
int[] sourceBounds = null;
int[] targetBounds = null;
int sourceAnnotationHeight = 0;
int targetAnnotationHeight = 0;
EditPart source = m_edge.getSource().getParent();
EditPart target = m_edge.getTarget().getParent();
NodeContainerEditPart nextNode = null;
if (source instanceof WorkflowInPortBarEditPart && target instanceof NodeContainerEditPart) {
// metanode start --> first node in metanode
WorkflowInPortBarEditPart sourceBar = ((WorkflowInPortBarEditPart) source);
NodeContainerEditPart targetNode = (NodeContainerEditPart) target;
Rectangle bounds = sourceBar.getFigure().getBounds();
org.eclipse.swt.graphics.Point p = getViewer().getControl().toControl(bounds.x, bounds.y);
sourceBounds = new int[] { p.x, p.y, bounds.width, bounds.height };
targetBounds = targetNode.getNodeContainer().getUIInformation().getBounds();
targetAnnotationHeight = targetNode.getNodeAnnotationEditPart().getModel().getHeight();
nextNode = targetNode;
} else if (source instanceof NodeContainerEditPart && target instanceof WorkflowOutPortBarEditPart) {
// last node in metanode --> metanode end
NodeContainerEditPart sourceNode = (NodeContainerEditPart) source;
WorkflowOutPortBarEditPart targetBar = (WorkflowOutPortBarEditPart) target;
sourceBounds = sourceNode.getNodeContainer().getUIInformation().getBounds();
Rectangle bounds = targetBar.getFigure().getBounds();
targetBounds = new int[] { bounds.x, bounds.y, bounds.width, bounds.height };
sourceAnnotationHeight = sourceNode.getNodeAnnotationEditPart().getModel().getHeight();
} else if (source instanceof WorkflowInPortBarEditPart && target instanceof WorkflowOutPortBarEditPart) {
// metanode start --> metanode end
WorkflowInPortBarEditPart sourceBar = (WorkflowInPortBarEditPart) source;
WorkflowOutPortBarEditPart targetBar = (WorkflowOutPortBarEditPart) target;
sourceBounds = sourceBar.getNodeContainer().getUIInformation().getBounds();
targetBounds = targetBar.getNodeContainer().getUIInformation().getBounds();
} else if (source instanceof NodeContainerEditPart && target instanceof NodeContainerEditPart) {
// node --> node
NodeContainerEditPart sourceNode = (NodeContainerEditPart) source;
NodeContainerEditPart targetNode = (NodeContainerEditPart) target;
sourceBounds = sourceNode.getNodeContainer().getUIInformation().getBounds();
targetBounds = targetNode.getNodeContainer().getUIInformation().getBounds();
sourceAnnotationHeight = sourceNode.getNodeAnnotationEditPart().getModel().getHeight();
targetAnnotationHeight = targetNode.getNodeAnnotationEditPart().getModel().getHeight();
nextNode = targetNode;
}
if (sourceBounds != null && targetBounds != null) {
sourceBounds = recomputeBounds(sourceBounds);
targetBounds = recomputeBounds(targetBounds);
if (0 <= targetBounds[0] - sourceBounds[0] && targetBounds[0] - sourceBounds[0] >= adjustedMinimumNodeDistanceBeforeInsertion) {
m_distanceToMoveTarget = 0;
} else {
m_distanceToMoveTarget = adjustedMinimumNodeDistance + (sourceBounds[0] + adjustedMinimumNodeDistance - targetBounds[0]);
m_distanceToMoveTarget = (int) (m_distanceToMoveTarget / zoom);
}
insertLocation = new Point(getXLocation(dropLocation.x, sourceBounds[0], zoom), getYLocation(dropLocation.y, sourceBounds[1], targetBounds[1], zoom));
if (WorkflowEditor.getActiveEditorSnapToGrid()) {
insertLocation = WorkflowEditor.getActiveEditorClosestGridLocation(insertLocation);
}
getViewer().getSelectionManager().deselectAll();
if (nextNode != null && selectSuccessor(sourceBounds[0], targetBounds[0], sourceBounds[1], sourceBounds[1] + sourceBounds[3] + sourceAnnotationHeight, targetBounds[1], targetBounds[1] + targetBounds[3] + targetAnnotationHeight, zoom)) {
selectNodes(nextNode, zoom);
}
}
return insertLocation;
}
use of org.knime.workbench.editor2.editparts.NodeContainerEditPart in project knime-core by knime.
the class WorkflowEditorDropTargetListener method selectNodes.
/**
* Selects all nodes after this node which are around the same y coordinate and closer than
* {@link WorkflowEditorDropTargetListener#m_distanceToMoveTarget} +
* {@link WorkflowEditorDropTargetListener#MINIMUM_NODE_DISTANCE}
*
* All selected elements will be moved by the move action.
*
* @param node from which on the selection starts
*/
private void selectNodes(final NodeContainerEditPart node, final double zoom) {
getViewer().getSelectionManager().appendSelection(node);
for (ConnectionContainerEditPart c : node.getOutgoingConnections()) {
EditPart source = c.getSource().getParent();
EditPart target = c.getTarget().getParent();
if (source instanceof NodeContainerEditPart && target instanceof NodeContainerEditPart) {
NodeContainerEditPart sourceNode = (NodeContainerEditPart) source;
NodeContainerEditPart targetNode = (NodeContainerEditPart) target;
int[] sourceBounds = sourceNode.getNodeContainer().getUIInformation().getBounds();
int[] targetBounds = targetNode.getNodeContainer().getUIInformation().getBounds();
sourceBounds = recomputeBounds(sourceBounds);
targetBounds = recomputeBounds(targetBounds);
int sourceYTop = sourceBounds[1];
int sourceYBot = sourceYTop + sourceBounds[3] + sourceNode.getNodeAnnotationEditPart().getModel().getHeight();
int targetYTop = targetBounds[1];
int targetYBot = targetYTop + targetBounds[3] + targetNode.getNodeAnnotationEditPart().getModel().getHeight();
if (selectSuccessor(sourceBounds[0], targetBounds[0], sourceYTop, sourceYBot, targetYTop, targetYBot, zoom)) {
selectNodes(targetNode, zoom);
}
}
}
}
use of org.knime.workbench.editor2.editparts.NodeContainerEditPart in project knime-core by knime.
the class WorkflowSelectionDragEditPartsTracker method getEmbracedConnections.
/**
* Returns the connections whose source and target is contained in the argument list.
* @param parts list of selected nodes
* @return the connections whose source and target is contained in the argument list.
*/
public static ConnectionContainerEditPart[] getEmbracedConnections(final List<EditPart> parts) {
// result list
List<ConnectionContainerEditPart> result = new ArrayList<ConnectionContainerEditPart>();
for (EditPart part : parts) {
if (part instanceof NodeContainerEditPart || part instanceof AbstractWorkflowPortBarEditPart) {
EditPart containerPart = part;
ConnectionContainerEditPart[] outPortConnectionParts = getOutportConnections(containerPart);
// selected list, the connections bendpoints must be adapted
for (ConnectionContainerEditPart connectionPart : outPortConnectionParts) {
// get the in-port-node part of the connection and check
AbstractPortEditPart inPortPart = null;
if (connectionPart.getTarget() != null && ((AbstractPortEditPart) connectionPart.getTarget()).isInPort()) {
inPortPart = (AbstractPortEditPart) connectionPart.getTarget();
} else if (connectionPart.getSource() != null) {
inPortPart = (AbstractPortEditPart) connectionPart.getSource();
}
if (inPortPart != null && isPartInList(inPortPart.getParent(), parts)) {
result.add(connectionPart);
}
}
}
}
return result.toArray(new ConnectionContainerEditPart[result.size()]);
}
Aggregations