|
1 |
| package edu.rice.cs.plt.swing; |
|
2 |
| |
|
3 |
| import java.applet.Applet; |
|
4 |
| import java.applet.AppletContext; |
|
5 |
| import java.applet.AppletStub; |
|
6 |
| import java.applet.AudioClip; |
|
7 |
| import java.awt.Graphics; |
|
8 |
| import java.awt.Image; |
|
9 |
| import java.awt.Toolkit; |
|
10 |
| import java.io.InputStream; |
|
11 |
| import java.net.MalformedURLException; |
|
12 |
| import java.net.URL; |
|
13 |
| import java.util.Collections; |
|
14 |
| import java.util.Enumeration; |
|
15 |
| import java.util.HashMap; |
|
16 |
| import java.util.Iterator; |
|
17 |
| import java.util.Map; |
|
18 |
| |
|
19 |
| import javax.swing.JComponent; |
|
20 |
| |
|
21 |
| import edu.rice.cs.plt.io.IOUtil; |
|
22 |
| import edu.rice.cs.plt.iter.IterUtil; |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| public class AppletComponent extends JComponent { |
|
43 |
| |
|
44 |
| private enum State { FRESH, PAUSED, RUNNING }; |
|
45 |
| |
|
46 |
| private final Applet _applet; |
|
47 |
| private final Map<String, String> _params; |
|
48 |
| private final URL _root; |
|
49 |
| private State _state; |
|
50 |
| |
|
51 |
0
| public AppletComponent(Applet applet) {
|
|
52 |
0
| this(applet, null, Collections.<String, String>emptyMap());
|
|
53 |
| } |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
0
| public AppletComponent(Applet applet, URL root, Map<String, String> params) {
|
|
63 |
0
| _applet = applet;
|
|
64 |
0
| _params = params;
|
|
65 |
0
| if (root == null) {
|
|
66 |
0
| try { _root = IOUtil.WORKING_DIRECTORY.toURI().toURL(); }
|
|
67 |
0
| catch (MalformedURLException e) { throw new RuntimeException("Can't convert the working dir to a URL"); }
|
|
68 |
| } |
|
69 |
0
| else { _root = root; }
|
|
70 |
0
| _state = State.FRESH;
|
|
71 |
0
| add(_applet);
|
|
72 |
0
| _applet.setStub(new Stub());
|
|
73 |
| } |
|
74 |
| |
|
75 |
0
| public AppletComponent(Applet applet, int width, int height) {
|
|
76 |
0
| this(applet, width, height, null, Collections.<String, String>emptyMap());
|
|
77 |
| } |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
0
| public AppletComponent(Applet applet, int width, int height, URL root, Map<String, String> params) {
|
|
89 |
0
| this(applet, root, params);
|
|
90 |
0
| _applet.setSize(width, height);
|
|
91 |
| } |
|
92 |
| |
|
93 |
0
| protected void paintComponent(Graphics g) {
|
|
94 |
0
| updateState();
|
|
95 |
0
| super.paintComponent(g);
|
|
96 |
| } |
|
97 |
| |
|
98 |
0
| private void updateState() {
|
|
99 |
0
| if (!_state.equals(State.RUNNING)) {
|
|
100 |
0
| if (_state.equals(State.FRESH)) {
|
|
101 |
0
| _state = State.PAUSED;
|
|
102 |
| |
|
103 |
| |
|
104 |
0
| edu.rice.cs.plt.concurrent.ConcurrentUtil.runInThread(new Runnable() {
|
|
105 |
0
| public void run() {
|
|
106 |
0
| _applet.init();
|
|
107 |
0
| _applet.start();
|
|
108 |
0
| _state = State.RUNNING;
|
|
109 |
0
| if (!_applet.getSize().equals(getSize())) { _applet.setSize(getSize()); }
|
|
110 |
0
| validate();
|
|
111 |
| } |
|
112 |
| }); |
|
113 |
| } |
|
114 |
| } |
|
115 |
0
| if (_state.equals(State.RUNNING)) {
|
|
116 |
| |
|
117 |
0
| if (!_applet.getSize().equals(getSize())) { _applet.setSize(getSize()); }
|
|
118 |
| } |
|
119 |
| } |
|
120 |
| |
|
121 |
| private class Stub implements AppletStub, AppletContext { |
|
122 |
| private final Map<String, InputStream> _streams; |
|
123 |
0
| public Stub() { _streams = new HashMap<String, InputStream>(); }
|
|
124 |
0
| public boolean isActive() { return _state.equals(State.RUNNING); }
|
|
125 |
0
| public URL getDocumentBase() { return _root; }
|
|
126 |
0
| public URL getCodeBase() { return _root; }
|
|
127 |
0
| public String getParameter(String name) { return _params.get(name); }
|
|
128 |
0
| public AppletContext getAppletContext() { return this; }
|
|
129 |
0
| public void appletResize(int width, int height) { AppletComponent.this.setSize(width, height); }
|
|
130 |
0
| public AudioClip getAudioClip(URL url) {
|
|
131 |
0
| return new AudioClip() {
|
|
132 |
0
| public void play() {}
|
|
133 |
0
| public void loop() {}
|
|
134 |
0
| public void stop() {}
|
|
135 |
| }; |
|
136 |
| } |
|
137 |
0
| public Image getImage(URL url) { return Toolkit.getDefaultToolkit().getImage(url); }
|
|
138 |
0
| public Applet getApplet(String name) { return null; }
|
|
139 |
0
| public Enumeration<Applet> getApplets() {
|
|
140 |
0
| return IterUtil.asEnumeration(IterUtil.singleton(_applet).iterator());
|
|
141 |
| } |
|
142 |
0
| public void showDocument(URL url) {}
|
|
143 |
0
| public void showDocument(URL url, String target) {}
|
|
144 |
0
| public void showStatus(String status) {}
|
|
145 |
0
| public void setStream(String key, InputStream stream) { _streams.put(key, stream); }
|
|
146 |
0
| public InputStream getStream(String key) { return _streams.get(key); }
|
|
147 |
0
| public Iterator<String> getStreamKeys() { return _streams.keySet().iterator(); }
|
|
148 |
| } |
|
149 |
| |
|
150 |
| } |