001package jmri.util; 002 003import java.awt.Frame; 004import java.awt.event.ActionEvent; 005import java.util.List; 006import javax.swing.AbstractAction; 007import javax.swing.JCheckBoxMenuItem; 008import javax.swing.JMenu; 009import javax.swing.JMenuItem; 010import javax.swing.JSeparator; 011import javax.swing.event.MenuEvent; 012import jmri.util.swing.WindowInterface; 013 014/** 015 * Creates a menu showing all open windows and allows to bring one in front 016 * 017 * @author Giorgio Terdina Copyright 2008 018 */ 019public class WindowMenu extends JMenu implements javax.swing.event.MenuListener { 020 021 private Frame parentFrame; // Keep note of the window containing the menu 022 private List<JmriJFrame> framesList; // Keep the list of windows, in order to find out which window was selected 023 024 public WindowMenu(WindowInterface wi) { 025 super(Bundle.getMessage("MenuWindow")); 026 parentFrame = wi.getFrame(); 027 addMenuListener(this); 028 } 029 030 @Override 031 public void menuSelected(MenuEvent e) { 032 String windowName; 033 framesList = JmriJFrame.getFrameList(); 034 removeAll(); 035 036 add(new AbstractAction(Bundle.getMessage("MenuItemMinimize")) { 037 @Override 038 public void actionPerformed(ActionEvent e) { 039 // the next line works on Java 2, but not 1.1.8 040 if (parentFrame != null) { 041 parentFrame.setState(Frame.ICONIFIED); 042 } 043 } 044 }); 045 add(new JSeparator()); 046 047 int framesNumber = framesList.size(); 048 for (int i = 0; i < framesNumber; i++) { 049 JmriJFrame iFrame = framesList.get(i); 050 windowName = iFrame.getTitle(); 051 if (windowName.equals("")) { 052 windowName = "Untitled"; 053 } 054 JCheckBoxMenuItem newItem = new JCheckBoxMenuItem(new AbstractAction(windowName) { 055 @Override 056 public void actionPerformed(ActionEvent e) { 057 JMenuItem selectedItem = (JMenuItem) e.getSource(); 058 // Since different windows can have the same name, look for the position of the selected menu item 059 int itemCount = getItemCount(); 060 // Skip possible other items at the top of the menu (for example, "Minimize") 061 int firstItem = itemCount - framesList.size(); 062 for (int i = firstItem; i < itemCount; i++) { 063 if (selectedItem == getItem(i)) { 064 i -= firstItem; 065 // Retrieve the corresponding window 066 if (i < framesList.size()) { // "i" should always be < framesList.size(), but it's better to make sure 067 framesList.get(i).setVisible(true); 068 framesList.get(i).setExtendedState(Frame.NORMAL); 069 return; 070 } 071 } 072 } 073 } 074 }); 075 if (iFrame == parentFrame) { 076 newItem.setState(true); 077 } 078 add(newItem); 079 } 080 } 081 082 @Override 083 public void menuDeselected(MenuEvent e) { 084 } 085 086 @Override 087 public void menuCanceled(MenuEvent e) { 088 } 089 090}