2016-06-15 52 views
0

我想清除所有節點和連接中的Zest圖,以便我可以使用新的節點和連接重新繪製Graph。要意識到,我寫了下面的方法嘗試清除Zest圖

 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); 
    } 
} 

這崩潰我prorgam一個錯誤

異常線程 「main」 org.eclipse.swt.SWTException:小工具設置

作爲一種解決方法,我嘗試將節點和連接設置爲不可見工作,但不可見對象似乎弄亂了我的Zest佈局,所以如果有辦法實際處理節點和連接,我會這樣做。

以下是錯誤按摩

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) 

編輯:感謝巴茲我發現我錯了。鼠標監聽器不得不在我之前設置的圖形節點中搜索文本,所以當然程序必須崩潰。我改變了我的代碼以避免它,現在Baz提出的方法可以完美運行。

+0

你吃過看看這個:https://www.eclipse.org/forums/index.php/t/79464/? – Baz

+0

我用一個很好的例子更新了我的答案。 – Baz

回答

0

檢查isDisposed()調用dispose()之前將避免這個問題:

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(); 
    } 
} 

這是一個測試的例子,與選擇的節點,即使工作得很好:

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(); 
} 
+0

這是不該做的。我想我的問題可能是我想要處理的GraphNode被選中。有沒有辦法取消選擇graphNode? – hanneshelge

+0

嘗試'Graph#setSelection(null)'或'Graph#setSelection(new GraphItem [0])'。 – Baz

+0

使用.isSelected()進行檢查我發現沒有選定的GraphNode,因此我可以將其作爲問題排除。 我也發現處理連接不會產生任何問題。然而,處置GraphNodes仍然讓我的部件被處置錯誤 – hanneshelge