001package jmri.jmrit.throttle;
002
003import java.awt.BorderLayout;
004import java.awt.Dimension;
005import java.awt.event.ActionEvent;
006
007import javax.swing.*;
008
009import jmri.ConsistManager;
010import jmri.InstanceManager;
011import jmri.jmrit.catalog.NamedIcon;
012import jmri.util.swing.JmriMouseEvent;
013import jmri.util.swing.JmriMouseListener;
014
015import org.jdom2.Element;
016import org.slf4j.Logger;
017import org.slf4j.LoggerFactory;
018
019/**
020 * A panel to display a list of active JMRI throttles
021 *
022 * @author Lionel Jeanson - 2009-2021
023 *
024 */
025
026public class ThrottlesListPanel extends JPanel {
027
028    private final ThrottlesTableModel throttleFramesLM;
029    private JTable throttleFrames;
030
031    public ThrottlesListPanel() {
032        super();
033        throttleFramesLM = new ThrottlesTableModel();
034        ConsistManager consistManager = InstanceManager.getNullableDefault(jmri.ConsistManager.class);
035        if (consistManager != null && consistManager.isEnabled()) {
036            consistManager.addConsistListListener(throttleFramesLM);
037        }
038        initGUI();
039    }
040
041    public ThrottlesTableModel getTableModel() {
042        return throttleFramesLM;
043    }
044
045    private void initGUI() {
046        throttleFrames = new JTable(throttleFramesLM);
047        throttleFrames.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
048        throttleFrames.setRowHeight(ThrottlesTableCellRenderer.LINE_HEIGHT);
049        throttleFrames.setTableHeader(null);
050        throttleFrames.setDragEnabled(true);
051        throttleFrames.setDropMode(DropMode.INSERT_ROWS);
052        throttleFrames.setTransferHandler(new ThrottlesTableTransferHandler(throttleFrames)); 
053        throttleFrames.setDefaultRenderer(Object.class, new ThrottlesTableCellRenderer());
054        throttleFrames.addMouseListener(JmriMouseListener.adapt(new JmriMouseListener() {
055            @Override
056            public void mouseClicked(JmriMouseEvent e) {                
057                int ntw = throttleFrames.columnAtPoint(e.getPoint());
058                int ntf = throttleFrames.rowAtPoint(e.getPoint());
059                log.debug("Click in table at row {} (frame) / col {} (window)", ntf, ntw);
060                ThrottleFrame tf = ((ThrottleFrame) throttleFramesLM.getValueAt(ntf, ntw));
061                if (tf != null) {
062                    tf.toFront();
063                }
064            }
065
066            @Override
067            public void mouseEntered(JmriMouseEvent arg0) {
068            }
069
070            @Override
071            public void mouseExited(JmriMouseEvent arg0) {
072            }
073
074            @Override
075            public void mousePressed(JmriMouseEvent arg0) {
076            }
077
078            @Override
079            public void mouseReleased(JmriMouseEvent arg0) {
080            }
081        }));
082
083        JScrollPane scrollPane1 = new JScrollPane(throttleFrames);
084        setLayout(new BorderLayout());
085        setPreferredSize(new Dimension(320, 200));
086
087        JToolBar throttleToolBar = new JToolBar("Throttles list toolbar");
088        JButton jbNew = new JButton();
089        jbNew.setIcon(new NamedIcon("resources/icons/throttles/new.png", "resources/icons/throttles/new.png"));
090        jbNew.setToolTipText(Bundle.getMessage("ThrottleToolBarNewWindowToolTip"));
091        jbNew.setVerticalTextPosition(SwingConstants.BOTTOM);
092        jbNew.setHorizontalTextPosition(SwingConstants.CENTER);
093        jbNew.addActionListener((ActionEvent e) -> {
094            ThrottleControllerUI tf = InstanceManager.getDefault(ThrottleFrameManager.class).createThrottleController();
095            tf.toFront();
096        });
097        throttleToolBar.add(jbNew);
098
099        throttleToolBar.addSeparator();
100        throttleToolBar.add(new StopAllButton());
101        throttleToolBar.add(new LargePowerManagerButton(false));
102
103        add(throttleToolBar, BorderLayout.PAGE_START);
104        add(scrollPane1, BorderLayout.CENTER);
105
106        throttleToolBar.addSeparator();
107        JButton jbPreferences = new JButton();
108        jbPreferences.setIcon(new NamedIcon("resources/icons/throttles/preferences.png", "resources/icons/throttles/Preferences24.png"));
109        jbPreferences.setToolTipText(Bundle.getMessage("ThrottleToolBarPreferencesToolTip"));
110        jbPreferences.setVerticalTextPosition(SwingConstants.BOTTOM);
111        jbPreferences.setHorizontalTextPosition(SwingConstants.CENTER);
112        jbPreferences.addActionListener(new ThrottlesPreferencesAction());
113        throttleToolBar.add(jbPreferences);
114    }
115
116    public Element getXml() {
117        Element me = new Element("ThrottlesListPanel");
118        java.util.ArrayList<Element> children = new java.util.ArrayList<>(1);
119        children.add(WindowPreferences.getPreferences(this.getTopLevelAncestor()));
120        me.setContent(children);
121        return me;
122    }
123
124    public void setXml(Element tlp) {
125        Element window = tlp.getChild("window");
126        if (window != null) {
127            if (this.getTopLevelAncestor() == null) {
128                InstanceManager.getDefault(ThrottleFrameManager.class).showThrottlesList();
129            }
130            WindowPreferences.setPreferences(this.getTopLevelAncestor(), window);
131        }
132    }
133
134    void applyPreferences() {
135        repaint();
136    }
137    
138    private final static Logger log = LoggerFactory.getLogger(ThrottlesListPanel.class);    
139}