001package jmri.jmrit.throttle.list; 002 003import java.awt.BorderLayout; 004import java.awt.Dimension; 005import java.awt.event.ActionEvent; 006import java.beans.PropertyChangeEvent; 007import java.beans.PropertyChangeListener; 008 009import javax.swing.*; 010 011import jmri.ConsistManager; 012import jmri.InstanceManager; 013import jmri.jmrit.catalog.NamedIcon; 014import jmri.jmrit.throttle.ThrottleFrameManager; 015import jmri.jmrit.throttle.ThrottlesPreferencesAction; 016import jmri.jmrit.throttle.buttons.LargePowerManagerButton; 017import jmri.jmrit.throttle.buttons.StopAllButton; 018import jmri.jmrit.throttle.implementation.WindowPreferences; 019import jmri.jmrit.throttle.interfaces.ThrottleControllerUI; 020import jmri.jmrit.throttle.preferences.ThrottlesPreferences; 021import jmri.util.swing.JmriMouseEvent; 022import jmri.util.swing.JmriMouseListener; 023 024import org.jdom2.Element; 025import org.slf4j.Logger; 026import org.slf4j.LoggerFactory; 027 028/** 029 * A panel to display a list of active JMRI UI throttles 030 * 031 * <hr> 032 * This file is part of JMRI. 033 * <p> 034 * JMRI is free software; you can redistribute it and/or modify it under the 035 * terms of version 2 of the GNU General Public License as published by the Free 036 * Software Foundation. See the "COPYING" file for a copy of this license. 037 * <p> 038 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY 039 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 040 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 041 * 042 * @author Lionel Jeanson - 2009-2026 043 * 044 */ 045 046public class ThrottlesListPanel extends JPanel implements PropertyChangeListener { 047 048 private final ThrottlesTableModel throttleFramesLM; 049 private JTable throttleFrames; 050 051 public ThrottlesListPanel() { 052 super(); 053 throttleFramesLM = new ThrottlesTableModel(); 054 ConsistManager consistManager = InstanceManager.getNullableDefault(jmri.ConsistManager.class); 055 if (consistManager != null && consistManager.isEnabled()) { 056 consistManager.addConsistListListener(throttleFramesLM); 057 } 058 InstanceManager.getDefault(ThrottlesPreferences.class).addPropertyChangeListener(this); 059 initGUI(); 060 } 061 062 public ThrottlesTableModel getTableModel() { 063 return throttleFramesLM; 064 } 065 066 private void initGUI() { 067 throttleFrames = new JTable(throttleFramesLM); 068 throttleFrames.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 069 throttleFrames.setRowHeight(ThrottlesTableCellRenderer.LINE_HEIGHT); 070 throttleFrames.setTableHeader(null); 071 throttleFrames.setDragEnabled(true); 072 throttleFrames.setDropMode(DropMode.INSERT_ROWS); 073 throttleFrames.setTransferHandler(new ThrottlesTableTransferHandler(throttleFrames)); 074 throttleFrames.setDefaultRenderer(Object.class, new ThrottlesTableCellRenderer()); 075 throttleFrames.addMouseListener(JmriMouseListener.adapt(new JmriMouseListener() { 076 @Override 077 public void mouseClicked(JmriMouseEvent e) { 078 int ntw = throttleFrames.columnAtPoint(e.getPoint()); 079 int ntf = throttleFrames.rowAtPoint(e.getPoint()); 080 log.debug("Click in table at row {} (frame) / col {} (window)", ntf, ntw); 081 ThrottleControllerUI tf = throttleFramesLM.getValueAt(ntf, ntw); 082 if (tf != null) { 083 tf.toFront(); 084 } 085 // refresh selected/unselected 086 throttleFramesLM.fireTableDataChanged(); 087 } 088 089 @Override 090 public void mouseEntered(JmriMouseEvent arg0) { 091 } 092 093 @Override 094 public void mouseExited(JmriMouseEvent arg0) { 095 } 096 097 @Override 098 public void mousePressed(JmriMouseEvent arg0) { 099 } 100 101 @Override 102 public void mouseReleased(JmriMouseEvent arg0) { 103 } 104 })); 105 106 JScrollPane scrollPane1 = new JScrollPane(throttleFrames); 107 setLayout(new BorderLayout()); 108 setPreferredSize(new Dimension(320, 200)); 109 110 JToolBar throttleToolBar = new JToolBar("Throttles list toolbar"); 111 JButton jbNew = new JButton(); 112 jbNew.setIcon(new NamedIcon("resources/icons/throttles/new.png", "resources/icons/throttles/new.png")); 113 jbNew.setToolTipText(Bundle.getMessage("ThrottleToolBarNewWindowToolTip")); 114 jbNew.setVerticalTextPosition(SwingConstants.BOTTOM); 115 jbNew.setHorizontalTextPosition(SwingConstants.CENTER); 116 jbNew.addActionListener((ActionEvent e) -> { 117 ThrottleControllerUI tf = InstanceManager.getDefault(ThrottleFrameManager.class).createThrottleFrame(); 118 tf.toFront(); 119 }); 120 throttleToolBar.add(jbNew); 121 122 throttleToolBar.addSeparator(); 123 throttleToolBar.add(new StopAllButton()); 124 throttleToolBar.add(new LargePowerManagerButton(false)); 125 126 add(throttleToolBar, BorderLayout.PAGE_START); 127 add(scrollPane1, BorderLayout.CENTER); 128 129 throttleToolBar.addSeparator(); 130 JButton jbPreferences = new JButton(); 131 jbPreferences.setIcon(new NamedIcon("resources/icons/throttles/preferences.png", "resources/icons/throttles/Preferences24.png")); 132 jbPreferences.setToolTipText(Bundle.getMessage("ThrottleToolBarPreferencesToolTip")); 133 jbPreferences.setVerticalTextPosition(SwingConstants.BOTTOM); 134 jbPreferences.setHorizontalTextPosition(SwingConstants.CENTER); 135 jbPreferences.addActionListener(new ThrottlesPreferencesAction()); 136 throttleToolBar.add(jbPreferences); 137 } 138 139 public Element getXml() { 140 Element me = new Element("ThrottlesListPanel"); 141 java.util.ArrayList<Element> children = new java.util.ArrayList<>(1); 142 if (this.getTopLevelAncestor() != null) { 143 children.add(WindowPreferences.getPreferences(this.getTopLevelAncestor())); 144 } 145 me.setContent(children); 146 return me; 147 } 148 149 public void setXml(Element tlp) { 150 Element window = tlp.getChild("window"); 151 if (window != null) { 152 if (this.getTopLevelAncestor() == null) { 153 InstanceManager.getDefault(ThrottleFrameManager.class).showThrottlesList(); 154 } 155 WindowPreferences.setPreferences(this.getTopLevelAncestor(), window); 156 } 157 } 158 159 private void applyPreferences() { 160 repaint(); 161 } 162 163 @Override 164 public void propertyChange(PropertyChangeEvent evt) { 165 if (ThrottlesPreferences.prefPopertyName.compareTo(evt.getPropertyName()) == 0) { 166 applyPreferences(); 167 } 168 } 169 170 private static final Logger log = LoggerFactory.getLogger(ThrottlesListPanel.class); 171}