2

I am having some trouble dealing with pick events on JUNG graphs. I have been using the GraphMouseListener interface to listen for clicks on vertices, but now I need to add support for picking edges, and I can't seem to find a way to do it on the documentation.

Any pointers?

Thank you

pnsilva
  • 655
  • 1
  • 9
  • 20
  • Refer to the `GraphEditorDemo.java` example or other examples as mentioned in my post [http://stackoverflow.com/questions/8226624/how-do-i-use-jung2-in-a-mvc-gui](http://stackoverflow.com/questions/8226624/how-do-i-use-jung2-in-a-mvc-gui) – ee. Dec 08 '11 at 10:04
  • Thanks for reply. How can I override the clicking on edges? I didn't find any examples showing how to do this. – pnsilva Dec 08 '11 at 19:42
  • There are some examples for picking edges, even in the `GraphEditorDemo.java`...but you need to change the mouse mode from the mouse mode list to picking mode. But, if your desire is to have a picking behavior ala Photoshop, then it doesn't behave like one. It just allows you to pick an edge and listen to its event. – ee. Dec 09 '11 at 00:56
  • But what listener lets me implement the edge picking? I can see GraphMouseListener in the samples, but I can only pick vertices with these. – pnsilva Dec 09 '11 at 22:14

2 Answers2

1

Solved it by adding item listeners to the picked states of vertices and edges:

visualizationViewer.getPickedEdgeState().addItemListener(new EdgePickListener());
pnsilva
  • 655
  • 1
  • 9
  • 20
0

You can also create your own MouseEdgeListenerTranslator:

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;

import edu.uci.ics.jung.algorithms.layout.GraphElementAccessor;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.visualization.VisualizationViewer;

public class MouseEdgeListenerTranslator<V,E> extends MouseAdapter {

    private VisualizationViewer<V,E> vv;
    private GraphEdgeMouseListener<E> geml;

    public MouseEdgeListenerTranslator(GraphEdgeMouseListener<E> geml, VisualizationViewer<V,E> vv){
        this.geml = geml;
        this.vv = vv;
    }

    private E getEdge(Point2D point){
        Point2D p = point;
        GraphElementAccessor<V,E> pickSupport = vv.getPickSupport();
        Layout<V,E> layout = vv.getGraphLayout();
        E e = null;
        if(pickSupport != null){
            e = pickSupport.getEdge(layout, p.getX(), p.getY());
        }
        return e;
    }

    public void mouseClicked(MouseEvent me){
        E e = getEdge(me.getPoint());
        if(e != null){
            geml.graphClicked(e, me);
        }
    }

    public void mousePressed(MouseEvent me){
        E e = getEdge(me.getPoint());
        if(e != null){
            geml.graphPressed(e, me);
        }
    }

    public void mouseReleased(MouseEvent me){
        E e = getEdge(me.getPoint());
        if(e != null){
            geml.graphReleased(e, me);
        }
    }

}    

and then add it to your own subclass of VisualizationViewer in a GraphEdgeMouseListener:

import edu.uci.ics.jung.visualization.VisualizationModel;
import edu.uci.ics.jung.visualization.VisualizationViewer;

public class MyVisualizationViewer<V,E> extends VisualizationViewer<V,E> {

    public MyVisualizationViewer(VisualizationModel<V,E> visualizationModel, Dimension size){
         super(visualizationModel, size);
     }

     public void addGraphEdgeMouseListener(GraphEdgeMouseListener<E> geml){
         addMouseListener(new MouseEdgeListenerTranslator<V,E>(geml, this));
    }

}

and

import java.awt.event.MouseEvent; 

public interface GraphEdgeMouseListener<E> {

    void graphClicked(E e, MouseEvent me);
    void graphPressed(E e, MouseEvent me);
    void graphReleased(E e, MouseEvent me);

}