- Timestamp:
- 2014-08-30T14:14:46+02:00 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/util/AdvancedKeyPressDetector.java
r7293 r7469 1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.gui.util; 3 4 import static org.openstreetmap.josm.tools.I18n.tr; 3 5 4 6 import java.awt.AWTEvent; … … 13 15 import java.util.Set; 14 16 import java.util.TreeSet; 17 15 18 import javax.swing.JFrame; 16 19 import javax.swing.SwingUtilities; … … 19 22 import org.openstreetmap.josm.Main; 20 23 21 import static org.openstreetmap.josm.tools.I18n.tr;22 24 /** 23 25 * Helper object that allows cross-platform detection of key press and realease events … … 38 40 39 41 /** 40 * Adds an object that wants to receive key press and release events 42 * Adds an object that wants to receive key press and release events. 43 * @param l listener to add 41 44 */ 42 public void addKeyListener(KeyPressReleaseListener l) {45 public synchronized void addKeyListener(KeyPressReleaseListener l) { 43 46 keyListeners.add(l); 44 47 } 45 48 46 49 /** 47 * Adds an object that wants to receive key modifier changed events 50 * Adds an object that wants to receive key modifier changed events. 51 * @param l listener to add 48 52 */ 49 public void addModifierListener(ModifierListener l) {53 public synchronized void addModifierListener(ModifierListener l) { 50 54 modifierListeners.add(l); 51 55 } 52 56 53 57 /** 54 * Removes the listener 58 * Removes the listener. 59 * @param l listener to remove 55 60 */ 56 public void removeKeyListener(KeyPressReleaseListener l) {61 public synchronized void removeKeyListener(KeyPressReleaseListener l) { 57 62 keyListeners.remove(l); 58 63 } 59 64 60 65 /** 61 * Removes the key modifier listener 66 * Removes the key modifier listener. 67 * @param l listener to remove 62 68 */ 63 public void removeModifierListener(ModifierListener l) {69 public synchronized void removeModifierListener(ModifierListener l) { 64 70 modifierListeners.remove(l); 65 71 } 66 67 72 68 73 /** … … 80 85 timer.stop(); 81 86 if (set.remove(releaseEvent.getKeyCode())) { 82 for (KeyPressReleaseListener q: keyListeners) { 83 q.doKeyReleased(releaseEvent); 87 synchronized (this) { 88 for (KeyPressReleaseListener q: keyListeners) { 89 q.doKeyReleased(releaseEvent); 90 } 84 91 } 85 92 } … … 93 100 */ 94 101 public void unregister() { 95 timer.stop(); set.clear(); 96 if (!keyListeners.isEmpty()) { 97 Main.warn(tr("Some of the key listeners forgot to remove themselves: {0}"), keyListeners.toString()); 98 } 99 if (!modifierListeners.isEmpty()) { 100 Main.warn(tr("Some of the key modifier listeners forgot to remove themselves: {0}"), modifierListeners.toString()); 102 timer.stop(); 103 set.clear(); 104 synchronized (this) { 105 if (!keyListeners.isEmpty()) { 106 Main.warn(tr("Some of the key listeners forgot to remove themselves: {0}"), keyListeners.toString()); 107 } 108 if (!modifierListeners.isEmpty()) { 109 Main.warn(tr("Some of the key modifier listeners forgot to remove themselves: {0}"), modifierListeners.toString()); 110 } 101 111 } 102 112 try { … … 112 122 timer.stop(); 113 123 } else if (set.add((e.getKeyCode()))) { 114 for (KeyPressReleaseListener q: keyListeners) { 115 q.doKeyPressed(e); 124 synchronized (this) { 125 for (KeyPressReleaseListener q: keyListeners) { 126 q.doKeyPressed(e); 127 } 116 128 } 117 129 } … … 120 132 timer.stop(); 121 133 if (set.remove(e.getKeyCode())) { 122 for (KeyPressReleaseListener q: keyListeners) { 123 q.doKeyReleased(e); 134 synchronized (this) { 135 for (KeyPressReleaseListener q: keyListeners) { 136 q.doKeyReleased(e); 137 } 124 138 } 125 139 } … … 142 156 if (previousModifiers != modif) { 143 157 previousModifiers = modif; 144 for (ModifierListener m: modifierListeners) { 145 m.modifiersChanged(modif); 158 synchronized (this) { 159 for (ModifierListener m: modifierListeners) { 160 m.modifiersChanged(modif); 161 } 146 162 } 147 163 }
Note:
See TracChangeset
for help on using the changeset viewer.