001package jmri.jmrit.progsupport;
002
003import javax.swing.*;
004import jmri.AddressedProgrammerManager;
005import jmri.GlobalProgrammerManager;
006import jmri.InstanceManager;
007import jmri.Programmer;
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011/**
012 * Provide a JPanel to configure the programming mode.
013 * <p>
014 * The using code should get a configured programmer with getProgrammer().
015 * <p>
016 * This pane will only display ops mode options if ops mode is available, as
017 * evidenced by an attempt to get an ops mode programmer at startup time.
018 * <p>
019 * For service mode, you can get the programmer either from the JPanel or direct
020 * from the instance manager. For ops mode, you have to get it from here.
021 * <p>
022 * Note that you should call the dispose() method when you're really done, so
023 * that a ProgModePane object can disconnect its listeners.
024 *
025 * @author Bob Jacobsen Copyright (C) 2001
026 */
027public class ProgModePane extends ProgModeSelector {
028
029    // GUI member declarations
030    ProgOpsModePane mOpsPane;
031    ProgServiceModePane mServicePane;
032    ButtonGroup group = new ButtonGroup();
033
034    /**
035     * @param direction controls layout, either BoxLayout.X_AXIS or
036     *                  BoxLayout.Y_AXIS
037     */
038    public ProgModePane(int direction) {
039
040        if (log.isDebugEnabled()) {
041            log.debug("AddressedProgrammerManager:");
042            InstanceManager.getList(AddressedProgrammerManager.class).forEach((p) -> {
043                log.debug("  item: {}", p.toString());
044            });
045            log.debug("GlobalProgrammerManager:");
046            InstanceManager.getList(GlobalProgrammerManager.class).forEach((p) -> {
047                log.debug("  item: {}", p.toString());
048            });
049        }
050
051        // general GUI config
052        setLayout(new BoxLayout(this, direction));
053
054        boolean addSep = false;
055
056        // create the ops mode 1st, so the service is 2nd,
057        // so it's the one that's selected
058        mOpsPane = null;
059        if (InstanceManager.getNullableDefault(AddressedProgrammerManager.class) != null
060                && InstanceManager.getDefault(AddressedProgrammerManager.class).isAddressedModePossible()) {
061
062            mOpsPane = new ProgOpsModePane(direction, group);
063        }
064
065        // service mode support, if present
066        if (InstanceManager.getNullableDefault(GlobalProgrammerManager.class) != null
067                && InstanceManager.getDefault(GlobalProgrammerManager.class).isGlobalProgrammerAvailable()) {
068
069            mServicePane = new ProgServiceModePane(direction, group);
070            JPanel temp = new JPanel();
071            temp.add(mServicePane);
072            temp.setBorder(javax.swing.BorderFactory.createTitledBorder(Bundle.getMessage("TitleProgramServiceMode")));
073            add(temp);
074            addSep = true;
075        }
076
077        // ops mode support added if present
078        if (mOpsPane != null) {
079
080            if (addSep) {
081                add(new JSeparator());
082            }
083            add(mOpsPane);
084        }
085    }
086
087    /**
088     * Default implementation of "isSelected" just returns true.
089     *
090     * @return Always true in this implementation
091     */
092    @Override
093    public boolean isSelected() {
094        return true;
095    }
096
097    /**
098     * Get the configured programmer
099     */
100    @Override
101    public Programmer getProgrammer() {
102        if (mServicePane!=null && mServicePane.isSelected()) {
103            return mServicePane.getProgrammer();
104        } else if (mOpsPane != null && mOpsPane.isSelected()) {
105            return mOpsPane.getProgrammer();
106        } else {
107            return null;
108        }
109    }
110
111    @Override
112    public void dispose() {
113        if (mServicePane != null) {
114            mServicePane.dispose();
115        }
116        mServicePane = null;
117        if (mOpsPane != null) {
118            mOpsPane.dispose();
119        }
120        mOpsPane = null;
121    }
122
123    private final static Logger log = LoggerFactory.getLogger(ProgModePane.class);
124
125}