0

I am trying to clear a Zest Graph from all nodes and connections, so that I can redraw the Graph with new Nodes and Connections. To realize that I wrote the following method

     public void clearGraph( Graph graph ) {        
     Object[] objects = graph.getConnections().toArray() ;          
         for (int i = 0 ; i < objects.length; i++){
             GraphConnection graCon = (GraphConnection) objects[i];              
             graCon.dispose();
             //graCon.setVisible(false);
         }           
     objects = graph.getNodes().toArray();      
     for ( int i = 0 ; i < objects.length; i++){
             GraphNode graNode = (GraphNode) objects[i];
             graNode.dispose();
            //graNode.setVisible(false);
     }
}

This crashes my prorgam with an error

Exception in thread "main" org.eclipse.swt.SWTException: Widget is disposed

As a workaround I tried to set the Nodes and Connections to invisible, that workes, but the invisible objects seem to mess up my Zest Layout so if there is a way to actually dispose the Nodes and Connections I would preffer that way.

Here is the Error Massage

Exception in thread "main" org.eclipse.swt.SWTException: Widget is disposed
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.widgets.Widget.error(Unknown Source)
at org.eclipse.swt.widgets.Widget.checkWidget(Unknown Source)
at org.eclipse.swt.widgets.Item.getText(Unknown Source)
at com.mycom.timelineview.views.IndicatorFactorVisualisationView$2.mouseDoubleClick(IndicatorFactorVisualisationView.java:221)
at org.eclipse.swt.widgets.TypedListener.handleEvent(Unknown Source)
at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
at com.mycom.timelineview.views.IndicatorFactorVisualisationView.indicatorFactorWindow(IndicatorFactorVisualisationView.java:249)
at com.mycom.timelineview.views.IndicatorFactorVisualisationView.<init>(IndicatorFactorVisualisationView.java:71)
at com.mycom.timelineview.views.SpiderWebMouseListener.chartMouseClicked(SpiderWebMouseListener.java:102)
at org.jfree.experimental.chart.swt.ChartComposite.mouseDown(ChartComposite.java:1621)
at org.eclipse.swt.widgets.TypedListener.handleEvent(Unknown Source)
at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
at com.mycom.timelineview.views.SpiderWebView.createPartControl1(SpiderWebView.java:622)
at com.mycom.timelineview.views.InformationPlatformAppView2$7.handleEvent(InformationPlatformAppView2.java:628)
at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
at com.mycom.timelineview.views.InformationPlatformAppView2.main(InformationPlatformAppView2.java:1330)

EDIT: Thanks to Baz I found my mistake. The Mouse listener had to search for text in a graph node I disposed before so of course the programm had to crash. I changed my code to avoid it and now the method Baz proposed runs perfectly.

1 Answers1

0

Checking isDisposed() before calling dispose() will avert this problem:

public void clearGraph( Graph graph )
{       
    Object[] objects = graph.getConnections().toArray() ;           
    for (int i = 0 ; i < objects.length; i++)
    {
        GraphConnection graCon = (GraphConnection) objects[i];
        if(!graCon.isDisposed())
            graCon.dispose();
    }            

    objects = graph.getNodes().toArray();       
    for (int i = 0; i < objects.length; i++)
    {
        GraphNode graNode = (GraphNode) objects[i];
        if(!graNode.isDisposed())
            graNode.dispose();
    }
}

Here's a test example that works just fine even with selected nodes:

public static void main(String[] args)
{
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Stackoverflow");
    shell.setLayout(new GridLayout(1, false));

    Graph g = new Graph(shell, SWT.NONE);
    g.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    Random random = new Random(System.currentTimeMillis());

    final List<GraphNode> nodes = new ArrayList<>();
    for (int i = 0; i < 10; i++)
    {
        GraphNode node = new GraphNode(g, SWT.NONE);
        node.setText("TEST");
        node.setLocation(random.nextInt(400), random.nextInt(400));
        nodes.add(node);
    }

    for (int i = 0; i < 50; i++)
    {
        GraphNode source;
        GraphNode target;

        do
        {
            source = nodes.get(random.nextInt(nodes.size()));
            target = nodes.get(random.nextInt(nodes.size()));
        } while (source.equals(target));

        new GraphConnection(g, SWT.NONE, source, target);
    }

    Button clear = new Button(shell, SWT.NONE);
    clear.setText("Clear");
    clear.addListener(SWT.Selection, e -> {
        for(GraphNode node : nodes)
        {
            node.dispose();
        }
        nodes.clear();
    });
    clear.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));

    shell.pack();
    shell.open();

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}
Baz
  • 36,440
  • 11
  • 68
  • 94
  • That dosn't do it. I guess my problem might be that the GraphNode I want to dispose is selected. Is there a way to unselect a graphNode? – hanneshelge Jun 15 '16 at 13:21
  • Try `Graph#setSelection(null)` or `Graph#setSelection(new GraphItem[0])`. – Baz Jun 15 '16 at 13:27
  • checking with .isSelected() I found out that there is no selected GraphNode, so I can rule that out as a problem. I also found out that disposing the connections dosn't make any problems. However disposing the GraphNodes still gets me the widget is disposed error – hanneshelge Jun 15 '16 at 13:35
  • @hanneshelge Can you please add the whole stacktrace to your question? – Baz Jun 15 '16 at 13:39
  • @hanneshelge Also, what happens if you just remove the nodes, does it work then and do the connections go as well? – Baz Jun 15 '16 at 13:42
  • If I do only dispose the nodes and not the connections I get the same crash. disposing the connections seems fine, but disposing the nodes the same way gets me a crash – hanneshelge Jun 15 '16 at 13:50
  • @hanneshelge What are you calling `getText()` on in your mouse double click listener? – Baz Jun 15 '16 at 13:56