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        if (jmri.InstanceManager.getDefault(jmri.PermissionManager.class).isEnabled()) {
078            fileMenu.add(new jmri.jmrit.permission.swing.LoginAction());
079            fileMenu.add(new jmri.jmrit.permission.swing.LogoutAction());
080            fileMenu.add(new jmri.jmrit.permission.swing.ChangePasswordAction());
081            fileMenu.add(new JSeparator());
082        }
083
084        fileMenu.add(new PrintDecoderListAction(Bundle.getMessage("MenuPrintDecoderDefinitions"), wi.getFrame(), false));  // NOI18N
085        fileMenu.add(new PrintDecoderListAction(Bundle.getMessage("MenuPrintPreviewDecoderDefinitions"), wi.getFrame(), true));  // NOI18N
086
087        // Use Mac OS X native Quit if using Aqua look and feel
088        if (!(SystemType.isMacOSX() && UIManager.getLookAndFeel().isNativeLookAndFeel())) {
089            fileMenu.add(new JSeparator());
090            fileMenu.add(new AbstractAction(Bundle.getMessage("MenuItemQuit")) {  // NOI18N
091                @Override
092                public void actionPerformed(ActionEvent e) {
093                    handleQuit();
094                }
095            });
096        }
097    }
098
099    static private void editMenu(JMenuBar menuBar, WindowInterface wi) {
100
101        JMenu editMenu = new JMenu(Bundle.getMessage("MenuEdit"));  // NOI18N
102        menuBar.add(editMenu);
103
104        // cut, copy, paste
105        AbstractAction a;
106        a = new DefaultEditorKit.CutAction();
107        a.putValue(Action.NAME, Bundle.getMessage("MenuItemCut"));  // NOI18N
108        editMenu.add(a);
109        a = new DefaultEditorKit.CopyAction();
110        a.putValue(Action.NAME, Bundle.getMessage("MenuItemCopy"));  // NOI18N
111        editMenu.add(a);
112        a = new DefaultEditorKit.PasteAction();
113        a.putValue(Action.NAME, Bundle.getMessage("MenuItemPaste"));  // NOI18N
114        editMenu.add(a);
115
116        // prefs
117        prefsAction = new apps.gui3.tabbedpreferences.TabbedPreferencesAction(Bundle.getMessage("MenuItemPreferences"));  // NOI18N
118
119        // Put prefs in Apple's prefered area on Mac OS X
120        if (SystemType.isMacOSX()) {
121            Application.getApplication().setPreferencesHandler((EventObject eo) -> {
122                prefsAction.actionPerformed(null);
123            });
124        }
125        // Include prefs in Edit menu if not on Mac OS X or not using Aqua Look and Feel
126        if (!SystemType.isMacOSX() || !UIManager.getLookAndFeel().isNativeLookAndFeel()) {
127            editMenu.addSeparator();
128            editMenu.add(prefsAction);
129        }
130
131    }
132
133    static private void toolsMenu(JMenuBar menuBar, WindowInterface wi) {
134        menuBar.add(new ToolsMenu(Bundle.getMessage("MenuTools")));  // NOI18N
135    }
136
137    /**
138     * Add a script menu to the main menu bar.
139     *
140     * @param menuBar the menu bar to add the script menu to
141     * @param wi      the window interface containing menuBar
142     */
143    static private void scriptMenu(JMenuBar menuBar, WindowInterface wi) {
144        JMenu scriptMenu = new JMenu(rb.getString("MenuScripting"));  // NOI18N
145        scriptMenu.add(new jmri.jmrit.jython.RunJythonScript(rb.getString("MenuItemScript")));  // NOI18N
146        scriptMenu.add(new jmri.jmrit.automat.monitor.AutomatTableAction(rb.getString("MenuItemMonitor")));  // NOI18N
147        scriptMenu.add(new jmri.jmrit.jython.JythonWindow(rb.getString("MenuItemScriptLog")));  // NOI18N
148        scriptMenu.add(new jmri.script.swing.InputWindowAction(rb.getString("MenuItemScriptInput")));  // NOI18N
149        menuBar.add(scriptMenu);
150    }
151
152    static private void operationsMenu(JMenuBar menuBar, WindowInterface wi) {
153        menuBar.add(new OperationsMenu());
154    }
155
156    static private void rosterMenu(JMenuBar menuBar, WindowInterface wi, JPanel pane) {
157        menuBar.add(new RosterMenu(Bundle.getMessage("MenuRoster"), RosterMenu.MAINMENU, pane));  // NOI18N
158    }
159
160    static private void panelMenu(JMenuBar menuBar, WindowInterface wi) {
161        menuBar.add(new PanelMenu());
162    }
163
164    /**
165     * Show only active systems in the menu bar.
166     *
167     * @param menuBar the menu to attach systems menus to
168     * @param wi      ignored, but available for overriding methods to use if
169     *                needed
170     */
171    static private void systemsMenu(JMenuBar menuBar, WindowInterface wi) {
172        ActiveSystemsMenu.addItems(menuBar);
173    }
174
175    static private void debugMenu(JMenuBar menuBar, WindowInterface wi, JPanel pane) {
176        menuBar.add(new DebugMenu(pane));
177    }
178
179//     protected void developmentMenu(JMenuBar menuBar, WindowInterface wi) {
180//         JMenu devMenu = new JMenu("Development");
181//         menuBar.add(devMenu);
182//         devMenu.add(new jmri.jmrit.symbolicprog.autospeed.AutoSpeedAction("Auto-speed tool"));
183//         devMenu.add(new JSeparator());
184//         devMenu.add(new jmri.jmrit.automat.SampleAutomatonAction("Sample automaton 1"));
185//         devMenu.add(new jmri.jmrit.automat.SampleAutomaton2Action("Sample automaton 2"));
186//         devMenu.add(new jmri.jmrit.automat.SampleAutomaton3Action("Sample automaton 3"));
187//         //devMenu.add(new JSeparator());
188//         //devMenu.add(new jmri.jmrix.serialsensor.SerialSensorAction("Serial port sensors"));
189//     }
190
191    static private void helpMenu(JMenuBar menuBar, WindowInterface wi, JPanel containedPane, String windowHelpID) {
192        // create menu and standard items
193        JMenu helpMenu = HelpUtil.makeHelpMenu(windowHelpID, true);
194
195        // use as main help menu
196        menuBar.add(helpMenu);
197    }
198
199    /**
200     * The application decided to quit, handle that.
201     */
202    static private void handleQuit() {
203        AppsBase.handleQuit();
204    }
205
206//     static private final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AppsMainMenu.class);
207}