GO.JS获取连线的节点和删除连线和监听删除节点事件

监听连线事件:

myDiagram.addDiagramListener("LinkDrawn", function(e){});可以监听连线事件

或者:

 this.$store.state.process.myDiagram = gm(go.Diagram, "myDiagramDiv", // must name or refer to the DIV HTML element
        {
          // initialContentAlignment: go.Spot.Center, // 居中显示
          // initialAutoScale: go.Diagram.Uniform, //自动缩放,适应屏幕
          allowDrop: true, // must be true to accept drops from the Palette
          ***"LinkDrawn": this.showLinkLabel,***
          ***"SelectionDeleting": this.deleteNode,*** 
          // "LinkRelinked": this.showLinkLabel,
          // scrollsPageOnFocus: false,
          "undoManager.isEnabled": true, // enable undo & redo
          layout: gm(go.TreeLayout, {
            angle: 90,
            nodeSpacing: 10,
            layerSpacing: 40,
            // layerStyle: go.TreeLayout.LayerUniform
          })
        });

代码如下:

showLinkLabel(e) {
//获取开始节点linkdataarray中的from所代表的节点
  let startNode = e.diagram.model.findNodeDataForKey(e.subject.data.from);
//获取结束节点linkdataarray中的to所代表的节点
  let endNode = e.diagram.model.findNodeDataForKey(e.subject.data.to);
//判断节点是否符合规则(就是是否符合顺序要求)
  if ((startNode.type === 4 || startNode.type === 2) && endNode.type === 3 || startNode.type === 3 && (endNode.type === 4 || endNode.type === 2)) {
    return;
  }
  if (startNode.type > endNode.type) {
	//删除连线     e.subject.data代表当前所画的连线
    e.diagram.model.removeLinkData(e.subject.data);
    return this.$Message.error(startNode.name + "到" + endNode.name + "节点间连线错误")
  }

},

另外附删除节点事件:

deleteNode(e) {
        //获取所删除的节点
        e.diagram.selection.each(nodeOrLink => {
          if(nodeOrLink instanceof go.Node){//所选择节点
            let nodeData = nodeOrLink.data;
            let dataLoadCols = this.$store.state.process.dataCols;
            let index = dataLoadCols.findIndex(item => item.id === nodeData.id);
            if(index !== -1){
              dataLoadCols.splice(index,1);
            }
          }
        });
      },
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Eclipse SWT 实现在两个树之间绘制连线的示例代码: ```java import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Tree; import org.eclipse.swt.widgets.TreeItem; public class TreeLinkExample { private static TreeItem sourceItem = null; private static TreeItem targetItem = null; public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); Tree tree1 = new Tree(shell, SWT.BORDER); for (int i = 0; i < 5; i++) { TreeItem item = new TreeItem(tree1, SWT.NONE); item.setText("Item " + i); } Tree tree2 = new Tree(shell, SWT.BORDER); for (int i = 0; i < 5; i++) { TreeItem item = new TreeItem(tree2, SWT.NONE); item.setText("Item " + i); } Canvas canvas = new Canvas(shell, SWT.NONE); canvas.addPaintListener(new PaintListener() { @Override public void paintControl(PaintEvent e) { if (sourceItem != null && targetItem != null) { GC gc = e.gc; Rectangle sourceBounds = sourceItem.getBounds(); Rectangle targetBounds = targetItem.getBounds(); Point sourceCenter = new Point(sourceBounds.x + sourceBounds.width / 2, sourceBounds.y + sourceBounds.height / 2); Point targetCenter = new Point(targetBounds.x + targetBounds.width / 2, targetBounds.y + targetBounds.height / 2); gc.setLineWidth(2); gc.setForeground(new Color(e.display, 0, 0, 255)); gc.drawLine(sourceCenter.x, sourceCenter.y, targetCenter.x, targetCenter.y); } } }); tree1.addListener(SWT.Selection, event -> { sourceItem = (TreeItem) event.item; redrawCanvas(canvas); }); tree2.addListener(SWT.Selection, event -> { targetItem = (TreeItem) event.item; redrawCanvas(canvas); }); shell.setSize(400, 300); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } private static void redrawCanvas(Canvas canvas) { canvas.redraw(); canvas.update(); } } ``` 这个示例程序创建了两个树控件和一个 `Canvas` 控件,并在树控件上添加了 `Selection` 监听器,在监听器中记录了选中的项。同时,在 `Canvas` 上添加了 `PaintListener`,在监听器中根据选中的两个树项绘制了一条连接线。当有树项被选中时,先记录下来,然后调用 `redrawCanvas` 方法重绘 `Canvas` 控件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值