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

Last change on this file since 5003 was 5003, checked in by xeen, 12 years ago

fix #3409

File size: 4.5 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 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}
Note: See TracBrowser for help on using the repository browser.