001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Pavel Dolgov
019: * @version $Revision: 1.2 $
020: */package org.apache.harmony.applet;
021:
022: import java.applet.Applet;
023: import java.applet.AppletContext;
024: import java.applet.AppletStub;
025: import java.awt.Container;
026: import java.awt.Toolkit;
027: import java.awt.Window;
028: import java.net.URL;
029:
030: import org.apache.harmony.awt.ComponentInternals;
031:
032: /**
033: * Applet's state and parameters, implementation <b>AppletStub</b> interface
034: */
035: class Proxy implements AppletStub {
036:
037: final DocumentSlice docSlice;
038: final Parameters params;
039:
040: private final AppletThread mainThread;
041: private Applet applet;
042: private Container parent;
043: private boolean active;
044:
045: Proxy(DocumentSlice ds, Parameters params) {
046: this .docSlice = ds;
047: this .params = params;
048:
049: mainThread = new AppletThread(this );
050: ds.add(this );
051: }
052:
053: Applet getApplet() {
054: return applet;
055: }
056:
057: void create() {
058: if (Thread.currentThread() == mainThread) {
059: createImpl();
060: } else {
061: if (!mainThread.isAlive()) {
062: mainThread.start();
063: }
064: mainThread.postCommand(new Command("create") {
065: @Override
066: public void run() {
067: createImpl();
068: }
069: });
070: }
071: }
072:
073: private void createImpl() {
074: Toolkit toolkit = Toolkit.getDefaultToolkit();
075: if (toolkit == null) {
076: throw new InternalError("Toolkit is null");
077: }
078:
079: if ((params.container != null)
080: && (params.container instanceof Container)) {
081:
082: parent = (Container) params.container;
083: } else {
084:
085: ComponentInternals ci = ComponentInternals
086: .getComponentInternals();
087: parent = ci.attachNativeWindow(params.parentWindowId);
088: }
089:
090: applet = createApplet();
091: applet.setStub(this );
092:
093: parent.add(applet);
094: parent.validate();
095:
096: initImpl();
097: startImpl();
098:
099: parent.setVisible(true);
100: }
101:
102: void start() {
103: if (Thread.currentThread() == mainThread) {
104: startImpl();
105: } else {
106: mainThread.postCommand(new Command("start") {
107: @Override
108: public void run() {
109: startImpl();
110: }
111: });
112: }
113: }
114:
115: private void startImpl() {
116: if (applet != null) {
117: applet.start();
118: active = true;
119: docSlice.showStatus("Applet started");
120: }
121: }
122:
123: void stop() {
124: if (Thread.currentThread() == mainThread) {
125: stopImpl();
126: } else {
127: mainThread.postCommand(new Command("stop") {
128: @Override
129: public void run() {
130: stopImpl();
131: }
132: });
133: }
134: }
135:
136: private void stopImpl() {
137: if (applet != null) {
138: active = false;
139: applet.stop();
140: docSlice.showStatus("Applet stopped");
141: }
142: }
143:
144: void init() {
145: if (Thread.currentThread() == mainThread) {
146: initImpl();
147: } else {
148: mainThread.postCommand(new Command("init") {
149: @Override
150: public void run() {
151: initImpl();
152: }
153: });
154: }
155: }
156:
157: private void initImpl() {
158: if (applet != null) {
159: applet.init();
160: docSlice.showStatus("Applet initialized");
161: }
162: }
163:
164: void destroy() {
165: if (Thread.currentThread() == mainThread) {
166: destroyImpl();
167: } else {
168: mainThread.postCommand(new Command("destroy") {
169: @Override
170: public void run() {
171: destroyImpl();
172: }
173: });
174: }
175: }
176:
177: private void destroyImpl() {
178: if (applet != null) {
179: applet.destroy();
180: docSlice.showStatus("Applet destroyed");
181: }
182: }
183:
184: void dispose() {
185: if (Thread.currentThread() == mainThread) {
186: disposeImpl();
187: } else {
188: mainThread.postCommand(new Command("dispose") {
189: @Override
190: public void run() {
191: disposeImpl();
192: }
193: });
194: }
195: }
196:
197: private void disposeImpl() {
198: if (applet != null) {
199: parent.setVisible(false);
200: parent.remove(applet);
201: if (parent instanceof Window) {
202: ((Window) parent).dispose();
203: }
204: applet.stop();
205: applet.destroy();
206: applet = null;
207: parent = null;
208: }
209:
210: mainThread.exit();
211: }
212:
213: private Applet createApplet() {
214: Class<?> appletClass;
215: try {
216: appletClass = docSlice.codeBase.classLoader
217: .loadClass(params.className);
218: } catch (ClassNotFoundException e) {
219: throw new RuntimeException(e);
220: }
221:
222: try {
223: return (Applet) appletClass.newInstance();
224: } catch (InstantiationException e) {
225: throw new RuntimeException(e);
226: } catch (IllegalAccessException e) {
227: throw new RuntimeException(e);
228: }
229: }
230:
231: public URL getCodeBase() {
232: return docSlice.codeBase.codeBase;
233: }
234:
235: public void appletResize(int width, int height) {
236: docSlice.codeBase.factory.appletResize(this , width, height);
237: }
238:
239: public AppletContext getAppletContext() {
240: return docSlice;
241: }
242:
243: public URL getDocumentBase() {
244: return docSlice.document.docBase;
245: }
246:
247: public String getParameter(String name) {
248: return params.getParameter(name);
249: }
250:
251: public boolean isActive() {
252: return active;
253: }
254:
255: }
|