[2512] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.util;
|
---|
| 3 |
|
---|
| 4 | import java.util.ArrayList;
|
---|
| 5 | import java.util.LinkedList;
|
---|
| 6 | import java.util.List;
|
---|
| 7 |
|
---|
[6912] | 8 | import javax.swing.CellEditor;
|
---|
[2512] | 9 | import javax.swing.event.CellEditorListener;
|
---|
| 10 | import javax.swing.event.ChangeEvent;
|
---|
| 11 |
|
---|
[6912] | 12 | /**
|
---|
| 13 | * Utility class used to ease implementation of {@link CellEditor} interface,
|
---|
| 14 | * or one of its sub-interfaces, for classes that cannot extend {@link javax.swing.AbstractCellEditor}.
|
---|
| 15 | * @since 6912
|
---|
| 16 | */
|
---|
| 17 | public class CellEditorSupport {
|
---|
| 18 | private final CellEditor editor;
|
---|
| 19 | private final List<CellEditorListener> listeners;
|
---|
[2512] | 20 |
|
---|
[6912] | 21 | /**
|
---|
| 22 | * Constructs a new {@code CellEditorSupport}.
|
---|
| 23 | * @param editor The cell editor backed by this
|
---|
| 24 | */
|
---|
| 25 | public CellEditorSupport(CellEditor editor) {
|
---|
[2512] | 26 | this.editor = editor;
|
---|
[6912] | 27 | this.listeners = new LinkedList<CellEditorListener>();
|
---|
[2512] | 28 | }
|
---|
| 29 |
|
---|
| 30 | protected List<CellEditorListener> getListeners() {
|
---|
| 31 | synchronized (this) {
|
---|
| 32 | return new ArrayList<CellEditorListener>(listeners);
|
---|
| 33 | }
|
---|
| 34 | }
|
---|
| 35 |
|
---|
[6912] | 36 | /**
|
---|
| 37 | * Worker for {@link CellEditor#addCellEditorListener(CellEditorListener)} method.
|
---|
| 38 | * @param l the CellEditorListener
|
---|
| 39 | */
|
---|
| 40 | public final void addCellEditorListener(CellEditorListener l) {
|
---|
[2512] | 41 | synchronized (this) {
|
---|
| 42 | if (l != null && ! listeners.contains(l)) {
|
---|
| 43 | listeners.add(l);
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
[6912] | 47 |
|
---|
| 48 | /**
|
---|
| 49 | * Worker for {@link CellEditor#removeCellEditorListener(CellEditorListener)} method.
|
---|
| 50 | * @param l the CellEditorListener
|
---|
| 51 | */
|
---|
| 52 | public final void removeCellEditorListener(CellEditorListener l) {
|
---|
[2512] | 53 | synchronized (this) {
|
---|
| 54 | if (l != null &&listeners.contains(l)) {
|
---|
| 55 | listeners.remove(l);
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[6912] | 60 | /**
|
---|
| 61 | * Fires "editing canceled" event to listeners.
|
---|
| 62 | */
|
---|
| 63 | public final void fireEditingCanceled() {
|
---|
[2512] | 64 | for (CellEditorListener listener: getListeners()) {
|
---|
| 65 | listener.editingCanceled(new ChangeEvent(editor));
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[6912] | 69 | /**
|
---|
| 70 | * Fires "editing stopped" event to listeners.
|
---|
| 71 | */
|
---|
| 72 | public final void fireEditingStopped() {
|
---|
[2512] | 73 | for (CellEditorListener listener: getListeners()) {
|
---|
| 74 | listener.editingStopped(new ChangeEvent(editor));
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 | }
|
---|