4

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.

Fadhlie Ikram
  • 119
  • 2
  • 14

4 Answers4

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

thats the one from your 1st code

this is the 2nd part

/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));
Fadhlie Ikram
  • 119
  • 2
  • 14
  • Almost there but you have forgotten to include the method that show the popup menu `popup.show(vv, e.getX(), e.getY());` – ecle May 22 '12 at 17:16
1

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.

ecle
  • 3,952
  • 1
  • 18
  • 22
  • btw, how can i read the java file from jung library? i mean, before, i learn C++(visual basic), and i can rightclick at any function, and open the source file to see how things work under the hood(the code for that function). I dont know how i can do the same with java(im using eclipse)? it says source not found. – Fadhlie Ikram May 22 '12 at 07:21
  • also, the mouse popup dowest work, it run, but doest pop up when i press right-click. – Fadhlie Ikram May 22 '12 at 08:34
  • *update it seems that there are event happening, because i was able to print out dummy statement that i set in the console. but the actual menu still didnt appear. what could be the problem? – Fadhlie Ikram May 22 '12 at 14:38
  • @Joshua O'Madadhain final String pickV = pickSupport.getVertex(vv.getGraphLayout(),ivp.getX(), ivp.getY()); if(pickV != null) { System.out.println("pickVisnotNull"); popup.add(new AbstractAction("Add New") { – Fadhlie Ikram May 22 '12 at 14:51
  • Sorry, I will update my post to show where the example class above should be used – ecle May 22 '12 at 15:14
  • http://stackoverflow.com/questions/8226624/how-do-i-use-jung2-in-a-mvc-gui/8229383#8229383 – ecle May 22 '12 at 15:24
  • If you don't want to compile from maven sources, you can add `jung-samples-x.y.z.jar` as a referenced library under Referenced Libraries in your Eclipse project. Open the project's Build Path, go to Libraries tab and modify the given reference to set its source attachment with `jung-samples-x.y.z-sources.jar` – ecle May 22 '12 at 15:31
  • im sorry, but im lost after the go to library tab. what actually should i do next? – Fadhlie Ikram May 22 '12 at 15:46
  • Rephrased: Open the project's Build Path, go to Libraries tab, select `jung-samples-x.y.z.jar` from the main tree (if you have added the jar), open its sub-tree to set its source attachment with `jung-samples-x.y.z-sources.jar` – ecle May 22 '12 at 15:50
  • See my updated my post to use `PopupGraphMousePlugin` class with `AbstractModalGraphMouse` class since I've missed that one in the original post – ecle May 22 '12 at 15:56
  • Or, please show what you have tried (by posting it into your existing post or create a new post to avoid confusion) – ecle May 22 '12 at 15:57
  • Okay. will try to update my code, and will get back to you after i get the result. thanks for the help! really appreciate it! btw i dont know how to post the code here. is there advance option like the forum which provide simple text formatting and tags. – Fadhlie Ikram May 22 '12 at 16:02
0

This isn't particularly a JUNG question--the canvas is just a standard-issue Swing canvas with some additional capability grafted on, and you're not clicking on a graph element--but GraphEditorDemo and PluggableRendererDemo demonstrate something similar.

Joshua O'Madadhain
  • 2,704
  • 1
  • 14
  • 18
  • yes, i already see GraphEditorDemo, but it seems the Create Vertex pop up menu was not in the same file. I couldn't find where in the code, the implementation of "Create Vertex" jpopup menu. – Fadhlie Ikram May 13 '12 at 09:17
  • btw, how can i read the java file from jung library? i mean, before, i learn C++(visual basic), and i can rightclick at any function, and open the source file to see how things work under the hood(the code for that function). I dont know how i can do the same with java(im using eclipse)? it says source not found. – Fadhlie Ikram May 22 '12 at 07:22
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)
Fadhlie Ikram
  • 119
  • 2
  • 14
  • @eee could you please check why this doest work when i right-clicked on canvas. The dummy text didnt shows. Code is from PluggableRendererDemo that im trying to modify. – Fadhlie Ikram Jun 06 '12 at 08:34
  • @Joshua O'Madadhain could you please check the code why it doesnt work when i clicked on the canvas? – Fadhlie Ikram Jun 06 '12 at 08:36