001package jmri.jmrix.loconet.clockmon;
002
003import java.awt.event.ActionEvent;
004import java.awt.event.ActionListener;
005import javax.swing.BoxLayout;
006import javax.swing.JButton;
007import javax.swing.JLabel;
008import javax.swing.JPanel;
009import javax.swing.JTextField;
010
011import jmri.jmrix.loconet.LnConstants;
012import jmri.jmrix.loconet.LocoNetSlot;
013import jmri.jmrix.loconet.LocoNetSystemConnectionMemo;
014import jmri.jmrix.loconet.SlotListener;
015import jmri.jmrix.loconet.swing.LnPanel;
016import jmri.util.swing.WrapLayout;
017
018/**
019 * Pane displaying a LocoNet clock monitor.
020 * <p>
021 * Some of the message formats used in this class are Copyright Digitrax, Inc.
022 * and used with permission as part of the JMRI project. That permission does
023 * not extend to uses in other software products. If you wish to use this code,
024 * algorithm or these message formats outside of JMRI, please contact Digitrax
025 * Inc for separate permission.
026 * <p>
027 * The original module has been converted to a clock monitor by removing all
028 * active items (Dave Duchamp 2007-2008).
029 *
030 * @author Bob Jacobsen Copyright (C) 2003, 2004, 2010
031 */
032public class ClockMonPane extends LnPanel implements SlotListener {
033
034    @Override
035    public String getHelpTarget() {
036        return "package.jmri.jmrix.loconet.clockmon.ClockMonFrame"; // NOI18N
037    }
038
039    @Override
040    public String getTitle() {
041        return getTitle(Bundle.getMessage("MenuItemClockMon"));
042    }
043
044    public ClockMonPane() {
045        super();
046    }
047
048    @Override
049    public void initComponents(final LocoNetSystemConnectionMemo memo) {
050        super.initComponents(memo);
051
052        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
053
054        // add GUI items
055        JPanel panel1 = new JPanel();
056        panel1.setLayout(new WrapLayout());
057        panel1.add(new JLabel(Bundle.getMessage("ClockDayLabel")));
058        panel1.add(days);
059        days.setPreferredSize(spacer.getPreferredSize());
060        panel1.add(new JLabel(Bundle.getMessage("ClockTimeLabel")));
061        panel1.add(hours);
062        hours.setPreferredSize(spacer.getPreferredSize());
063        panel1.add(new JLabel(":"));
064        panel1.add(minutes);
065        minutes.setPreferredSize(spacer.getPreferredSize());
066        panel1.add(new JLabel("."));
067        panel1.add(frac_mins);
068        add(panel1);
069
070        JPanel panel2 = new JPanel();
071        panel2.setLayout(new WrapLayout());
072        panel2.add(new JLabel(Bundle.getMessage("ClockRateLabel")));
073        panel2.add(rate);
074        add(panel2);
075
076        JPanel panel3 = new JPanel();
077        panel3.setLayout(new WrapLayout());
078        panel3.add(readButton);
079        add(panel3);
080        // Load GUI element contents with current slot contents
081        notifyChangedSlot(memo.getSlotManager().slot(LnConstants.FC_SLOT));
082
083        // install "read" button handler
084        readButton.addActionListener(new ActionListener() {
085            @Override
086            public void actionPerformed(ActionEvent a) {
087                memo.getSlotManager().sendReadSlot(LnConstants.FC_SLOT);
088            }
089        }
090        );
091        // listen for updated slot contents
092        if (memo.getSlotManager() != null) {
093            memo.getSlotManager().addSlotListener(this);
094        } else {
095            log.error("No LocoNet connection available, can't function"); // NOI18N
096        }
097    }
098
099    /**
100     * Handle changed slot contents, due to clock changes.
101     *
102     */
103    @Override
104    public void notifyChangedSlot(LocoNetSlot s) {
105        if (s.getSlot() != LnConstants.FC_SLOT) {
106            return; // only watch clock slot
107        }
108        if (log.isDebugEnabled()) {
109            log.debug("slot update {}", s); // NOI18N
110        }
111
112        // update GUI from the new slot contents
113        days.setText("" + s.getFcDays());
114        hours.setText("" + s.getFcHours());
115        minutes.setText("" + s.getFcMinutes());
116        rate.setText("" + s.getFcRate());
117        frac_mins.setText("" + s.getFcFracMins());
118    }
119
120    @Override
121    public void dispose() {
122        // Drop LocoNet connection
123        if (memo.getSlotManager() != null) {
124            memo.getSlotManager().removeSlotListener(this);
125        }
126
127        // take apart the JFrame
128        super.dispose();
129    }
130
131    JTextField days = new JTextField("00"); // NOI18N
132    JTextField hours = new JTextField("00"); // NOI18N
133    JTextField minutes = new JTextField("00"); // NOI18N
134    JTextField frac_mins = new JTextField("00"); // NOI18N
135
136    JTextField rate = new JTextField(4);
137
138    JButton readButton = new JButton(Bundle.getMessage("ButtonRead"));
139    final static JTextField spacer = new JTextField("123");
140
141    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ClockMonPane.class);
142
143}