001package jmri.jmrit.etcs.dmi.swing;
002
003import java.awt.*;
004
005import javax.annotation.CheckForNull;
006import javax.swing.*;
007
008import jmri.jmrit.etcs.ResourceUtil;
009import jmri.util.swing.JmriMouseAdapter;
010import jmri.util.swing.JmriMouseEvent;
011
012/**
013 * Class to demonstrate features of ERTMS DMI Panel F, the Menu area.
014 * @author Steve Young Copyright (C) 2024
015 */
016public class DmiPanelF extends JPanel {
017
018    private final JButton f1;
019    private final JButton f2;
020    private final JButton f3;
021    private final JButton f4;
022    private final JButton f5;
023
024    private final DmiPanel main;
025
026    public DmiPanelF(@CheckForNull DmiPanel mainPane){
027
028        super();
029        setLayout(null); // Set the layout manager to null
030
031        setBackground(DmiPanel.BACKGROUND_COLOUR);
032        setBounds(580, 15, 60, 450);
033
034        main = mainPane;
035
036        f1 = new JButton(Bundle.getMessage("ButtonMain"));
037        f2 = new JButton(Bundle.getMessage("ButtonOverRide"));
038        f3 = new JButton(Bundle.getMessage("ButtonDataView"));
039        f4 = new JButton("Spec");
040        f5 = new JButton();
041
042        f1.setBounds(0, 0, 60, 50);
043        f2.setBounds(0, 50, 60, 50);
044        f3.setBounds(0, 100, 60, 50);
045        f4.setBounds(0, 150, 60, 50);
046        f5.setBounds(0, 200, 60, 50);
047
048        f1.setBackground(DmiPanel.BACKGROUND_COLOUR);
049        f2.setBackground(DmiPanel.BACKGROUND_COLOUR);
050        f3.setBackground(DmiPanel.BACKGROUND_COLOUR);
051        f4.setBackground(DmiPanel.BACKGROUND_COLOUR);
052        f5.setBackground(DmiPanel.BACKGROUND_COLOUR);
053
054        f1.setForeground(DmiPanel.GREY);
055        f2.setForeground(DmiPanel.GREY);
056        f3.setForeground(DmiPanel.GREY);
057        f4.setForeground(DmiPanel.GREY);
058        
059
060        f1.setBorder(javax.swing.BorderFactory.createLineBorder(Color.black, 1));
061        f2.setBorder(javax.swing.BorderFactory.createLineBorder(Color.black, 1));
062        f3.setBorder(javax.swing.BorderFactory.createLineBorder(Color.black, 1));
063        f4.setBorder(javax.swing.BorderFactory.createLineBorder(Color.black, 1));
064        f5.setBorder(javax.swing.BorderFactory.createLineBorder(Color.black, 1));
065
066        f1.setFocusable(false);
067        f2.setFocusable(false);
068        f3.setFocusable(false);
069        f4.setFocusable(false);
070        f5.setFocusable(false);
071
072        f1.setHorizontalAlignment(SwingConstants.CENTER);
073        f2.setHorizontalAlignment(SwingConstants.CENTER);
074        f3.setHorizontalAlignment(SwingConstants.CENTER);
075        f4.setHorizontalAlignment(SwingConstants.CENTER);
076        f5.setHorizontalAlignment(SwingConstants.CENTER);
077
078        f1.setFont(new Font(DmiPanel.FONT_NAME, Font.PLAIN, 17));
079        f2.setFont(new Font(DmiPanel.FONT_NAME, Font.PLAIN, 17));
080        f3.setFont(new Font(DmiPanel.FONT_NAME, Font.PLAIN, 17));
081        f4.setFont(new Font(DmiPanel.FONT_NAME, Font.PLAIN, 17));
082        f5.setIcon(ResourceUtil.getImageIcon( "SE_04.bmp"));
083
084        add(f1);
085        add(f2);
086        add(f3);
087        add(f4);
088        add(f5);
089
090        f5.addMouseListener(jmri.util.swing.JmriMouseListener.adapt(new PopupListener()));
091    }
092
093    /**
094     * Listener to trigger display of table header column menu.
095     */
096    private class PopupListener extends JmriMouseAdapter {
097
098        private final jmri.UserPreferencesManager p = jmri.InstanceManager.getDefault(jmri.UserPreferencesManager.class);
099        private JCheckBoxMenuItem jcmi;
100
101        /**
102         * {@inheritDoc}
103         */
104        @Override
105        public void mousePressed(JmriMouseEvent e) {
106            if (e.isPopupTrigger()) {
107                showPopup(e);
108            }
109        }
110
111        /**
112         * {@inheritDoc}
113         */
114        @Override
115        public void mouseReleased(JmriMouseEvent e) {
116            if (e.isPopupTrigger()) {
117                showPopup(e);
118            }
119        }
120
121        /**
122         * {@inheritDoc}
123         */
124        @Override
125        public void mouseClicked(JmriMouseEvent e) {
126            if (e.isPopupTrigger()) {
127                showPopup(e);
128            }
129        }
130
131        private void showPopup(JmriMouseEvent e){
132
133            JPopupMenu popupMenu = new JPopupMenu();
134
135            JMenuItem mi = new JMenuItem(Bundle.getMessage("RunDemo"));
136            mi.addActionListener(this::runDemo);
137            popupMenu.add(mi);
138
139            jcmi = new JCheckBoxMenuItem(Bundle.getMessage("CentreSpeedText"));
140            jcmi.setToolTipText(Bundle.getMessage("GlobalPreference"));
141            jcmi.setSelected(p.getSimplePreferenceState(DmiPanel.PROPERTY_CENTRE_TEXT));
142            jcmi.addActionListener(this::setCentreSpeedText);
143            popupMenu.add(jcmi);
144            popupMenu.show(e.getComponent(), e.getX(), e.getY());
145        }
146
147        private void runDemo(java.awt.event.ActionEvent e){
148            log.debug("running demo from {}", e.getActionCommand());
149            new DmiDemo(main).runDemo();
150        }
151
152        private void setCentreSpeedText(java.awt.event.ActionEvent e) {
153            log.debug("set centre speed pref from {}", e.getActionCommand());
154            p.setSimplePreferenceState(DmiPanel.PROPERTY_CENTRE_TEXT, jcmi.isSelected());
155            main.repaint();
156        }
157    }
158
159    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DmiPanelF.class);
160
161}