001package jmri.util.table;
002
003import javax.swing.*;
004import javax.swing.table.TableCellEditor;
005import java.awt.*;
006import java.awt.event.*;
007import java.util.EventObject;
008
009/**
010 * Make a JToggleButton in a table cell function. Used eg. in OBlock tables for units
011 * <p>
012 * Works with {@link ToggleButtonRenderer}.
013 * <p>
014 * Adapted from {@link ButtonRenderer}
015 * <p>
016 * This also now implements and registers as a MouseListener, so you can change
017 * the mouse-event behavior by overriding the needed methods.
018 * @author Egbert Broerse 2020
019 */
020public class ToggleButtonEditor extends BasicCellEditor
021        implements ActionListener,
022        TableCellEditor,
023        MouseListener {
024
025    public ToggleButtonEditor(JToggleButton button, String on, String off) {
026        super(button);
027        onText = on;
028        offText = off;
029        button.addActionListener(this);
030        button.addMouseListener(this);
031        //button.addItemListener(new ItemListener() {
032        //            public void itemStateChanged(ItemEvent ev) {
033        //                if (ev.getStateChange() == ItemEvent.SELECTED) {
034        //                    System.out.println("button is selected");
035        //                } else if (ev.getStateChange() == ItemEvent.DESELECTED) {
036        //                    System.out.println("button is not selected");
037        //                }
038        //            }
039        //});
040        button.putClientProperty("JComponent.sizeVariant", "small");
041        button.putClientProperty("JToggleButton.buttonType", "square");
042    }
043
044    public void setForeground(Color foreground) {
045        this.foreground = foreground;
046        editor.setForeground(foreground);
047    }
048
049    public void setBackground(Color background) {
050        this.background = background;
051        editor.setBackground(background);
052    }
053
054    public void setFont(Font font) {
055        this.font = font;
056        editor.setFont(font);
057    }
058
059    @Override
060    public Object getCellEditorValue() {
061        return value;
062    }
063
064    @Override
065    public void editingStarted(EventObject event) {
066        // Edit starting - click the button if necessary
067        if (!(event instanceof MouseEvent)) {
068            // Keyboard event - click the button
069            SwingUtilities.invokeLater(new Runnable() {
070                @Override
071                public void run() {
072                    ((JToggleButton) editor).doClick();
073                }
074            });
075        }
076    }
077
078    @Override
079    public Component getTableCellEditorComponent(JTable tbl,
080            Object value, boolean isSelected,
081            int row, int column) {
082        editor.setForeground(foreground != null ? foreground
083                : tbl.getForeground());
084        editor.setBackground(background != null ? background
085                : tbl.getBackground());
086        editor.setFont(font != null ? font : tbl.getFont());
087
088        this.value = value;
089        setValue(value, isSelected);
090        return editor;
091    }
092
093    protected void setValue(Object value, Boolean sel) {
094        JToggleButton button = (JToggleButton) editor;
095            switch (value.toString()) {
096                case "true":
097                    button.setText(onText);
098//                    button.setSelected(sel);
099                    button.setEnabled(true);
100                    break;
101                case "false":
102                    button.setText(offText);
103//                    button.setSelected(sel);
104                    button.setEnabled(true);
105                    break;
106                default:
107                    button.setText("?");
108                    button.setEnabled(false);
109            }
110        }
111
112    @Override
113    public void actionPerformed(ActionEvent evt) {
114        JToggleButton tBtn = (JToggleButton)evt.getSource();
115        if (tBtn.isSelected()) {
116            tBtn.setText(onText);
117            value = Boolean.TRUE;
118        } else {
119            tBtn.setText(offText);
120            value = Boolean.FALSE;
121        }
122        // Button pressed - stop the edit
123        stopCellEditing();
124    }
125
126    @Override
127    public void mousePressed(MouseEvent e) {
128    }
129
130    @Override
131    public void mouseExited(MouseEvent e) {
132    }
133
134    @Override
135    public void mouseEntered(MouseEvent e) {
136    }
137
138    @Override
139    public void mouseReleased(MouseEvent e) {
140    }
141
142    @Override
143    public void mouseClicked(MouseEvent e) {
144    }
145
146    protected String onText;
147    protected String offText;
148    protected Object value;
149    protected Color foreground;
150    protected Color background;
151    protected Font font;
152}