001package jmri.jmrit.throttle;
002
003import java.awt.*;
004
005import javax.swing.*;
006import javax.swing.border.BevelBorder;
007import javax.swing.table.TableCellRenderer;
008
009import jmri.InstanceManager;
010import jmri.Throttle;
011import jmri.jmrit.consisttool.ConsistListCellRenderer;
012import jmri.jmrit.roster.RosterIconFactory;
013import jmri.util.FileUtil;
014
015/**
016 * A TableCellRender to graphicaly display an active throttles in a summary table
017 * (see ThrottlesListPanel)
018 * 
019 * @author Lionel Jeanson - 2011
020 * 
021 */
022
023public class ThrottlesTableCellRenderer implements TableCellRenderer {
024
025    private static final ImageIcon FWD_ICN = new ImageIcon(FileUtil.findURL("resources/icons/throttles/dirFwdOn.png"));
026    private static final ImageIcon BCK_ICN = new ImageIcon(FileUtil.findURL("resources/icons/throttles/dirBckOn.png"));
027    private static final ImageIcon ESTOP_ICN = new ImageIcon(FileUtil.findURL("resources/icons/throttles/estop24.png"));
028    private static final ImageIcon STOP_ICN = new ImageIcon(FileUtil.findURL("resources/icons/throttles/stopOn24.png"));    
029    final static int IMAGE_HEIGHT = 32;
030    private static final RosterIconFactory ICN_FACT = new RosterIconFactory(IMAGE_HEIGHT);
031    final static int LINE_HEIGHT = 42;    
032    
033    @Override
034    public Component getTableCellRendererComponent(JTable jtable, Object value, boolean bln, boolean bln1, int i, int i1) {
035        JPanel retPanel = new JPanel();
036        retPanel.setLayout(new BorderLayout());
037
038        if (value == null) {
039            return retPanel;
040        }
041
042        ThrottleFrame tf = (ThrottleFrame) value;
043        // loco icon
044        if ((tf.getAddressPanel().getConsistAddress() != null) && (tf.getAddressPanel().getThrottle() != null)) {
045            // consists
046            JLabel consistLabel = new JLabel();
047            consistLabel.setOpaque(false);
048            consistLabel.setHorizontalAlignment(JLabel.RIGHT);
049            consistLabel.setVerticalAlignment(JLabel.CENTER);
050            consistLabel.setIcon(ConsistListCellRenderer.getConsistIcon(tf.getAddressPanel().getConsistAddress(), ICN_FACT));
051            consistLabel.setText(tf.getAddressPanel().getConsistAddress().toString());
052            retPanel.add(consistLabel, BorderLayout.CENTER);
053        } else { // regular locomotive
054            ImageIcon icon = null;
055            String text;
056            if (tf.getRosterEntry() != null) {
057                icon = ICN_FACT.getIcon(tf.getAddressPanel().getRosterEntry());
058                text = tf.getAddressPanel().getRosterEntry().getId();
059            } else if ((tf.getAddressPanel().getCurrentAddress() != null) && (tf.getAddressPanel().getThrottle() != null)) {
060                switch (tf.getAddressPanel().getCurrentAddress().getNumber()) {
061                    case 0:
062                        text = Bundle.getMessage("ThrottleDCControl") + " - " + tf.getAddressPanel().getCurrentAddress();
063                        break;
064                    case 3:
065                        text = Bundle.getMessage("ThrottleDCCControl") + " - " + tf.getAddressPanel().getCurrentAddress();
066                        break;
067                    default:
068                        text = Bundle.getMessage("ThrottleAddress") + " " + tf.getAddressPanel().getCurrentAddress();
069                        break;
070                }
071            } else {
072                text = Bundle.getMessage("ThrottleNotAssigned");
073            }
074            if (icon != null) {
075                icon.setImageObserver(jtable);
076            }
077            JLabel locoID = new JLabel();
078            locoID.setHorizontalAlignment(JLabel.RIGHT);
079            locoID.setVerticalAlignment(JLabel.CENTER);
080            locoID.setIcon(icon);
081            locoID.setText(text);        
082            retPanel.add(locoID, BorderLayout.CENTER);
083        }
084        
085        if (tf.getAddressPanel().getThrottle() != null) {
086            final ThrottlesPreferences preferences = InstanceManager.getDefault(ThrottlesPreferences.class);
087            JPanel ctrlPanel = new JPanel();
088            ctrlPanel.setLayout(new BorderLayout());
089            Throttle thr = tf.getAddressPanel().getThrottle();
090            // direction
091            JLabel dir = new JLabel();
092            if (preferences.isUsingExThrottle() && preferences.isUsingLargeSpeedSlider()) {
093                if (thr.getIsForward()) {
094                    dir.setIcon(FWD_ICN);
095                } else {
096                    dir.setIcon(BCK_ICN);
097                }
098            } else {
099                if (thr.getIsForward()) {
100                    dir.setText(Bundle.getMessage("ButtonForward"));
101                } else {
102                    dir.setText(Bundle.getMessage("ButtonReverse"));
103                }
104            }
105            dir.setVerticalAlignment(JLabel.CENTER);
106            ctrlPanel.add(dir, BorderLayout.WEST);
107            // speed
108            if (preferences.isUsingExThrottle() && preferences.isUsingLargeSpeedSlider()) {
109                JLayeredPane layerPane = new JLayeredPane();
110                int cmpWidth = jtable.getWidth()/jtable.getColumnCount()/4 ;
111                layerPane.setPreferredSize(new Dimension(cmpWidth, LINE_HEIGHT - 8));
112                // speed
113                JProgressBar speedBar = new javax.swing.JProgressBar();
114                speedBar.setBounds(0,0,cmpWidth, LINE_HEIGHT - 8);
115                speedBar.setMinimum(0);
116                speedBar.setMaximum(1000);
117                speedBar.setValue((int) (thr.getSpeedSetting() * 1000f));
118                layerPane.add(speedBar, JLayeredPane.DEFAULT_LAYER);                
119                // estop & stop icon
120                if (thr.getSpeedSetting() == -1) {
121                    JLabel estop = new JLabel();                    
122                    estop.setHorizontalAlignment(JLabel.CENTER);
123                    estop.setIcon(ESTOP_ICN);
124                    estop.setBounds(0,0,cmpWidth, LINE_HEIGHT - 8);
125                    layerPane.add(estop, JLayeredPane.PALETTE_LAYER);
126                } else if (thr.getSpeedSetting() == 0) {
127                    JLabel stop = new JLabel();                    
128                    stop.setHorizontalAlignment(JLabel.CENTER);
129                    stop.setIcon(STOP_ICN);
130                    stop.setBounds(0,0,cmpWidth, LINE_HEIGHT - 8);
131                    layerPane.add(stop, JLayeredPane.PALETTE_LAYER);
132                }
133                ctrlPanel.add(layerPane, BorderLayout.EAST);
134            } else {
135                JLabel speedLabel = new JLabel("");
136                if (thr.getSpeedSetting() == -1) {
137                    speedLabel.setText(" " + Bundle.getMessage("ButtonEStop") + " ");
138                } else {
139                    speedLabel.setText(" " + (int) (thr.getSpeedSetting() * 100f) + "% ");
140                }
141                ctrlPanel.add(speedLabel, BorderLayout.CENTER);
142            }
143            ctrlPanel.setOpaque(false);
144            retPanel.add(ctrlPanel, BorderLayout.EAST);
145        }
146        // visibility -> selected
147        if (tf.isVisible()) {
148            Color selBackground = javax.swing.UIManager.getDefaults().getColor("List.selectionBackground");
149            if (selBackground == null) {
150                selBackground = Color.ORANGE;
151            }
152            Color selForeground = javax.swing.UIManager.getDefaults().getColor("List.selectionForeground");
153            if (selForeground == null) {
154                selForeground = Color.BLACK;
155            }
156            retPanel.setBackground(selBackground);
157            setForegroundAllComp( retPanel, selForeground );
158            retPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
159        } else {
160            retPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
161        }
162        return retPanel;
163    }
164    
165    private void setForegroundAllComp(JComponent cmp, Color color) {
166        if (cmp != null) {
167            cmp.setForeground(color);
168            for (Component c : cmp.getComponents()) {
169                if (c instanceof JComponent) {
170                    setForegroundAllComp( (JComponent) c, color);
171                }
172            }
173        }
174    }
175}