4

我想创建一个弹出菜单,如果我右键单击画布就会出现。我怎样才能做到这一点?我应该修改哪个功能?任何帮助,将不胜感激。

4

4 回答 4

2
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
     */
    @SuppressWarnings("unchecked")
    protected void handlePopup(MouseEvent e) {
        final VisualizationViewer<String,String> vv =(VisualizationViewer<String,String>)e.getSource();
        final Point2D p = e.getPoint();
        final Point2D ivp = p;
        JPopupMenu popup = new JPopupMenu();

        System.out.println("mouse event!");


        GraphElementAccessor<String,String> pickSupport = vv.getPickSupport();
        System.out.println("GraphElementAccessor!");
        if(pickSupport != null) {



            final String pickV = pickSupport.getVertex(vv.getGraphLayout(), ivp.getX(), ivp.getY());

            if(pickV != null) {
               System.out.println("pickVisnotNull");


               popup.add(new AbstractAction("Add New") {
                   /**
                 * 
                 */


                public void actionPerformed(ActionEvent e) {
                   System.out.println("person added");  
                   }
               });//new abstraction

            }
        }///if picksupport



    }//handlePopup(MouseEvent e)
}//PopupGraphMousePlugin

那是你第一个代码中的那个

这是第二部分

/Next, let's just build a simple mouse, to allow picking, translating, and zooming.
    AbstractModalGraphMouse gMouse = new DefaultModalGraphMouse<Object, Object>();
    vv.setGraphMouse(gMouse); //Add the mouse to our Visualization-Viewer.
    //PluggableGraphMouse pgm = new PluggableGraphMouse();
    gMouse.add(new PickingGraphMousePlugin<Object, Object>());
    //pgm.add(new TranslatingGraphMousePlugin(MouseEvent.BUTTON3_MASK));
    gMouse.add(new PopupGraphMousePlugin());
    gMouse.add(new ScalingGraphMousePlugin(new CrossoverScalingControl(), 0, 1 / 1.1f, 1.1f));
于 2012-05-22T16:24:22.583 回答
1

当右键单击顶点或画布时,以下代码将创建一个弹出菜单...

/**
 * 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处理鼠标侦听器的类中以使其工作:

private AbstractModalGraphMouse graphMouse;
...
graphMouse = new DefaultModalGraphMouse<Object, Object>();
vvMap.setGraphMouse(graphMouse);
graphMouse.add(new PopupGraphMousePlugin());

上面的代码确实有效。但是,如果您对通用零件使用不同的用户数据,那么您需要修改代码以适合您的设计。

于 2012-05-19T01:19:50.550 回答
0

这不是一个特别的 JUNG 问题——画布只是一个标准问题的 Swing 画布,移植了一些额外的功能,并且您没有单击图形元素——但是 GraphEditorDemo 和 PluggableRendererDemo 展示了类似的东西。

于 2012-05-11T17:17:36.373 回答
0
protected void handlePopup(MouseEvent e) {
        final VisualizationViewer<Integer,Number> vv = (VisualizationViewer<Integer,Number>)e.getSource();
        Point2D p = e.getPoint();

        //vv.getRenderContext().getBasicTransformer().inverseViewTransform(e.getPoint());

        GraphElementAccessor<Integer,Number> pickSupport = vv.getPickSupport();
        if(pickSupport != null) {
            System.out.println("POPUP MOUSE is not NULL!");
            final Integer v = pickSupport.getVertex(vv.getGraphLayout(), p.getX(), p.getY());
            if(v != null) {
                JPopupMenu popup = new JPopupMenu();

                popup.add(new AbstractAction("Decrease Transparency") {
                    public void actionPerformed(ActionEvent e) {
                        Double value = Math.min(1,transparency.get(v).doubleValue()+0.1);
                        transparency.put(v, value);//vertex, value

                        vv.repaint();
                    }
                });
                popup.add(new AbstractAction("Increase Transparency"){
                    public void actionPerformed(ActionEvent e) {
                        Double value = Math.max(0, 
                                transparency.get(v).doubleValue()-0.1);
                            transparency.put(v, value);

                        vv.repaint();
                    }
                });
                popup.show(vv, e.getX(), e.getY());
            } 
            else {
                final Number edge = pickSupport.getEdge(vv.getGraphLayout(), p.getX(), p.getY());
                if(edge != null) {
                    JPopupMenu popup = new JPopupMenu();
                    popup.add(new AbstractAction(edge.toString()) {
                        public void actionPerformed(ActionEvent e) {
                            System.err.println("got "+edge);
                        }

                    });//popup.add
                    popup.show(vv, e.getX(), e.getY());

                }//if edge != null)
            }



        }// if(pickSupport != null)

        //rightclicked on Canvas
        else{
            System.out.println("On Canvas!");
        }


    }//handlePopup(MouseEvent e)
于 2012-06-06T08:31:02.760 回答