FSF GCC merge 02/23/03
[official-gcc.git] / libjava / java / applet / Applet.java
blob5990c6392dad18f9ce26b5631b93e77e1a081527
1 /* Applet.java -- Java base applet class
2 Copyright (C) 1999, 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.applet;
41 import java.awt.Dimension;
42 import java.awt.GraphicsEnvironment;
43 import java.awt.HeadlessException;
44 import java.awt.Image;
45 import java.awt.Panel;
46 import java.io.IOException;
47 import java.io.ObjectInputStream;
48 import java.net.MalformedURLException;
49 import java.net.URL;
50 import java.util.Locale;
51 import javax.accessibility.AccessibleContext;
52 import javax.accessibility.AccessibleRole;
53 import javax.accessibility.AccessibleState;
54 import javax.accessibility.AccessibleStateSet;
56 /**
57 * This is the base applet class. An applet is a Java program that
58 * runs inside a web browser or other applet viewer in a restricted
59 * environment.
61 * <p>To be useful, a subclass should override at least start(). Also useful
62 * are init, stop, and destroy for control purposes, and getAppletInfo and
63 * getParameterInfo for descriptive purposes.
65 * @author Aaron M. Renn <arenn@urbanophile.com>
66 * @author Eric Blake <ebb9@email.byu.edu>
67 * @since 1.0
68 * @status updated to 1.4
70 public class Applet extends Panel
72 /**
73 * Compatible with JDK 1.0+.
75 private static final long serialVersionUID = -5836846270535785031L;
77 /** The applet stub for this applet. */
78 private transient AppletStub stub;
80 /**
81 * The accessibility context for this applet.
83 * @serial the accessibleContext for this
84 * @since 1.2
86 private AccessibleContext accessibleContext;
88 /**
89 * Default constructor for subclasses.
91 * @throws HeadlessException if in a headless environment
93 public Applet()
95 if (GraphicsEnvironment.isHeadless())
96 throw new HeadlessException();
99 /**
100 * The browser calls this method to set the applet's stub, which is the
101 * low level interface to the browser. Manually setting this to null is
102 * asking for problems down the road.
104 * @param stub the applet stub for this applet
106 public final void setStub(AppletStub stub)
108 this.stub = stub;
112 * Tests whether or not this applet is currently active. An applet is active
113 * just before the browser invokes start(), and becomes inactive just
114 * before the browser invokes stop().
116 * @return <code>true</code> if this applet is active
118 public boolean isActive()
120 return stub.isActive();
124 * Returns the basename URL of the document this applet is embedded in. This
125 * is everything up to the final '/'.
127 * @return the URL of the document this applet is embedded in
128 * @see #getCodeBase()
130 public URL getDocumentBase()
132 return stub.getDocumentBase();
136 * Returns the URL of the code base for this applet.
138 * @return the URL of the code base for this applet
140 public URL getCodeBase()
142 return stub.getCodeBase();
146 * Returns the value of the specified parameter that was specified in
147 * the <code>&lt;APPLET&gt;</code> tag for this applet.
149 * @param name the parameter name
150 * @return the parameter value, or null if the parameter does not exist
151 * @throws NullPointerException if name is null
153 public String getParameter(String name)
155 return stub.getParameter(name);
159 * Returns the applet context for this applet.
161 * @return the applet context for this applet
163 public AppletContext getAppletContext()
165 return stub.getAppletContext();
169 * Requests that the applet window for this applet be resized.
171 * @param width the new width in pixels
172 * @param height the new height in pixels
174 public void resize(int width, int height)
176 stub.appletResize(width, height);
180 * Requests that the applet window for this applet be resized.
182 * @param dim the requested dimensions
183 * @throws NullPointerException if dim is null
185 public void resize(Dimension dim)
187 resize(dim.width, dim.height);
191 * Displays the specified message in the status window if that window
192 * exists.
194 * @param message the status message, may be null
196 public void showStatus(String message)
198 getAppletContext().showStatus(message);
202 * Returns an image from the specified URL. Note that the image is not
203 * actually retrieved until the applet attempts to display it, so this
204 * method returns immediately.
206 * @param url the URL of the image
207 * @return the retrieved image
208 * @throws NullPointerException if url is null
210 public Image getImage(URL url)
212 return getAppletContext().getImage(url);
216 * Returns an image from the specified absolute URL, and relative path
217 * from that URL. Note that the image is not actually retrieved until the
218 * applet attempts to display it, so this method returns immediately.
219 * This calls <code>getImage(new URL(url, name))</code>, but if building
220 * the new URL fails, this returns null.
222 * @param url the base URL of the image
223 * @param name the name of the image relative to the URL
224 * @return the retrieved image, or null on failure
225 * @see #getImage(URL)
227 public Image getImage(URL url, String name)
231 return getImage(new URL(url, name));
233 catch (MalformedURLException e)
235 return null;
240 * Returns an audio clip from the specified URL. This clip is not tied to
241 * any particular applet.
243 * XXX Classpath does not yet implement this.
245 * @param url the URL of the audio clip
246 * @return the retrieved audio clip
247 * @throws NullPointerException if url is null
248 * @see #getAudioClip(URL)
249 * @since 1.2
251 public static final AudioClip newAudioClip(URL url)
253 // This requires an implementation of AudioClip in gnu.java.applet.
254 throw new Error("Not implemented");
258 * Returns an audio clip from the specified URL. Note that the clip is not
259 * actually retrieved until the applet attempts to play it, so this method
260 * returns immediately.
262 * @param url the URL of the audio clip
263 * @return the retrieved audio clip
264 * @throws NullPointerException if url is null
266 public AudioClip getAudioClip(URL url)
268 return getAppletContext().getAudioClip(url);
272 * Returns an audio clip from the specified absolute URL, and relative path
273 * from that URL. Note that the clip is not actually retrieved until the
274 * applet attempts to play it, so this method returns immediately. This
275 * calls <code>getAudioClip(new URL(url, name))</code>, but if building
276 * the new URL fails, this returns null.
278 * @param url the base URL of the audio clip
279 * @param name the name of the clip relative to the URL
280 * @return the retrieved audio clip, or null on failure
281 * @see #getAudioClip(URL)
283 public AudioClip getAudioClip(URL url, String name)
287 return getAudioClip(new URL(url, name));
289 catch (MalformedURLException e)
291 return null;
296 * Returns a descriptive string with applet defined information. The
297 * implementation in this class returns <code>null</code>, so subclasses
298 * must override to return information.
300 * @return a string describing the author, version, and applet copyright
302 public String getAppletInfo()
304 return null;
308 * Returns the locale for this applet, if it has been set. If no applet
309 * specific locale has been set, the default locale is returned.
311 * @return the locale for this applet
312 * @see Component#setLocale(Locale)
313 * @since 1.1
315 public Locale getLocale()
317 return super.getLocale();
321 * Returns a list of parameters this applet supports. Each element of
322 * the outer array is an array of three strings with the name of the
323 * parameter, the data type or valid values, and a description. This
324 * method is optional and the default implementation returns null.
326 * @return the list of parameters supported by this applet
328 public String[][] getParameterInfo()
330 return null;
334 * Loads and plays the audio clip pointed to by the specified URL. This does
335 * nothing if the URL does not point to a valid audio clip.
337 * @param url the URL of the audio clip
338 * @throws NullPointerException if url is null
339 * @see #getAudioClip(URL)
341 public void play(URL url)
343 AudioClip ac = getAudioClip(url);
346 ac.play();
348 catch (Exception ignored)
354 * Loads and plays the audio clip pointed to by the specified absolute URL,
355 * and relative path from that URL. This does nothing if the URL cannot be
356 * constructed, or if it does not point to a valid audio clip.
358 * @param url the base URL of the audio clip
359 * @param name the name of the audio clip relative to the URL
360 * @see #getAudioClip(URL, String)
361 * @see #play(URL)
363 public void play(URL url, String name)
367 getAudioClip(url, name).play();
369 catch (Exception ignored)
375 * This method is called when the applet is first loaded, before start().
376 * The default implementation does nothing; override to do any one-time
377 * initialization.
379 * @see #start()
380 * @see #stop()
381 * @see #destroy()
383 public void init()
388 * This method is called when the applet should start running. This is
389 * normally each time a web page containing it is loaded. The default
390 * implemention does nothing; override for your applet to be useful.
392 * @see #init()
393 * @see #stop()
394 * @see #destroy()
396 public void start()
401 * This method is called when the applet should stop running. This is
402 * normally when the next web page is loaded. The default implementation
403 * does nothing; override for your applet to stop using resources when
404 * it is no longer visible, but may be restarted soon.
406 * @see #init()
407 * @see #start()
408 * @see #destroy()
410 public void stop()
415 * This method is called when the applet is being unloaded. The default
416 * implementation does nothing; override for your applet to clean up
417 * resources on exit.
419 * @see #init()
420 * @see #start()
421 * @see #stop()
423 public void destroy()
428 * Gets the AccessibleContext associated with this applet, creating one if
429 * necessary. This always returns an instance of {@link AccessibleApplet}.
431 * @return the accessibility context of this applet
432 * @since 1.3
434 public AccessibleContext getAccessibleContext()
436 if (accessibleContext == null)
437 accessibleContext = new AccessibleApplet();
438 return accessibleContext;
442 * Read an applet from an object stream. This checks for a headless
443 * environment, then does the normal read.
445 * @param s the stream to read from
446 * @throws ClassNotFoundException if a class is not found
447 * @throws IOException if deserialization fails
448 * @throws HeadlessException if this is a headless environment
449 * @see GraphicsEnvironment#isHeadless()
450 * @since 1.4
452 private void readObject(ObjectInputStream s)
453 throws ClassNotFoundException, IOException
455 if (GraphicsEnvironment.isHeadless())
456 throw new HeadlessException();
457 s.defaultReadObject();
461 * This class provides accessibility support for Applets, and is the
462 * runtime type returned by {@link #getAccessibleContext()}.
464 * @author Eric Blake <ebb9@email.byu.edu>
465 * @since 1.3
467 protected class AccessibleApplet extends AccessibleAWTPanel
470 * Compatible with JDK 1.4+.
472 private static final long serialVersionUID = 8127374778187708896L;
475 * The default constructor.
477 protected AccessibleApplet()
482 * Get the role of this accessible object, a frame.
484 * @return the role of the object
485 * @see AccessibleRole#FRAME
487 public AccessibleRole getAccessibleRole()
489 return AccessibleRole.FRAME;
493 * Get the state set of this accessible object. In addition to the default
494 * states of a Component, the applet can also be active.
496 * @return the role of the object
497 * @see AccessibleState
499 public AccessibleStateSet getAccessibleStateSet()
501 AccessibleStateSet s = super.getAccessibleStateSet();
502 if (isActive())
503 s.add(AccessibleState.ACTIVE);
504 return s;
506 } // class AccessibleApplet
507 } // class Applet