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