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 |
|
---|
8 | import javax.swing.CellEditor;
|
---|
9 | import javax.swing.event.CellEditorListener;
|
---|
10 | import javax.swing.event.ChangeEvent;
|
---|
11 |
|
---|
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;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Constructs a new {@code CellEditorSupport}.
|
---|
23 | * @param editor The cell editor backed by this
|
---|
24 | */
|
---|
25 | public CellEditorSupport(CellEditor editor) {
|
---|
26 | this.editor = editor;
|
---|
27 | this.listeners = new LinkedList<CellEditorListener>();
|
---|
28 | }
|
---|
29 |
|
---|
30 | protected List<CellEditorListener> getListeners() {
|
---|
31 | synchronized (this) {
|
---|
32 | return new ArrayList<CellEditorListener>(listeners);
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Worker for {@link CellEditor#addCellEditorListener(CellEditorListener)} method.
|
---|
38 | * @param l the CellEditorListener
|
---|
39 | */
|
---|
40 | public final void addCellEditorListener(CellEditorListener l) {
|
---|
41 | synchronized (this) {
|
---|
42 | if (l != null && ! listeners.contains(l)) {
|
---|
43 | listeners.add(l);
|
---|
44 | }
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Worker for {@link CellEditor#removeCellEditorListener(CellEditorListener)} method.
|
---|
50 | * @param l the CellEditorListener
|
---|
51 | */
|
---|
52 | public final void removeCellEditorListener(CellEditorListener l) {
|
---|
53 | synchronized (this) {
|
---|
54 | if (l != null &&listeners.contains(l)) {
|
---|
55 | listeners.remove(l);
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Fires "editing canceled" event to listeners.
|
---|
62 | */
|
---|
63 | public final void fireEditingCanceled() {
|
---|
64 | for (CellEditorListener listener: getListeners()) {
|
---|
65 | listener.editingCanceled(new ChangeEvent(editor));
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Fires "editing stopped" event to listeners.
|
---|
71 | */
|
---|
72 | public final void fireEditingStopped() {
|
---|
73 | for (CellEditorListener listener: getListeners()) {
|
---|
74 | listener.editingStopped(new ChangeEvent(editor));
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|