1 | // License: GPL. For details, see LICENSE file.
|
---|
2 |
|
---|
3 | package org.openstreetmap.josm.gui.widgets;
|
---|
4 |
|
---|
5 | import java.awt.Color;
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.ActionListener;
|
---|
9 |
|
---|
10 | import javax.swing.AbstractCellEditor;
|
---|
11 | import javax.swing.Action;
|
---|
12 | import javax.swing.Icon;
|
---|
13 | import javax.swing.JButton;
|
---|
14 | import javax.swing.JTable;
|
---|
15 | import javax.swing.UIManager;
|
---|
16 | import javax.swing.border.LineBorder;
|
---|
17 | import javax.swing.table.TableCellEditor;
|
---|
18 | import javax.swing.table.TableCellRenderer;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | *
|
---|
22 | * Class based on: http://www.camick.com/java/source/ButtonColumn.java
|
---|
23 | * https://tips4java.wordpress.com/2009/07/12/table-button-column/
|
---|
24 | *
|
---|
25 | */
|
---|
26 | public class ButtonColumn extends AbstractCellEditor implements TableCellRenderer, TableCellEditor, ActionListener {
|
---|
27 | private final Action action;
|
---|
28 | private final JButton renderButton;
|
---|
29 | private final JButton editButton;
|
---|
30 | private Object editorValue;
|
---|
31 | private String buttonName = null;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Creates a column that is rendered as a button with no action bound to the click event
|
---|
35 | */
|
---|
36 | public ButtonColumn() {
|
---|
37 | this(null);
|
---|
38 | }
|
---|
39 |
|
---|
40 | public ButtonColumn(Action action, String buttonName) {
|
---|
41 | this(action);
|
---|
42 | this.buttonName = buttonName;
|
---|
43 | }
|
---|
44 |
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Creates a column that is rendered as a button
|
---|
48 | *
|
---|
49 | * @param action action to be performed when button is pressed
|
---|
50 | */
|
---|
51 | public ButtonColumn(Action action) {
|
---|
52 | this.action = action;
|
---|
53 | renderButton = new JButton();
|
---|
54 | editButton = new JButton();
|
---|
55 | editButton.setFocusPainted(false);
|
---|
56 | editButton.addActionListener(this);
|
---|
57 | editButton.setBorder(new LineBorder(Color.BLUE));
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Override
|
---|
61 | public Object getCellEditorValue() {
|
---|
62 | return editorValue;
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | public void actionPerformed(ActionEvent e) {
|
---|
67 | this.action.actionPerformed(e);
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Override
|
---|
71 | public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
|
---|
72 | this.editorValue = value;
|
---|
73 | if (buttonName != null) {
|
---|
74 | editButton.setText(buttonName);
|
---|
75 | editButton.setIcon(null);
|
---|
76 | } else if (value == null) {
|
---|
77 | editButton.setText("");
|
---|
78 | editButton.setIcon(null);
|
---|
79 | } else if (value instanceof Icon) {
|
---|
80 | editButton.setText("");
|
---|
81 | editButton.setIcon((Icon) value);
|
---|
82 | } else {
|
---|
83 | editButton.setText(value.toString());
|
---|
84 | editButton.setIcon(null);
|
---|
85 | }
|
---|
86 | this.editorValue = value;
|
---|
87 | return editButton;
|
---|
88 | }
|
---|
89 |
|
---|
90 | @Override
|
---|
91 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
|
---|
92 | boolean hasFocus, int row, int column) {
|
---|
93 | if (isSelected) {
|
---|
94 | renderButton.setForeground(table.getSelectionForeground());
|
---|
95 | renderButton.setBackground(table.getSelectionBackground());
|
---|
96 | } else {
|
---|
97 | renderButton.setForeground(table.getForeground());
|
---|
98 | renderButton.setBackground(UIManager.getColor("Button.background"));
|
---|
99 | }
|
---|
100 |
|
---|
101 | renderButton.setFocusPainted(hasFocus);
|
---|
102 |
|
---|
103 | if (buttonName != null) {
|
---|
104 | renderButton.setText(buttonName);
|
---|
105 | renderButton.setIcon(null);
|
---|
106 | } else if (value == null) {
|
---|
107 | renderButton.setText("");
|
---|
108 | renderButton.setIcon(null);
|
---|
109 | } else if (value instanceof Icon) {
|
---|
110 | renderButton.setText("");
|
---|
111 | renderButton.setIcon((Icon) value);
|
---|
112 | } else {
|
---|
113 | renderButton.setText(value.toString());
|
---|
114 | renderButton.setIcon(null);
|
---|
115 | }
|
---|
116 | return renderButton;
|
---|
117 | }
|
---|
118 |
|
---|
119 | }
|
---|