001package apps;
002
003import java.awt.event.ActionEvent;
004import java.util.EventObject;
005import java.util.ResourceBundle;
006
007import javax.swing.*;
008import javax.swing.text.DefaultEditorKit;
009
010import apps.jmrit.DebugMenu;
011import apps.plaf.macosx.Application;
012
013import jmri.jmrit.ToolsMenu;
014import jmri.jmrit.decoderdefn.PrintDecoderListAction;
015import jmri.jmrit.display.PanelMenu;
016import jmri.jmrit.operations.OperationsMenu;
017import jmri.jmrit.roster.swing.RosterMenu;
018import jmri.jmrix.ActiveSystemsMenu;
019import jmri.util.HelpUtil;
020import jmri.util.SystemType;
021import jmri.util.WindowMenu;
022import jmri.util.swing.WindowInterface;
023
024/**
025  * Create the main menu for PanelPro and related apps.  Includes opening PanelPro from
026  * DecoderPro3.
027  * <p>
028  * Redundant menu code was removed from {@link apps.Apps} and {@link apps.AppsLaunchFrame}.
029  *
030  * @author Dave Sand Copyright (C) 2021
031  */
032public class AppsMainMenu {
033
034    static final ResourceBundle rb = ResourceBundle.getBundle("jmri.jmrit.jython.Bundle");      // Link for script menu items  // NOI18N
035    static Action prefsAction;
036
037    public AppsMainMenu() {
038    }
039
040    /**
041    * Add menus to a menu bar.
042    * <p>
043    * This does not include the development menu.
044    *
045    * @param menuBar The existing menu bar
046    * @param wi      The WindowInterface to associate actions in menus with
047    * @param pane    The JPanel to associate actions in menus with
048    * @param windowHelpID The the help id to be assigned to Help / Window Help...
049    */
050    static protected void createMenus(JMenuBar menuBar, WindowInterface wi, JPanel pane, String windowHelpID) {
051        fileMenu(menuBar, wi);
052        editMenu(menuBar, wi);
053        toolsMenu(menuBar, wi);
054        rosterMenu(menuBar, wi, pane);
055        panelMenu(menuBar, wi);
056        scriptMenu(menuBar, wi);
057        // check to see if operations should be in the main menu
058        if (jmri.jmrit.operations.setup.Setup.isMainMenuEnabled()) {
059            operationsMenu(menuBar, wi);
060        }
061        systemsMenu(menuBar, wi);
062        debugMenu(menuBar, wi, pane);
063        menuBar.add(new WindowMenu(wi));
064        helpMenu(menuBar, wi, pane, windowHelpID);
065    }
066
067    static private void fileMenu(JMenuBar menuBar, WindowInterface wi) {
068        JMenu fileMenu = new JMenu(Bundle.getMessage("MenuFile"));  // NOI18N
069        menuBar.add(fileMenu);
070
071        fileMenu.add(new jmri.configurexml.LoadXmlUserAction(Bundle.getMessage("FileMenuItemLoad")));  // NOI18N
072        fileMenu.add(new jmri.configurexml.StoreXmlUserAction(Bundle.getMessage("FileMenuItemStore")));  // NOI18N
073        fileMenu.add(new jmri.jmrit.revhistory.swing.FileHistoryAction(Bundle.getMessage("FileMenuItemHistory")));  // NOI18N
074
075        fileMenu.add(new JSeparator());
076
077        fileMenu.add(new PrintDecoderListAction(Bundle.getMessage("MenuPrintDecoderDefinitions"), wi.getFrame(), false));  // NOI18N
078        fileMenu.add(new PrintDecoderListAction(Bundle.getMessage("MenuPrintPreviewDecoderDefinitions"), wi.getFrame(), true));  // NOI18N
079
080        // Use Mac OS X native Quit if using Aqua look and feel
081        if (!(SystemType.isMacOSX() && UIManager.getLookAndFeel().isNativeLookAndFeel())) {
082            fileMenu.add(new JSeparator());
083            fileMenu.add(new AbstractAction(Bundle.getMessage("MenuItemQuit")) {  // NOI18N
084                @Override
085                public void actionPerformed(ActionEvent e) {
086                    handleQuit();
087                }
088            });
089        }
090    }
091
092    static private void editMenu(JMenuBar menuBar, WindowInterface wi) {
093
094        JMenu editMenu = new JMenu(Bundle.getMessage("MenuEdit"));  // NOI18N
095        menuBar.add(editMenu);
096
097        // cut, copy, paste
098        AbstractAction a;
099        a = new DefaultEditorKit.CutAction();
100        a.putValue(Action.NAME, Bundle.getMessage("MenuItemCut"));  // NOI18N
101        editMenu.add(a);
102        a = new DefaultEditorKit.CopyAction();
103        a.putValue(Action.NAME, Bundle.getMessage("MenuItemCopy"));  // NOI18N
104        editMenu.add(a);
105        a = new DefaultEditorKit.PasteAction();
106        a.putValue(Action.NAME, Bundle.getMessage("MenuItemPaste"));  // NOI18N
107        editMenu.add(a);
108
109        // prefs
110        prefsAction = new apps.gui3.tabbedpreferences.TabbedPreferencesAction(Bundle.getMessage("MenuItemPreferences"));  // NOI18N
111
112        // Put prefs in Apple's prefered area on Mac OS X
113        if (SystemType.isMacOSX()) {
114            Application.getApplication().setPreferencesHandler((EventObject eo) -> {
115                prefsAction.actionPerformed(null);
116            });
117        }
118        // Include prefs in Edit menu if not on Mac OS X or not using Aqua Look and Feel
119        if (!SystemType.isMacOSX() || !UIManager.getLookAndFeel().isNativeLookAndFeel()) {
120            editMenu.addSeparator();
121            editMenu.add(prefsAction);
122        }
123
124    }
125
126    static private void toolsMenu(JMenuBar menuBar, WindowInterface wi) {
127        menuBar.add(new ToolsMenu(Bundle.getMessage("MenuTools")));  // NOI18N
128    }
129
130    /**
131     * Add a script menu to the main menu bar.
132     *
133     * @param menuBar the menu bar to add the script menu to
134     * @param wi      the window interface containing menuBar
135     */
136    static private void scriptMenu(JMenuBar menuBar, WindowInterface wi) {
137        JMenu scriptMenu = new JMenu(rb.getString("MenuScripting"));  // NOI18N
138        scriptMenu.add(new jmri.jmrit.jython.RunJythonScript(rb.getString("MenuItemScript")));  // NOI18N
139        scriptMenu.add(new jmri.jmrit.automat.monitor.AutomatTableAction(rb.getString("MenuItemMonitor")));  // NOI18N
140        scriptMenu.add(new jmri.jmrit.jython.JythonWindow(rb.getString("MenuItemScriptLog")));  // NOI18N
141        scriptMenu.add(new jmri.script.swing.InputWindowAction(rb.getString("MenuItemScriptInput")));  // NOI18N
142        menuBar.add(scriptMenu);
143    }
144
145    static private void operationsMenu(JMenuBar menuBar, WindowInterface wi) {
146        menuBar.add(new OperationsMenu());
147    }
148
149    static private void rosterMenu(JMenuBar menuBar, WindowInterface wi, JPanel pane) {
150        menuBar.add(new RosterMenu(Bundle.getMessage("MenuRoster"), RosterMenu.MAINMENU, pane));  // NOI18N
151    }
152
153    static private void panelMenu(JMenuBar menuBar, WindowInterface wi) {
154        menuBar.add(new PanelMenu());
155    }
156
157    /**
158     * Show only active systems in the menu bar.
159     *
160     * @param menuBar the menu to attach systems menus to
161     * @param wi      ignored, but available for overriding methods to use if
162     *                needed
163     */
164    static private void systemsMenu(JMenuBar menuBar, WindowInterface wi) {
165        ActiveSystemsMenu.addItems(menuBar);
166    }
167
168    static private void debugMenu(JMenuBar menuBar, WindowInterface wi, JPanel pane) {
169        menuBar.add(new DebugMenu(pane));
170    }
171
172//     protected void developmentMenu(JMenuBar menuBar, WindowInterface wi) {
173//         JMenu devMenu = new JMenu("Development");
174//         menuBar.add(devMenu);
175//         devMenu.add(new jmri.jmrit.symbolicprog.autospeed.AutoSpeedAction("Auto-speed tool"));
176//         devMenu.add(new JSeparator());
177//         devMenu.add(new jmri.jmrit.automat.SampleAutomatonAction("Sample automaton 1"));
178//         devMenu.add(new jmri.jmrit.automat.SampleAutomaton2Action("Sample automaton 2"));
179//         devMenu.add(new jmri.jmrit.automat.SampleAutomaton3Action("Sample automaton 3"));
180//         //devMenu.add(new JSeparator());
181//         //devMenu.add(new jmri.jmrix.serialsensor.SerialSensorAction("Serial port sensors"));
182//     }
183
184    static private void helpMenu(JMenuBar menuBar, WindowInterface wi, JPanel containedPane, String windowHelpID) {
185        // create menu and standard items
186        JMenu helpMenu = HelpUtil.makeHelpMenu(windowHelpID, true);
187
188        // use as main help menu
189        menuBar.add(helpMenu);
190    }
191
192    /**
193     * The application decided to quit, handle that.
194     */
195    static private void handleQuit() {
196        AppsBase.handleQuit();
197    }
198
199//     static private final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AppsMainMenu.class);
200}