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