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("ButtonClose")) {
037            @Override
038            public void actionPerformed(ActionEvent e) {
039                if (parentFrame != null) {
040                    parentFrame.dispatchEvent(
041                              new java.awt.event.WindowEvent(parentFrame, 
042                                        java.awt.event.WindowEvent.WINDOW_CLOSING));
043                 }
044            }    
045        });
046        add(new AbstractAction(Bundle.getMessage("MenuItemMinimize")) {
047            @Override
048            public void actionPerformed(ActionEvent e) {
049                // the next line works on Java 2, but not 1.1.8
050                if (parentFrame != null) {
051                    parentFrame.setState(Frame.ICONIFIED);
052                }
053            }
054        });
055        add(new JSeparator());
056
057        int framesNumber = framesList.size();
058        for (int i = 0; i < framesNumber; i++) {
059            JmriJFrame iFrame = framesList.get(i);
060            windowName = iFrame.getTitle();
061            if (windowName.equals("")) {
062                windowName = "Untitled";
063            }
064            JCheckBoxMenuItem newItem = new JCheckBoxMenuItem(new AbstractAction(windowName) {
065                @Override
066                public void actionPerformed(ActionEvent e) {
067                    JMenuItem selectedItem = (JMenuItem) e.getSource();
068                    // Since different windows can have the same name, look for the position of the selected menu item
069                    int itemCount = getItemCount();
070                    // Skip possible other items at the top of the menu (for example, "Minimize")
071                    int firstItem = itemCount - framesList.size();
072                    for (int i = firstItem; i < itemCount; i++) {
073                        if (selectedItem == getItem(i)) {
074                            i -= firstItem;
075                            // Retrieve the corresponding window
076                            if (i < framesList.size()) { // "i" should always be < framesList.size(), but it's better to make sure
077                                framesList.get(i).setVisible(true);
078                                framesList.get(i).setExtendedState(Frame.NORMAL);
079                                return;
080                            }
081                        }
082                    }
083                }
084            });
085            if (iFrame == parentFrame) {
086                newItem.setState(true);
087            }
088            add(newItem);
089        }
090    }
091
092    @Override
093    public void menuDeselected(MenuEvent e) {
094    }
095
096    @Override
097    public void menuCanceled(MenuEvent e) {
098    }
099
100}