1. /*
  2. * @(#)Toolkit.java 1.156 01/02/09
  3. *
  4. * Copyright 1995-2001 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.awt;
  11. import java.util.MissingResourceException;
  12. import java.util.Properties;
  13. import java.util.ResourceBundle;
  14. import java.util.StringTokenizer;
  15. import java.awt.event.*;
  16. import java.awt.peer.*;
  17. import java.awt.im.InputMethodHighlight;
  18. import java.awt.image.ImageObserver;
  19. import java.awt.image.ImageProducer;
  20. import java.awt.image.ColorModel;
  21. import java.awt.datatransfer.Clipboard;
  22. import java.awt.dnd.DnDConstants;
  23. import java.awt.dnd.DragSource;
  24. import java.awt.dnd.DragGestureRecognizer;
  25. import java.awt.dnd.DragGestureEvent;
  26. import java.awt.dnd.DragGestureListener;
  27. import java.awt.dnd.InvalidDnDOperationException;
  28. import java.awt.dnd.peer.DragSourceContextPeer;
  29. import java.net.URL;
  30. import java.io.BufferedInputStream;
  31. import java.io.File;
  32. import java.io.FileInputStream;
  33. import java.security.AccessController;
  34. import java.security.PrivilegedAction;
  35. import java.util.EventListener;
  36. import java.util.Map;
  37. import java.util.HashMap;
  38. import java.util.WeakHashMap;
  39. import java.beans.PropertyChangeListener;
  40. import java.beans.PropertyChangeSupport;
  41. /**
  42. * This class is the abstract superclass of all actual
  43. * implementations of the Abstract Window Toolkit. Subclasses of
  44. * <code>Toolkit</code> are used to bind the various components
  45. * to particular native toolkit implementations.
  46. * <p>
  47. * Most applications should not call any of the methods in this
  48. * class directly. The methods defined by <code>Toolkit</code> are
  49. * the "glue" that joins the platform-independent classes in the
  50. * <code>java.awt</code> package with their counterparts in
  51. * <code>java.awt.peer</code>. Some methods defined by
  52. * <code>Toolkit</code> query the native operating system directly.
  53. *
  54. * @version 1.156, 02/09/01
  55. * @author Sami Shaio
  56. * @author Arthur van Hoff
  57. * @author Fred Ecks
  58. * @since JDK1.0
  59. */
  60. public abstract class Toolkit {
  61. /**
  62. * Creates this toolkit's implementation of <code>Button</code> using
  63. * the specified peer interface.
  64. * @param target the button to be implemented.
  65. * @return this toolkit's implementation of <code>Button</code>.
  66. * @see java.awt.Button
  67. * @see java.awt.peer.ButtonPeer
  68. */
  69. protected abstract ButtonPeer createButton(Button target);
  70. /**
  71. * Creates this toolkit's implementation of <code>TextField</code> using
  72. * the specified peer interface.
  73. * @param target the text field to be implemented.
  74. * @return this toolkit's implementation of <code>TextField</code>.
  75. * @see java.awt.TextField
  76. * @see java.awt.peer.TextFieldPeer
  77. */
  78. protected abstract TextFieldPeer createTextField(TextField target);
  79. /**
  80. * Creates this toolkit's implementation of <code>Label</code> using
  81. * the specified peer interface.
  82. * @param target the label to be implemented.
  83. * @return this toolkit's implementation of <code>Label</code>.
  84. * @see java.awt.Label
  85. * @see java.awt.peer.LabelPeer
  86. */
  87. protected abstract LabelPeer createLabel(Label target);
  88. /**
  89. * Creates this toolkit's implementation of <code>List</code> using
  90. * the specified peer interface.
  91. * @param target the list to be implemented.
  92. * @return this toolkit's implementation of <code>List</code>.
  93. * @see java.awt.List
  94. * @see java.awt.peer.ListPeer
  95. */
  96. protected abstract ListPeer createList(List target);
  97. /**
  98. * Creates this toolkit's implementation of <code>Checkbox</code> using
  99. * the specified peer interface.
  100. * @param target the check box to be implemented.
  101. * @return this toolkit's implementation of <code>Checkbox</code>.
  102. * @see java.awt.Checkbox
  103. * @see java.awt.peer.CheckboxPeer
  104. */
  105. protected abstract CheckboxPeer createCheckbox(Checkbox target);
  106. /**
  107. * Creates this toolkit's implementation of <code>Scrollbar</code> using
  108. * the specified peer interface.
  109. * @param target the scroll bar to be implemented.
  110. * @return this toolkit's implementation of <code>Scrollbar</code>.
  111. * @see java.awt.Scrollbar
  112. * @see java.awt.peer.ScrollbarPeer
  113. */
  114. protected abstract ScrollbarPeer createScrollbar(Scrollbar target);
  115. /**
  116. * Creates this toolkit's implementation of <code>ScrollPane</code> using
  117. * the specified peer interface.
  118. * @param target the scroll pane to be implemented.
  119. * @return this toolkit's implementation of <code>ScrollPane</code>.
  120. * @see java.awt.ScrollPane
  121. * @see java.awt.peer.ScrollPanePeer
  122. * @since JDK1.1
  123. */
  124. protected abstract ScrollPanePeer createScrollPane(ScrollPane target);
  125. /**
  126. * Creates this toolkit's implementation of <code>TextArea</code> using
  127. * the specified peer interface.
  128. * @param target the text area to be implemented.
  129. * @return this toolkit's implementation of <code>TextArea</code>.
  130. * @see java.awt.TextArea
  131. * @see java.awt.peer.TextAreaPeer
  132. */
  133. protected abstract TextAreaPeer createTextArea(TextArea target);
  134. /**
  135. * Creates this toolkit's implementation of <code>Choice</code> using
  136. * the specified peer interface.
  137. * @param target the choice to be implemented.
  138. * @return this toolkit's implementation of <code>Choice</code>.
  139. * @see java.awt.Choice
  140. * @see java.awt.peer.ChoicePeer
  141. */
  142. protected abstract ChoicePeer createChoice(Choice target);
  143. /**
  144. * Creates this toolkit's implementation of <code>Frame</code> using
  145. * the specified peer interface.
  146. * @param target the frame to be implemented.
  147. * @return this toolkit's implementation of <code>Frame</code>.
  148. * @see java.awt.Frame
  149. * @see java.awt.peer.FramePeer
  150. */
  151. protected abstract FramePeer createFrame(Frame target);
  152. /**
  153. * Creates this toolkit's implementation of <code>Canvas</code> using
  154. * the specified peer interface.
  155. * @param target the canvas to be implemented.
  156. * @return this toolkit's implementation of <code>Canvas</code>.
  157. * @see java.awt.Canvas
  158. * @see java.awt.peer.CanvasPeer
  159. */
  160. protected abstract CanvasPeer createCanvas(Canvas target);
  161. /**
  162. * Creates this toolkit's implementation of <code>Panel</code> using
  163. * the specified peer interface.
  164. * @param target the panel to be implemented.
  165. * @return this toolkit's implementation of <code>Panel</code>.
  166. * @see java.awt.Panel
  167. * @see java.awt.peer.PanelPeer
  168. */
  169. protected abstract PanelPeer createPanel(Panel target);
  170. /**
  171. * Creates this toolkit's implementation of <code>Window</code> using
  172. * the specified peer interface.
  173. * @param target the window to be implemented.
  174. * @return this toolkit's implementation of <code>Window</code>.
  175. * @see java.awt.Window
  176. * @see java.awt.peer.WindowPeer
  177. */
  178. protected abstract WindowPeer createWindow(Window target);
  179. /**
  180. * Creates this toolkit's implementation of <code>Dialog</code> using
  181. * the specified peer interface.
  182. * @param target the dialog to be implemented.
  183. * @return this toolkit's implementation of <code>Dialog</code>.
  184. * @see java.awt.Dialog
  185. * @see java.awt.peer.DialogPeer
  186. */
  187. protected abstract DialogPeer createDialog(Dialog target);
  188. /**
  189. * Creates this toolkit's implementation of <code>MenuBar</code> using
  190. * the specified peer interface.
  191. * @param target the menu bar to be implemented.
  192. * @return this toolkit's implementation of <code>MenuBar</code>.
  193. * @see java.awt.MenuBar
  194. * @see java.awt.peer.MenuBarPeer
  195. */
  196. protected abstract MenuBarPeer createMenuBar(MenuBar target);
  197. /**
  198. * Creates this toolkit's implementation of <code>Menu</code> using
  199. * the specified peer interface.
  200. * @param target the menu to be implemented.
  201. * @return this toolkit's implementation of <code>Menu</code>.
  202. * @see java.awt.Menu
  203. * @see java.awt.peer.MenuPeer
  204. */
  205. protected abstract MenuPeer createMenu(Menu target);
  206. /**
  207. * Creates this toolkit's implementation of <code>PopupMenu</code> using
  208. * the specified peer interface.
  209. * @param target the popup menu to be implemented.
  210. * @return this toolkit's implementation of <code>PopupMenu</code>.
  211. * @see java.awt.PopupMenu
  212. * @see java.awt.peer.PopupMenuPeer
  213. * @since JDK1.1
  214. */
  215. protected abstract PopupMenuPeer createPopupMenu(PopupMenu target);
  216. /**
  217. * Creates this toolkit's implementation of <code>MenuItem</code> using
  218. * the specified peer interface.
  219. * @param target the menu item to be implemented.
  220. * @return this toolkit's implementation of <code>MenuItem</code>.
  221. * @see java.awt.MenuItem
  222. * @see java.awt.peer.MenuItemPeer
  223. */
  224. protected abstract MenuItemPeer createMenuItem(MenuItem target);
  225. /**
  226. * Creates this toolkit's implementation of <code>FileDialog</code> using
  227. * the specified peer interface.
  228. * @param target the file dialog to be implemented.
  229. * @return this toolkit's implementation of <code>FileDialog</code>.
  230. * @see java.awt.FileDialog
  231. * @see java.awt.peer.FileDialogPeer
  232. */
  233. protected abstract FileDialogPeer createFileDialog(FileDialog target);
  234. /**
  235. * Creates this toolkit's implementation of <code>CheckboxMenuItem</code> using
  236. * the specified peer interface.
  237. * @param target the checkbox menu item to be implemented.
  238. * @return this toolkit's implementation of <code>CheckboxMenuItem</code>.
  239. * @see java.awt.CheckboxMenuItem
  240. * @see java.awt.peer.CheckboxMenuItemPeer
  241. */
  242. protected abstract CheckboxMenuItemPeer createCheckboxMenuItem(CheckboxMenuItem target);
  243. private static java.awt.LightweightPeer lightweightMarker;
  244. /**
  245. * Creates a peer for a component or container. This peer is windowless
  246. * and allows the Component and Container classes to be extended directly
  247. * to create windowless components that are defined entirely in java.
  248. *
  249. * @param target The Component to be created.
  250. */
  251. protected java.awt.peer.LightweightPeer createComponent(Component target) {
  252. if (lightweightMarker == null) {
  253. lightweightMarker = new java.awt.LightweightPeer(target);
  254. }
  255. return lightweightMarker;
  256. }
  257. /**
  258. * Creates this toolkit's implementation of <code>Font</code> using
  259. * the specified peer interface.
  260. * @param target the font to be implemented.
  261. * @return this toolkit's implementation of <code>Font</code>.
  262. * @see java.awt.Font
  263. * @see java.awt.peer.FontPeer
  264. * @see java.awt.GraphicsEnvironment#getAllFonts
  265. * @deprecated see java.awt.GraphicsEnvironment#getAllFonts
  266. */
  267. protected abstract FontPeer getFontPeer(String name, int style);
  268. // The following method is called by the private method
  269. // <code>updateSystemColors</code> in <code>SystemColor</code>.
  270. /**
  271. * Fills in the integer array that is supplied as an argument
  272. * with the current system color values.
  273. *
  274. * @param an integer array.
  275. * @since JDK1.1
  276. */
  277. protected void loadSystemColors(int[] systemColors) {
  278. }
  279. /**
  280. * Gets the size of the screen.
  281. * @return the size of this toolkit's screen, in pixels.
  282. */
  283. public abstract Dimension getScreenSize();
  284. /**
  285. * Returns the screen resolution in dots-per-inch.
  286. * @return this toolkit's screen resolution, in dots-per-inch.
  287. */
  288. public abstract int getScreenResolution();
  289. /**
  290. * Determines the color model of this toolkit's screen.
  291. * <p>
  292. * <code>ColorModel</code> is an abstract class that
  293. * encapsulates the ability to translate between the
  294. * pixel values of an image and its red, green, blue,
  295. * and alpha components.
  296. * <p>
  297. * This toolkit method is called by the
  298. * <code>getColorModel</code> method
  299. * of the <code>Component</code> class.
  300. * @return the color model of this toolkit's screen.
  301. * @see java.awt.image.ColorModel
  302. * @see java.awt.Component#getColorModel
  303. */
  304. public abstract ColorModel getColorModel();
  305. /**
  306. * Returns the names of the available fonts in this toolkit.<p>
  307. * For 1.1, the following font names are deprecated (the replacement
  308. * name follows):
  309. * <ul>
  310. * <li>TimesRoman (use Serif)
  311. * <li>Helvetica (use SansSerif)
  312. * <li>Courier (use Monospaced)
  313. * </ul><p>
  314. * The ZapfDingbats fontname is also deprecated in 1.1 but the characters
  315. * are defined in Unicode starting at 0x2700, and as of 1.1 Java supports
  316. * those characters.
  317. * @return the names of the available fonts in this toolkit.
  318. * @deprecated see {@link java.awt.GraphicsEnvironment#getAvailableFontFamilyNames()}
  319. * @see java.awt.GraphicsEnvironment#getAvailableFontFamilyNames()
  320. */
  321. public abstract String[] getFontList();
  322. /**
  323. * Gets the screen device metrics for rendering of the font.
  324. * @param font a font.
  325. * @return the screen metrics of the specified font in this toolkit.
  326. * @deprecated This returns integer metrics for the default screen.
  327. * @see java.awt.font.LineMetrics
  328. * @see java.awt.Font#getLineMetrics
  329. * @see java.awt.GraphicsEnvironment#getScreenDevices
  330. */
  331. public abstract FontMetrics getFontMetrics(Font font);
  332. /**
  333. * Synchronizes this toolkit's graphics state. Some window systems
  334. * may do buffering of graphics events.
  335. * <p>
  336. * This method ensures that the display is up-to-date. It is useful
  337. * for animation.
  338. */
  339. public abstract void sync();
  340. /**
  341. * The default toolkit.
  342. */
  343. private static Toolkit toolkit;
  344. /**
  345. * Loads additional classes into the VM, using the property
  346. * 'assistive_technologies' specified in the Sun reference
  347. * implementation by a line in the 'accessibility.properties'
  348. * file. The form is "assistive_technologies=..." where
  349. * the "..." is a comma-separated list of assistive technology
  350. * classes to load. Each class is loaded in the order given
  351. * and a single instance of each is created using
  352. * Class.forName(class).newInstance(). All errors are handled
  353. * via an AWTError exception.
  354. *
  355. * <p>The assumption is made that assistive technology classes are supplied
  356. * as part of INSTALLED (as opposed to: BUNDLED) extensions or specified
  357. * on the class path
  358. * (and therefore can be loaded using the class loader returned by
  359. * a call to <code>ClassLoader.getSystemClassLoader</code>, whose
  360. * delegation parent is the extension class loader for installed
  361. * extensions).
  362. */
  363. private static void loadAssistiveTechnologies() {
  364. final String sep = File.separator;
  365. final Properties properties = new Properties();
  366. java.security.AccessController.doPrivileged(
  367. new java.security.PrivilegedAction() {
  368. public Object run() {
  369. try {
  370. File propsFile = new File(
  371. System.getProperty("java.home") + sep + "lib" +
  372. sep + "accessibility.properties");
  373. FileInputStream in =
  374. new FileInputStream(propsFile);
  375. // Inputstream has been buffered in Properties class
  376. properties.load(in);
  377. in.close();
  378. } catch (Exception e) {
  379. // File does not exist; no classes will be auto loaded
  380. }
  381. String magPresent =
  382. System.getProperty("javax.accessibility.screen_magnifier_present");
  383. if (magPresent == null) {
  384. magPresent =
  385. properties.getProperty("screen_magnifier_present",
  386. null);
  387. if (magPresent != null) {
  388. System.setProperty("javax.accessibility.screen_magnifier_present",
  389. magPresent);
  390. }
  391. }
  392. return null;
  393. }
  394. });
  395. String atNames = properties.getProperty("assistive_technologies",null);
  396. ClassLoader cl = ClassLoader.getSystemClassLoader();
  397. if (atNames != null) {
  398. StringTokenizer parser = new StringTokenizer(atNames," ,");
  399. String atName;
  400. while (parser.hasMoreTokens()) {
  401. atName = parser.nextToken();
  402. try {
  403. Class clazz;
  404. if (cl != null) {
  405. clazz = cl.loadClass(atName);
  406. } else {
  407. clazz = Class.forName(atName);
  408. }
  409. clazz.newInstance();
  410. } catch (ClassNotFoundException e) {
  411. throw new AWTError("Assistive Technology not found: "
  412. + atName);
  413. } catch (InstantiationException e) {
  414. throw new AWTError("Could not instantiate Assistive"
  415. + " Technology: " + atName);
  416. } catch (IllegalAccessException e) {
  417. throw new AWTError("Could not access Assistive"
  418. + " Technology: " + atName);
  419. } catch (Exception e) {
  420. throw new AWTError("Error trying to install Assistive"
  421. + " Technology: " + atName + " " + e);
  422. }
  423. }
  424. }
  425. }
  426. /**
  427. * Gets the default toolkit.
  428. * <p>
  429. * If there is a system property named <code>"awt.toolkit"</code>,
  430. * that property is treated as the name of a class that is a subclass
  431. * of <code>Toolkit</code>.
  432. * <p>
  433. * If the system property does not exist, then the default toolkit
  434. * used is the class named <code>"sun.awt.motif.MToolkit"</code>,
  435. * which is a motif implementation of the Abstract Window Toolkit.
  436. * <p>
  437. * Also loads additional classes into the VM, using the property
  438. * 'assistive_technologies' specified in the Sun reference
  439. * implementation by a line in the 'accessibility.properties'
  440. * file. The form is "assistive_technologies=..." where
  441. * the "..." is a comma-separated list of assistive technology
  442. * classes to load. Each class is loaded in the order given
  443. * and a single instance of each is created using
  444. * Class.forName(class).newInstance(). This is done just after
  445. * the AWT toolkit is created. All errors are handled via an
  446. * AWTError exception.
  447. * @return the default toolkit.
  448. * @exception AWTError if a toolkit could not be found, or
  449. * if one could not be accessed or instantiated.
  450. */
  451. public static synchronized Toolkit getDefaultToolkit() {
  452. if (toolkit == null) {
  453. try {
  454. // We disable the JIT during toolkit initialization. This
  455. // tends to touch lots of classes that aren't needed again
  456. // later and therefore JITing is counter-productiive.
  457. java.lang.Compiler.disable();
  458. java.security.AccessController.doPrivileged(
  459. new java.security.PrivilegedAction() {
  460. public Object run() {
  461. String nm = null;
  462. Class cls = null;
  463. try {
  464. nm = System.getProperty("awt.toolkit",
  465. "sun.awt.motif.MToolkit");
  466. try {
  467. cls = Class.forName(nm);
  468. } catch (ClassNotFoundException e) {
  469. ClassLoader cl = ClassLoader.getSystemClassLoader();
  470. if (cl != null) {
  471. try {
  472. cls = cl.loadClass(nm);
  473. } catch (ClassNotFoundException ee) {
  474. throw new AWTError("Toolkit not found: " + nm);
  475. }
  476. }
  477. }
  478. if (cls != null) {
  479. toolkit = (Toolkit)cls.newInstance();
  480. }
  481. } catch (InstantiationException e) {
  482. throw new AWTError("Could not instantiate Toolkit: " +
  483. nm);
  484. } catch (IllegalAccessException e) {
  485. throw new AWTError("Could not access Toolkit: " + nm);
  486. }
  487. return null;
  488. }
  489. });
  490. loadAssistiveTechnologies();
  491. } finally {
  492. // Make sure to always re-enable the JIT.
  493. java.lang.Compiler.enable();
  494. }
  495. }
  496. return toolkit;
  497. }
  498. /**
  499. * Returns an image which gets pixel data from the specified file,
  500. * whose format can be either GIF, JPEG or PNG.
  501. * The underlying toolkit attempts to resolve multiple requests
  502. * with the same filename to the same returned Image.
  503. * Since the mechanism required to facilitate this sharing of
  504. * Image objects may continue to hold onto images that are no
  505. * longer of use for an indefinate period of time, developers
  506. * are encouraged to implement their own caching of images by
  507. * using the createImage variant wherever available.
  508. * @param filename the name of a file containing pixel data
  509. * in a recognized file format.
  510. * @return an image which gets its pixel data from
  511. * the specified file.
  512. * @see #createImage(java.lang.String)
  513. */
  514. public abstract Image getImage(String filename);
  515. /**
  516. * Returns an image which gets pixel data from the specified URL.
  517. * The pixel data referenced by the specified URL must be in one
  518. * of the following formats: GIF, JPEG or PNG.
  519. * The underlying toolkit attempts to resolve multiple requests
  520. * with the same URL to the same returned Image.
  521. * Since the mechanism required to facilitate this sharing of
  522. * Image objects may continue to hold onto images that are no
  523. * longer of use for an indefinate period of time, developers
  524. * are encouraged to implement their own caching of images by
  525. * using the createImage variant wherever available.
  526. * @param url the URL to use in fetching the pixel data.
  527. * @return an image which gets its pixel data from
  528. * the specified URL.
  529. * @see #createImage(java.net.URL)
  530. */
  531. public abstract Image getImage(URL url);
  532. /**
  533. * Returns an image which gets pixel data from the specified file.
  534. * The returned Image is a new object which will not be shared
  535. * with any other caller of this method or its getImage variant.
  536. * @param filename the name of a file containing pixel data
  537. * in a recognized file format.
  538. * @return an image which gets its pixel data from
  539. * the specified file.
  540. * @see #getImage(java.lang.String)
  541. */
  542. public abstract Image createImage(String filename);
  543. /**
  544. * Returns an image which gets pixel data from the specified URL.
  545. * The returned Image is a new object which will not be shared
  546. * with any other caller of this method or its getImage variant.
  547. * @param url the URL to use in fetching the pixel data.
  548. * @return an image which gets its pixel data from
  549. * the specified URL.
  550. * @see #getImage(java.net.URL)
  551. */
  552. public abstract Image createImage(URL url);
  553. /**
  554. * Prepares an image for rendering.
  555. * <p>
  556. * If the values of the width and height arguments are both
  557. * <code>-1</code>, this method prepares the image for rendering
  558. * on the default screen; otherwise, this method prepares an image
  559. * for rendering on the default screen at the specified width and height.
  560. * <p>
  561. * The image data is downloaded asynchronously in another thread,
  562. * and an appropriately scaled screen representation of the image is
  563. * generated.
  564. * <p>
  565. * This method is called by components <code>prepareImage</code>
  566. * methods.
  567. * <p>
  568. * Information on the flags returned by this method can be found
  569. * with the definition of the <code>ImageObserver</code> interface.
  570. * @param image the image for which to prepare a
  571. * screen representation.
  572. * @param width the width of the desired screen
  573. * representation, or <code>-1</code>.
  574. * @param height the height of the desired screen
  575. * representation, or <code>-1</code>.
  576. * @param observer the <code>ImageObserver</code>
  577. * object to be notified as the
  578. * image is being prepared.
  579. * @return <code>true</code> if the image has already been
  580. * fully prepared; <code>false</code> otherwise.
  581. * @see java.awt.Component#prepareImage(java.awt.Image,
  582. * java.awt.image.ImageObserver)
  583. * @see java.awt.Component#prepareImage(java.awt.Image,
  584. * int, int, java.awt.image.ImageObserver)
  585. * @see java.awt.image.ImageObserver
  586. */
  587. public abstract boolean prepareImage(Image image, int width, int height,
  588. ImageObserver observer);
  589. /**
  590. * Indicates the construction status of a specified image that is
  591. * being prepared for display.
  592. * <p>
  593. * If the values of the width and height arguments are both
  594. * <code>-1</code>, this method returns the construction status of
  595. * a screen representation of the specified image in this toolkit.
  596. * Otherwise, this method returns the construction status of a
  597. * scaled representation of the image at the specified width
  598. * and height.
  599. * <p>
  600. * This method does not cause the image to begin loading.
  601. * An application must call <code>prepareImage</code> to force
  602. * the loading of an image.
  603. * <p>
  604. * This method is called by the component's <code>checkImage</code>
  605. * methods.
  606. * <p>
  607. * Information on the flags returned by this method can be found
  608. * with the definition of the <code>ImageObserver</code> interface.
  609. * @param image the image whose status is being checked.
  610. * @param width the width of the scaled version whose status is
  611. * being checked, or <code>-1</code>.
  612. * @param height the height of the scaled version whose status
  613. * is being checked, or <code>-1</code>.
  614. * @param observer the <code>ImageObserver</code> object to be
  615. * notified as the image is being prepared.
  616. * @return the bitwise inclusive <strong>OR</strong> of the
  617. * <code>ImageObserver</code> flags for the
  618. * image data that is currently available.
  619. * @see java.awt.Toolkit#prepareImage(java.awt.Image,
  620. * int, int, java.awt.image.ImageObserver)
  621. * @see java.awt.Component#checkImage(java.awt.Image,
  622. * java.awt.image.ImageObserver)
  623. * @see java.awt.Component#checkImage(java.awt.Image,
  624. * int, int, java.awt.image.ImageObserver)
  625. * @see java.awt.image.ImageObserver
  626. */
  627. public abstract int checkImage(Image image, int width, int height,
  628. ImageObserver observer);
  629. /**
  630. * Creates an image with the specified image producer.
  631. * @param producer the image producer to be used.
  632. * @return an image with the specified image producer.
  633. * @see java.awt.Image
  634. * @see java.awt.image.ImageProducer
  635. * @see java.awt.Component#createImage(java.awt.image.ImageProducer)
  636. */
  637. public abstract Image createImage(ImageProducer producer);
  638. /**
  639. * Creates an image which decodes the image stored in the specified
  640. * byte array.
  641. * <p>
  642. * The data must be in some image format, such as GIF or JPEG,
  643. * that is supported by this toolkit.
  644. * @param imagedata an array of bytes, representing
  645. * image data in a supported image format.
  646. * @return an image.
  647. * @since JDK1.1
  648. */
  649. public Image createImage(byte[] imagedata) {
  650. return createImage(imagedata, 0, imagedata.length);
  651. }
  652. /**
  653. * Creates an image which decodes the image stored in the specified
  654. * byte array, and at the specified offset and length.
  655. * The data must be in some image format, such as GIF or JPEG,
  656. * that is supported by this toolkit.
  657. * @param imagedata an array of bytes, representing
  658. * image data in a supported image format.
  659. * @param imageoffset the offset of the beginning
  660. * of the data in the array.
  661. * @param imagelength the length of the data in the array.
  662. * @return an image.
  663. * @since JDK1.1
  664. */
  665. public abstract Image createImage(byte[] imagedata,
  666. int imageoffset,
  667. int imagelength);
  668. /**
  669. * Gets a <code>PrintJob</code> object which is the result of initiating
  670. * a print operation on the toolkit's platform.
  671. * <p>
  672. * Each actual implementation of this method should first check if there
  673. * is a security manager installed. If there is, the method should call
  674. * the security manager's <code>checkPrintJobAccess</code> method to
  675. * ensure initiation of a print operation is allowed. If the default
  676. * implementation of <code>checkPrintJobAccess</code> is used (that is,
  677. * that method is not overriden), then this results in a call to the
  678. * security manager's <code>checkPermission</code> method with a <code>
  679. * RuntimePermission("queuePrintJob")</code> permission.
  680. *
  681. * @param frame the parent of the print dialog. May not be null.
  682. * @param jobtitle the title of the PrintJob. A null title is equivalent
  683. * to "".
  684. * @param props a Properties object containing zero or more properties.
  685. * Properties are not standardized and are not consistent across
  686. * implementations. Because of this, PrintJobs which require job
  687. * and page control should use the version of this function which
  688. * takes JobAttributes and PageAttributes objects. This object
  689. * may be updated to reflect the user's job choices on exit. May
  690. * be null.
  691. *
  692. * @return a <code>PrintJob</code> object, or <code>null</code> if the
  693. * user cancelled the print job.
  694. * @throws NullPointerException if frame is null
  695. * @throws SecurityException if this thread is not allowed to initiate a
  696. * print job request
  697. * @see java.awt.PrintJob
  698. * @see java.lang.RuntimePermission
  699. * @since JDK1.1
  700. */
  701. public abstract PrintJob getPrintJob(Frame frame, String jobtitle,
  702. Properties props);
  703. /**
  704. * Gets a <code>PrintJob</code> object which is the result of initiating
  705. * a print operation on the toolkit's platform.
  706. * <p>
  707. * Each actual implementation of this method should first check if there
  708. * is a security manager installed. If there is, the method should call
  709. * the security manager's <code>checkPrintJobAccess</code> method to
  710. * ensure initiation of a print operation is allowed. If the default
  711. * implementation of <code>checkPrintJobAccess</code> is used (that is,
  712. * that method is not overriden), then this results in a call to the
  713. * security manager's <code>checkPermission</code> method with a <code>
  714. * RuntimePermission("queuePrintJob")</code> permission.
  715. *
  716. * @param frame the parent of the print dialog. May be null if and only
  717. * if jobAttributes is not null and jobAttributes.getDialog()
  718. * returns JobAttributes.DialogType.NONE or
  719. * JobAttributes.DialogType.COMMON.
  720. * @param jobtitle the title of the PrintJob. A null title is equivalent
  721. * to "".
  722. * @param jobAttributes a set of job attributes which will control the
  723. * PrintJob. The attributes will be updated to reflect the user's
  724. * choices as outlined in the JobAttributes documentation. May be
  725. * null.
  726. * @param pageAttributes a set of page attributes which will control the
  727. * PrintJob. The attributes will be applied to every page in the
  728. * job. The attributes will be updated to reflect the user's
  729. * choices as outlined in the PageAttributes documentation. May be
  730. * null.
  731. *
  732. * @return a <code>PrintJob</code> object, or <code>null</code> if the
  733. * user cancelled the print job.
  734. * @throws NullPointerException if frame is null and either jobAttributes
  735. * is null or jobAttributes.getDialog() returns
  736. * JobAttributes.DialogType.NATIVE.
  737. * @throws IllegalArgumentException if pageAttributes specifies differing
  738. * cross feed and feed resolutions
  739. * @throws SecurityException if this thread is not allowed to initiate a
  740. * print job request, or if jobAttributes specifies print to file,
  741. * and this thread is not allowed to access the file system
  742. * @see java.awt.PrintJob
  743. * @see java.lang.RuntimePermission
  744. * @see java.awt.JobAttributes
  745. * @see java.awt.PageAttributes
  746. * @since 1.3
  747. */
  748. public PrintJob getPrintJob(Frame frame, String jobtitle,
  749. JobAttributes jobAttributes,
  750. PageAttributes pageAttributes) {
  751. // Override to add printing support with new job/page control classes
  752. if (this != Toolkit.getDefaultToolkit()) {
  753. return Toolkit.getDefaultToolkit().getPrintJob(frame, jobtitle,
  754. jobAttributes,
  755. pageAttributes);
  756. } else {
  757. return getPrintJob(frame, jobtitle, null);
  758. }
  759. }
  760. /**
  761. * Emits an audio beep.
  762. * @since JDK1.1
  763. */
  764. public abstract void beep();
  765. /**
  766. * Gets the singleton instance of the system Clipboard which interfaces
  767. * with clipboard facilities provided by the native platform. This
  768. * clipboard enables data transfer between Java programs and native
  769. * applications which use native clipboard facilities.
  770. * <p>
  771. * In addition to any and all formats specified in the flavormap.properties
  772. * file, or other file specified by the <code>AWT.DnD.flavorMapFileURL
  773. * </code> Toolkit property, text returned by the system Clipboard's <code>
  774. * getTransferData()</code> method is available in the following flavors:
  775. * <ul>
  776. * <li>DataFlavor.stringFlavor</li>
  777. * <li>DataFlavor.plainTextFlavor (<b>deprecated</b>)</li>
  778. * </ul>
  779. * As with <code>java.awt.datatransfer.StringSelection</code>, if the
  780. * requested flavor is <code>DataFlavor.plainTextFlavor</code>, or an
  781. * equivalent flavor, a Reader is returned. <b>Note:</b> The behavior of
  782. * the system Clipboard's <code>getTransferData()</code> method for <code>
  783. * DataFlavor.plainTextFlavor</code>, and equivalent DataFlavors, is
  784. * inconsistent with the definition of <code>DataFlavor.plainTextFlavor
  785. * </code>. Because of this, support for <code>
  786. * DataFlavor.plainTextFlavor</code>, and equivalent flavors, is
  787. * <b>deprecated</b>.
  788. * <p>
  789. * Each actual implementation of this method should first check if there
  790. * is a security manager installed. If there is, the method should call
  791. * the security manager's <code>checkSystemClipboardAccess</code> method
  792. * to ensure it's ok to to access the system clipboard. If the default
  793. * implementation of <code>checkSystemClipboardAccess</code> is used (that
  794. * is, that method is not overriden), then this results in a call to the
  795. * security manager's <code>checkPermission</code> method with an <code>
  796. * AWTPermission("accessClipboard")</code> permission.
  797. *
  798. * @return the system Clipboard
  799. * @see java.awt.datatransfer.Clipboard
  800. * @see java.awt.datatransfer.StringSelection
  801. * @see java.awt.datatransfer.DataFlavor.stringFlavor
  802. * @see java.awt.datatransfer.DataFlavor.plainTextFlavor
  803. * @see java.io.Reader
  804. * @see java.awt.AWTPermission
  805. * @since JDK1.1
  806. */
  807. public abstract Clipboard getSystemClipboard();
  808. /**
  809. * Determines which modifier key is the appropriate accelerator
  810. * key for menu shortcuts.
  811. * <p>
  812. * Menu shortcuts, which are embodied in the
  813. * <code>MenuShortcut</code> class, are handled by the
  814. * <code>MenuBar</code> class.
  815. * <p>
  816. * By default, this method returns <code>Event.CTRL_MASK</code>.
  817. * Toolkit implementations should override this method if the
  818. * <b>Control</b> key isn't the correct key for accelerators.
  819. * @return the modifier mask on the <code>Event</code> class
  820. * that is used for menu shortcuts on this toolkit.
  821. * @see java.awt.MenuBar
  822. * @see java.awt.MenuShortcut
  823. * @since JDK1.1
  824. */
  825. public int getMenuShortcutKeyMask() {
  826. return Event.CTRL_MASK;
  827. }
  828. /**
  829. * Returns whether the given locking key on the keyboard is currently in
  830. * its "on" state.
  831. * Valid key codes are
  832. * {@link java.awt.event.KeyEvent#VK_CAPS_LOCK VK_CAPS_LOCK},
  833. * {@link java.awt.event.KeyEvent#VK_NUM_LOCK VK_NUM_LOCK},
  834. * {@link java.awt.event.KeyEvent#VK_SCROLL_LOCK VK_SCROLL_LOCK}, and
  835. * {@link java.awt.event.KeyEvent#VK_KANA_LOCK VK_KANA_LOCK}.
  836. *
  837. * @exception java.lang.IllegalArgumentException if <code>keyCode</code>
  838. * is not one of the valid key codes
  839. * @exception java.lang.UnsupportedOperationException if the host system doesn't
  840. * allow getting the state of this key programmatically, or if the keyboard
  841. * doesn't have this key
  842. * @since 1.3
  843. */
  844. public boolean getLockingKeyState(int keyCode) {
  845. if (! (keyCode == KeyEvent.VK_CAPS_LOCK || keyCode == KeyEvent.VK_NUM_LOCK ||
  846. keyCode == KeyEvent.VK_SCROLL_LOCK || keyCode == KeyEvent.VK_KANA_LOCK)) {
  847. throw new IllegalArgumentException("invalid key for Toolkit.getLockingKeyState");
  848. }
  849. throw new UnsupportedOperationException("Toolkit.getLockingKeyState");
  850. }
  851. /**
  852. * Sets the state of the given locking key on the keyboard.
  853. * Valid key codes are
  854. * {@link java.awt.event.KeyEvent#VK_CAPS_LOCK VK_CAPS_LOCK},
  855. * {@link java.awt.event.KeyEvent#VK_NUM_LOCK VK_NUM_LOCK},
  856. * {@link java.awt.event.KeyEvent#VK_SCROLL_LOCK VK_SCROLL_LOCK}, and
  857. * {@link java.awt.event.KeyEvent#VK_KANA_LOCK VK_KANA_LOCK}.
  858. * <p>
  859. * Depending on the platform, setting the state of a locking key may
  860. * involve event processing and therefore may not be immediately
  861. * observable through getLockingKeyState.
  862. *
  863. * @exception java.lang.IllegalArgumentException if <code>keyCode</code>
  864. * is not one of the valid key codes
  865. * @exception java.lang.UnsupportedOperationException if the host system doesn't
  866. * allow setting the state of this key programmatically, or if the keyboard
  867. * doesn't have this key
  868. * @since 1.3
  869. */
  870. public void setLockingKeyState(int keyCode, boolean on) {
  871. if (! (keyCode == KeyEvent.VK_CAPS_LOCK || keyCode == KeyEvent.VK_NUM_LOCK ||
  872. keyCode == KeyEvent.VK_SCROLL_LOCK || keyCode == KeyEvent.VK_KANA_LOCK)) {
  873. throw new IllegalArgumentException("invalid key for Toolkit.setLockingKeyState");
  874. }
  875. throw new UnsupportedOperationException("Toolkit.setLockingKeyState");
  876. }
  877. /**
  878. * Give native peers the ability to query the native container
  879. * given a native component (eg the direct parent may be lightweight).
  880. */
  881. protected static Container getNativeContainer(Component c) {
  882. return c.getNativeContainer();
  883. }
  884. /**
  885. * Creates a new custom cursor object.
  886. * If the image to display is invalid, the cursor will be hidden (made
  887. * completely transparent), and the hotspot will be set to (0, 0).
  888. * @param image the image to display when the cursor is active.
  889. * @param hotSpot the X and Y of the large cursor's hot spot. The
  890. * hotSpot values must be less than the Dimension returned by
  891. * getBestCursorSize().
  892. * @param name a localized description of the cursor, for Java Accessibility use.
  893. * @exception IndexOutOfBoundsException if the hotSpot values are outside
  894. * the bounds of the cursor.
  895. * @since 1.2
  896. */
  897. public Cursor createCustomCursor(Image cursor, Point hotSpot, String name)
  898. throws IndexOutOfBoundsException
  899. {
  900. // Override to implement custom cursor support.
  901. if (this != Toolkit.getDefaultToolkit()) {
  902. return Toolkit.getDefaultToolkit().
  903. createCustomCursor(cursor, hotSpot, name);
  904. } else {
  905. return new Cursor(Cursor.DEFAULT_CURSOR);
  906. }
  907. }
  908. /**
  909. * Returns the supported cursor dimension which is closest to the desired
  910. * sizes. Systems which only support a single cursor size will return that
  911. * size regardless of the desired sizes. Systems which don't support custom
  912. * cursors will return a dimension of 0, 0. <p>
  913. * Note: if an image is used whose dimensions don't match a supported size
  914. * (as returned by this method), the Toolkit implementation will attempt to
  915. * resize the image to a supported size.
  916. * Since converting low-resolution images is difficult,
  917. * no guarantees are made as to the quality of a cursor image which isn't a
  918. * supported size. It is therefore recommended that this method
  919. * be called and an appropriate image used so no image conversion is made.
  920. *
  921. * @param desiredWidth the preferred cursor width the component would like
  922. * to use.
  923. * @param desiredHeight the preferred cursor height the component would like
  924. * to use.
  925. * @return the closest matching supported cursor size, or a dimension of 0,0 if
  926. * the Toolkit implementation doesn't support custom cursors.
  927. * @since 1.2
  928. */
  929. public Dimension getBestCursorSize(int preferredWidth, int preferredHeight) {
  930. // Override to implement custom cursor support.
  931. if (this != Toolkit.getDefaultToolkit()) {
  932. return Toolkit.getDefaultToolkit().
  933. getBestCursorSize(preferredWidth, preferredHeight);
  934. } else {
  935. return new Dimension(0, 0);
  936. }
  937. }
  938. /**
  939. * Returns the maximum number of colors the Toolkit supports in a custom cursor
  940. * palette.<p>
  941. * Note: if an image is used which has more colors in its palette than
  942. * the supported maximum, the Toolkit implementation will attempt to flatten the
  943. * palette to the maximum. Since converting low-resolution images is difficult,
  944. * no guarantees are made as to the quality of a cursor image which has more
  945. * colors than the system supports. It is therefore recommended that this method
  946. * be called and an appropriate image used so no image conversion is made.
  947. *
  948. * @return the maximum number of colors, or zero if custom cursors are not
  949. * supported by this Toolkit implementation.
  950. * @since 1.2
  951. */
  952. public int getMaximumCursorColors() {
  953. // Override to implement custom cursor support.
  954. if (this != Toolkit.getDefaultToolkit()) {
  955. return Toolkit.getDefaultToolkit().getMaximumCursorColors();
  956. } else {
  957. return 0;
  958. }
  959. }
  960. /**
  961. * Support for I18N: any visible strings should be stored in
  962. * java.awt.resources.awt.properties. The ResourceBundle is stored
  963. * here, so that only one copy is maintained.
  964. */
  965. private static ResourceBundle resources;
  966. /**
  967. * Initialize JNI field and method ids
  968. */
  969. private static native void initIDs();
  970. /**
  971. * WARNING: This is a temporary workaround for a problem in the
  972. * way the AWT loads native libraries. A number of classes in the
  973. * AWT package have a native method, initIDs(), which initializes
  974. * the JNI field and method ids used in the native portion of
  975. * their implementation.
  976. *
  977. * Since the use and storage of these ids is done by the
  978. * implementation libraries, the implementation of these method is
  979. * provided by the particular AWT implementations
  980. * (i.e. "Toolkit"s/Peer), such as Motif, Win32 or Tiny. The
  981. * problem is that this means that the native libraries must be
  982. * loaded by the java.* classes, which do not necessarily know the
  983. * names of the libraries to load. A better way of doing this
  984. * would be to provide a separate library which defines java.awt.*
  985. * initIDs, and exports the relevant symbols out to the
  986. * implementation libraries.
  987. *
  988. * For now, we know it's done by the implementation, and we assume
  989. * that the name of the library is "awt". -br.
  990. *
  991. * If you change loadLibraries(), please add the change to
  992. * java.awt.image.ColorModel.loadLibraries(). Unfortunately,
  993. * classes can be loaded in java.awt.image that depend on
  994. * libawt and there is no way to call Toolkit.loadLibraries()
  995. * directly. -hung
  996. */
  997. private static boolean loaded = false;
  998. static void loadLibraries() {
  999. if (!loaded) {
  1000. java.security.AccessController.doPrivileged(
  1001. new sun.security.action.LoadLibraryAction("awt"));
  1002. loaded = true;
  1003. }
  1004. }
  1005. static {
  1006. java.security.AccessController.doPrivileged(
  1007. new java.security.PrivilegedAction() {
  1008. public Object run() {
  1009. try {
  1010. resources =
  1011. ResourceBundle.getBundle("java.awt.resources.awt");
  1012. } catch (MissingResourceException e) {
  1013. // No resource file; defaults will be used.
  1014. }
  1015. return null;
  1016. }
  1017. });
  1018. // ensure that the proper libraries are loaded
  1019. loadLibraries();
  1020. initIDs();
  1021. }
  1022. /**
  1023. * Gets a property with the specified key and default.
  1024. * This method returns defaultValue if the property is not found.
  1025. */
  1026. public static String getProperty(String key, String defaultValue) {
  1027. if (resources != null) {
  1028. try {
  1029. return resources.getString(key);
  1030. }
  1031. catch (MissingResourceException e) {}
  1032. }
  1033. return defaultValue;
  1034. }
  1035. /**
  1036. * Get the application's or applet's EventQueue instance.
  1037. * Depending on the Toolkit implementation, different EventQueues
  1038. * may be returned for different applets. Applets should
  1039. * therefore not assume that the EventQueue instance returned
  1040. * by this method will be shared by other applets or the system.
  1041. *
  1042. * <p>First, if there is a security manager, its
  1043. * <code>checkAwtEventQueueAccess</code>
  1044. * method is called.
  1045. * If the default implementation of <code>checkAwtEventQueueAccess</code>
  1046. * is used (that is, that method is not overriden), then this results in
  1047. * a call to the security manager's <code>checkPermission</code> method
  1048. * with an <code>AWTPermission("accessEventQueue")</code> permission.
  1049. *
  1050. * @return the <code>EventQueue</code> object.
  1051. * @throws SecurityException
  1052. * if a security manager exists and its <code>{@link
  1053. * java.lang.SecurityManager#checkAwtEventQueueAccess}</code> method denies
  1054. * access to the EventQueue.
  1055. * @see java.awt.AWTPermission
  1056. */
  1057. public final EventQueue getSystemEventQueue() {
  1058. SecurityManager security = System.getSecurityManager();
  1059. if (security != null) {
  1060. security.checkAwtEventQueueAccess();
  1061. }
  1062. return getSystemEventQueueImpl();
  1063. }
  1064. /*
  1065. * Get the application's or applet's EventQueue instance, without
  1066. * checking access. For security reasons, this can only be called
  1067. * from a Toolkit subclass. Implementations wishing to modify
  1068. * the default EventQueue support should subclass this method.
  1069. */
  1070. protected abstract EventQueue getSystemEventQueueImpl();
  1071. /* Accessor method for use by AWT package routines. */
  1072. static EventQueue getEventQueue() {
  1073. return getDefaultToolkit().getSystemEventQueueImpl();
  1074. }
  1075. /**
  1076. * create the peer for a DragSourceContext
  1077. */
  1078. public abstract DragSourceContextPeer createDragSourceContextPeer(DragGestureEvent dge) throws InvalidDnDOperationException;
  1079. /**
  1080. * create a concrete, platform dependent, subclass of the abstract
  1081. * DragGestureRecognizer class requested, and associate it with the
  1082. * DragSource, Component and DragGestureListener specified
  1083. *
  1084. * subclasses should override this to provide their own implementation
  1085. *
  1086. * @param abstractRecognizerClass The abstract class of the required recognizer
  1087. * @param ds The DragSource
  1088. * @param c The Component target for the DragGestureRecognizer
  1089. * @param srcActions The actions permitted for the gesture
  1090. * @param dgl The DragGestureListener
  1091. *
  1092. * @Return the new object or null
  1093. */
  1094. public DragGestureRecognizer createDragGestureRecognizer(Class abstractRecognizerClass, DragSource ds, Component c, int srcActions, DragGestureListener dgl) {
  1095. return null;
  1096. }
  1097. /**
  1098. * obtain a value for the specified desktop property.
  1099. *
  1100. * A desktop property is a uniquely named value for a resource that
  1101. * is Toolkit global in nature. Usually it also is an abstract representation
  1102. * for an underlying platform dependent desktop setting.
  1103. */
  1104. public final synchronized Object getDesktopProperty(String propertyName) {
  1105. if (desktopProperties.isEmpty()) {
  1106. initializeDesktopProperties();
  1107. }
  1108. Object value = desktopProperties.get(propertyName);
  1109. if (value == null) {
  1110. value = lazilyLoadDesktopProperty(propertyName);
  1111. if (value != null) {
  1112. setDesktopProperty(propertyName, value);
  1113. }
  1114. }
  1115. return value;
  1116. }
  1117. /**
  1118. * set the named desktop property to the specified value and fire a
  1119. * property change event to notify any listeners that the value has changed
  1120. */
  1121. protected final void setDesktopProperty(String name, Object newValue) {
  1122. Object oldValue;
  1123. synchronized (this) {
  1124. oldValue = desktopProperties.get(name);
  1125. desktopProperties.put(name, newValue);
  1126. }
  1127. desktopPropsSupport.firePropertyChange(name, oldValue, newValue);
  1128. }
  1129. /**
  1130. * an opportunity to lazily evaluate desktop property values.
  1131. */
  1132. protected Object lazilyLoadDesktopProperty(String name) {
  1133. return null;
  1134. }
  1135. /**
  1136. * initializeDesktopProperties
  1137. */
  1138. protected void initializeDesktopProperties() {
  1139. }
  1140. /**
  1141. * add the specified property change listener for the named desktop
  1142. * property
  1143. * If pcl is null, no exception is thrown and no action is performed.
  1144. *
  1145. * @param name The name of the property to listen for
  1146. * @param pcl The property change listener
  1147. */
  1148. public synchronized void addPropertyChangeListener(String name, PropertyChangeListener pcl) {
  1149. if (pcl == null) {
  1150. return;
  1151. }
  1152. desktopPropsSupport.addPropertyChangeListener(name, pcl);
  1153. }
  1154. /**
  1155. * remove the specified property change listener for the named
  1156. * desktop property
  1157. * If pcl is null, no exception is thrown and no action is performed.
  1158. *
  1159. */
  1160. public synchronized void removePropertyChangeListener(String name, PropertyChangeListener pcl) {
  1161. if (pcl == null) {
  1162. return;
  1163. }
  1164. desktopPropsSupport.removePropertyChangeListener(name, pcl);
  1165. }
  1166. protected final Map desktopProperties = new HashMap();
  1167. protected final PropertyChangeSupport desktopPropsSupport = new PropertyChangeSupport(this);
  1168. private AWTEventListener eventListener = null;
  1169. private WeakHashMap listener2SelectiveListener = new WeakHashMap();
  1170. private AWTPermission listenToAllAWTEventsPermission = null;
  1171. /**
  1172. * Adds an AWTEventListener to receive all AWTEvents dispatched
  1173. * system-wide that conform to the given <code>eventMask</code>.
  1174. * <p>
  1175. * First, if there is a security manager, its <code>checkPermission</code>
  1176. * method is called with an
  1177. * <code>AWTPermission("listenToAllAWTEvents")</code> permission.
  1178. * This may result in a SecurityException.
  1179. * <p>
  1180. * <code>eventMask</code> is a bitmask of event types to receive.
  1181. * It is constructed by bitwise OR-ing together the event masks
  1182. * defined in <code>AWTEvent</code>.
  1183. * <p>
  1184. * Note: event listener use is not recommended for normal
  1185. * application use, but are intended solely to support special
  1186. * purpose facilities including support for accessibility,
  1187. * event record/playback, and diagnostic tracing.
  1188. *
  1189. * If listener is null, no exception is thrown and no action is performed.
  1190. *
  1191. * @param listener the event listener.
  1192. * @param eventMask the bitmask of event types to receive
  1193. * @throws SecurityException
  1194. * if a security manager exists and its
  1195. * <code>checkPermission</code> method doesn't allow the operation.
  1196. * @see java.awt.event.AWTEventListener
  1197. * @see java.awt.Toolkit#addAWTEventListener
  1198. * @see java.awt.AWTEvent
  1199. * @see SecurityManager#checkPermission
  1200. * @see java.awt.AWTPermission
  1201. * @since 1.2
  1202. */
  1203. public void addAWTEventListener(AWTEventListener listener, long eventMask) {
  1204. if (listener == null) {
  1205. return;
  1206. }
  1207. SecurityManager security = System.getSecurityManager();
  1208. if (security != null) {
  1209. if (listenToAllAWTEventsPermission == null) {
  1210. listenToAllAWTEventsPermission =
  1211. new AWTPermission("listenToAllAWTEvents");
  1212. }
  1213. security.checkPermission(listenToAllAWTEventsPermission);
  1214. }
  1215. synchronized (this) {
  1216. SelectiveAWTEventListener selectiveListener =
  1217. (SelectiveAWTEventListener)listener2SelectiveListener
  1218. .get(listener);
  1219. if (selectiveListener == null) {
  1220. // Create a new selectiveListener.
  1221. selectiveListener =
  1222. new SelectiveAWTEventListener(listener, eventMask);
  1223. listener2SelectiveListener.put(listener, selectiveListener);
  1224. eventListener = ToolkitEventMulticaster.add(eventListener,
  1225. selectiveListener);
  1226. }
  1227. // OR the eventMask into the selectiveListener's event mask.
  1228. selectiveListener.orEventMasks(eventMask);
  1229. }
  1230. }
  1231. /**
  1232. * Removes an AWTEventListener from receiving dispatched AWTEvents.
  1233. * <p>
  1234. * First, if there is a security manager, its <code>checkPermission</code>
  1235. * method is called with an
  1236. * <code>AWTPermission("listenToAllAWTEvents")</code> permission.
  1237. * This may result in a SecurityException.
  1238. * <p>
  1239. * Note: event listener use is not recommended for normal
  1240. * application use, but are intended solely to support special
  1241. * purpose facilities including support for accessibility,
  1242. * event record/playback, and diagnostic tracing.
  1243. *
  1244. * If listener is null, no exception is thrown and no action is performed.
  1245. *
  1246. * @param listener the event listener.
  1247. * @throws SecurityException
  1248. * if a security manager exists and its
  1249. * <code>checkPermission</code> method doesn't allow the operation.
  1250. * @see java.awt.event.AWTEventListener
  1251. * @see java.awt.Toolkit#addAWTEventListener
  1252. * @see java.awt.AWTEvent
  1253. * @see SecurityManager#checkPermission
  1254. * @see java.awt.AWTPermission
  1255. * @since 1.2
  1256. */
  1257. public void removeAWTEventListener(AWTEventListener listener) {
  1258. if (listener == null) {
  1259. return;
  1260. }
  1261. SecurityManager security = System.getSecurityManager();
  1262. if (security != null) {
  1263. if (listenToAllAWTEventsPermission == null) {
  1264. listenToAllAWTEventsPermission =
  1265. new AWTPermission("listenToAllAWTEvents");
  1266. }
  1267. security.checkPermission(listenToAllAWTEventsPermission);
  1268. }
  1269. synchronized (this) {
  1270. SelectiveAWTEventListener selectiveListener =
  1271. (SelectiveAWTEventListener)listener2SelectiveListener
  1272. .get(listener);
  1273. if (selectiveListener != null)
  1274. listener2SelectiveListener.remove(listener);
  1275. eventListener = ToolkitEventMulticaster.remove(eventListener,
  1276. (selectiveListener == null) ? listener : selectiveListener);
  1277. }
  1278. }
  1279. /*
  1280. * This method notifies any AWTEventListeners that an event
  1281. * is about to be dispatched.
  1282. *
  1283. * @param theEvent the event which will be dispatched.
  1284. */
  1285. void notifyAWTEventListeners(AWTEvent theEvent) {
  1286. if (eventListener != null) {
  1287. synchronized (this) {
  1288. if (eventListener != null)
  1289. eventListener.eventDispatched(theEvent);
  1290. }
  1291. }
  1292. }
  1293. static private class ToolkitEventMulticaster extends AWTEventMulticaster
  1294. implements AWTEventListener {
  1295. // Implementation cloned from AWTEventMulticaster.
  1296. ToolkitEventMulticaster(AWTEventListener a, AWTEventListener b) {
  1297. super(a, b);
  1298. }
  1299. static AWTEventListener add(AWTEventListener a,
  1300. AWTEventListener b) {
  1301. if (a == null) return b;
  1302. if (b == null) return a;
  1303. return new ToolkitEventMulticaster(a, b);
  1304. }
  1305. static AWTEventListener remove(AWTEventListener l,
  1306. AWTEventListener oldl) {
  1307. return (AWTEventListener) removeInternal(l, oldl);
  1308. }
  1309. // #4178589: must overload remove(EventListener) to call our add()
  1310. // instead of the static addInternal() so we allocate a
  1311. // ToolkitEventMulticaster instead of an AWTEventMulticaster.
  1312. // Note: this method is called by AWTEventListener.removeInternal(),
  1313. // so its method signature must match AWTEventListener.remove().
  1314. protected EventListener remove(EventListener oldl) {
  1315. if (oldl == a) return b;
  1316. if (oldl == b) return a;
  1317. AWTEventListener a2 = (AWTEventListener)removeInternal(a, oldl);
  1318. AWTEventListener b2 = (AWTEventListener)removeInternal(b, oldl);
  1319. if (a2 == a && b2 == b) {
  1320. return this; // it's not here
  1321. }
  1322. return add(a2, b2);
  1323. }
  1324. public void eventDispatched(AWTEvent event) {
  1325. ((AWTEventListener)a).eventDispatched(event);
  1326. ((AWTEventListener)b).eventDispatched(event);
  1327. }
  1328. }
  1329. private class SelectiveAWTEventListener implements AWTEventListener {
  1330. AWTEventListener listener;
  1331. private long eventMask;
  1332. static final int LONG_BITS = 64;
  1333. // This array contains the number of times to call the eventlistener
  1334. // for each event type.
  1335. int[] calls = new int[LONG_BITS];
  1336. public void orEventMasks(long mask) {
  1337. eventMask |= mask;
  1338. // For each event bit set in mask, increment its call count.
  1339. for (int i=0; i<LONG_BITS; i++) {
  1340. // If no bits are set, break out of loop.
  1341. if (mask == 0) {
  1342. break;
  1343. }
  1344. if ((mask & 1L) != 0) { // Always test bit 0.
  1345. calls[i]++;
  1346. }
  1347. mask >>>= 1; // Right shift, fill with zeros on left.
  1348. }
  1349. }
  1350. SelectiveAWTEventListener(AWTEventListener l, long mask) {
  1351. listener = l;
  1352. eventMask = mask;
  1353. }
  1354. public void eventDispatched(AWTEvent event) {
  1355. long eventBit = 0; // Used to save the bit of the event type.
  1356. if (((eventBit = eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0 &&
  1357. event.id >= ComponentEvent.COMPONENT_FIRST &&
  1358. event.id <= ComponentEvent.COMPONENT_LAST)
  1359. || ((eventBit = eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0 &&
  1360. event.id >= ContainerEvent.CONTAINER_FIRST &&
  1361. event.id <= ContainerEvent.CONTAINER_LAST)
  1362. || ((eventBit = eventMask & AWTEvent.FOCUS_EVENT_MASK) != 0 &&
  1363. event.id >= FocusEvent.FOCUS_FIRST &&
  1364. event.id <= FocusEvent.FOCUS_LAST)
  1365. || ((eventBit = eventMask & AWTEvent.KEY_EVENT_MASK) != 0 &&
  1366. event.id >= KeyEvent.KEY_FIRST &&
  1367. event.id <= KeyEvent.KEY_LAST)
  1368. || ((eventBit = eventMask & AWTEvent.MOUSE_MOTION_EVENT_MASK) != 0 &&
  1369. (event.id == MouseEvent.MOUSE_MOVED ||
  1370. event.id == MouseEvent.MOUSE_DRAGGED))
  1371. || ((eventBit = eventMask & AWTEvent.MOUSE_EVENT_MASK) != 0 &&
  1372. event.id != MouseEvent.MOUSE_MOVED &&
  1373. event.id != MouseEvent.MOUSE_DRAGGED &&
  1374. event.id >= MouseEvent.MOUSE_FIRST &&
  1375. event.id <= MouseEvent.MOUSE_LAST)
  1376. || ((eventBit = eventMask & AWTEvent.WINDOW_EVENT_MASK) != 0 &&
  1377. event.id >= WindowEvent.WINDOW_FIRST &&
  1378. event.id <= WindowEvent.WINDOW_LAST)
  1379. || ((eventBit = eventMask & AWTEvent.ACTION_EVENT_MASK) != 0 &&
  1380. event.id >= ActionEvent.ACTION_FIRST &&
  1381. event.id <= ActionEvent.ACTION_LAST)
  1382. || ((eventBit = eventMask & AWTEvent.ADJUSTMENT_EVENT_MASK) != 0 &&
  1383. event.id >= AdjustmentEvent.ADJUSTMENT_FIRST &&
  1384. event.id <= AdjustmentEvent.ADJUSTMENT_LAST)
  1385. || ((eventBit = eventMask & AWTEvent.ITEM_EVENT_MASK) != 0 &&
  1386. event.id >= ItemEvent.ITEM_FIRST &&
  1387. event.id <= ItemEvent.ITEM_LAST)
  1388. || ((eventBit = eventMask & AWTEvent.TEXT_EVENT_MASK) != 0 &&
  1389. event.id >= TextEvent.TEXT_FIRST &&
  1390. event.id <= TextEvent.TEXT_LAST)
  1391. || ((eventBit = eventMask & AWTEvent.INPUT_METHOD_EVENT_MASK) != 0 &&
  1392. event.id >= InputMethodEvent.INPUT_METHOD_FIRST &&
  1393. event.id <= InputMethodEvent.INPUT_METHOD_LAST)
  1394. || ((eventBit = eventMask & AWTEvent.PAINT_EVENT_MASK) != 0 &&
  1395. event.id >= PaintEvent.PAINT_FIRST &&
  1396. event.id <= PaintEvent.PAINT_LAST)
  1397. || ((eventBit = eventMask & AWTEvent.INVOCATION_EVENT_MASK) != 0 &&
  1398. event.id >= InvocationEvent.INVOCATION_FIRST &&
  1399. event.id <= InvocationEvent.INVOCATION_LAST)
  1400. || ((eventBit = eventMask & AWTEvent.HIERARCHY_EVENT_MASK) != 0 &&
  1401. event.id == HierarchyEvent.HIERARCHY_CHANGED)
  1402. || ((eventBit = eventMask & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) != 0 &&
  1403. (event.id == HierarchyEvent.ANCESTOR_MOVED ||
  1404. event.id == HierarchyEvent.ANCESTOR_RESIZED))) {
  1405. // Get the index of the call count for this event type.
  1406. int ci = (int) (Math.log(eventBit)/Math.log(2));
  1407. // Call the listener as many times as it was added for this
  1408. // event type.
  1409. for (int i=0; i<calls[ci]; i++) {
  1410. listener.eventDispatched(event);
  1411. }
  1412. }
  1413. }
  1414. }
  1415. /**
  1416. * Returns a map of visual attributes for the abstract level description
  1417. * of the given input method highlight, or null if no mapping is found.
  1418. * The style field of the input method highlight is ignored. The map
  1419. * returned is unmodifiable.
  1420. * @param highlight input method highlight
  1421. * @return style attribute map, or null
  1422. * @since 1.3
  1423. */
  1424. public abstract Map mapInputMethodHighlight(InputMethodHighlight highlight);
  1425. }
  1426. /**
  1427. * Implements the LightweightPeer interface for use in lightweight components
  1428. * that have no native window associated with them. This gets created by
  1429. * default in Component so that Component and Container can be directly
  1430. * extended to create useful components written entirely in java. These
  1431. * components must be hosted somewhere higher up in the component tree by a
  1432. * native container (such as a Frame).
  1433. *
  1434. * This implementation provides no useful semantics and serves only as a
  1435. * marker. One could provide alternative implementations in java that do
  1436. * something useful for some of the other peer interfaces to minimize the
  1437. * native code.
  1438. *
  1439. * @author Timothy Prinzing
  1440. */
  1441. class LightweightPeer implements java.awt.peer.LightweightPeer {
  1442. public LightweightPeer(Component target) {
  1443. }
  1444. public boolean isFocusTraversable() {
  1445. return false;
  1446. }
  1447. public void setVisible(boolean b) {
  1448. }
  1449. public void show() {
  1450. }
  1451. public void hide() {
  1452. }
  1453. public void setEnabled(boolean b) {
  1454. }
  1455. public void enable() {
  1456. }
  1457. public void disable() {
  1458. }
  1459. public void paint(Graphics g) {
  1460. }
  1461. public void repaint(long tm, int x, int y, int width, int height) {
  1462. }
  1463. public void print(Graphics g) {
  1464. }
  1465. public void setBounds(int x, int y, int width, int height) {
  1466. }
  1467. public void reshape(int x, int y, int width, int height) {
  1468. }
  1469. public void coalescePaintEvent(PaintEvent e) {
  1470. }
  1471. public boolean handleEvent(Event e) {
  1472. return false;
  1473. }
  1474. public void handleEvent(java.awt.AWTEvent arg0) {
  1475. }
  1476. public Dimension getPreferredSize() {
  1477. return new Dimension(1,1);
  1478. }
  1479. public Dimension getMinimumSize() {
  1480. return new Dimension(1,1);
  1481. }
  1482. public java.awt.Toolkit getToolkit() {
  1483. return null;
  1484. }
  1485. public ColorModel getColorModel() {
  1486. return null;
  1487. }
  1488. public Graphics getGraphics() {
  1489. return null;
  1490. }
  1491. public GraphicsConfiguration getGraphicsConfiguration() {
  1492. return null;
  1493. }
  1494. public FontMetrics getFontMetrics(Font font) {
  1495. return null;
  1496. }
  1497. public void dispose() {
  1498. // no native code
  1499. }
  1500. public void setForeground(Color c) {
  1501. }
  1502. public void setBackground(Color c) {
  1503. }
  1504. public void setFont(Font f) {
  1505. }
  1506. public void setCursor(Cursor cursor) {
  1507. }
  1508. public void requestFocus() {
  1509. }
  1510. public Image createImage(ImageProducer producer) {
  1511. return null;
  1512. }
  1513. public Image createImage(int width, int height) {
  1514. return null;
  1515. }
  1516. public boolean prepareImage(Image img, int w, int h, ImageObserver o) {
  1517. return false;
  1518. }
  1519. public int checkImage(Image img, int w, int h, ImageObserver o) {
  1520. return 0;
  1521. }
  1522. public Dimension preferredSize() {
  1523. return getPreferredSize();
  1524. }
  1525. public Dimension minimumSize() {
  1526. return getMinimumSize();
  1527. }
  1528. public Point getLocationOnScreen() {
  1529. return null;
  1530. }
  1531. }