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

Last change on this file since 6901 was 6104, checked in by Don-vip, 11 years ago

see #8902 - Small performance enhancements / coding style (patch by shinigami):

  • while -> foreach
  • for -> for each

plus:

  • cleanup of FileDrop class to make it more integrated into JOSM core + remove warnings
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 (final JCheckBox b : checkBoxes) {
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 @Override
63 public void actionPerformed(ActionEvent e) {
64 b.setSelected(!b.isSelected());
65 fireEditingStopped();
66 }
67 });
68 }
69
70 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>"));
71 }
72
73 protected void updateCheckboxes(Object v) {
74 if (checkBoxes[0] != null && checkBoxes[1] != null) {
75 boolean[] values;
76 if(v instanceof SaveLayerInfo) {
77 values = new boolean[2];
78 values[0] = ((SaveLayerInfo) v).isDoUploadToServer();
79 values[1] = ((SaveLayerInfo) v).isDoSaveToFile();
80 } else {
81 values = (boolean[]) v;
82 }
83 checkBoxes[0].setSelected(values[0]);
84 checkBoxes[1].setSelected(values[1]);
85 }
86 }
87
88 @Override
89 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
90 updateCheckboxes(value);
91 return this;
92 }
93
94 @Override
95 public void addCellEditorListener(CellEditorListener l) {
96 if (l != null) {
97 listeners.addIfAbsent(l);
98 }
99 }
100
101 protected void fireEditingCanceled() {
102 for (CellEditorListener l: listeners) {
103 l.editingCanceled(new ChangeEvent(this));
104 }
105 }
106
107 protected void fireEditingStopped() {
108 for (CellEditorListener l: listeners) {
109 l.editingStopped(new ChangeEvent(this));
110 }
111 }
112
113 @Override
114 public void cancelCellEditing() {
115 fireEditingCanceled();
116 }
117
118 @Override
119 public Object getCellEditorValue() {
120 boolean[] values = new boolean[2];
121 values[0] = checkBoxes[0].isSelected();
122 values[1] = checkBoxes[1].isSelected();
123 return values;
124 }
125
126 @Override
127 public boolean isCellEditable(EventObject anEvent) {
128 return true;
129 }
130
131 @Override
132 public void removeCellEditorListener(CellEditorListener l) {
133 listeners.remove(l);
134 }
135
136 @Override
137 public boolean shouldSelectCell(EventObject anEvent) {
138 return true;
139 }
140
141 @Override
142 public boolean stopCellEditing() {
143 fireEditingStopped();
144 return true;
145 }
146
147 @Override
148 public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
149 updateCheckboxes(value);
150 return this;
151 }
152}
Note: See TracBrowser for help on using the repository browser.