|
19 | 19 |
|
20 | 20 | import javax.swing.AbstractButton; |
21 | 21 | import javax.swing.JComboBox; |
| 22 | +import javax.swing.JComponent; |
22 | 23 | import javax.swing.JScrollBar; |
23 | 24 |
|
24 | 25 |
|
25 | 26 | public class A2SEvent implements Runnable { |
26 | 27 |
|
27 | 28 | private Event e; |
28 | | - private Component c; |
| 29 | + private Object target; |
29 | 30 |
|
30 | | - |
31 | | - public A2SEvent(Component c, AWTEvent e) { |
32 | | - this.c = c; |
33 | | - this.e = A2SEvent.convertToOld(e); |
| 31 | + public A2SEvent(AWTEvent e) { |
| 32 | + this.target = e.getSource(); |
| 33 | + this.e = A2SEvent.convertToOld(e); |
34 | 34 | } |
35 | 35 |
|
36 | 36 |
|
37 | 37 | @SuppressWarnings("deprecation") |
38 | 38 | @Override |
39 | 39 | public void run() { |
40 | | - Component c = this.c; |
41 | 40 | Event e = this.e; |
| 41 | + Component target = (Component) this.target; |
42 | 42 |
|
43 | 43 | /** |
44 | 44 | * Otherwise the states have not changed |
45 | 45 | * |
46 | 46 | * @j2sNative |
47 | 47 | * |
48 | | - * setTimeout(function() { c.handleEvent(e);}); |
| 48 | + * setTimeout(function() { |
49 | 49 | * |
50 | 50 | */ |
51 | | - { |
52 | | - c.handleEvent(e); |
53 | | - } |
| 51 | + target.postEvent(e); |
| 52 | + /** |
| 53 | + * Otherwise the states have not changed |
| 54 | + * |
| 55 | + * @j2sNative |
| 56 | + * |
| 57 | + * }); |
| 58 | + */ |
54 | 59 | } |
55 | 60 |
|
56 | 61 |
|
@@ -223,21 +228,37 @@ static Event convertToOld(AWTEvent e) { |
223 | 228 | return null; |
224 | 229 | } |
225 | 230 |
|
226 | | - public static Component addComponent(EventListener listener, Component comp) { |
| 231 | + public static Component addListener(JComponent container, Component comp) { |
| 232 | + A2SContainer top = (container == null ? null : ((A2SContainer) container.getTopLevelAncestor())); |
| 233 | + if (top == null) |
| 234 | + top = ((A2SContainer) ((JComponent) comp).getTopLevelAncestor()); |
| 235 | + if (top == null) |
| 236 | + return comp; |
| 237 | + A2SListener listener = top.getA2SListener(); |
227 | 238 | if (comp instanceof AbstractButton) { |
228 | | - ((AbstractButton) comp).addActionListener((ActionListener) listener); |
| 239 | + if (!isListener(((AbstractButton) comp).getActionListeners(), listener)) |
| 240 | + ((AbstractButton) comp).addActionListener((ActionListener) listener); |
229 | 241 | } else if (comp instanceof TextField) { |
230 | | - ((TextField) comp).addActionListener((ActionListener) listener); |
| 242 | + if (!isListener(((TextField) comp).getActionListeners(), listener)) |
| 243 | + ((TextField) comp).addActionListener((ActionListener) listener); |
231 | 244 | } else if (comp instanceof JComboBox) { |
232 | | - ((JComboBox) comp).addActionListener((ActionListener) listener); |
| 245 | + if (!isListener(((JComboBox<?>) comp).getActionListeners(), listener)) |
| 246 | + ((JComboBox<?>) comp).addActionListener((ActionListener) listener); |
233 | 247 | } else if (comp instanceof JScrollBar) { |
234 | | - ((JScrollBar) comp).addAdjustmentListener((AdjustmentListener) listener); |
| 248 | + if (!isListener(((JScrollBar) comp).getAdjustmentListeners(), listener)) |
| 249 | + ((JScrollBar) comp).addAdjustmentListener((AdjustmentListener) listener); |
235 | 250 | } |
236 | 251 | return comp; |
237 | 252 | } |
238 | 253 |
|
239 | 254 |
|
240 | | - |
241 | | - |
| 255 | + private static boolean isListener(EventListener[] listeners, EventListener listener) { |
| 256 | + if (listener == null) |
| 257 | + return true; |
| 258 | + for (int i = listeners.length; --i >= 0;) |
| 259 | + if (listeners[i] == listener) |
| 260 | + return true; |
| 261 | + return false; |
| 262 | + } |
242 | 263 |
|
243 | 264 | } |
0 commit comments