弹出菜单使用鼠标右键单击JUNG [英] Pop-up menu using mouse rightclick in JUNG
本文介绍了弹出菜单使用鼠标右键单击JUNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个弹出式菜单,如果我右键点击画布就会出现。我该怎么办?我应该修改哪些功能?
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());
}
}
}
}
将类添加到 AbstractModalGraphMouse
类,该类处理鼠标监听器以使其工作:
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.
这篇关于弹出菜单使用鼠标右键单击JUNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文