001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    package graphlab.plugins.main.rightclick;
005    
006    import graphlab.graph.event.EdgeEvent;
007    import graphlab.graph.event.GraphEvent;
008    import graphlab.graph.event.VertexEvent;
009    import graphlab.graph.graph.AbstractGraphRenderer;
010    import graphlab.platform.core.AbstractAction;
011    import graphlab.platform.core.BlackBoard;
012    
013    import javax.swing.*;
014    import java.awt.event.ActionEvent;
015    import java.awt.event.ActionListener;
016    import java.awt.event.MouseEvent;
017    
018    /**
019     * @author azin azadi
020     */
021    public class PopupMenuHandler extends AbstractAction {
022    
023        /**
024         * constructor
025         *
026         * @param bb the blackboard of the action
027         */
028        public PopupMenuHandler(BlackBoard bb) {
029            super(bb);
030            listen4Event(VertexEvent.EVENT_KEY);
031            listen4Event(EdgeEvent.EVENT_KEY);
032            listen4Event(GraphEvent.EVENT_KEY);
033    
034    //        GraphPropertyEditor gpv=new GraphPropertyEditor(blackboard);
035    //        GraphPropertyEditor gpe=new GraphPropertyEditor(blackboard);
036    //        gpv.getPropertyEditor().getTable().setTableHeader(new JTableHeader());
037    //        gpe.getPropertyEditor().getTable().setTableHeader(new JTableHeader());
038    //        vertexMnu.add(gpv.getPropertyEditor().getTable());
039    //        edgeMnu.add(gpe.getPropertyEditor().getTable());
040        }
041    
042        public void performAction(String eventName, Object value) {
043    //        if (eventName ==VertexEvent.name)) {
044    //            VertexEvent vcd = blackboard.get(VertexEvent.name);
045    //            if (vcd.eventType == VertexEvent.CLICKED) {
046    //                if (vcd.mouseBtn == MouseEvent.BUTTON3) {
047    //                    Point vp = GraphPoint.createViewPoint(g, vcd.mousePos);
048    //                    vertexMnu.show((Component) vcd.v.view, vp.x, vp.y);
049    //                }
050    //            }
051    //        }
052    //        if (eventName ==EdgeEvent.name)) {
053    //            EdgeEvent ecd = blackboard.get(EdgeEvent.name);
054    //            if (ecd.eventType == EdgeEvent.CLICKED) {
055    //                if (ecd.mouseBtn == MouseEvent.BUTTON3) {
056    //                    Point ep = GraphPoint.createViewPoint(g, ecd.mousePos);
057    //                    edgeMnu.show((Component) ecd.e.view, ep.x, ep.y);
058    //                }
059    //            }
060    //        }
061    
062            if (eventName.equals(GraphEvent.EVENT_KEY)) {
063                GraphEvent ge = (GraphEvent) value;
064                if (ge.eventType == GraphEvent.CLICKED) {
065                    if (ge.mouseBtn == MouseEvent.BUTTON3) {
066                        AbstractGraphRenderer gv = blackboard.getData(AbstractGraphRenderer.EVENT_KEY);
067                        graphMnu.show(gv, (int) ge.mousePos.x, (int) ge.mousePos.y);
068                    }
069                }
070            }
071        }
072    
073        private static JPopupMenu graphMnu = new JPopupMenu();
074        private static JPopupMenu vertexMnu = new JPopupMenu();
075        private static JPopupMenu edgeMnu = new JPopupMenu();
076    
077        /**
078         * registers a popup menu that will be shown on each graph that assigned to Graph.name in blackboard (after the assignment)
079         *
080         * @param id    the string shown on mnu
081         * @param index place of it
082         * @param n     this action will be enabled(in it's group) and then the performJob will be called
083         */
084        public static void registerGraphPopupMenu(String id, int index, graphlab.platform.core.AbstractAction n, boolean forceEnable) {
085            registerPMenu(graphMnu, id, index, n, forceEnable);
086        }
087    
088        public static void registerVertexPopupMenu(String id, int index, graphlab.platform.core.AbstractAction n, boolean forceEnable) {
089            registerPMenu(vertexMnu, id, index, n, forceEnable);
090        }
091    
092        public static void registerEdgePopupMenu(String id, int index, graphlab.platform.core.AbstractAction n, boolean forceEnable) {
093            registerPMenu(edgeMnu, id, index, n, forceEnable);
094        }
095    
096        //todo: i hate the force enable, it destroys all the design...
097        private static void registerPMenu(JPopupMenu mnu, final String id, final int index, final graphlab.platform.core.AbstractAction n, final boolean forceEnable) {
098            JMenuItem item = new JMenuItem(id);
099            mnu.add(item, index);
100            mnu.validate();
101            item.addActionListener(new ActionListener() {
102                public void actionPerformed(ActionEvent e) {
103                    if (forceEnable) {
104    //                    Configuration conf = n.getBlackBoard().getData(UIEventHandler.CONF);
105    //                    conf.enableAction(n);
106                        if (n.isEnable()) {
107                            n.performAction("popup menu: " + id, null);
108                        }
109                    } else {
110                        n.performAction("popup menu: " + id, null);
111                    }
112                }
113            });
114        }
115    }