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