The following code will create a popup menu by right-clicking on the top or on the canvas ...
protected class PopupGraphMousePlugin extends AbstractPopupGraphMousePlugin implements MouseListener { public PopupGraphMousePlugin() { this(MouseEvent.BUTTON3_MASK); } public PopupGraphMousePlugin(int modifiers) { super(modifiers); } 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 {
Add the class to the AbstractModalGraphMouse
class, which the mouse processes to make it work:
private AbstractModalGraphMouse graphMouse; ... graphMouse = new DefaultModalGraphMouse<Object, Object>(); vvMap.setGraphMouse(graphMouse); graphMouse.add(new PopupGraphMousePlugin());
The codes above work. But if you use different user data for common parts, you need to change the codes to suit your design.
source share