| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.io;
|
|---|
| 3 |
|
|---|
| 4 | import java.awt.Component;
|
|---|
| 5 | import java.awt.event.MouseAdapter;
|
|---|
| 6 | import java.awt.event.MouseEvent;
|
|---|
| 7 | import java.util.EventObject;
|
|---|
| 8 | import java.util.concurrent.CopyOnWriteArrayList;
|
|---|
| 9 |
|
|---|
| 10 | import javax.swing.JCheckBox;
|
|---|
| 11 | import javax.swing.JTable;
|
|---|
| 12 | import javax.swing.event.CellEditorListener;
|
|---|
| 13 | import javax.swing.event.ChangeEvent;
|
|---|
| 14 | import javax.swing.table.TableCellEditor;
|
|---|
| 15 |
|
|---|
| 16 | /**
|
|---|
| 17 | * This is {@see TableCellEditor} for a boolean flag. It is used in two table columns of
|
|---|
| 18 | * {@see SaveLayersTable} and renders the values of {@see SaveLayerInfo#isDoSaveToFile()}
|
|---|
| 19 | * and {@see SaveLayerInfo#isDoUploadToServer()}
|
|---|
| 20 | *
|
|---|
| 21 | */
|
|---|
| 22 | class SaveFlagCellEditor extends JCheckBox implements TableCellEditor {
|
|---|
| 23 | private CopyOnWriteArrayList<CellEditorListener> listeners;
|
|---|
| 24 | private boolean value;
|
|---|
| 25 |
|
|---|
| 26 | public SaveFlagCellEditor() {
|
|---|
| 27 | listeners = new CopyOnWriteArrayList<CellEditorListener>();
|
|---|
| 28 | addMouseListener(
|
|---|
| 29 | new MouseAdapter() {
|
|---|
| 30 | @Override
|
|---|
| 31 | public void mouseExited(MouseEvent e) {
|
|---|
| 32 | stopCellEditing();
|
|---|
| 33 | }
|
|---|
| 34 | }
|
|---|
| 35 | );
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | public void addCellEditorListener(CellEditorListener l) {
|
|---|
| 39 | if (!listeners.contains(l)) {
|
|---|
| 40 | listeners.add(l);
|
|---|
| 41 | }
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | protected void fireEditingCanceled() {
|
|---|
| 45 | for (CellEditorListener l: listeners) {
|
|---|
| 46 | l.editingCanceled(new ChangeEvent(this));
|
|---|
| 47 | }
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | protected void fireEditingStopped() {
|
|---|
| 51 | for (CellEditorListener l: listeners) {
|
|---|
| 52 | l.editingStopped(new ChangeEvent(this));
|
|---|
| 53 | }
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | public void cancelCellEditing() {
|
|---|
| 57 | fireEditingCanceled();
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | public Object getCellEditorValue() {
|
|---|
| 61 | return value;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | public boolean isCellEditable(EventObject anEvent) {
|
|---|
| 65 | return true;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | public void removeCellEditorListener(CellEditorListener l) {
|
|---|
| 69 | if (listeners.contains(l)) {
|
|---|
| 70 | listeners.remove(l);
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | public boolean shouldSelectCell(EventObject anEvent) {
|
|---|
| 75 | return true;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | public void setInitialValue(boolean value) {
|
|---|
| 79 | this.value = value;
|
|---|
| 80 | setSelected(value);
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | public boolean stopCellEditing() {
|
|---|
| 84 | this.value = isSelected();
|
|---|
| 85 | fireEditingStopped();
|
|---|
| 86 | return true;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
|
|---|
| 90 | SaveLayerInfo info = (SaveLayerInfo)value;
|
|---|
| 91 | switch(column) {
|
|---|
| 92 | case 4: setInitialValue(info.isDoUploadToServer()); break;
|
|---|
| 93 | case 5: setInitialValue(info.isDoSaveToFile()); break;
|
|---|
| 94 | }
|
|---|
| 95 | return this;
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|