001package jmri.jmrit.roster.swing;
002
003import java.awt.Component;
004import java.awt.Frame;
005import javax.swing.AbstractAction;
006import javax.swing.JMenu;
007import jmri.jmrit.roster.CopyRosterItemAction;
008import jmri.jmrit.roster.DeleteRosterItemAction;
009import jmri.jmrit.roster.ExportRosterItemAction;
010import jmri.jmrit.roster.FullBackupExportAction;
011import jmri.jmrit.roster.FullBackupImportAction;
012import jmri.jmrit.roster.ImportRosterItemAction;
013import jmri.jmrit.roster.PrintListAction;
014import jmri.jmrit.roster.PrintRosterAction;
015import jmri.jmrit.roster.swing.speedprofile.SpeedProfileAction;
016import org.slf4j.Logger;
017import org.slf4j.LoggerFactory;
018
019/**
020 * Provides a context-specific menu for handling the Roster.
021 *
022 * @author Bob Jacobsen Copyright (C) 2001, 2002, 2008
023 * @author Dennis Miller Copyright (C) 2005
024 * @see jmri.jmrit.roster.RosterEntry
025 * @see jmri.jmrit.roster.Roster
026 */
027public class RosterMenu extends JMenu {
028
029    /**
030     * Ctor argument defining that the menu object will be used as part of the
031     * main menu of the program, away from any GUI that can select or use a
032     * RosterEntry.
033     */
034    static public final int MAINMENU = 1;
035
036    /**
037     * Ctor argument defining that the menu object will be used as a menu on a
038     * GUI object that can select a RosterEntry.
039     */
040    static public final int SELECTMENU = 2;
041
042    /**
043     * Ctor argument defining that the menu object will be used as a menu on a
044     * GUI object that is dealing with a single RosterEntry.
045     */
046    static public final int ENTRYMENU = 3;
047
048    /**
049     * Create a menu of Roster tools.
050     *
051     * @param pMenuName Name for the menu
052     * @param pMenuType Select where the menu will be used, hence the right set
053     *                  of items to be enabled.
054     * @param pWho      The Component using this menu, used to ensure that
055     *                  dialog boxes will pop in the right place.
056     */
057    public RosterMenu(String pMenuName, int pMenuType, Component pWho) {
058        super(pMenuName);
059
060        // create the menu
061        AbstractAction dp3Action = new jmri.jmrit.roster.swing.RosterFrameAction(Bundle.getMessage("MenuItemRoster"), false);
062        dp3Action.setEnabled(true);
063
064        AbstractAction createAction = new jmri.jmrit.symbolicprog.tabbedframe.PaneNewProgAction(Bundle.getMessage("MenuItemCreate"));
065        createAction.setEnabled(false);
066
067        AbstractAction editAction = new jmri.jmrit.symbolicprog.tabbedframe.PaneEditAction(Bundle.getMessage("MenuItemEdit"));
068        editAction.setEnabled(false);
069
070        AbstractAction exportAction = new ExportRosterItemAction(Bundle.getMessage("MenuItemExport"), pWho);
071        exportAction.setEnabled(false);
072
073        AbstractAction importAction = new ImportRosterItemAction(Bundle.getMessage("MenuItemImport"), pWho);
074        importAction.setEnabled(false);
075
076        AbstractAction copyAction = new CopyRosterItemAction(Bundle.getMessage("MenuItemCopy"), pWho);
077        copyAction.setEnabled(false);
078
079        AbstractAction deleteAction = new DeleteRosterItemAction(Bundle.getMessage("MenuItemDelete"), pWho);
080        deleteAction.setEnabled(false);
081
082        AbstractAction deleteGroupAction = new DeleteRosterGroupAction(Bundle.getMessage("MenuGroupDelete"), pWho);
083        deleteGroupAction.setEnabled(false);
084
085        AbstractAction createGroupAction = new CreateRosterGroupAction(Bundle.getMessage("MenuGroupCreate"), pWho);
086        createGroupAction.setEnabled(false);
087
088        AbstractAction rosterEntryToGroupAction = new RosterEntryToGroupAction(Bundle.getMessage("MenuGroupAssociate"), pWho);
089        rosterEntryToGroupAction.setEnabled(false);
090
091        AbstractAction removeRosterEntryToGroupAction = new RemoveRosterEntryToGroupAction(Bundle.getMessage("MenuGroupDisassociate"), pWho);
092        removeRosterEntryToGroupAction.setEnabled(false);
093
094        AbstractAction rosterGroupTableAction = new jmri.jmrit.roster.swing.rostergroup.RosterGroupTableAction(Bundle.getMessage("MenuGroupTable"));
095        rosterGroupTableAction.setEnabled(false);
096
097        AbstractAction rosterExportAction = new FullBackupExportAction(Bundle.getMessage("MenuFullExport"), pWho);
098        rosterExportAction.setEnabled(false);
099
100        AbstractAction rosterImportAction = new FullBackupImportAction(Bundle.getMessage("MenuFullImport"), pWho);
101        rosterImportAction.setEnabled(false);
102
103        AbstractAction speedProfileAction = new SpeedProfileAction(Bundle.getMessage("MenuSpeedProfile"));
104        speedProfileAction.setEnabled(false);
105
106        // Need a frame here, but are not passed one
107        Frame newFrame = new Frame();
108        AbstractAction printAction = new PrintRosterAction(Bundle.getMessage("MenuItemPrintSummary"), newFrame, false);
109        printAction.setEnabled(false);
110        AbstractAction listAction = new PrintListAction(Bundle.getMessage("MenuItemPrintList"), newFrame, false);
111        listAction.setEnabled(false);
112        AbstractAction previewAction = new PrintRosterAction(Bundle.getMessage("MenuItemPreviewSummary"), newFrame, true);
113        previewAction.setEnabled(false);
114        AbstractAction previewListAction = new PrintListAction(Bundle.getMessage("MenuItemPreviewList"), newFrame, true);
115        previewListAction.setEnabled(false);
116
117        JMenu groupMenu = new JMenu(Bundle.getMessage("MenuRosterGroups"));
118        groupMenu.add(createGroupAction);
119        groupMenu.add(deleteGroupAction);
120        groupMenu.add(rosterGroupTableAction);
121        groupMenu.add(rosterEntryToGroupAction);
122        groupMenu.add(removeRosterEntryToGroupAction);
123
124        add(dp3Action);
125        addSeparator();
126        add(createAction);
127        add(editAction);
128        add(copyAction);
129        add(importAction);
130        add(exportAction);
131        add(deleteAction);
132        add(printAction);
133        add(listAction);
134        add(previewAction);
135        add(previewListAction);
136        addSeparator();
137        add(groupMenu);
138        addSeparator();
139        add(rosterExportAction);
140        add(rosterImportAction);
141        add(speedProfileAction);
142
143        // activate the correct items
144        switch (pMenuType) {
145            case MAINMENU:
146                createAction.setEnabled(true);
147                editAction.setEnabled(true);
148                deleteAction.setEnabled(true);
149                importAction.setEnabled(true);
150                exportAction.setEnabled(true);
151                copyAction.setEnabled(true);
152                deleteGroupAction.setEnabled(true);
153                createGroupAction.setEnabled(true);
154                rosterEntryToGroupAction.setEnabled(true);
155                removeRosterEntryToGroupAction.setEnabled(true);
156                printAction.setEnabled(true);
157                listAction.setEnabled(true);
158                previewAction.setEnabled(true);
159                previewListAction.setEnabled(true);
160                rosterGroupTableAction.setEnabled(true);
161                rosterExportAction.setEnabled(true);
162                rosterImportAction.setEnabled(true);
163                speedProfileAction.setEnabled(true);
164                break;
165            case SELECTMENU:
166            case ENTRYMENU:
167                printAction.setEnabled(true);
168                previewAction.setEnabled(true);
169                break;
170            default:
171                log.error("RosterMenu constructed without a valid menuType parameter: {}", pMenuType);
172        }
173    }
174
175    // initialize logging
176    private final static Logger log = LoggerFactory.getLogger(RosterMenu.class);
177
178}