001package jmri.jmrit.jython; 002 003import java.awt.Component; 004import java.awt.event.ActionEvent; 005import java.awt.event.ActionListener; 006import java.awt.event.MouseAdapter; 007import java.awt.event.MouseEvent; 008import java.util.ResourceBundle; 009import javax.swing.JMenuItem; 010import javax.swing.JPopupMenu; 011 012public class JynstrumentPopupMenu extends JPopupMenu { 013 014 private static final ResourceBundle jythonBundle = ResourceBundle.getBundle("jmri/jmrit/jython/Bundle"); 015 016 Jynstrument jynstrument; // The jynstrument itself 017 018 public JynstrumentPopupMenu(Jynstrument it) { 019 super(it.getName()); 020 jynstrument = it; 021 initMenu(); 022 it.addMouseListener(new MouseAdapter() { 023 @Override 024 public void mousePressed(MouseEvent e) { 025 maybeShowPopup(e); 026 } 027 028 @Override 029 public void mouseReleased(MouseEvent e) { 030 maybeShowPopup(e); 031 } 032 033 private void maybeShowPopup(MouseEvent e) { 034 Component cmp = e.getComponent(); 035 while ((cmp != null) && (!(cmp instanceof Jynstrument))) { 036 cmp = cmp.getParent(); 037 } 038 if (cmp == null) { 039 return; 040 } 041 Jynstrument it = (Jynstrument) cmp; 042 if (e.isPopupTrigger()) { 043 it.getPopUpMenu().show(e.getComponent(), e.getX(), e.getY()); 044 } 045 } 046 }); 047 } 048 049 private void initMenu() { 050 // Quit option 051 JMenuItem quitMenuItem = new JMenuItem(jythonBundle.getString("JynstrumentPopupMenuQuit")); 052 quitMenuItem.addActionListener(new ActionListener() { 053 @Override 054 public void actionPerformed(ActionEvent e) { 055 jynstrument.exit(); 056 jynstrument = null; 057 } 058 }); 059 add(quitMenuItem); 060 // Edit option 061/* JMenuItem editMenuItem = new JMenuItem(jythonBundle.getString("JynstrumentPopupMenuEdit")); 062 editMenuItem.addActionListener(new ActionListener() { 063 public void actionPerformed(ActionEvent e) { 064 // if (!java.awt.Desktop.isDesktopSupported()) //TODO: Need Java 6 065 // return; 066 log.debug("Not implemented"); 067 } 068 } ); 069 editMenuItem.setEnabled(false); 070 add(editMenuItem); 071 // Reload option 072 JMenuItem reloadMenuItem = new JMenuItem(jythonBundle.getString("JynstrumentPopupMenuReload")); 073 reloadMenuItem.addActionListener(new ActionListener() { 074 public void actionPerformed(ActionEvent e) { 075 log.debug("Not implemented"); // TODO 076 } 077 } ); 078 reloadMenuItem.setEnabled(false); 079 add(reloadMenuItem);*/ 080 // Debug option 081 add(new jmri.jmrit.jython.JythonWindow(jythonBundle.getString("JynstrumentPopupMenuDebug"))); 082 // A separator to differentiate Jynstrument private menu items 083 addSeparator(); 084 } 085}