001package jmri.jmrit.symbolicprog.autospeed;
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.tabbedframe.PaneOpsProgFrame;
017import org.slf4j.Logger;
018import org.slf4j.LoggerFactory;
019
020/**
021 * Action to start the an "Auto Speed Configurer" by creating a frame to select
022 * loco, etc.
023 * <p>
024 * The resulting JFrame is constructed on the fly here, and has no specific
025 * type.
026 *
027 * @see jmri.jmrit.symbolicprog.tabbedframe.PaneOpsProgAction
028 *
029 * @author Bob Jacobsen Copyright (C) 2001
030 */
031public class AutoSpeedAction extends AbstractAction {
032
033    Object o1, o2, o3, o4;
034    JLabel statusLabel;
035
036    public AutoSpeedAction(String s) {
037        super(s);
038
039        statusLabel = new JLabel("idle");
040
041        // disable ourself if ops programming is not possible
042        if (jmri.InstanceManager.getNullableDefault(jmri.AddressedProgrammerManager.class) == null
043                || !jmri.InstanceManager.getDefault(jmri.AddressedProgrammerManager.class).isAddressedModePossible()) {
044            setEnabled(false);
045        }
046
047    }
048
049    @Override
050    public void actionPerformed(ActionEvent e) {
051
052        if (log.isInfoEnabled()) {
053            log.debug("auto speed tool requested");
054        }
055
056        // create the initial frame that steers
057        final JFrame f = new JFrame("Auto-speed Tool Setup");
058        f.getContentPane().setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS));
059
060        // add the Roster menu
061        JMenuBar menuBar = new JMenuBar();
062        // menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));
063        menuBar.add(new jmri.jmrit.roster.swing.RosterMenu("Roster", jmri.jmrit.roster.swing.RosterMenu.MAINMENU, f));
064        f.setJMenuBar(menuBar);
065
066        // known loco on main track
067        JPanel pane1 = new KnownLocoSelPane(false) {  // no ident in ops mode yet
068
069            @Override
070            protected void startProgrammer(DecoderFile decoderFile, RosterEntry re,
071                    String filename) {
072                String title = "Set speed info for " + re.getId() + " on main track";
073                // find the ops-mode programmer
074                int address = Integer.parseInt(re.getDccAddress());
075                boolean longAddr = true;
076                if (address < 100) {
077                    longAddr = false;
078                }
079                Programmer programmer = InstanceManager.getDefault(jmri.AddressedProgrammerManager.class)
080                        .getAddressedProgrammer(longAddr, address);
081                // and created the frame
082                JFrame p = new PaneOpsProgFrame(decoderFile, re,
083                        title, "programmers" + File.separator + filename + ".xml",
084                        programmer);
085                p.pack();
086                p.setVisible(true);
087                f.setVisible(false);
088                f.dispose();
089            }
090        };
091
092        // load primary frame
093        pane1.setAlignmentX(JLabel.CENTER_ALIGNMENT);
094        f.getContentPane().add(pane1);
095
096        f.pack();
097        if (log.isInfoEnabled()) {
098            log.debug("setup created");
099        }
100        f.setVisible(true);
101    }
102
103    private final static Logger log = LoggerFactory.getLogger(AutoSpeedAction.class);
104
105}