1 package org.catacomb.graph.gui; 2 3 import org.catacomb.be.Position; 4 import org.catacomb.interlish.structure.ContextMenu; 5 import org.catacomb.interlish.structure.SelectionActor; 6 import org.catacomb.report.E; 7 8 import javax.swing.JPopupMenu; 9 import javax.swing.JMenuItem; 10 11 import java.awt.event.ActionEvent; 12 import java.awt.event.ActionListener; 13 14 15 public class GraphContextMenu implements ContextMenu { 16 17 WorldCanvas canvas; 18 19 JPopupMenu pMenu; 20 21 SelectionActor handler; 22 23 int nel = 0; 24 25 public GraphContextMenu(WorldCanvas wc) { 26 canvas = wc; 27 pMenu = new JPopupMenu(); 28 } 29 30 31 public void itemSelected(String s) { 32 if (handler == null) { 33 E.warning("no handler for event " + s); 34 35 } else { 36 handler.selectionAction(this, s); 37 } 38 } 39 40 41 public void setContext(String[] options, SelectionActor h) { 42 pMenu.removeAll(); 43 nel = 0; 44 handler = h; 45 for (String s : options) { 46 if (s.equals("---")) { 47 pMenu.addSeparator(); 48 } else { 49 JMenuItem jmi = new JMenuItem(s); 50 jmi.addActionListener(new GCMHandler(this, s)); 51 pMenu.add(jmi); 52 nel++; 53 } 54 } 55 } 56 57 58 public void showAt(Position pos) { 59 if (nel > 0) { 60 int[] ixy = canvas.getIntPosition(pos); 61 pMenu.show(canvas, ixy[0], ixy[1]); 62 } 63 } 64 65 66 67 68 class GCMHandler implements ActionListener { 69 GraphContextMenu gcm; 70 String svalue; 71 GCMHandler(GraphContextMenu v, String s) { 72 gcm = v; 73 svalue = s; 74 } 75 76 public void actionPerformed(ActionEvent e) { 77 gcm.itemSelected(svalue); 78 79 } 80 81 82 } 83 84 85 }