001package jmri.jmrit.symbolicprog.tabbedframe;
002
003import java.awt.event.ActionEvent;
004import java.io.File;
005import javax.swing.AbstractAction;
006import javax.swing.BoxLayout;
007import javax.swing.JFrame;
008import javax.swing.JLabel;
009import javax.swing.JMenuBar;
010import javax.swing.JPanel;
011import jmri.InstanceManager;
012import jmri.Programmer;
013import jmri.jmrit.decoderdefn.DecoderFile;
014import jmri.jmrit.roster.RosterEntry;
015import jmri.jmrit.symbolicprog.KnownLocoSelPane;
016import jmri.jmrit.symbolicprog.SymbolicProgBundle;
017import jmri.util.JmriJFrame;
018import org.slf4j.Logger;
019import org.slf4j.LoggerFactory;
020
021/**
022 * Swing action to create and register a frame for selecting the information
023 * needed to open a PaneProgFrame in service mode.
024 * <p>
025 * The name is a historical accident, and probably should have included
026 * "ServiceMode" or something.
027 * <p>
028 * The resulting JFrame is constructed on the fly here, and has no specific
029 * type.
030 *
031 * @see jmri.jmrit.symbolicprog.tabbedframe.PaneOpsProgAction
032 *
033 * @author Bob Jacobsen Copyright (C) 2001
034 */
035public class PaneOpsProgAction extends AbstractAction {
036
037    Object o1, o2, o3, o4;
038    JLabel statusLabel;
039
040    public PaneOpsProgAction() {
041        this("DecoderPro ops-mode programmer");
042    }
043
044    public PaneOpsProgAction(String s) {
045        super(s);
046
047        statusLabel = new JLabel(SymbolicProgBundle.getMessage("StateIdle"));
048
049        // disable ourself if ops programming is not possible
050        if (jmri.InstanceManager.getNullableDefault(jmri.AddressedProgrammerManager.class) == null
051                || !jmri.InstanceManager.getDefault(jmri.AddressedProgrammerManager.class).isAddressedModePossible()) {
052            setEnabled(false);
053            // This needs to return so the xmlThread is not started;
054            return;
055        }
056
057    }
058
059    @Override
060    public void actionPerformed(ActionEvent e) {
061
062        if (log.isDebugEnabled()) {
063            log.debug("Pane programmer requested");
064        }
065
066        // create the initial frame that steers
067        final JmriJFrame f = new JmriJFrame(SymbolicProgBundle.getMessage("FrameOpsProgrammerSetup"));
068        f.getContentPane().setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS));
069
070        // add the Roster menu
071        JMenuBar menuBar = new JMenuBar();
072        // menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));
073        menuBar.add(new jmri.jmrit.roster.swing.RosterMenu(SymbolicProgBundle.getMessage("MenuRoster"),
074                jmri.jmrit.roster.swing.RosterMenu.MAINMENU, f));
075        f.setJMenuBar(menuBar);
076
077        // known loco on main track
078        JPanel pane1 = new KnownLocoSelPane(false) {  // no ident in ops mode yet
079
080            @Override
081            protected void startProgrammer(DecoderFile decoderFile, RosterEntry re,
082                    String filename) {
083                String title = java.text.MessageFormat.format(SymbolicProgBundle.getMessage("FrameOpsProgrammerTitle"),
084                        new Object[]{re.getId()});
085                // find the ops-mode programmer
086                int address = Integer.parseInt(re.getDccAddress());
087                boolean longAddr = re.isLongAddress();
088                Programmer programmer = InstanceManager.getDefault(jmri.AddressedProgrammerManager.class)
089                        .getAddressedProgrammer(longAddr, address);
090                // and created the frame
091                JFrame p = new PaneOpsProgFrame(decoderFile, re,
092                        title, "programmers" + File.separator + filename + ".xml",
093                        programmer);
094                p.pack();
095                p.setVisible(true);
096
097                // f.setVisible(false);
098                // f.dispose();
099            }
100        };
101
102        // load primary frame
103        pane1.setAlignmentX(JLabel.CENTER_ALIGNMENT);
104        f.getContentPane().add(pane1);
105
106        f.pack();
107        if (log.isDebugEnabled()) {
108            log.debug("Tab-Programmer setup created");
109        }
110        f.setVisible(true);
111    }
112
113    private final static Logger log = LoggerFactory.getLogger(PaneOpsProgAction.class);
114
115}