use of org.knime.core.node.workflow.NodeUIInformation in project knime-core by knime.
the class DropNodeCommand method execute.
/**
* {@inheritDoc}
*/
@Override
public void execute() {
// Add node to workflow and get the container
WorkflowManager hostWFM = getHostWFM();
try {
NodeID id = hostWFM.addNodeAndApplyContext(m_factory, m_dropContext);
m_container = hostWFM.getNodeContainer(id);
// create extra info and set it
NodeUIInformation info = NodeUIInformation.builder().setNodeLocation(m_location.x, m_location.y, -1, -1).setHasAbsoluteCoordinates(false).setSnapToGrid(m_snapToGrid).setIsDropLocation(true).build();
m_container.setUIInformation(info);
// Open the dialog. Some times.
if (m_container instanceof SingleNodeContainer && m_container.getNodeContainerState().isIdle() && m_container.hasDialog() && // and has only a variable in port
m_container.getNrInPorts() == 1) {
// if not executable and has a dialog and is fully connected
// This is embedded in a special JFace wrapper dialog
//
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
try {
WrappedNodeDialog dlg = new WrappedNodeDialog(Display.getCurrent().getActiveShell(), m_container);
dlg.open();
} catch (Exception e) {
// they need to open it manually then
}
}
});
}
} catch (Throwable t) {
// if fails notify the user
LOGGER.debug("Node cannot be created.", t);
MessageBox mb = new MessageBox(Display.getDefault().getActiveShell(), SWT.ICON_WARNING | SWT.OK);
mb.setText("Node cannot be created.");
mb.setMessage("The selected node could not be created " + "due to the following reason:\n" + t.getMessage());
mb.open();
return;
}
}
use of org.knime.core.node.workflow.NodeUIInformation in project knime-core by knime.
the class CreateMetaNodeTemplateCommand method execute.
/**
* {@inheritDoc}
*/
@Override
public void execute() {
// Add node to workflow and get the container
LoadMetaNodeTemplateRunnable loadRunnable = null;
try {
IWorkbench wb = PlatformUI.getWorkbench();
IProgressService ps = wb.getProgressService();
// this one sets the workflow manager in the editor
loadRunnable = new LoadMetaNodeTemplateRunnable(getHostWFM(), m_templateKNIMEFolder);
ps.run(false, true, loadRunnable);
MetaNodeLinkUpdateResult result = loadRunnable.getLoadResult();
m_container = (NodeContainer) result.getLoadedInstance();
if (m_container == null) {
throw new RuntimeException("No template returned by load routine, see log for details");
}
// create extra info and set it
NodeUIInformation info = NodeUIInformation.builder().setNodeLocation(m_location.x, m_location.y, -1, -1).setHasAbsoluteCoordinates(false).setSnapToGrid(m_snapToGrid).setIsDropLocation(true).build();
m_container.setUIInformation(info);
} catch (Throwable t) {
Throwable cause = t;
while ((cause.getCause() != null) && (cause.getCause() != cause)) {
cause = cause.getCause();
}
String error = "The selected node could not be created";
if (cause instanceof FileNotFoundException) {
error += " because a file could not be found: " + cause.getMessage();
MessageDialog.openError(Display.getDefault().getActiveShell(), "Node cannot be created.", error);
} else if (cause instanceof IOException) {
error += " because of an I/O error: " + cause.getMessage();
MessageDialog.openError(Display.getDefault().getActiveShell(), "Node cannot be created.", error);
} else if (cause instanceof InvalidSettingsException) {
error += " because the metanode contains invalid settings: " + cause.getMessage();
MessageDialog.openError(Display.getDefault().getActiveShell(), "Node cannot be created.", error);
} else if (cause instanceof UnsupportedWorkflowVersionException) {
error += " because the metanode version is incompatible: " + cause.getMessage();
MessageDialog.openError(Display.getDefault().getActiveShell(), "Node cannot be created.", error);
} else if ((cause instanceof CanceledExecutionException) || (cause instanceof InterruptedException)) {
LOGGER.info("Metanode loading was canceled by the user", cause);
} else {
LOGGER.error(String.format("Metanode loading failed with %s: %s", cause.getClass().getSimpleName(), cause.getMessage()), cause);
error += ": " + cause.getMessage();
MessageDialog.openError(Display.getDefault().getActiveShell(), "Node cannot be created.", error);
}
}
}
use of org.knime.core.node.workflow.NodeUIInformation in project knime-core by knime.
the class InsertHelper method reconnect.
/**
* Guesses the incoming/outgoing connections of the new node based on the connections of the old node.
* The connections are added from port 0 to port n. If no port type matches no connection is added.
* @param container of the new created node
* @param snapToGrid should new node snap to grid
* @param x coordinate of the new node
* @param y coordinate of the new node
*/
public void reconnect(final NodeContainer container, final boolean snapToGrid, final int x, final int y) {
// reset node position
NodeUIInformation info = NodeUIInformation.builder().setNodeLocation(x, y, -1, -1).setHasAbsoluteCoordinates(false).setSnapToGrid(snapToGrid).setIsDropLocation(false).build();
container.setUIInformation(info);
// skip fv port of nodes for now
int p = (container instanceof WorkflowManager) ? 0 : 1;
while (!hostWFM.canAddConnection(m_edge.getSource(), m_edge.getSourcePort(), container.getID(), p)) {
// search for a valid connection of the source node to this node
p++;
if (p >= container.getNrInPorts()) {
break;
}
}
if (p < container.getNrInPorts()) {
hostWFM.addConnection(m_edge.getSource(), m_edge.getSourcePort(), container.getID(), p);
} else if (!(container instanceof WorkflowManager)) {
// try if the fv port of nodes would work
if (hostWFM.canAddConnection(m_edge.getSource(), m_edge.getSourcePort(), container.getID(), 0)) {
hostWFM.addConnection(m_edge.getSource(), m_edge.getSourcePort(), container.getID(), 0);
}
}
// skip fv port of nodes for now;
int pout = (container instanceof WorkflowManager) ? 0 : 1;
while (!hostWFM.canAddConnection(container.getID(), pout, m_edge.getDest(), m_edge.getDestPort())) {
// search for a valid connection of this node to the destination node of the edge
pout++;
if (pout >= container.getNrOutPorts()) {
break;
}
}
if (pout < container.getNrOutPorts()) {
hostWFM.addConnection(container.getID(), pout, m_edge.getDest(), m_edge.getDestPort());
} else if (!(container instanceof WorkflowManager)) {
// try if the fv port of nodes would work
if (hostWFM.canAddConnection(container.getID(), 0, m_edge.getDest(), m_edge.getDestPort())) {
hostWFM.addConnection(container.getID(), 0, m_edge.getDest(), m_edge.getDestPort());
}
}
}
use of org.knime.core.node.workflow.NodeUIInformation in project knime-core by knime.
the class PasteFromWorkflowPersistorCommand method execute.
/**
* {@inheritDoc}
*/
@Override
public void execute() {
WorkflowManager manager = m_editor.getWorkflowManager().get();
WorkflowPersistor copyPersistor = m_clipboardObject.getCopyPersistor();
m_pastedContent = manager.paste(copyPersistor);
NodeID[] pastedNodes = m_pastedContent.getNodeIDs();
WorkflowAnnotation[] pastedAnnos = m_pastedContent.getAnnotations();
// fast lookup below
Set<NodeID> newIDs = new HashSet<NodeID>();
List<int[]> insertedElementBounds = new ArrayList<int[]>();
for (NodeID i : pastedNodes) {
NodeContainer nc = manager.getNodeContainer(i);
NodeUIInformation ui = nc.getUIInformation();
int[] bounds = ui.getBounds();
insertedElementBounds.add(bounds);
}
for (WorkflowAnnotation a : pastedAnnos) {
int[] bounds = new int[] { a.getX(), a.getY(), a.getWidth(), a.getHeight() };
insertedElementBounds.add(bounds);
}
int[] moveDist = m_shiftCalculator.calculateShift(insertedElementBounds, manager, m_clipboardObject);
// for redo-operations we need the exact same shift.
m_shiftCalculator = new FixedShiftCalculator(moveDist);
for (NodeID id : pastedNodes) {
newIDs.add(id);
NodeContainer nc = manager.getNodeContainer(id);
NodeUIInformation oldUI = nc.getUIInformation();
NodeUIInformation newUI = NodeUIInformation.builder(oldUI).translate(moveDist).build();
nc.setUIInformation(newUI);
}
for (ConnectionContainer conn : manager.getConnectionContainers()) {
if (newIDs.contains(conn.getDest()) && newIDs.contains(conn.getSource())) {
// get bend points and move them
ConnectionUIInformation oldUI = conn.getUIInfo();
if (oldUI != null) {
ConnectionUIInformation newUI = ConnectionUIInformation.builder(oldUI).translate(moveDist).build();
conn.setUIInfo(newUI);
}
}
}
for (WorkflowAnnotation a : pastedAnnos) {
a.shiftPosition(moveDist[0], moveDist[1]);
}
EditPartViewer partViewer = m_editor.getViewer();
partViewer.deselectAll();
// select the new ones....
if (partViewer.getRootEditPart().getContents() != null && partViewer.getRootEditPart().getContents() instanceof WorkflowRootEditPart) {
WorkflowRootEditPart rootEditPart = (WorkflowRootEditPart) partViewer.getRootEditPart().getContents();
rootEditPart.setFutureSelection(pastedNodes);
rootEditPart.setFutureAnnotationSelection(Arrays.asList(pastedAnnos));
}
}
use of org.knime.core.node.workflow.NodeUIInformation in project knime-core by knime.
the class HorizAlignmentCenter method doLayout.
/**
* @param wfm the manager holding the nodes to layout
* @param nodeParts the nodes to align
* @return a map with offsets for the nodes
*/
static Map<NodeContainerEditPart, Integer> doLayout(final WorkflowManagerUI wfm, final NodeContainerEditPart[] nodeParts) {
if (nodeParts.length == 0) {
return Collections.emptyMap();
}
NodeContainerEditPart[] nodes = nodeParts.clone();
// sorts by the x position
Arrays.sort(nodes, new Comparator<NodeContainerEditPart>() {
@Override
public int compare(final NodeContainerEditPart o1, final NodeContainerEditPart o2) {
NodeUIInformation ui1 = o1.getNodeContainer().getUIInformation();
NodeUIInformation ui2 = o2.getNodeContainer().getUIInformation();
if (ui1 == null || ui2 == null) {
return 0;
}
if (ui1.getBounds()[0] < ui2.getBounds()[0]) {
return -1;
} else {
return (ui1.getBounds()[0] > ui2.getBounds()[0]) ? 1 : 0;
}
}
});
// most left node is the anchor that doesn't change
HashMap<NodeContainerEditPart, Integer> offsets = new HashMap<NodeContainerEditPart, Integer>();
NodeUIInformation nui = nodes[0].getNodeContainer().getUIInformation();
if (nui == null) {
LOGGER.warn("Only nodes with location information can be aligned.");
return Collections.emptyMap();
}
int refY = nui.getBounds()[1];
for (int i = 1; /* idx 0 is anchor */
i < nodes.length; i++) {
NodeContainerUI nc = nodes[i].getNodeContainer();
NodeUIInformation ui = nc.getUIInformation();
if (ui.getBounds()[1] != refY) {
offsets.put(nodes[i], refY - ui.getBounds()[1]);
}
}
return offsets;
}
Aggregations