Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 321488

Summary: [Zest] graphConnection.addListener not working
Product: [Tools] GEF Reporter: Lars Vogel <Lars.Vogel>
Component: GEF-Legacy ZestAssignee: gef-inbox <gef-inbox>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: Lars.Vogel, steeg
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Lars Vogel CLA 2010-08-02 06:07:19 EDT
The selection listener is not called if the connection is selected. Am I'm doing something wrong?

graphConnection.addListener(SWT.SELECTED, new Listener() {

			@Override
			public void handleEvent(Event event) {
				System.out.println("Selected");
			}

		});

Full coding example (View):

---------------
				
package de.vogella.zest.first;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.zest.core.widgets.Graph;
import org.eclipse.zest.core.widgets.GraphConnection;
import org.eclipse.zest.core.widgets.GraphNode;
import org.eclipse.zest.core.widgets.ZestStyles;
import org.eclipse.zest.layouts.LayoutStyles;
import org.eclipse.zest.layouts.algorithms.SpringLayoutAlgorithm;
import org.eclipse.zest.layouts.algorithms.TreeLayoutAlgorithm;

public class View extends ViewPart {
	public static final String ID = "de.vogella.zest.first.view";
	private Graph graph;
	private int layout = 1;

	public void createPartControl(Composite parent) {
		// Graph will hold all other objects
		graph = new Graph(parent, SWT.NONE);
		// Now a few nodes
		GraphNode node1 = new GraphNode(graph, SWT.NONE, "Jim");
		GraphNode node2 = new GraphNode(graph, SWT.NONE, "Jack");
		GraphNode node3 = new GraphNode(graph, SWT.NONE, "Joe");
		GraphNode node4 = new GraphNode(graph, SWT.NONE, "Bill");
		// Lets have a directed connection
		new GraphConnection(graph, ZestStyles.CONNECTIONS_DIRECTED, node1,
				node2);
		// Lets have a dotted graph connection
		new GraphConnection(graph, ZestStyles.CONNECTIONS_DOT, node2, node3);
		// Standard connection
		new GraphConnection(graph, SWT.NONE, node3, node1);
		// Change line color and line width
		GraphConnection graphConnection = new GraphConnection(graph, SWT.NONE,
				node1, node4);
		graphConnection.changeLineColor(parent.getDisplay().getSystemColor(
				SWT.COLOR_GREEN));
		// Also set a text
		graphConnection.setText("This is a text");
		graphConnection.setHighlightColor(parent.getDisplay().getSystemColor(
				SWT.COLOR_RED));
		graphConnection.setLineWidth(3);
		graphConnection.addListener(SWT.SELECTED, new Listener() {

			@Override
			public void handleEvent(Event event) {
				System.out.println("Selected");
			}

		});
		graph.setLayoutAlgorithm(new SpringLayoutAlgorithm(
				LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);

	}

	public void setLayoutManager() {
		switch (layout) {
		case 1:
			graph.setLayoutAlgorithm(new TreeLayoutAlgorithm(
					LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
			layout++;
			break;
		case 2:
			graph.setLayoutAlgorithm(new SpringLayoutAlgorithm(
					LayoutStyles.NO_LAYOUT_NODE_RESIZING), true);
			layout = 1;
			break;

		}

	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
	}
}
Comment 1 Fabian Steeg CLA 2010-08-06 22:37:23 EDT
(In reply to comment #0)

I believe events on GraphItems are not supported in general, but you could add a SelectionListener to the graph itself and check if the event's item is the connection:

graph.addSelectionListener(new SelectionListener() {
  public void widgetSelected(SelectionEvent e) {
    if(e.item == graphConnection) 
      System.out.println("Selected Connection");
  }
  //...
}
Comment 2 Lars Vogel CLA 2013-11-18 07:40:37 EST
(In reply to Fabian Steeg from comment #1)
> (In reply to comment #0)
> 
> I believe events on GraphItems are not supported in general, but you could
> add a SelectionListener to the graph itself and check if the event's item is
> the connection:
> 
> graph.addSelectionListener(new SelectionListener() {
>   public void widgetSelected(SelectionEvent e) {
>     if(e.item == graphConnection) 
>       System.out.println("Selected Connection");
>   }
>   //...
> }

Thanks. That is a good workaround. I mark this bug as fixed.