1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.Dimension;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.ActionListener;
|
---|
11 | import java.util.EventObject;
|
---|
12 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
13 |
|
---|
14 | import javax.swing.AbstractAction;
|
---|
15 | import javax.swing.ActionMap;
|
---|
16 | import javax.swing.JCheckBox;
|
---|
17 | import javax.swing.JPanel;
|
---|
18 | import javax.swing.JTable;
|
---|
19 | import javax.swing.event.CellEditorListener;
|
---|
20 | import javax.swing.event.ChangeEvent;
|
---|
21 | import javax.swing.table.TableCellEditor;
|
---|
22 | import javax.swing.table.TableCellRenderer;
|
---|
23 |
|
---|
24 | import org.openstreetmap.josm.tools.GBC;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * This class creates a table cell that features two checkboxes, Upload and Save. It
|
---|
28 | * handles everything on its own, in other words it renders itself and also functions
|
---|
29 | * as editor so the checkboxes may be set by the user.
|
---|
30 | *
|
---|
31 | * Intended usage is like this:
|
---|
32 | * ActionFlagsTableCell aftc = new ActionFlagsTableCell();
|
---|
33 | * col = new TableColumn(0);
|
---|
34 | * col.setCellRenderer(aftc);
|
---|
35 | * col.setCellEditor(aftc);
|
---|
36 | */
|
---|
37 | class ActionFlagsTableCell extends JPanel implements TableCellRenderer, TableCellEditor {
|
---|
38 | protected JCheckBox[] checkBoxes = new JCheckBox[2];
|
---|
39 | private CopyOnWriteArrayList<CellEditorListener> listeners;
|
---|
40 |
|
---|
41 | private ActionListener al = new ActionListener() {
|
---|
42 | public void actionPerformed(ActionEvent e) {
|
---|
43 | fireEditingStopped();
|
---|
44 | }
|
---|
45 | };
|
---|
46 |
|
---|
47 | public ActionFlagsTableCell() {
|
---|
48 | super();
|
---|
49 | listeners = new CopyOnWriteArrayList<CellEditorListener>();
|
---|
50 |
|
---|
51 | setLayout(new GridBagLayout());
|
---|
52 | checkBoxes[0] = new JCheckBox(tr("Upload"));
|
---|
53 | checkBoxes[1] = new JCheckBox(tr("Save"));
|
---|
54 |
|
---|
55 | ActionMap am = getActionMap();
|
---|
56 | for(int i=0; i<checkBoxes.length; i++) {
|
---|
57 | final JCheckBox b = checkBoxes[i];
|
---|
58 | add(b, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
59 | b.setPreferredSize(new Dimension(b.getPreferredSize().width, 19));
|
---|
60 | b.addActionListener(al);
|
---|
61 | am.put(b.getText(), new AbstractAction() {
|
---|
62 | public void actionPerformed(ActionEvent e) {
|
---|
63 | b.setSelected(!b.isSelected());
|
---|
64 | fireEditingStopped();
|
---|
65 | }
|
---|
66 | });
|
---|
67 | }
|
---|
68 |
|
---|
69 | setToolTipText(tr("<html>Select which actions to perform for this layer, if you click the leftmost button.<br/>Check \"upload\" to upload the changes to the OSM server.<br/>Check \"Save\" to save the layer to the file specified on the left.</html>"));
|
---|
70 | }
|
---|
71 |
|
---|
72 | protected void updateCheckboxes(Object v) {
|
---|
73 | boolean[] values;
|
---|
74 | if(v instanceof SaveLayerInfo) {
|
---|
75 | values = new boolean[2];
|
---|
76 | values[0] = ((SaveLayerInfo) v).isDoUploadToServer();
|
---|
77 | values[1] = ((SaveLayerInfo) v).isDoSaveToFile();
|
---|
78 | } else {
|
---|
79 | values = (boolean[]) v;
|
---|
80 | }
|
---|
81 | checkBoxes[0].setSelected(values[0]);
|
---|
82 | checkBoxes[1].setSelected(values[1]);
|
---|
83 | }
|
---|
84 |
|
---|
85 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
86 | updateCheckboxes(value);
|
---|
87 | return this;
|
---|
88 | }
|
---|
89 |
|
---|
90 | public void addCellEditorListener(CellEditorListener l) {
|
---|
91 | if (l != null) {
|
---|
92 | listeners.addIfAbsent(l);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | protected void fireEditingCanceled() {
|
---|
97 | for (CellEditorListener l: listeners) {
|
---|
98 | l.editingCanceled(new ChangeEvent(this));
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | protected void fireEditingStopped() {
|
---|
103 | for (CellEditorListener l: listeners) {
|
---|
104 | l.editingStopped(new ChangeEvent(this));
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | public void cancelCellEditing() {
|
---|
109 | fireEditingCanceled();
|
---|
110 | }
|
---|
111 |
|
---|
112 | public Object getCellEditorValue() {
|
---|
113 | boolean[] values = new boolean[2];
|
---|
114 | values[0] = checkBoxes[0].isSelected();
|
---|
115 | values[1] = checkBoxes[1].isSelected();
|
---|
116 | return values;
|
---|
117 | }
|
---|
118 |
|
---|
119 | public boolean isCellEditable(EventObject anEvent) {
|
---|
120 | return true;
|
---|
121 | }
|
---|
122 |
|
---|
123 | public void removeCellEditorListener(CellEditorListener l) {
|
---|
124 | listeners.remove(l);
|
---|
125 | }
|
---|
126 |
|
---|
127 | public boolean shouldSelectCell(EventObject anEvent) {
|
---|
128 | return true;
|
---|
129 | }
|
---|
130 |
|
---|
131 | public boolean stopCellEditing() {
|
---|
132 | fireEditingStopped();
|
---|
133 | return true;
|
---|
134 | }
|
---|
135 |
|
---|
136 | public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
|
---|
137 | updateCheckboxes(value);
|
---|
138 | return this;
|
---|
139 | }
|
---|
140 | }
|
---|