Skip to content

Commit f420b13

Browse files
committed
reconfigured A2S listeners
1 parent db09ade commit f420b13

File tree

16 files changed

+307
-148
lines changed

16 files changed

+307
-148
lines changed

sources/net.sf.j2s.core/test/src/a2s/A2SEvent.java

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,43 @@
1919

2020
import javax.swing.AbstractButton;
2121
import javax.swing.JComboBox;
22+
import javax.swing.JComponent;
2223
import javax.swing.JScrollBar;
2324

2425

2526
public class A2SEvent implements Runnable {
2627

2728
private Event e;
28-
private Component c;
29+
private Object target;
2930

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);
3434
}
3535

3636

3737
@SuppressWarnings("deprecation")
3838
@Override
3939
public void run() {
40-
Component c = this.c;
4140
Event e = this.e;
41+
Component target = (Component) this.target;
4242

4343
/**
4444
* Otherwise the states have not changed
4545
*
4646
* @j2sNative
4747
*
48-
* setTimeout(function() { c.handleEvent(e);});
48+
* setTimeout(function() {
4949
*
5050
*/
51-
{
52-
c.handleEvent(e);
53-
}
51+
target.postEvent(e);
52+
/**
53+
* Otherwise the states have not changed
54+
*
55+
* @j2sNative
56+
*
57+
* });
58+
*/
5459
}
5560

5661

@@ -223,21 +228,37 @@ static Event convertToOld(AWTEvent e) {
223228
return null;
224229
}
225230

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();
227238
if (comp instanceof AbstractButton) {
228-
((AbstractButton) comp).addActionListener((ActionListener) listener);
239+
if (!isListener(((AbstractButton) comp).getActionListeners(), listener))
240+
((AbstractButton) comp).addActionListener((ActionListener) listener);
229241
} else if (comp instanceof TextField) {
230-
((TextField) comp).addActionListener((ActionListener) listener);
242+
if (!isListener(((TextField) comp).getActionListeners(), listener))
243+
((TextField) comp).addActionListener((ActionListener) listener);
231244
} else if (comp instanceof JComboBox) {
232-
((JComboBox) comp).addActionListener((ActionListener) listener);
245+
if (!isListener(((JComboBox<?>) comp).getActionListeners(), listener))
246+
((JComboBox<?>) comp).addActionListener((ActionListener) listener);
233247
} else if (comp instanceof JScrollBar) {
234-
((JScrollBar) comp).addAdjustmentListener((AdjustmentListener) listener);
248+
if (!isListener(((JScrollBar) comp).getAdjustmentListeners(), listener))
249+
((JScrollBar) comp).addAdjustmentListener((AdjustmentListener) listener);
235250
}
236251
return comp;
237252
}
238253

239254

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+
}
242263

243264
}

sources/net.sf.j2s.core/test/src/a2s/A2SListener.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,70 +12,64 @@
1212

1313
public class A2SListener implements AdjustmentListener, ActionListener, KeyListener, MouseListener, MouseMotionListener {
1414

15-
private Applet applet;
16-
17-
public A2SListener(Applet applet) {
18-
this.applet = applet;
19-
}
20-
2115
@Override
2216
public void actionPerformed(ActionEvent e) {
23-
new A2SEvent(applet, e).run();
17+
new A2SEvent(e).run();
2418
}
2519

2620
@Override
2721
public void mouseDragged(MouseEvent e) {
28-
new A2SEvent(applet, e).run();
22+
new A2SEvent(e).run();
2923
}
3024

3125
@Override
3226
public void mouseMoved(MouseEvent e) {
33-
new A2SEvent(applet, e).run();
27+
new A2SEvent(e).run();
3428
}
3529

3630
@Override
3731
public void mouseClicked(MouseEvent e) {
38-
new A2SEvent(applet, e).run();
32+
new A2SEvent(e).run();
3933
}
4034

4135
@Override
4236
public void mousePressed(MouseEvent e) {
43-
new A2SEvent(applet, e).run();
37+
new A2SEvent(e).run();
4438
}
4539

4640
@Override
4741
public void mouseReleased(MouseEvent e) {
48-
new A2SEvent(applet, e).run();
42+
new A2SEvent(e).run();
4943
}
5044

5145
@Override
5246
public void mouseEntered(MouseEvent e) {
53-
new A2SEvent(applet, e).run();
47+
new A2SEvent(e).run();
5448
}
5549

5650
@Override
5751
public void mouseExited(MouseEvent e) {
58-
new A2SEvent(applet, e).run();
52+
new A2SEvent(e).run();
5953
}
6054

6155
@Override
6256
public void keyTyped(KeyEvent e) {
63-
new A2SEvent(applet, e).run();
57+
new A2SEvent(e).run();
6458
}
6559

6660
@Override
6761
public void keyPressed(KeyEvent e) {
68-
new A2SEvent(applet, e).run();
62+
new A2SEvent(e).run();
6963
}
7064

7165
@Override
7266
public void keyReleased(KeyEvent e) {
73-
new A2SEvent(applet, e).run();
67+
new A2SEvent(e).run();
7468
}
7569

7670
@Override
7771
public void adjustmentValueChanged(AdjustmentEvent e) {
78-
new A2SEvent(applet, e).run();
72+
new A2SEvent(e).run();
7973
}
8074

8175
}
Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,60 @@
11
package a2s;
22

3-
import java.awt.Component;
43
import java.awt.Graphics;
4+
import java.awt.HeadlessException;
55

66
import javax.swing.JApplet;
77
import javax.swing.JPanel;
88

99
@SuppressWarnings("serial")
10-
public class Applet extends JApplet {
11-
12-
private A2SListener listener;
13-
14-
// Note: applet.paint(g) needs to include super.paint(g), or buttons will not
15-
// show.
16-
17-
@Override
18-
public void init() {
19-
listener = new A2SListener(this);
20-
addMouseListener(listener);
21-
addMouseMotionListener(listener);
10+
public class Applet extends JApplet implements A2SContainer {
11+
12+
13+
public Applet() throws HeadlessException {
14+
super();
15+
listener = new A2SListener();
16+
addMouseListener(listener);
17+
addMouseMotionListener(listener);
2218
setContentPane(new JPanel() {
2319
@Override
2420
public void paintComponent(Graphics g) {
25-
super.paintComponent(g);
26-
//System.out.println("init " + this.getSize());
21+
super.paintComponent(g);
22+
// System.out.println("init " + this.getSize());
2723
try {
28-
if (this.getWidth() > 0)
29-
paintMe(g);
24+
if (this.getWidth() > 0)
25+
paintMe(g);
3026
} catch (Throwable e) {
3127
System.out.println(e);
3228
e.printStackTrace();
3329
/**
3430
* @j2sNative
3531
*
36-
* debugger;
32+
* debugger;
3733
*/
38-
{}
34+
{
35+
}
3936
}
4037
}
41-
});
38+
});
39+
}
40+
41+
protected A2SListener listener;
42+
43+
@Override
44+
public A2SListener getA2SListener() {
45+
return listener;
4246
}
4347

48+
// Note: applet.paint(g) needs to include super.paint(g), or buttons will
49+
// not
50+
// show.
51+
52+
// @Override
53+
// public void init() {
54+
// }
55+
4456
protected void paintMe(Graphics g) {
4557
System.out.println("paintMe has not been implemented!");
4658
}
4759

48-
@Override
49-
public Component add(Component comp) {
50-
super.add(comp);
51-
return A2SEvent.addComponent(listener, comp);
52-
}
53-
5460
}

sources/net.sf.j2s.core/test/src/a2s/Button.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package a2s;
22

33
import java.awt.Insets;
4+
import java.awt.event.ActionEvent;
45

56
import javax.swing.JButton;
67

7-
88
public class Button extends JButton {
99

1010
public Button() {
@@ -22,4 +22,9 @@ public Insets getMargin() {
2222
return awtInsets;
2323
}
2424

25+
protected void fireActionPerformed(ActionEvent event) {
26+
A2SEvent.addListener(null, this);
27+
super.fireActionPerformed(event);
28+
}
29+
2530
}

sources/net.sf.j2s.core/test/src/a2s/Checkbox.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package a2s;
22

3+
import java.awt.GraphicsEnvironment;
4+
import java.awt.HeadlessException;
5+
import java.awt.event.ActionEvent;
6+
37
import javax.swing.ButtonGroup;
48
import javax.swing.JCheckBox;
59
import javax.swing.JRadioButton;
@@ -22,7 +26,20 @@ public Checkbox(String string, boolean b) {
2226
super(string, b);
2327
}
2428

25-
public Checkbox() {
29+
public Checkbox(String label, boolean state, CheckboxGroup group)
30+
throws HeadlessException {
31+
setLabel(label);
32+
setState(state);
33+
if (group != null)
34+
group.add(this);
35+
if (state && (group != null)) {
36+
setSelected(state);
37+
}
38+
}
39+
public Checkbox(String label, CheckboxGroup group, boolean state)
40+
throws HeadlessException {
41+
this(label, state, group);
42+
} public Checkbox() {
2643
super();
2744
}
2845

@@ -34,4 +51,10 @@ public void setState(boolean b) {
3451
setSelected(b);
3552
}
3653

54+
protected void fireActionPerformed(ActionEvent event) {
55+
A2SEvent.addListener(null, this);
56+
super.fireActionPerformed(event);
57+
}
58+
59+
3760
}

sources/net.sf.j2s.core/test/src/a2s/Frame.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@
44

55
import javax.swing.JFrame;
66

7-
import java.awt.AWTEvent;
7+
public class Frame extends JFrame implements A2SContainer {
88

9-
public class Frame extends JFrame {
9+
10+
A2SListener listener;
11+
12+
@Override
13+
public A2SListener getA2SListener() {
14+
// TODO Auto-generated method stub
15+
return null;
16+
}
1017

1118

1219
public Frame() {
1320
super();
21+
listener = new A2SListener();
1422
}
1523

1624
public Frame(String title) {
@@ -55,4 +63,5 @@ public void unsetMenuBar() {
5563
public MenuBar getMenubar() {
5664
return (MenuBar) getJMenuBar();
5765
}
66+
5867
}
Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
package a2s;
22

3-
import java.awt.Component;
43
import java.awt.LayoutManager;
5-
import java.awt.event.ActionEvent;
6-
import java.awt.event.ActionListener;
7-
import java.util.EventListener;
84

9-
import javax.swing.AbstractButton;
10-
import javax.swing.JComboBox;
115
import javax.swing.JPanel;
126

137
public class Panel extends JPanel {
@@ -18,11 +12,6 @@ public Panel(LayoutManager layout) {
1812

1913
public Panel() {
2014
super();
21-
}
22-
23-
public Component add(Component comp) {
24-
super.add(comp);
25-
return A2SEvent.addComponent((EventListener) this.getTopLevelAncestor(), comp);
26-
}
15+
}
2716

2817
}

sources/net.sf.j2s.core/test/src/com/falstad/BarWaves.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,8 +1408,6 @@ void doPlay() {
14081408
failed = true;
14091409
else {
14101410
b[ii] = (byte) to_ulaw[128 + (int) dy];
1411-
if (ii < 100)
1412-
System.out.println(ii + " " + b[ii]);
14131411
}
14141412
}
14151413
sndmin /= scale;

0 commit comments

Comments
 (0)