001: /*
002: * $RCSfile: JMainFrame.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.4 $
041: * $Date: 2007/02/09 17:20:10 $
042: * $State: Exp $
043: */
044:
045: // JMainFrame - run an Applet as an application
046: //
047: // Copyright (C) 1996 by Jef Poskanzer <jef@acme.com>. All rights reserved.
048: //
049: // Redistribution and use in source and binary forms, with or without
050: // modification, are permitted provided that the following conditions
051: // are met:
052: // 1. Redistributions of source code must retain the above copyright
053: // notice, this list of conditions and the following disclaimer.
054: // 2. Redistributions in binary form must reproduce the above copyright
055: // notice, this list of conditions and the following disclaimer in the
056: // documentation and/or other materials provided with the distribution.
057: //
058: // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
059: // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
060: // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
061: // ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
062: // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
063: // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
064: // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
065: // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
066: // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
067: // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
068: // SUCH DAMAGE.
069: //
070: // Visit the ACME Labs Java page for up-to-date versions of this and other
071: // fine Java utilities: http://www.acme.com/java/
072: // ---------------------------------------------------------------------
073: package com.sun.j3d.utils.applet;
074:
075: import java.applet.*;
076: import java.awt.*;
077: import java.awt.event.*;
078: import java.awt.image.*;
079: import java.io.*;
080: import java.net.*;
081: import javax.swing.*;
082: import java.util.*;
083:
084: public class JMainFrame extends JFrame implements Runnable, AppletStub,
085: AppletContext {
086:
087: private String[] args = null;
088: private static int instances = 0;
089: private String name;
090: private Applet applet;
091: private Label label = null;
092: private Dimension appletSize;
093:
094: private static final String PARAM_PROP_PREFIX = "parameter.";
095:
096: public JMainFrame(Applet applet, String[] args, int width,
097: int height) {
098: build(applet, args, width, height);
099: }
100:
101: public JMainFrame(Applet applet, String[] args) {
102: build(applet, args, -1, -1);
103: }
104:
105: public JMainFrame(Applet applet, int width, int height) {
106: build(applet, null, width, height);
107: }
108:
109: private void build(Applet applet, String[] args, int width,
110: int height) {
111: ++instances;
112: this .applet = applet;
113: this .args = args;
114: applet.setStub(this );
115: name = applet.getClass().getName();
116: setTitle(name);
117:
118: // Set up properties.
119: Properties props = System.getProperties();
120: props.put("browser", "Acme.MainFrame");
121: props.put("browser.version", "11jul96");
122: props.put("browser.vendor", "Acme Laboratories");
123: props.put("browser.vendor.url", "http://www.acme.com/");
124:
125: // Turn args into parameters by way of the properties list.
126: if (args != null)
127: parseArgs(args, props);
128:
129: // If width and height are specified in the parameters, override
130: // the compiled-in values.
131: String widthStr = getParameter("width");
132: if (widthStr != null)
133: width = Integer.parseInt(widthStr);
134: String heightStr = getParameter("height");
135: if (heightStr != null)
136: height = Integer.parseInt(heightStr);
137:
138: // Were width and height specified somewhere?
139: if (width == -1 || height == -1) {
140: System.err.println("Width and height must be specified.");
141: return;
142: }
143:
144: // Lay out components.
145: Container contentPane = getContentPane();
146: contentPane.add(applet, BorderLayout.CENTER);
147:
148: // Set up size.
149: pack();
150: validate();
151: appletSize = applet.getSize();
152: applet.setSize(width, height);
153: setVisible(true);
154:
155: /*
156: Added WindowListener inner class to detect close events.
157: */
158: SecurityManager sm = System.getSecurityManager();
159: boolean doExit = true;
160: if (sm != null) {
161: try {
162: sm.checkExit(0);
163: } catch (SecurityException e) {
164: doExit = false;
165: }
166: }
167:
168: final boolean _doExit = doExit;
169:
170: // WindowListener inner class to detect close events.
171: addWindowListener(new WindowAdapter() {
172: public void windowClosing(WindowEvent winEvent) {
173: if (JMainFrame.this .applet != null) {
174: JMainFrame.this .applet.destroy();
175: }
176: hide();
177: try {
178: dispose();
179: } catch (IllegalStateException e) {
180: }
181:
182: if (_doExit) {
183: System.exit(0);
184: }
185:
186: }
187: });
188:
189: // Start a separate thread to call the applet's init() and start()
190: // methods, in case they take a long time.
191: (new Thread(this )).start();
192:
193: }
194:
195: // Turn command-line arguments into Applet parameters, by way of the
196: // properties list.
197: private static void parseArgs(String[] args, Properties props) {
198: for (int i = 0; i < args.length; ++i) {
199: String arg = args[i];
200: int ind = arg.indexOf('=');
201: if (ind == -1)
202: props.put(PARAM_PROP_PREFIX + arg.toLowerCase(), "");
203: else
204: props.put(PARAM_PROP_PREFIX
205: + arg.substring(0, ind).toLowerCase(), arg
206: .substring(ind + 1));
207: }
208: }
209:
210: // Methods from Runnable.
211:
212: /// Separate thread to call the applet's init() and start() methods.
213: public void run() {
214: showStatus(name + " initializing...");
215: applet.init();
216: validate();
217: showStatus(name + " starting...");
218: applet.start();
219: validate();
220: showStatus(name + " running...");
221: }
222:
223: // Methods from AppletStub.
224:
225: public boolean isActive() {
226: return true;
227: }
228:
229: public URL getDocumentBase() {
230: // Returns the current directory.
231: String dir = System.getProperty("user.dir");
232: String urlDir = dir.replace(File.separatorChar, '/');
233: try {
234: return new URL("file:" + urlDir + "/");
235: } catch (MalformedURLException e) {
236: return null;
237: }
238: }
239:
240: public URL getCodeBase() {
241: // Hack: loop through each item in CLASSPATH, checking if
242: // the appropriately named .class file exists there. But
243: // this doesn't account for .zip files.
244: String path = System.getProperty("java.class.path");
245: Enumeration st = new StringTokenizer(path, ":");
246: while (st.hasMoreElements()) {
247: String dir = (String) st.nextElement();
248: String filename = dir + File.separatorChar + name
249: + ".class";
250: File file = new File(filename);
251: if (file.exists()) {
252: String urlDir = dir.replace(File.separatorChar, '/');
253: try {
254: return new URL("file:" + urlDir + "/");
255: } catch (MalformedURLException e) {
256: return null;
257: }
258: }
259: }
260: return null;
261: }
262:
263: public String getParameter(String name) {
264: // Return a parameter via the munged names in the properties list.
265: return System.getProperty(PARAM_PROP_PREFIX
266: + name.toLowerCase());
267: }
268:
269: public void appletResize(int width, int height) {
270: // Change the frame's size by the same amount that the applet's
271: // size is changing.
272: Dimension frameSize = getSize();
273: frameSize.width += width - appletSize.width;
274: frameSize.height += height - appletSize.height;
275: setSize(frameSize);
276: appletSize = applet.getSize();
277: }
278:
279: public AppletContext getAppletContext() {
280: return this ;
281: }
282:
283: // Methods from AppletContext.
284:
285: public AudioClip getAudioClip(URL url) {
286: // This is an internal undocumented routine. However, it
287: // also provides needed functionality not otherwise available.
288: // I suspect that in a future release, JavaSoft will add an
289: // audio content handler which encapsulates this, and then
290: // we can just do a getContent just like for images.
291: return new sun.applet.AppletAudioClip(url);
292: }
293:
294: public Image getImage(URL url) {
295: Toolkit tk = Toolkit.getDefaultToolkit();
296: try {
297: ImageProducer prod = (ImageProducer) url.getContent();
298: return tk.createImage(prod);
299: } catch (IOException e) {
300: return null;
301: }
302: }
303:
304: public Applet getApplet(String name) {
305: // Returns this Applet or nothing.
306: if (name.equals(this .name))
307: return applet;
308: return null;
309: }
310:
311: public Enumeration getApplets() {
312: // Just yields this applet.
313: Vector v = new Vector();
314: v.addElement(applet);
315: return v.elements();
316: }
317:
318: public void showDocument(URL url) {
319: // Ignore.
320: }
321:
322: public void showDocument(URL url, String target) {
323: // Ignore.
324: }
325:
326: public void showStatus(String status) {
327: if (label != null)
328: label.setText(status);
329: }
330:
331: public void setStream(String key, java.io.InputStream stream) {
332: throw new RuntimeException("Not Implemented");
333: // TODO implement setStream method
334: }
335:
336: public java.io.InputStream getStream(String key) {
337: throw new RuntimeException("Not Implemented");
338: // TODO implement getStream method
339: }
340:
341: public java.util.Iterator getStreamKeys() {
342: throw new RuntimeException("Not Implemented");
343: // TODO implement getStreamKeys method
344: }
345: }
|