I want to create a pop-up menu, which will appear if i right clicked on the canvas . How can I do this? which function should i modify? Any help would be appreciated.
Following code will create a popup menu when right-clicking either on the vertex or on the canvas...
/**
* a GraphMousePlugin that offers popup
* menu support
*/
protected class PopupGraphMousePlugin extends AbstractPopupGraphMousePlugin
implements MouseListener {
public PopupGraphMousePlugin() {
this(MouseEvent.BUTTON3_MASK);
}
public PopupGraphMousePlugin(int modifiers) {
super(modifiers);
}
/**
* If this event is over a station (vertex), pop up a menu to
* allow the user to perform a few actions; else, pop up a menu over the layout/canvas
*
* @param e
*/
protected void handlePopup(MouseEvent e) {
final VisualizationViewer<GeoLocation.Station,GeoLocation.Link> vv =
(VisualizationViewer<GeoLocation.Station,GeoLocation.Link>)e.getSource();
final Point2D p = e.getPoint();
final Point2D ivp = p;
GraphElementAccessor<GeoLocation.Station,GeoLocation.Link> pickSupport = vv.getPickSupport();
if(pickSupport != null) {
JPopupMenu popup = new JPopupMenu();
final GeoLocation.Station station = pickSupport.getVertex(vv.getGraphLayout(), ivp.getX(), ivp.getY());
if(station != null) {
boolean isRadio = station.getParentSet().contains(STATION_IDENTIFIER_KEY);
if(isRadio)
if (station.getId().equalsIgnoreCase(SneakPeek.getUsername())){
String follow = "Follow " + station.getId();
if (followLocal){
follow = "Do not follow " + station.getId();
}
else {
follow = "Follow " + station.getId();
}
popup.add(new AbstractAction("<html><center>" + follow) {
public void actionPerformed(ActionEvent e) {
followLocal = !followLocal;
}
});
}
if(popup.getComponentCount() > 0) {
popup.show(vv, e.getX(), e.getY());
}
}
}
else { //to pop-up over the canvas/layout rather than over the station
popup.add(new AbstractAction("Create Unit") {
public void actionPerformed(ActionEvent e) {
//do something here
}
});
if(popup.getComponentCount() > 0) {
popup.show(vv, e.getX(), e.getY());
}
}
}
}
Add the class into an AbstractModalGraphMouse
class which handles mouse listeners to make it work:
private AbstractModalGraphMouse graphMouse;
...
graphMouse = new DefaultModalGraphMouse<Object, Object>();
vvMap.setGraphMouse(graphMouse);
graphMouse.add(new PopupGraphMousePlugin());
The codes above do work. But, if you use different user data for the generic parts, then you need to modify the codes to suit your design.