001package jmri.jmrit.throttle.list; 002 003import java.awt.*; 004 005import javax.swing.*; 006import javax.swing.border.BevelBorder; 007import javax.swing.table.TableCellRenderer; 008 009import jmri.InstanceManager; 010import jmri.jmrit.throttle.interfaces.ThrottleControllerUI; 011import jmri.jmrit.throttle.preferences.ThrottlesPreferences; 012import jmri.util.FileUtil; 013 014/** 015 * A TableCellRender to graphicaly display an active throttles in a summary table 016 * (see ThrottlesListPanel) 017 * 018 * <hr> 019 * This file is part of JMRI. 020 * <p> 021 * JMRI is free software; you can redistribute it and/or modify it under the 022 * terms of version 2 of the GNU General Public License as published by the Free 023 * Software Foundation. See the "COPYING" file for a copy of this license. 024 * <p> 025 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY 026 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 027 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 028 * 029 * @author Lionel Jeanson - 2011-2026 030 * 031 */ 032 033public class ThrottlesTableCellRenderer implements TableCellRenderer { 034 035 private static final ImageIcon FWD_ICN = new ImageIcon(FileUtil.findURL("resources/icons/throttles/dirFwdOn.png")); 036 private static final ImageIcon BCK_ICN = new ImageIcon(FileUtil.findURL("resources/icons/throttles/dirBckOn.png")); 037 private static final ImageIcon ESTOP_ICN = new ImageIcon(FileUtil.findURL("resources/icons/throttles/estop24.png")); 038 private static final ImageIcon STOP_ICN = new ImageIcon(FileUtil.findURL("resources/icons/throttles/stopOn24.png")); 039 static final int LINE_HEIGHT = 42; 040 041 @Override 042 public Component getTableCellRendererComponent(JTable jtable, Object value, boolean bln, boolean bln1, int i, int i1) { 043 JPanel retPanel = new JPanel(); 044 retPanel.setLayout(new BorderLayout()); 045 046 if (value == null) { 047 return retPanel; 048 } 049 050 ThrottleControllerUI tf = (ThrottleControllerUI) value; 051 052 retPanel.add(tf.getLabel(), BorderLayout.CENTER); 053 054 if (tf.getThrottle() != null) { 055 final ThrottlesPreferences preferences = InstanceManager.getDefault(ThrottlesPreferences.class); 056 JPanel ctrlPanel = new JPanel(); 057 ctrlPanel.setLayout(new BorderLayout()); 058 // direction 059 JLabel dir = new JLabel(); 060 if (preferences.isUsingExThrottle() && preferences.isUsingLargeSpeedSlider()) { 061 if (tf.getThrottle().getIsForward()) { 062 dir.setIcon(FWD_ICN); 063 } else { 064 dir.setIcon(BCK_ICN); 065 } 066 } else { 067 if (tf.getThrottle().getIsForward()) { 068 dir.setText(Bundle.getMessage("ButtonForward")); 069 } else { 070 dir.setText(Bundle.getMessage("ButtonReverse")); 071 } 072 } 073 dir.setVerticalAlignment(JLabel.CENTER); 074 ctrlPanel.add(dir, BorderLayout.WEST); 075 // speed 076 if (preferences.isUsingExThrottle() && preferences.isUsingLargeSpeedSlider()) { 077 JLayeredPane layerPane = new JLayeredPane(); 078 int cmpWidth = jtable.getWidth()/jtable.getColumnCount()/4 ; 079 layerPane.setPreferredSize(new Dimension(cmpWidth, LINE_HEIGHT - 8)); 080 // speed 081 JProgressBar speedBar = new javax.swing.JProgressBar(); 082 speedBar.setBounds(0,0,cmpWidth, LINE_HEIGHT - 8); 083 speedBar.setMinimum(0); 084 speedBar.setMaximum(1000); 085 speedBar.setValue((int) (tf.getThrottle().getSpeedSetting() * 1000f)); 086 layerPane.add(speedBar, JLayeredPane.DEFAULT_LAYER); 087 // estop & stop icon 088 if (tf.getThrottle().getSpeedSetting() == -1) { 089 JLabel estop = new JLabel(); 090 estop.setHorizontalAlignment(JLabel.CENTER); 091 estop.setIcon(ESTOP_ICN); 092 estop.setBounds(0,0,cmpWidth, LINE_HEIGHT - 8); 093 layerPane.add(estop, JLayeredPane.PALETTE_LAYER); 094 } else if (tf.getThrottle().getSpeedSetting() == 0) { 095 JLabel stop = new JLabel(); 096 stop.setHorizontalAlignment(JLabel.CENTER); 097 stop.setIcon(STOP_ICN); 098 stop.setBounds(0,0,cmpWidth, LINE_HEIGHT - 8); 099 layerPane.add(stop, JLayeredPane.PALETTE_LAYER); 100 } 101 ctrlPanel.add(layerPane, BorderLayout.EAST); 102 } else { 103 JLabel speedLabel = new JLabel(""); 104 if (tf.getThrottle().getSpeedSetting() == -1) { 105 speedLabel.setText(" " + Bundle.getMessage("ButtonEStop") + " "); 106 } else { 107 speedLabel.setText(" " + (int) (tf.getThrottle().getSpeedSetting() * 100f) + "% "); 108 } 109 ctrlPanel.add(speedLabel, BorderLayout.CENTER); 110 } 111 ctrlPanel.setOpaque(false); 112 retPanel.add(ctrlPanel, BorderLayout.EAST); 113 } 114 if (tf.isVisible()) { 115 Color selBackground = javax.swing.UIManager.getDefaults().getColor("List.selectionBackground"); 116 if (selBackground == null) { 117 selBackground = Color.ORANGE; 118 } 119 Color selForeground = javax.swing.UIManager.getDefaults().getColor("List.selectionForeground"); 120 if (selForeground == null) { 121 selForeground = Color.BLACK; 122 } 123 retPanel.setBackground(selBackground); 124 setForegroundAllComp( retPanel, selForeground ); 125 retPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); 126 } else { 127 retPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED)); 128 } 129 return retPanel; 130 } 131 132 private void setForegroundAllComp(JComponent cmp, Color color) { 133 if (cmp != null) { 134 cmp.setForeground(color); 135 for (Component c : cmp.getComponents()) { 136 if (c instanceof JComponent) { 137 setForegroundAllComp( (JComponent) c, color); 138 } 139 } 140 } 141 } 142 143// private static final Logger log = LoggerFactory.getLogger(ThrottlesTableCellRenderer.class); 144 145}