Applet.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /* Applet.java -- Java base applet class
  2. Copyright (C) 1999, 2002 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.applet;
  32. import java.awt.Dimension;
  33. import java.awt.GraphicsEnvironment;
  34. import java.awt.HeadlessException;
  35. import java.awt.Image;
  36. import java.awt.Panel;
  37. import java.io.IOException;
  38. import java.io.ObjectInputStream;
  39. import java.net.MalformedURLException;
  40. import java.net.URL;
  41. import java.util.Locale;
  42. import javax.accessibility.AccessibleContext;
  43. import javax.accessibility.AccessibleRole;
  44. import javax.accessibility.AccessibleState;
  45. import javax.accessibility.AccessibleStateSet;
  46. /**
  47. * This is the base applet class. An applet is a Java program that
  48. * runs inside a web browser or other applet viewer in a restricted
  49. * environment.
  50. *
  51. * <p>To be useful, a subclass should override at least start(). Also useful
  52. * are init, stop, and destroy for control purposes, and getAppletInfo and
  53. * getParameterInfo for descriptive purposes.
  54. *
  55. * @author Aaron M. Renn <arenn@urbanophile.com>
  56. * @author Eric Blake <ebb9@email.byu.edu>
  57. * @since 1.0
  58. * @status updated to 1.4
  59. */
  60. public class Applet extends Panel
  61. {
  62. /**
  63. * Compatible with JDK 1.0+.
  64. */
  65. private static final long serialVersionUID = -5836846270535785031L;
  66. /** The applet stub for this applet. */
  67. private transient AppletStub stub;
  68. /**
  69. * The accessibility context for this applet.
  70. *
  71. * @serial the accessibleContext for this
  72. * @since 1.2
  73. */
  74. private AccessibleContext accessibleContext;
  75. /**
  76. * Default constructor for subclasses.
  77. *
  78. * @throws HeadlessException if in a headless environment
  79. */
  80. public Applet()
  81. {
  82. if (GraphicsEnvironment.isHeadless())
  83. throw new HeadlessException();
  84. }
  85. /**
  86. * The browser calls this method to set the applet's stub, which is the
  87. * low level interface to the browser. Manually setting this to null is
  88. * asking for problems down the road.
  89. *
  90. * @param stub the applet stub for this applet
  91. */
  92. public final void setStub(AppletStub stub)
  93. {
  94. this.stub = stub;
  95. }
  96. /**
  97. * Tests whether or not this applet is currently active. An applet is active
  98. * just before the browser invokes start(), and becomes inactive just
  99. * before the browser invokes stop().
  100. *
  101. * @return <code>true</code> if this applet is active
  102. */
  103. public boolean isActive()
  104. {
  105. return stub.isActive();
  106. }
  107. /**
  108. * Returns the basename URL of the document this applet is embedded in. This
  109. * is everything up to the final '/'.
  110. *
  111. * @return the URL of the document this applet is embedded in
  112. * @see #getCodeBase()
  113. */
  114. public URL getDocumentBase()
  115. {
  116. return stub.getDocumentBase();
  117. }
  118. /**
  119. * Returns the URL of the code base for this applet.
  120. *
  121. * @return the URL of the code base for this applet
  122. */
  123. public URL getCodeBase()
  124. {
  125. return stub.getCodeBase();
  126. }
  127. /**
  128. * Returns the value of the specified parameter that was specified in
  129. * the <code>&lt;APPLET&gt;</code> tag for this applet.
  130. *
  131. * @param name the parameter name
  132. * @return the parameter value, or null if the parameter does not exist
  133. * @throws NullPointerException if name is null
  134. */
  135. public String getParameter(String name)
  136. {
  137. return stub.getParameter(name);
  138. }
  139. /**
  140. * Returns the applet context for this applet.
  141. *
  142. * @return the applet context for this applet
  143. */
  144. public AppletContext getAppletContext()
  145. {
  146. return stub.getAppletContext();
  147. }
  148. /**
  149. * Requests that the applet window for this applet be resized.
  150. *
  151. * @param width the new width in pixels
  152. * @param height the new height in pixels
  153. */
  154. public void resize(int width, int height)
  155. {
  156. stub.appletResize(width, height);
  157. }
  158. /**
  159. * Requests that the applet window for this applet be resized.
  160. *
  161. * @param dim the requested dimensions
  162. * @throws NullPointerException if dim is null
  163. */
  164. public void resize(Dimension dim)
  165. {
  166. resize(dim.width, dim.height);
  167. }
  168. /**
  169. * Displays the specified message in the status window if that window
  170. * exists.
  171. *
  172. * @param message the status message, may be null
  173. */
  174. public void showStatus(String message)
  175. {
  176. getAppletContext().showStatus(message);
  177. }
  178. /**
  179. * Returns an image from the specified URL. Note that the image is not
  180. * actually retrieved until the applet attempts to display it, so this
  181. * method returns immediately.
  182. *
  183. * @param url the URL of the image
  184. * @return the retrieved image
  185. * @throws NullPointerException if url is null
  186. */
  187. public Image getImage(URL url)
  188. {
  189. return getAppletContext().getImage(url);
  190. }
  191. /**
  192. * Returns an image from the specified absolute URL, and relative path
  193. * from that URL. Note that the image is not actually retrieved until the
  194. * applet attempts to display it, so this method returns immediately.
  195. * This calls <code>getImage(new URL(url, name))</code>, but if building
  196. * the new URL fails, this returns null.
  197. *
  198. * @param url the base URL of the image
  199. * @param name the name of the image relative to the URL
  200. * @return the retrieved image, or null on failure
  201. * @see #getImage(URL)
  202. */
  203. public Image getImage(URL url, String name)
  204. {
  205. try
  206. {
  207. return getImage(new URL(url, name));
  208. }
  209. catch (MalformedURLException e)
  210. {
  211. return null;
  212. }
  213. }
  214. /**
  215. * Returns an audio clip from the specified URL. This clip is not tied to
  216. * any particular applet.
  217. *
  218. * XXX Classpath does not yet implement this.
  219. *
  220. * @param url the URL of the audio clip
  221. * @return the retrieved audio clip
  222. * @throws NullPointerException if url is null
  223. * @see #getAudioClip(URL)
  224. * @since 1.2
  225. */
  226. public static final AudioClip newAudioClip(URL url)
  227. {
  228. // This requires an implementation of AudioClip in gnu.java.applet.
  229. throw new Error("Not implemented");
  230. }
  231. /**
  232. * Returns an audio clip from the specified URL. Note that the clip is not
  233. * actually retrieved until the applet attempts to play it, so this method
  234. * returns immediately.
  235. *
  236. * @param url the URL of the audio clip
  237. * @return the retrieved audio clip
  238. * @throws NullPointerException if url is null
  239. */
  240. public AudioClip getAudioClip(URL url)
  241. {
  242. return getAppletContext().getAudioClip(url);
  243. }
  244. /**
  245. * Returns an audio clip from the specified absolute URL, and relative path
  246. * from that URL. Note that the clip is not actually retrieved until the
  247. * applet attempts to play it, so this method returns immediately. This
  248. * calls <code>getAudioClip(new URL(url, name))</code>, but if building
  249. * the new URL fails, this returns null.
  250. *
  251. * @param url the base URL of the audio clip
  252. * @param name the name of the clip relative to the URL
  253. * @return the retrieved audio clip, or null on failure
  254. * @see #getAudioClip(URL)
  255. */
  256. public AudioClip getAudioClip(URL url, String name)
  257. {
  258. try
  259. {
  260. return getAudioClip(new URL(url, name));
  261. }
  262. catch (MalformedURLException e)
  263. {
  264. return null;
  265. }
  266. }
  267. /**
  268. * Returns a descriptive string with applet defined information. The
  269. * implementation in this class returns <code>null</code>, so subclasses
  270. * must override to return information.
  271. *
  272. * @return a string describing the author, version, and applet copyright
  273. */
  274. public String getAppletInfo()
  275. {
  276. return null;
  277. }
  278. /**
  279. * Returns the locale for this applet, if it has been set. If no applet
  280. * specific locale has been set, the default locale is returned.
  281. *
  282. * @return the locale for this applet
  283. * @see Component#setLocale(Locale)
  284. * @since 1.1
  285. */
  286. public Locale getLocale()
  287. {
  288. return super.getLocale();
  289. }
  290. /**
  291. * Returns a list of parameters this applet supports. Each element of
  292. * the outer array is an array of three strings with the name of the
  293. * parameter, the data type or valid values, and a description. This
  294. * method is optional and the default implementation returns null.
  295. *
  296. * @return the list of parameters supported by this applet
  297. */
  298. public String[][] getParameterInfo()
  299. {
  300. return null;
  301. }
  302. /**
  303. * Loads and plays the audio clip pointed to by the specified URL. This does
  304. * nothing if the URL does not point to a valid audio clip.
  305. *
  306. * @param url the URL of the audio clip
  307. * @throws NullPointerException if url is null
  308. * @see #getAudioClip(URL)
  309. */
  310. public void play(URL url)
  311. {
  312. AudioClip ac = getAudioClip(url);
  313. try
  314. {
  315. ac.play();
  316. }
  317. catch (Exception ignored)
  318. {
  319. }
  320. }
  321. /**
  322. * Loads and plays the audio clip pointed to by the specified absolute URL,
  323. * and relative path from that URL. This does nothing if the URL cannot be
  324. * constructed, or if it does not point to a valid audio clip.
  325. *
  326. * @param url the base URL of the audio clip
  327. * @param name the name of the audio clip relative to the URL
  328. * @see #getAudioClip(URL, String)
  329. * @see #play(URL)
  330. */
  331. public void play(URL url, String name)
  332. {
  333. try
  334. {
  335. getAudioClip(url, name).play();
  336. }
  337. catch (Exception ignored)
  338. {
  339. }
  340. }
  341. /**
  342. * This method is called when the applet is first loaded, before start().
  343. * The default implementation does nothing; override to do any one-time
  344. * initialization.
  345. *
  346. * @see #start()
  347. * @see #stop()
  348. * @see #destroy()
  349. */
  350. public void init()
  351. {
  352. }
  353. /**
  354. * This method is called when the applet should start running. This is
  355. * normally each time a web page containing it is loaded. The default
  356. * implemention does nothing; override for your applet to be useful.
  357. *
  358. * @see #init()
  359. * @see #stop()
  360. * @see #destroy()
  361. */
  362. public void start()
  363. {
  364. }
  365. /**
  366. * This method is called when the applet should stop running. This is
  367. * normally when the next web page is loaded. The default implementation
  368. * does nothing; override for your applet to stop using resources when
  369. * it is no longer visible, but may be restarted soon.
  370. *
  371. * @see #init()
  372. * @see #start()
  373. * @see #destroy()
  374. */
  375. public void stop()
  376. {
  377. }
  378. /**
  379. * This method is called when the applet is being unloaded. The default
  380. * implementation does nothing; override for your applet to clean up
  381. * resources on exit.
  382. *
  383. * @see #init()
  384. * @see #start()
  385. * @see #stop()
  386. */
  387. public void destroy()
  388. {
  389. }
  390. /**
  391. * Gets the AccessibleContext associated with this applet, creating one if
  392. * necessary. This always returns an instance of {@link AccessibleApplet}.
  393. *
  394. * @return the accessibility context of this applet
  395. * @since 1.3
  396. */
  397. public AccessibleContext getAccessibleContext()
  398. {
  399. if (accessibleContext == null)
  400. accessibleContext = new AccessibleApplet();
  401. return accessibleContext;
  402. }
  403. /**
  404. * Read an applet from an object stream. This checks for a headless
  405. * environment, then does the normal read.
  406. *
  407. * @param s the stream to read from
  408. * @throws ClassNotFoundException if a class is not found
  409. * @throws IOException if deserialization fails
  410. * @throws HeadlessException if this is a headless environment
  411. * @see GraphicsEnvironment#isHeadless()
  412. * @since 1.4
  413. */
  414. private void readObject(ObjectInputStream s)
  415. throws ClassNotFoundException, IOException
  416. {
  417. if (GraphicsEnvironment.isHeadless())
  418. throw new HeadlessException();
  419. s.defaultReadObject();
  420. }
  421. /**
  422. * This class provides accessibility support for Applets, and is the
  423. * runtime type returned by {@link #getAccessibleContext()}.
  424. *
  425. * @author Eric Blake <ebb9@email.byu.edu>
  426. * @since 1.3
  427. */
  428. protected class AccessibleApplet extends AccessibleAWTPanel
  429. {
  430. /**
  431. * Compatible with JDK 1.4+.
  432. */
  433. private static final long serialVersionUID = 8127374778187708896L;
  434. /**
  435. * The default constructor.
  436. */
  437. protected AccessibleApplet()
  438. {
  439. }
  440. /**
  441. * Get the role of this accessible object, a frame.
  442. *
  443. * @return the role of the object
  444. * @see AccessibleRole#FRAME
  445. */
  446. public AccessibleRole getAccessibleRole()
  447. {
  448. return AccessibleRole.FRAME;
  449. }
  450. /**
  451. * Get the state set of this accessible object. In addition to the default
  452. * states of a Component, the applet can also be active.
  453. *
  454. * @return the role of the object
  455. * @see AccessibleState
  456. */
  457. public AccessibleStateSet getAccessibleStateSet()
  458. {
  459. AccessibleStateSet s = super.getAccessibleStateSet();
  460. if (isActive())
  461. s.add(AccessibleState.ACTIVE);
  462. return s;
  463. }
  464. } // class AccessibleApplet
  465. } // class Applet