001package jmri.util.table; 002 003import java.awt.Color; 004import java.awt.Component; 005import java.awt.Font; 006import java.awt.event.ActionEvent; 007import java.awt.event.ActionListener; 008import java.util.EventObject; 009import javax.swing.Icon; 010import javax.swing.JButton; 011import javax.swing.JTable; 012import javax.swing.SwingUtilities; 013import javax.swing.table.TableCellEditor; 014import jmri.util.swing.JmriMouseEvent; 015import jmri.util.swing.JmriMouseListener; 016 017/** 018 * Make a JButton in a table cell function. 019 * <p> 020 * Works with {@link ButtonRenderer}. 021 * <p> 022 * This was adapted from Core Swing Advanced Programming, Prentice Hall 023 * <p> 024 * Changes: Remove DataWithIcon reference. Change package 025 * <p> 026 * This also now implements and registers as a MouseListener, so you can change 027 * the mouse-event behavior by overriding the needed methods. 028 */ 029public class ButtonEditor extends BasicCellEditor 030 implements ActionListener, 031 TableCellEditor, 032 JmriMouseListener { 033 034 public ButtonEditor(JButton button) { 035 super(button); 036 button.addActionListener(this); 037 button.addMouseListener(JmriMouseListener.adapt(this)); 038 button.putClientProperty("JComponent.sizeVariant", "small"); 039 button.putClientProperty("JButton.buttonType", "square"); 040 } 041 042 public void setForeground(Color foreground) { 043 this.foreground = foreground; 044 editor.setForeground(foreground); 045 } 046 047 public void setBackground(Color background) { 048 this.background = background; 049 editor.setBackground(background); 050 } 051 052 public void setFont(Font font) { 053 this.font = font; 054 editor.setFont(font); 055 } 056 057 @Override 058 public Object getCellEditorValue() { 059 return value; 060 } 061 062 @Override 063 public void editingStarted(EventObject event) { 064 // Edit starting - click the button if necessary 065 if (!(event instanceof java.awt.event.MouseEvent)) { 066 // Keyboard event - click the button 067 SwingUtilities.invokeLater(new Runnable() { 068 @Override 069 public void run() { 070 ((JButton) editor).doClick(); 071 } 072 }); 073 } 074 } 075 076 @Override 077 public Component getTableCellEditorComponent(JTable tbl, 078 Object value, boolean isSelected, 079 int row, int column) { 080 editor.setForeground(foreground != null ? foreground 081 : tbl.getForeground()); 082 editor.setBackground(background != null ? background 083 : tbl.getBackground()); 084 editor.setFont(font != null ? font : tbl.getFont()); 085 086 this.value = value; 087 setValue(value); 088 return editor; 089 } 090 091 protected void setValue(Object value) { 092 JButton button = (JButton) editor; 093 if (value == null) { 094 button.setText(""); 095 button.setIcon(null); 096 button.setEnabled(false); 097 } else if (value instanceof Icon) { 098 button.setText(""); 099 button.setIcon((Icon) value); 100 button.setEnabled(true); 101 } else { 102 button.setText(value.toString()); 103 button.setIcon(null); 104 button.setEnabled(true); 105 } 106 } 107 108 @Override 109 public void actionPerformed(ActionEvent evt) { 110 // Button pressed - stop the edit 111 stopCellEditing(); 112 } 113 114 @Override 115 public void mousePressed(JmriMouseEvent e) { 116 } 117 118 @Override 119 public void mouseExited(JmriMouseEvent e) { 120 } 121 122 @Override 123 public void mouseEntered(JmriMouseEvent e) { 124 } 125 126 @Override 127 public void mouseReleased(JmriMouseEvent e) { 128 } 129 130 @Override 131 public void mouseClicked(JmriMouseEvent e) { 132 } 133 134 protected Object value; 135 protected Color foreground; 136 protected Color background; 137 protected Font font; 138}