source: josm/trunk/src/org/openstreetmap/josm/gui/io/ActionFlagsTableCell.java@ 6084

Last change on this file since 6084 was 6084, checked in by bastiK, 11 years ago

see #8902 - add missing @Override annotations (patch by shinigami)

File size: 4.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.Dimension;
8import java.awt.GridBagLayout;
9import java.awt.event.ActionEvent;
10import java.awt.event.ActionListener;
11import java.util.EventObject;
12import java.util.concurrent.CopyOnWriteArrayList;
13
14import javax.swing.AbstractAction;
15import javax.swing.ActionMap;
16import javax.swing.JCheckBox;
17import javax.swing.JPanel;
18import javax.swing.JTable;
19import javax.swing.event.CellEditorListener;
20import javax.swing.event.ChangeEvent;
21import javax.swing.table.TableCellEditor;
22import javax.swing.table.TableCellRenderer;
23
24import 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 */
37class ActionFlagsTableCell extends JPanel implements TableCellRenderer, TableCellEditor {
38 protected final JCheckBox[] checkBoxes = new JCheckBox[2];
39 private CopyOnWriteArrayList<CellEditorListener> listeners;
40
41 private ActionListener al = new ActionListener() {
42 @Override
43 public void actionPerformed(ActionEvent e) {
44 fireEditingStopped();
45 }
46 };
47
48 public ActionFlagsTableCell() {
49 super();
50 listeners = new CopyOnWriteArrayList<CellEditorListener>();
51
52 checkBoxes[0] = new JCheckBox(tr("Upload"));
53 checkBoxes[1] = new JCheckBox(tr("Save"));
54 setLayout(new GridBagLayout());
55
56 ActionMap am = getActionMap();
57 for(int i=0; i<checkBoxes.length; i++) {
58 final JCheckBox b = checkBoxes[i];
59 add(b, GBC.eol().fill(GBC.HORIZONTAL));
60 b.setPreferredSize(new Dimension(b.getPreferredSize().width, 19));
61 b.addActionListener(al);
62 am.put(b.getText(), new AbstractAction() {
63 @Override
64 public void actionPerformed(ActionEvent e) {
65 b.setSelected(!b.isSelected());
66 fireEditingStopped();
67 }
68 });
69 }
70
71 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>"));
72 }
73
74 protected void updateCheckboxes(Object v) {
75 if (checkBoxes[0] != null && checkBoxes[1] != null) {
76 boolean[] values;
77 if(v instanceof SaveLayerInfo) {
78 values = new boolean[2];
79 values[0] = ((SaveLayerInfo) v).isDoUploadToServer();
80 values[1] = ((SaveLayerInfo) v).isDoSaveToFile();
81 } else {
82 values = (boolean[]) v;
83 }
84 checkBoxes[0].setSelected(values[0]);
85 checkBoxes[1].setSelected(values[1]);
86 }
87 }
88
89 @Override
90 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
91 updateCheckboxes(value);
92 return this;
93 }
94
95 @Override
96 public void addCellEditorListener(CellEditorListener l) {
97 if (l != null) {
98 listeners.addIfAbsent(l);
99 }
100 }
101
102 protected void fireEditingCanceled() {
103 for (CellEditorListener l: listeners) {
104 l.editingCanceled(new ChangeEvent(this));
105 }
106 }
107
108 protected void fireEditingStopped() {
109 for (CellEditorListener l: listeners) {
110 l.editingStopped(new ChangeEvent(this));
111 }
112 }
113
114 @Override
115 public void cancelCellEditing() {
116 fireEditingCanceled();
117 }
118
119 @Override
120 public Object getCellEditorValue() {
121 boolean[] values = new boolean[2];
122 values[0] = checkBoxes[0].isSelected();
123 values[1] = checkBoxes[1].isSelected();
124 return values;
125 }
126
127 @Override
128 public boolean isCellEditable(EventObject anEvent) {
129 return true;
130 }
131
132 @Override
133 public void removeCellEditorListener(CellEditorListener l) {
134 listeners.remove(l);
135 }
136
137 @Override
138 public boolean shouldSelectCell(EventObject anEvent) {
139 return true;
140 }
141
142 @Override
143 public boolean stopCellEditing() {
144 fireEditingStopped();
145 return true;
146 }
147
148 @Override
149 public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
150 updateCheckboxes(value);
151 return this;
152 }
153}
Note: See TracBrowser for help on using the repository browser.