1. /*
  2. * @(#)Toolkit.java 1.189 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt;
  8. import java.util.MissingResourceException;
  9. import java.util.Properties;
  10. import java.util.ResourceBundle;
  11. import java.util.StringTokenizer;
  12. import java.awt.event.*;
  13. import java.awt.peer.*;
  14. import java.awt.*;
  15. import java.awt.im.InputMethodHighlight;
  16. import java.awt.image.ImageObserver;
  17. import java.awt.image.ImageProducer;
  18. import java.awt.image.ColorModel;
  19. import java.awt.datatransfer.Clipboard;
  20. import java.awt.dnd.DnDConstants;
  21. import java.awt.dnd.DragSource;
  22. import java.awt.dnd.DragGestureRecognizer;
  23. import java.awt.dnd.DragGestureEvent;
  24. import java.awt.dnd.DragGestureListener;
  25. import java.awt.dnd.InvalidDnDOperationException;
  26. import java.awt.dnd.peer.DragSourceContextPeer;
  27. import java.net.URL;
  28. import java.io.BufferedInputStream;
  29. import java.io.File;
  30. import java.io.FileInputStream;
  31. import java.security.AccessController;
  32. import java.security.PrivilegedAction;
  33. import java.util.EventListener;
  34. import java.util.Map;
  35. import java.util.HashMap;
  36. import java.util.WeakHashMap;
  37. import java.util.List;
  38. import java.util.ArrayList;
  39. import java.beans.PropertyChangeListener;
  40. import java.beans.PropertyChangeSupport;
  41. import sun.awt.DebugHelper;
  42. import sun.awt.HeadlessToolkit;
  43. import sun.awt.NullComponentPeer;
  44. import sun.security.util.SecurityConstants;
  45. /**
  46. * This class is the abstract superclass of all actual
  47. * implementations of the Abstract Window Toolkit. Subclasses of
  48. * <code>Toolkit</code> are used to bind the various components
  49. * to particular native toolkit implementations.
  50. * <p>
  51. * Most applications should not call any of the methods in this
  52. * class directly. The methods defined by <code>Toolkit</code> are
  53. * the "glue" that joins the platform-independent classes in the
  54. * <code>java.awt</code> package with their counterparts in
  55. * <code>java.awt.peer</code>. Some methods defined by
  56. * <code>Toolkit</code> query the native operating system directly.
  57. *
  58. * @version 1.189, 01/23/03
  59. * @author Sami Shaio
  60. * @author Arthur van Hoff
  61. * @author Fred Ecks
  62. * @since JDK1.0
  63. */
  64. public abstract class Toolkit {
  65. /**
  66. * Creates this toolkit's implementation of <code>Button</code> using
  67. * the specified peer interface.
  68. * @param target the button to be implemented.
  69. * @return this toolkit's implementation of <code>Button</code>.
  70. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  71. * returns true
  72. * @see java.awt.GraphicsEnvironment#isHeadless
  73. * @see java.awt.Button
  74. * @see java.awt.peer.ButtonPeer
  75. */
  76. protected abstract ButtonPeer createButton(Button target)
  77. throws HeadlessException;
  78. /**
  79. * Creates this toolkit's implementation of <code>TextField</code> using
  80. * the specified peer interface.
  81. * @param target the text field to be implemented.
  82. * @return this toolkit's implementation of <code>TextField</code>.
  83. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  84. * returns true
  85. * @see java.awt.GraphicsEnvironment#isHeadless
  86. * @see java.awt.TextField
  87. * @see java.awt.peer.TextFieldPeer
  88. */
  89. protected abstract TextFieldPeer createTextField(TextField target)
  90. throws HeadlessException;
  91. /**
  92. * Creates this toolkit's implementation of <code>Label</code> using
  93. * the specified peer interface.
  94. * @param target the label to be implemented.
  95. * @return this toolkit's implementation of <code>Label</code>.
  96. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  97. * returns true
  98. * @see java.awt.GraphicsEnvironment#isHeadless
  99. * @see java.awt.Label
  100. * @see java.awt.peer.LabelPeer
  101. */
  102. protected abstract LabelPeer createLabel(Label target)
  103. throws HeadlessException;
  104. /**
  105. * Creates this toolkit's implementation of <code>List</code> using
  106. * the specified peer interface.
  107. * @param target the list to be implemented.
  108. * @return this toolkit's implementation of <code>List</code>.
  109. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  110. * returns true
  111. * @see java.awt.GraphicsEnvironment#isHeadless
  112. * @see java.awt.List
  113. * @see java.awt.peer.ListPeer
  114. */
  115. protected abstract ListPeer createList(java.awt.List target)
  116. throws HeadlessException;
  117. /**
  118. * Creates this toolkit's implementation of <code>Checkbox</code> using
  119. * the specified peer interface.
  120. * @param target the check box to be implemented.
  121. * @return this toolkit's implementation of <code>Checkbox</code>.
  122. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  123. * returns true
  124. * @see java.awt.GraphicsEnvironment#isHeadless
  125. * @see java.awt.Checkbox
  126. * @see java.awt.peer.CheckboxPeer
  127. */
  128. protected abstract CheckboxPeer createCheckbox(Checkbox target)
  129. throws HeadlessException;
  130. /**
  131. * Creates this toolkit's implementation of <code>Scrollbar</code> using
  132. * the specified peer interface.
  133. * @param target the scroll bar to be implemented.
  134. * @return this toolkit's implementation of <code>Scrollbar</code>.
  135. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  136. * returns true
  137. * @see java.awt.GraphicsEnvironment#isHeadless
  138. * @see java.awt.Scrollbar
  139. * @see java.awt.peer.ScrollbarPeer
  140. */
  141. protected abstract ScrollbarPeer createScrollbar(Scrollbar target)
  142. throws HeadlessException;
  143. /**
  144. * Creates this toolkit's implementation of <code>ScrollPane</code> using
  145. * the specified peer interface.
  146. * @param target the scroll pane to be implemented.
  147. * @return this toolkit's implementation of <code>ScrollPane</code>.
  148. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  149. * returns true
  150. * @see java.awt.GraphicsEnvironment#isHeadless
  151. * @see java.awt.ScrollPane
  152. * @see java.awt.peer.ScrollPanePeer
  153. * @since JDK1.1
  154. */
  155. protected abstract ScrollPanePeer createScrollPane(ScrollPane target)
  156. throws HeadlessException;
  157. /**
  158. * Creates this toolkit's implementation of <code>TextArea</code> using
  159. * the specified peer interface.
  160. * @param target the text area to be implemented.
  161. * @return this toolkit's implementation of <code>TextArea</code>.
  162. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  163. * returns true
  164. * @see java.awt.GraphicsEnvironment#isHeadless
  165. * @see java.awt.TextArea
  166. * @see java.awt.peer.TextAreaPeer
  167. */
  168. protected abstract TextAreaPeer createTextArea(TextArea target)
  169. throws HeadlessException;
  170. /**
  171. * Creates this toolkit's implementation of <code>Choice</code> using
  172. * the specified peer interface.
  173. * @param target the choice to be implemented.
  174. * @return this toolkit's implementation of <code>Choice</code>.
  175. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  176. * returns true
  177. * @see java.awt.GraphicsEnvironment#isHeadless
  178. * @see java.awt.Choice
  179. * @see java.awt.peer.ChoicePeer
  180. */
  181. protected abstract ChoicePeer createChoice(Choice target)
  182. throws HeadlessException;
  183. /**
  184. * Creates this toolkit's implementation of <code>Frame</code> using
  185. * the specified peer interface.
  186. * @param target the frame to be implemented.
  187. * @return this toolkit's implementation of <code>Frame</code>.
  188. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  189. * returns true
  190. * @see java.awt.GraphicsEnvironment#isHeadless
  191. * @see java.awt.Frame
  192. * @see java.awt.peer.FramePeer
  193. */
  194. protected abstract FramePeer createFrame(Frame target)
  195. throws HeadlessException;
  196. /**
  197. * Creates this toolkit's implementation of <code>Canvas</code> using
  198. * the specified peer interface.
  199. * @param target the canvas to be implemented.
  200. * @return this toolkit's implementation of <code>Canvas</code>.
  201. * @see java.awt.Canvas
  202. * @see java.awt.peer.CanvasPeer
  203. */
  204. protected abstract CanvasPeer createCanvas(Canvas target);
  205. /**
  206. * Creates this toolkit's implementation of <code>Panel</code> using
  207. * the specified peer interface.
  208. * @param target the panel to be implemented.
  209. * @return this toolkit's implementation of <code>Panel</code>.
  210. * @see java.awt.Panel
  211. * @see java.awt.peer.PanelPeer
  212. */
  213. protected abstract PanelPeer createPanel(Panel target);
  214. /**
  215. * Creates this toolkit's implementation of <code>Window</code> using
  216. * the specified peer interface.
  217. * @param target the window to be implemented.
  218. * @return this toolkit's implementation of <code>Window</code>.
  219. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  220. * returns true
  221. * @see java.awt.GraphicsEnvironment#isHeadless
  222. * @see java.awt.Window
  223. * @see java.awt.peer.WindowPeer
  224. */
  225. protected abstract WindowPeer createWindow(Window target)
  226. throws HeadlessException;
  227. /**
  228. * Creates this toolkit's implementation of <code>Dialog</code> using
  229. * the specified peer interface.
  230. * @param target the dialog to be implemented.
  231. * @return this toolkit's implementation of <code>Dialog</code>.
  232. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  233. * returns true
  234. * @see java.awt.GraphicsEnvironment#isHeadless
  235. * @see java.awt.Dialog
  236. * @see java.awt.peer.DialogPeer
  237. */
  238. protected abstract DialogPeer createDialog(Dialog target)
  239. throws HeadlessException;
  240. /**
  241. * Creates this toolkit's implementation of <code>MenuBar</code> using
  242. * the specified peer interface.
  243. * @param target the menu bar to be implemented.
  244. * @return this toolkit's implementation of <code>MenuBar</code>.
  245. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  246. * returns true
  247. * @see java.awt.GraphicsEnvironment#isHeadless
  248. * @see java.awt.MenuBar
  249. * @see java.awt.peer.MenuBarPeer
  250. */
  251. protected abstract MenuBarPeer createMenuBar(MenuBar target)
  252. throws HeadlessException;
  253. /**
  254. * Creates this toolkit's implementation of <code>Menu</code> using
  255. * the specified peer interface.
  256. * @param target the menu to be implemented.
  257. * @return this toolkit's implementation of <code>Menu</code>.
  258. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  259. * returns true
  260. * @see java.awt.GraphicsEnvironment#isHeadless
  261. * @see java.awt.Menu
  262. * @see java.awt.peer.MenuPeer
  263. */
  264. protected abstract MenuPeer createMenu(Menu target)
  265. throws HeadlessException;
  266. /**
  267. * Creates this toolkit's implementation of <code>PopupMenu</code> using
  268. * the specified peer interface.
  269. * @param target the popup menu to be implemented.
  270. * @return this toolkit's implementation of <code>PopupMenu</code>.
  271. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  272. * returns true
  273. * @see java.awt.GraphicsEnvironment#isHeadless
  274. * @see java.awt.PopupMenu
  275. * @see java.awt.peer.PopupMenuPeer
  276. * @since JDK1.1
  277. */
  278. protected abstract PopupMenuPeer createPopupMenu(PopupMenu target)
  279. throws HeadlessException;
  280. /**
  281. * Creates this toolkit's implementation of <code>MenuItem</code> using
  282. * the specified peer interface.
  283. * @param target the menu item to be implemented.
  284. * @return this toolkit's implementation of <code>MenuItem</code>.
  285. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  286. * returns true
  287. * @see java.awt.GraphicsEnvironment#isHeadless
  288. * @see java.awt.MenuItem
  289. * @see java.awt.peer.MenuItemPeer
  290. */
  291. protected abstract MenuItemPeer createMenuItem(MenuItem target)
  292. throws HeadlessException;
  293. /**
  294. * Creates this toolkit's implementation of <code>FileDialog</code> using
  295. * the specified peer interface.
  296. * @param target the file dialog to be implemented.
  297. * @return this toolkit's implementation of <code>FileDialog</code>.
  298. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  299. * returns true
  300. * @see java.awt.GraphicsEnvironment#isHeadless
  301. * @see java.awt.FileDialog
  302. * @see java.awt.peer.FileDialogPeer
  303. */
  304. protected abstract FileDialogPeer createFileDialog(FileDialog target)
  305. throws HeadlessException;
  306. /**
  307. * Creates this toolkit's implementation of <code>CheckboxMenuItem</code> using
  308. * the specified peer interface.
  309. * @param target the checkbox menu item to be implemented.
  310. * @return this toolkit's implementation of <code>CheckboxMenuItem</code>.
  311. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  312. * returns true
  313. * @see java.awt.GraphicsEnvironment#isHeadless
  314. * @see java.awt.CheckboxMenuItem
  315. * @see java.awt.peer.CheckboxMenuItemPeer
  316. */
  317. protected abstract CheckboxMenuItemPeer createCheckboxMenuItem(
  318. CheckboxMenuItem target) throws HeadlessException;
  319. private static LightweightPeer lightweightMarker;
  320. /**
  321. * Creates a peer for a component or container. This peer is windowless
  322. * and allows the Component and Container classes to be extended directly
  323. * to create windowless components that are defined entirely in java.
  324. *
  325. * @param target The Component to be created.
  326. */
  327. protected LightweightPeer createComponent(Component target) {
  328. if (lightweightMarker == null) {
  329. lightweightMarker = new NullComponentPeer();
  330. }
  331. return lightweightMarker;
  332. }
  333. /**
  334. * Creates this toolkit's implementation of <code>Font</code> using
  335. * the specified peer interface.
  336. * @param name the font to be implemented
  337. * @param style the style of the font, such as <code>PLAIN</code>,
  338. * <code>BOLD</code>, <code>ITALIC</code>, or a combination
  339. * @return this toolkit's implementation of <code>Font</code>
  340. * @see java.awt.Font
  341. * @see java.awt.peer.FontPeer
  342. * @see java.awt.GraphicsEnvironment#getAllFonts
  343. * @deprecated see java.awt.GraphicsEnvironment#getAllFonts
  344. */
  345. protected abstract FontPeer getFontPeer(String name, int style);
  346. // The following method is called by the private method
  347. // <code>updateSystemColors</code> in <code>SystemColor</code>.
  348. /**
  349. * Fills in the integer array that is supplied as an argument
  350. * with the current system color values.
  351. *
  352. * @param systemColors an integer array.
  353. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  354. * returns true
  355. * @see java.awt.GraphicsEnvironment#isHeadless
  356. * @since JDK1.1
  357. */
  358. protected void loadSystemColors(int[] systemColors)
  359. throws HeadlessException {
  360. }
  361. /**
  362. * Controls whether the layout of Containers is validated dynamically
  363. * during resizing, or statically, after resizing is complete.
  364. * Note that this feature is not supported on all platforms, and
  365. * conversely, that this feature cannot be turned off on some platforms.
  366. * On platforms where dynamic layout during resize is not supported
  367. * (or is always supported), setting this property has no effect.
  368. * Note that this feature can be set or unset as a property of the
  369. * operating system or window manager on some platforms. On such
  370. * platforms, the dynamic resize property must be set at the operating
  371. * system or window manager level before this method can take effect.
  372. * This method does not change the underlying operating system or
  373. * window manager support or settings. The OS/WM support can be
  374. * queried using getDesktopProperty("awt.dynamicLayoutSupported").
  375. *
  376. * @param dynamic If true, Containers should re-layout their
  377. * components as the Container is being resized. If false,
  378. * the layout will be validated after resizing is finished.
  379. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  380. * returns true
  381. * @see #isDynamicLayoutSet()
  382. * @see #isDynamicLayoutActive()
  383. * @see #getDesktopProperty(String propertyName)
  384. * @see java.awt.GraphicsEnvironment#isHeadless
  385. * @since 1.4
  386. */
  387. public void setDynamicLayout(boolean dynamic)
  388. throws HeadlessException {
  389. }
  390. /**
  391. * Returns whether the layout of Containers is validated dynamically
  392. * during resizing, or statically, after resizing is complete.
  393. * Note: this method returns the value that was set programmatically;
  394. * it does not reflect support at the level of the operating system
  395. * or window manager for dynamic layout on resizing, or the current
  396. * operating system or window manager settings. The OS/WM support can
  397. * be queried using getDesktopProperty("awt.dynamicLayoutSupported").
  398. *
  399. * @return true if validation of Containers is done dynamically,
  400. * false if validation is done after resizing is finished.
  401. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  402. * returns true
  403. * @see #setDynamicLayout(boolean dynamic)
  404. * @see #isDynamicLayoutActive()
  405. * @see #getDesktopProperty(String propertyName)
  406. * @see java.awt.GraphicsEnvironment#isHeadless
  407. * @since 1.4
  408. */
  409. protected boolean isDynamicLayoutSet()
  410. throws HeadlessException {
  411. if (this != Toolkit.getDefaultToolkit()) {
  412. return Toolkit.getDefaultToolkit().isDynamicLayoutSet();
  413. } else {
  414. return false;
  415. }
  416. }
  417. /**
  418. * Returns whether dynamic layout of Containers on resize is
  419. * currently active (both set programmatically, and supported
  420. * by the underlying operating system and/or window manager).
  421. * The OS/WM support can be queried using
  422. * getDesktopProperty("awt.dynamicLayoutSupported").
  423. *
  424. * @return true if dynamic layout of Containers on resize is
  425. * currently active, false otherwise.
  426. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  427. * returns true
  428. * @see #setDynamicLayout(boolean dynamic)
  429. * @see #isDynamicLayoutSet()
  430. * @see #getDesktopProperty(String propertyName)
  431. * @see java.awt.GraphicsEnvironment#isHeadless
  432. * @since 1.4
  433. */
  434. public boolean isDynamicLayoutActive()
  435. throws HeadlessException {
  436. if (this != Toolkit.getDefaultToolkit()) {
  437. return Toolkit.getDefaultToolkit().isDynamicLayoutActive();
  438. } else {
  439. return false;
  440. }
  441. }
  442. /**
  443. * Gets the size of the screen. On systems with multiple displays, the
  444. * primary display is used. Multi-screen aware display dimensions are
  445. * available from <code>GraphicsConfiguration</code> and
  446. * <code>GraphicsDevice</code>.
  447. * @return the size of this toolkit's screen, in pixels.
  448. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  449. * returns true
  450. * @see java.awt.GraphicsConfiguration#getBounds
  451. * @see java.awt.GraphicsDevice#getDisplayMode
  452. * @see java.awt.GraphicsEnvironment#isHeadless
  453. */
  454. public abstract Dimension getScreenSize()
  455. throws HeadlessException;
  456. /**
  457. * Returns the screen resolution in dots-per-inch.
  458. * @return this toolkit's screen resolution, in dots-per-inch.
  459. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  460. * returns true
  461. * @see java.awt.GraphicsEnvironment#isHeadless
  462. */
  463. public abstract int getScreenResolution()
  464. throws HeadlessException;
  465. /**
  466. * Gets the insets of the screen.
  467. * @param gc a <code>GraphicsConfiguration</code>
  468. * @return the insets of this toolkit's screen, in pixels.
  469. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  470. * returns true
  471. * @see java.awt.GraphicsEnvironment#isHeadless
  472. * @since 1.4
  473. */
  474. public Insets getScreenInsets(GraphicsConfiguration gc)
  475. throws HeadlessException {
  476. if (this != Toolkit.getDefaultToolkit()) {
  477. return Toolkit.getDefaultToolkit().getScreenInsets(gc);
  478. } else {
  479. return new Insets(0, 0, 0, 0);
  480. }
  481. }
  482. /**
  483. * Determines the color model of this toolkit's screen.
  484. * <p>
  485. * <code>ColorModel</code> is an abstract class that
  486. * encapsulates the ability to translate between the
  487. * pixel values of an image and its red, green, blue,
  488. * and alpha components.
  489. * <p>
  490. * This toolkit method is called by the
  491. * <code>getColorModel</code> method
  492. * of the <code>Component</code> class.
  493. * @return the color model of this toolkit's screen.
  494. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  495. * returns true
  496. * @see java.awt.GraphicsEnvironment#isHeadless
  497. * @see java.awt.image.ColorModel
  498. * @see java.awt.Component#getColorModel
  499. */
  500. public abstract ColorModel getColorModel()
  501. throws HeadlessException;
  502. /**
  503. * Returns the names of the available fonts in this toolkit.<p>
  504. * For 1.1, the following font names are deprecated (the replacement
  505. * name follows):
  506. * <ul>
  507. * <li>TimesRoman (use Serif)
  508. * <li>Helvetica (use SansSerif)
  509. * <li>Courier (use Monospaced)
  510. * </ul><p>
  511. * The ZapfDingbats fontname is also deprecated in 1.1 but the characters
  512. * are defined in Unicode starting at 0x2700, and as of 1.1 Java supports
  513. * those characters.
  514. * @return the names of the available fonts in this toolkit.
  515. * @deprecated see {@link java.awt.GraphicsEnvironment#getAvailableFontFamilyNames()}
  516. * @see java.awt.GraphicsEnvironment#getAvailableFontFamilyNames()
  517. */
  518. public abstract String[] getFontList();
  519. /**
  520. * Gets the screen device metrics for rendering of the font.
  521. * @param font a font
  522. * @return the screen metrics of the specified font in this toolkit
  523. * @deprecated As of JDK version 1.2, replaced by the <code>Font</code>
  524. * method <code>getLineMetrics</code>.
  525. * @see java.awt.font.LineMetrics
  526. * @see java.awt.Font#getLineMetrics
  527. * @see java.awt.GraphicsEnvironment#getScreenDevices
  528. */
  529. public abstract FontMetrics getFontMetrics(Font font);
  530. /**
  531. * Synchronizes this toolkit's graphics state. Some window systems
  532. * may do buffering of graphics events.
  533. * <p>
  534. * This method ensures that the display is up-to-date. It is useful
  535. * for animation.
  536. */
  537. public abstract void sync();
  538. /**
  539. * The default toolkit.
  540. */
  541. private static Toolkit toolkit;
  542. /**
  543. * Loads additional classes into the VM, using the property
  544. * 'assistive_technologies' specified in the Sun reference
  545. * implementation by a line in the 'accessibility.properties'
  546. * file. The form is "assistive_technologies=..." where
  547. * the "..." is a comma-separated list of assistive technology
  548. * classes to load. Each class is loaded in the order given
  549. * and a single instance of each is created using
  550. * Class.forName(class).newInstance(). All errors are handled
  551. * via an AWTError exception.
  552. *
  553. * <p>The assumption is made that assistive technology classes are supplied
  554. * as part of INSTALLED (as opposed to: BUNDLED) extensions or specified
  555. * on the class path
  556. * (and therefore can be loaded using the class loader returned by
  557. * a call to <code>ClassLoader.getSystemClassLoader</code>, whose
  558. * delegation parent is the extension class loader for installed
  559. * extensions).
  560. */
  561. private static void loadAssistiveTechnologies() {
  562. // Get accessibility properties
  563. final String sep = File.separator;
  564. final Properties properties = new Properties();
  565. String atNames = (String)java.security.AccessController.doPrivileged(
  566. new java.security.PrivilegedAction() {
  567. public Object run() {
  568. // Try loading the per-user accessibility properties file.
  569. try {
  570. File propsFile = new File(
  571. System.getProperty("user.home") +
  572. sep + ".accessibility.properties");
  573. FileInputStream in =
  574. new FileInputStream(propsFile);
  575. // Inputstream has been buffered in Properties class
  576. properties.load(in);
  577. in.close();
  578. } catch (Exception e) {
  579. // Per-user accessibility properties file does not exist
  580. }
  581. // Try loading the system-wide accessibility properties
  582. // file only if a per-user accessibility properties
  583. // file does not exist or is empty.
  584. if (properties.size() == 0) {
  585. try {
  586. File propsFile = new File(
  587. System.getProperty("java.home") + sep + "lib" +
  588. sep + "accessibility.properties");
  589. FileInputStream in =
  590. new FileInputStream(propsFile);
  591. // Inputstream has been buffered in Properties class
  592. properties.load(in);
  593. in.close();
  594. } catch (Exception e) {
  595. // System-wide accessibility properties file does
  596. // not exist;
  597. }
  598. }
  599. // Get whether a screen magnifier is present. First check
  600. // the system property and then check the properties file.
  601. String magPresent = System.getProperty("javax.accessibility.screen_magnifier_present");
  602. if (magPresent == null) {
  603. magPresent = properties.getProperty("screen_magnifier_present", null);
  604. if (magPresent != null) {
  605. System.setProperty("javax.accessibility.screen_magnifier_present", magPresent);
  606. }
  607. }
  608. // Get the names of any assistive technolgies to load. First
  609. // check the system property and then check the properties
  610. // file.
  611. String classNames = System.getProperty("javax.accessibility.assistive_technologies");
  612. if (classNames == null) {
  613. classNames = properties.getProperty("assistive_technologies", null);
  614. if (classNames != null) {
  615. System.setProperty("javax.accessibility.assistive_technologies", classNames);
  616. }
  617. }
  618. return classNames;
  619. }
  620. });
  621. // Load any assistive technologies
  622. if (atNames != null) {
  623. ClassLoader cl = ClassLoader.getSystemClassLoader();
  624. StringTokenizer parser = new StringTokenizer(atNames," ,");
  625. String atName;
  626. while (parser.hasMoreTokens()) {
  627. atName = parser.nextToken();
  628. try {
  629. Class clazz;
  630. if (cl != null) {
  631. clazz = cl.loadClass(atName);
  632. } else {
  633. clazz = Class.forName(atName);
  634. }
  635. clazz.newInstance();
  636. } catch (ClassNotFoundException e) {
  637. throw new AWTError("Assistive Technology not found: "
  638. + atName);
  639. } catch (InstantiationException e) {
  640. throw new AWTError("Could not instantiate Assistive"
  641. + " Technology: " + atName);
  642. } catch (IllegalAccessException e) {
  643. throw new AWTError("Could not access Assistive"
  644. + " Technology: " + atName);
  645. } catch (Exception e) {
  646. throw new AWTError("Error trying to install Assistive"
  647. + " Technology: " + atName + " " + e);
  648. }
  649. }
  650. }
  651. }
  652. /**
  653. * Gets the default toolkit.
  654. * <p>
  655. * If there is a system property named <code>"awt.toolkit"</code>,
  656. * that property is treated as the name of a class that is a subclass
  657. * of <code>Toolkit</code>.
  658. * <p>
  659. * If the system property does not exist, then the default toolkit
  660. * used is the class named <code>"sun.awt.motif.MToolkit"</code>,
  661. * which is a motif implementation of the Abstract Window Toolkit.
  662. * <p>
  663. * Also loads additional classes into the VM, using the property
  664. * 'assistive_technologies' specified in the Sun reference
  665. * implementation by a line in the 'accessibility.properties'
  666. * file. The form is "assistive_technologies=..." where
  667. * the "..." is a comma-separated list of assistive technology
  668. * classes to load. Each class is loaded in the order given
  669. * and a single instance of each is created using
  670. * Class.forName(class).newInstance(). This is done just after
  671. * the AWT toolkit is created. All errors are handled via an
  672. * AWTError exception.
  673. * @return the default toolkit.
  674. * @exception AWTError if a toolkit could not be found, or
  675. * if one could not be accessed or instantiated.
  676. */
  677. public static synchronized Toolkit getDefaultToolkit() {
  678. if (toolkit == null) {
  679. try {
  680. // We disable the JIT during toolkit initialization. This
  681. // tends to touch lots of classes that aren't needed again
  682. // later and therefore JITing is counter-productiive.
  683. java.lang.Compiler.disable();
  684. java.security.AccessController.doPrivileged(
  685. new java.security.PrivilegedAction() {
  686. public Object run() {
  687. String nm = null;
  688. Class cls = null;
  689. try {
  690. nm = System.getProperty("awt.toolkit",
  691. "sun.awt.motif.MToolkit");
  692. try {
  693. cls = Class.forName(nm);
  694. } catch (ClassNotFoundException e) {
  695. ClassLoader cl = ClassLoader.getSystemClassLoader();
  696. if (cl != null) {
  697. try {
  698. cls = cl.loadClass(nm);
  699. } catch (ClassNotFoundException ee) {
  700. throw new AWTError("Toolkit not found: " + nm);
  701. }
  702. }
  703. }
  704. if (cls != null) {
  705. toolkit = (Toolkit)cls.newInstance();
  706. if (GraphicsEnvironment.isHeadless()) {
  707. toolkit = new HeadlessToolkit(toolkit);
  708. }
  709. }
  710. } catch (InstantiationException e) {
  711. throw new AWTError("Could not instantiate Toolkit: " +
  712. nm);
  713. } catch (IllegalAccessException e) {
  714. throw new AWTError("Could not access Toolkit: " + nm);
  715. }
  716. return null;
  717. }
  718. });
  719. loadAssistiveTechnologies();
  720. } finally {
  721. // Make sure to always re-enable the JIT.
  722. java.lang.Compiler.enable();
  723. }
  724. }
  725. return toolkit;
  726. }
  727. /**
  728. * Returns an image which gets pixel data from the specified file,
  729. * whose format can be either GIF, JPEG or PNG.
  730. * The underlying toolkit attempts to resolve multiple requests
  731. * with the same filename to the same returned Image.
  732. * Since the mechanism required to facilitate this sharing of
  733. * Image objects may continue to hold onto images that are no
  734. * longer of use for an indefinite period of time, developers
  735. * are encouraged to implement their own caching of images by
  736. * using the createImage variant wherever available.
  737. * @param filename the name of a file containing pixel data
  738. * in a recognized file format.
  739. * @return an image which gets its pixel data from
  740. * the specified file.
  741. * @see #createImage(java.lang.String)
  742. */
  743. public abstract Image getImage(String filename);
  744. /**
  745. * Returns an image which gets pixel data from the specified URL.
  746. * The pixel data referenced by the specified URL must be in one
  747. * of the following formats: GIF, JPEG or PNG.
  748. * The underlying toolkit attempts to resolve multiple requests
  749. * with the same URL to the same returned Image.
  750. * Since the mechanism required to facilitate this sharing of
  751. * Image objects may continue to hold onto images that are no
  752. * longer of use for an indefinite period of time, developers
  753. * are encouraged to implement their own caching of images by
  754. * using the createImage variant wherever available.
  755. * @param url the URL to use in fetching the pixel data.
  756. * @return an image which gets its pixel data from
  757. * the specified URL.
  758. * @see #createImage(java.net.URL)
  759. */
  760. public abstract Image getImage(URL url);
  761. /**
  762. * Returns an image which gets pixel data from the specified file.
  763. * The returned Image is a new object which will not be shared
  764. * with any other caller of this method or its getImage variant.
  765. * @param filename the name of a file containing pixel data
  766. * in a recognized file format.
  767. * @return an image which gets its pixel data from
  768. * the specified file.
  769. * @see #getImage(java.lang.String)
  770. */
  771. public abstract Image createImage(String filename);
  772. /**
  773. * Returns an image which gets pixel data from the specified URL.
  774. * The returned Image is a new object which will not be shared
  775. * with any other caller of this method or its getImage variant.
  776. * @param url the URL to use in fetching the pixel data.
  777. * @return an image which gets its pixel data from
  778. * the specified URL.
  779. * @see #getImage(java.net.URL)
  780. */
  781. public abstract Image createImage(URL url);
  782. /**
  783. * Prepares an image for rendering.
  784. * <p>
  785. * If the values of the width and height arguments are both
  786. * <code>-1</code>, this method prepares the image for rendering
  787. * on the default screen; otherwise, this method prepares an image
  788. * for rendering on the default screen at the specified width and height.
  789. * <p>
  790. * The image data is downloaded asynchronously in another thread,
  791. * and an appropriately scaled screen representation of the image is
  792. * generated.
  793. * <p>
  794. * This method is called by components <code>prepareImage</code>
  795. * methods.
  796. * <p>
  797. * Information on the flags returned by this method can be found
  798. * with the definition of the <code>ImageObserver</code> interface.
  799. * @param image the image for which to prepare a
  800. * screen representation.
  801. * @param width the width of the desired screen
  802. * representation, or <code>-1</code>.
  803. * @param height the height of the desired screen
  804. * representation, or <code>-1</code>.
  805. * @param observer the <code>ImageObserver</code>
  806. * object to be notified as the
  807. * image is being prepared.
  808. * @return <code>true</code> if the image has already been
  809. * fully prepared; <code>false</code> otherwise.
  810. * @see java.awt.Component#prepareImage(java.awt.Image,
  811. * java.awt.image.ImageObserver)
  812. * @see java.awt.Component#prepareImage(java.awt.Image,
  813. * int, int, java.awt.image.ImageObserver)
  814. * @see java.awt.image.ImageObserver
  815. */
  816. public abstract boolean prepareImage(Image image, int width, int height,
  817. ImageObserver observer);
  818. /**
  819. * Indicates the construction status of a specified image that is
  820. * being prepared for display.
  821. * <p>
  822. * If the values of the width and height arguments are both
  823. * <code>-1</code>, this method returns the construction status of
  824. * a screen representation of the specified image in this toolkit.
  825. * Otherwise, this method returns the construction status of a
  826. * scaled representation of the image at the specified width
  827. * and height.
  828. * <p>
  829. * This method does not cause the image to begin loading.
  830. * An application must call <code>prepareImage</code> to force
  831. * the loading of an image.
  832. * <p>
  833. * This method is called by the component's <code>checkImage</code>
  834. * methods.
  835. * <p>
  836. * Information on the flags returned by this method can be found
  837. * with the definition of the <code>ImageObserver</code> interface.
  838. * @param image the image whose status is being checked.
  839. * @param width the width of the scaled version whose status is
  840. * being checked, or <code>-1</code>.
  841. * @param height the height of the scaled version whose status
  842. * is being checked, or <code>-1</code>.
  843. * @param observer the <code>ImageObserver</code> object to be
  844. * notified as the image is being prepared.
  845. * @return the bitwise inclusive <strong>OR</strong> of the
  846. * <code>ImageObserver</code> flags for the
  847. * image data that is currently available.
  848. * @see java.awt.Toolkit#prepareImage(java.awt.Image,
  849. * int, int, java.awt.image.ImageObserver)
  850. * @see java.awt.Component#checkImage(java.awt.Image,
  851. * java.awt.image.ImageObserver)
  852. * @see java.awt.Component#checkImage(java.awt.Image,
  853. * int, int, java.awt.image.ImageObserver)
  854. * @see java.awt.image.ImageObserver
  855. */
  856. public abstract int checkImage(Image image, int width, int height,
  857. ImageObserver observer);
  858. /**
  859. * Creates an image with the specified image producer.
  860. * @param producer the image producer to be used.
  861. * @return an image with the specified image producer.
  862. * @see java.awt.Image
  863. * @see java.awt.image.ImageProducer
  864. * @see java.awt.Component#createImage(java.awt.image.ImageProducer)
  865. */
  866. public abstract Image createImage(ImageProducer producer);
  867. /**
  868. * Creates an image which decodes the image stored in the specified
  869. * byte array.
  870. * <p>
  871. * The data must be in some image format, such as GIF or JPEG,
  872. * that is supported by this toolkit.
  873. * @param imagedata an array of bytes, representing
  874. * image data in a supported image format.
  875. * @return an image.
  876. * @since JDK1.1
  877. */
  878. public Image createImage(byte[] imagedata) {
  879. return createImage(imagedata, 0, imagedata.length);
  880. }
  881. /**
  882. * Creates an image which decodes the image stored in the specified
  883. * byte array, and at the specified offset and length.
  884. * The data must be in some image format, such as GIF or JPEG,
  885. * that is supported by this toolkit.
  886. * @param imagedata an array of bytes, representing
  887. * image data in a supported image format.
  888. * @param imageoffset the offset of the beginning
  889. * of the data in the array.
  890. * @param imagelength the length of the data in the array.
  891. * @return an image.
  892. * @since JDK1.1
  893. */
  894. public abstract Image createImage(byte[] imagedata,
  895. int imageoffset,
  896. int imagelength);
  897. /**
  898. * Gets a <code>PrintJob</code> object which is the result of initiating
  899. * a print operation on the toolkit's platform.
  900. * <p>
  901. * Each actual implementation of this method should first check if there
  902. * is a security manager installed. If there is, the method should call
  903. * the security manager's <code>checkPrintJobAccess</code> method to
  904. * ensure initiation of a print operation is allowed. If the default
  905. * implementation of <code>checkPrintJobAccess</code> is used (that is,
  906. * that method is not overriden), then this results in a call to the
  907. * security manager's <code>checkPermission</code> method with a <code>
  908. * RuntimePermission("queuePrintJob")</code> permission.
  909. *
  910. * @param frame the parent of the print dialog. May not be null.
  911. * @param jobtitle the title of the PrintJob. A null title is equivalent
  912. * to "".
  913. * @param props a Properties object containing zero or more properties.
  914. * Properties are not standardized and are not consistent across
  915. * implementations. Because of this, PrintJobs which require job
  916. * and page control should use the version of this function which
  917. * takes JobAttributes and PageAttributes objects. This object
  918. * may be updated to reflect the user's job choices on exit. May
  919. * be null.
  920. *
  921. * @return a <code>PrintJob</code> object, or <code>null</code> if the
  922. * user cancelled the print job.
  923. * @throws NullPointerException if frame is null. This exception is
  924. * always thrown when GraphicsEnvironment.isHeadless() returns
  925. * true.
  926. * @throws SecurityException if this thread is not allowed to initiate a
  927. * print job request
  928. * @see java.awt.GraphicsEnvironment#isHeadless
  929. * @see java.awt.PrintJob
  930. * @see java.lang.RuntimePermission
  931. * @since JDK1.1
  932. */
  933. public abstract PrintJob getPrintJob(Frame frame, String jobtitle,
  934. Properties props);
  935. /**
  936. * Gets a <code>PrintJob</code> object which is the result of initiating
  937. * a print operation on the toolkit's platform.
  938. * <p>
  939. * Each actual implementation of this method should first check if there
  940. * is a security manager installed. If there is, the method should call
  941. * the security manager's <code>checkPrintJobAccess</code> method to
  942. * ensure initiation of a print operation is allowed. If the default
  943. * implementation of <code>checkPrintJobAccess</code> is used (that is,
  944. * that method is not overriden), then this results in a call to the
  945. * security manager's <code>checkPermission</code> method with a <code>
  946. * RuntimePermission("queuePrintJob")</code> permission.
  947. *
  948. * @param frame the parent of the print dialog. May be null if and only
  949. * if jobAttributes is not null and jobAttributes.getDialog()
  950. * returns JobAttributes.DialogType.NONE or
  951. * JobAttributes.DialogType.COMMON.
  952. * @param jobtitle the title of the PrintJob. A null title is equivalent
  953. * to "".
  954. * @param jobAttributes a set of job attributes which will control the
  955. * PrintJob. The attributes will be updated to reflect the user's
  956. * choices as outlined in the JobAttributes documentation. May be
  957. * null.
  958. * @param pageAttributes a set of page attributes which will control the
  959. * PrintJob. The attributes will be applied to every page in the
  960. * job. The attributes will be updated to reflect the user's
  961. * choices as outlined in the PageAttributes documentation. May be
  962. * null.
  963. *
  964. * @return a <code>PrintJob</code> object, or <code>null</code> if the
  965. * user cancelled the print job.
  966. * @throws NullPointerException if frame is null and either jobAttributes
  967. * is null or jobAttributes.getDialog() returns
  968. * JobAttributes.DialogType.NATIVE.
  969. * @throws IllegalArgumentException if pageAttributes specifies differing
  970. * cross feed and feed resolutions. This exception is always
  971. * thrown when GraphicsEnvironment.isHeadless() returns true.
  972. * @throws SecurityException if this thread is not allowed to initiate a
  973. * print job request, or if jobAttributes specifies print to file,
  974. * and this thread is not allowed to access the file system
  975. * @see java.awt.PrintJob
  976. * @see java.awt.GraphicsEnvironment#isHeadless
  977. * @see java.lang.RuntimePermission
  978. * @see java.awt.JobAttributes
  979. * @see java.awt.PageAttributes
  980. * @since 1.3
  981. */
  982. public PrintJob getPrintJob(Frame frame, String jobtitle,
  983. JobAttributes jobAttributes,
  984. PageAttributes pageAttributes) {
  985. // Override to add printing support with new job/page control classes
  986. if (GraphicsEnvironment.isHeadless()) {
  987. throw new IllegalArgumentException();
  988. }
  989. if (this != Toolkit.getDefaultToolkit()) {
  990. return Toolkit.getDefaultToolkit().getPrintJob(frame, jobtitle,
  991. jobAttributes,
  992. pageAttributes);
  993. } else {
  994. return getPrintJob(frame, jobtitle, null);
  995. }
  996. }
  997. /**
  998. * Emits an audio beep.
  999. * @since JDK1.1
  1000. */
  1001. public abstract void beep();
  1002. /**
  1003. * Gets the singleton instance of the system Clipboard which interfaces
  1004. * with clipboard facilities provided by the native platform. This
  1005. * clipboard enables data transfer between Java programs and native
  1006. * applications which use native clipboard facilities.
  1007. * <p>
  1008. * In addition to any and all formats specified in the flavormap.properties
  1009. * file, or other file specified by the <code>AWT.DnD.flavorMapFileURL
  1010. * </code> Toolkit property, text returned by the system Clipboard's <code>
  1011. * getTransferData()</code> method is available in the following flavors:
  1012. * <ul>
  1013. * <li>DataFlavor.stringFlavor</li>
  1014. * <li>DataFlavor.plainTextFlavor (<b>deprecated</b>)</li>
  1015. * </ul>
  1016. * As with <code>java.awt.datatransfer.StringSelection</code>, if the
  1017. * requested flavor is <code>DataFlavor.plainTextFlavor</code>, or an
  1018. * equivalent flavor, a Reader is returned. <b>Note:</b> The behavior of
  1019. * the system Clipboard's <code>getTransferData()</code> method for <code>
  1020. * DataFlavor.plainTextFlavor</code>, and equivalent DataFlavors, is
  1021. * inconsistent with the definition of <code>DataFlavor.plainTextFlavor
  1022. * </code>. Because of this, support for <code>
  1023. * DataFlavor.plainTextFlavor</code>, and equivalent flavors, is
  1024. * <b>deprecated</b>.
  1025. * <p>
  1026. * Each actual implementation of this method should first check if there
  1027. * is a security manager installed. If there is, the method should call
  1028. * the security manager's <code>checkSystemClipboardAccess</code> method
  1029. * to ensure it's ok to to access the system clipboard. If the default
  1030. * implementation of <code>checkSystemClipboardAccess</code> is used (that
  1031. * is, that method is not overriden), then this results in a call to the
  1032. * security manager's <code>checkPermission</code> method with an <code>
  1033. * AWTPermission("accessClipboard")</code> permission.
  1034. *
  1035. * @return the system Clipboard
  1036. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  1037. * returns true
  1038. * @see java.awt.GraphicsEnvironment#isHeadless
  1039. * @see java.awt.datatransfer.Clipboard
  1040. * @see java.awt.datatransfer.StringSelection
  1041. * @see java.awt.datatransfer.DataFlavor#stringFlavor
  1042. * @see java.awt.datatransfer.DataFlavor#plainTextFlavor
  1043. * @see java.io.Reader
  1044. * @see java.awt.AWTPermission
  1045. * @since JDK1.1
  1046. */
  1047. public abstract Clipboard getSystemClipboard()
  1048. throws HeadlessException;
  1049. /**
  1050. * Gets the singleton instance of the system selection as a
  1051. * <code>Clipboard</code> object. This allows an application to read and
  1052. * modify the current, system-wide selection.
  1053. * <p>
  1054. * An application is responsible for updating the system selection whenever
  1055. * the user selects text, using either the mouse or the keyboard.
  1056. * Typically, this is implemented by installing a
  1057. * <code>FocusListener</code> on all <code>Component</code>s which support
  1058. * text selection, and, between <code>FOCUS_GAINED</code> and
  1059. * <code>FOCUS_LOST</code> events delivered to that <code>Component</code>,
  1060. * updating the system selection <code>Clipboard</code> when the selection
  1061. * changes inside the <code>Component</code>. Properly updating the system
  1062. * selection ensures that a Java application will interact correctly with
  1063. * native applications and other Java applications running simultaneously
  1064. * on the system. Note that <code>java.awt.TextComponent</code> and
  1065. * <code>javax.swing.text.JTextComponent</code> already adhere to this
  1066. * policy. When using these classes, and their subclasses, developers need
  1067. * not write any additional code.
  1068. * <p>
  1069. * Some platforms do not support a system selection <code>Clipboard</code>.
  1070. * On those platforms, this method will return <code>null</code>. In such a
  1071. * case, an application is absolved from its responsibility to update the
  1072. * system selection <code>Clipboard</code> as described above.
  1073. * <p>
  1074. * Each actual implementation of this method should first check if there
  1075. * is a <code>SecurityManager</code> installed. If there is, the method
  1076. * should call the <code>SecurityManager</code>'s
  1077. * <code>checkSystemClipboardAccess</code> method to ensure that client
  1078. * code has access the system selection. If the default implementation of
  1079. * <code>checkSystemClipboardAccess</code> is used (that is, if the method
  1080. * is not overridden), then this results in a call to the
  1081. * <code>SecurityManager</code>'s <code>checkPermission</code> method with
  1082. * an <code>AWTPermission("accessClipboard")</code> permission.
  1083. *
  1084. * @return the system selection as a <code>Clipboard</code>, or
  1085. * <code>null</code> if the native platform does not support a
  1086. * system selection <code>Clipboard</code>
  1087. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  1088. * returns true
  1089. *
  1090. * @see java.awt.datatransfer.Clipboard
  1091. * @see java.awt.event.FocusListener
  1092. * @see java.awt.event.FocusEvent#FOCUS_GAINED
  1093. * @see java.awt.event.FocusEvent#FOCUS_LOST
  1094. * @see TextComponent
  1095. * @see javax.swing.text.JTextComponent
  1096. * @see AWTPermission
  1097. * @see GraphicsEnvironment#isHeadless
  1098. * @since 1.4
  1099. */
  1100. public Clipboard getSystemSelection() throws HeadlessException {
  1101. if (this != Toolkit.getDefaultToolkit()) {
  1102. return Toolkit.getDefaultToolkit().getSystemSelection();
  1103. } else {
  1104. GraphicsEnvironment.checkHeadless();
  1105. return null;
  1106. }
  1107. }
  1108. /**
  1109. * Determines which modifier key is the appropriate accelerator
  1110. * key for menu shortcuts.
  1111. * <p>
  1112. * Menu shortcuts, which are embodied in the
  1113. * <code>MenuShortcut</code> class, are handled by the
  1114. * <code>MenuBar</code> class.
  1115. * <p>
  1116. * By default, this method returns <code>Event.CTRL_MASK</code>.
  1117. * Toolkit implementations should override this method if the
  1118. * <b>Control</b> key isn't the correct key for accelerators.
  1119. * @return the modifier mask on the <code>Event</code> class
  1120. * that is used for menu shortcuts on this toolkit.
  1121. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  1122. * returns true
  1123. * @see java.awt.GraphicsEnvironment#isHeadless
  1124. * @see java.awt.MenuBar
  1125. * @see java.awt.MenuShortcut
  1126. * @since JDK1.1
  1127. */
  1128. public int getMenuShortcutKeyMask() throws HeadlessException {
  1129. return Event.CTRL_MASK;
  1130. }
  1131. /**
  1132. * Returns whether the given locking key on the keyboard is currently in
  1133. * its "on" state.
  1134. * Valid key codes are
  1135. * {@link java.awt.event.KeyEvent#VK_CAPS_LOCK VK_CAPS_LOCK},
  1136. * {@link java.awt.event.KeyEvent#VK_NUM_LOCK VK_NUM_LOCK},
  1137. * {@link java.awt.event.KeyEvent#VK_SCROLL_LOCK VK_SCROLL_LOCK}, and
  1138. * {@link java.awt.event.KeyEvent#VK_KANA_LOCK VK_KANA_LOCK}.
  1139. *
  1140. * @exception java.lang.IllegalArgumentException if <code>keyCode</code>
  1141. * is not one of the valid key codes
  1142. * @exception java.lang.UnsupportedOperationException if the host system doesn't
  1143. * allow getting the state of this key programmatically, or if the keyboard
  1144. * doesn't have this key
  1145. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  1146. * returns true
  1147. * @see java.awt.GraphicsEnvironment#isHeadless
  1148. * @since 1.3
  1149. */
  1150. public boolean getLockingKeyState(int keyCode)
  1151. throws UnsupportedOperationException {
  1152. if (! (keyCode == KeyEvent.VK_CAPS_LOCK || keyCode == KeyEvent.VK_NUM_LOCK ||
  1153. keyCode == KeyEvent.VK_SCROLL_LOCK || keyCode == KeyEvent.VK_KANA_LOCK)) {
  1154. throw new IllegalArgumentException("invalid key for Toolkit.getLockingKeyState");
  1155. }
  1156. throw new UnsupportedOperationException("Toolkit.getLockingKeyState");
  1157. }
  1158. /**
  1159. * Sets the state of the given locking key on the keyboard.
  1160. * Valid key codes are
  1161. * {@link java.awt.event.KeyEvent#VK_CAPS_LOCK VK_CAPS_LOCK},
  1162. * {@link java.awt.event.KeyEvent#VK_NUM_LOCK VK_NUM_LOCK},
  1163. * {@link java.awt.event.KeyEvent#VK_SCROLL_LOCK VK_SCROLL_LOCK}, and
  1164. * {@link java.awt.event.KeyEvent#VK_KANA_LOCK VK_KANA_LOCK}.
  1165. * <p>
  1166. * Depending on the platform, setting the state of a locking key may
  1167. * involve event processing and therefore may not be immediately
  1168. * observable through getLockingKeyState.
  1169. *
  1170. * @exception java.lang.IllegalArgumentException if <code>keyCode</code>
  1171. * is not one of the valid key codes
  1172. * @exception java.lang.UnsupportedOperationException if the host system doesn't
  1173. * allow setting the state of this key programmatically, or if the keyboard
  1174. * doesn't have this key
  1175. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  1176. * returns true
  1177. * @see java.awt.GraphicsEnvironment#isHeadless
  1178. * @since 1.3
  1179. */
  1180. public void setLockingKeyState(int keyCode, boolean on)
  1181. throws UnsupportedOperationException {
  1182. if (! (keyCode == KeyEvent.VK_CAPS_LOCK || keyCode == KeyEvent.VK_NUM_LOCK ||
  1183. keyCode == KeyEvent.VK_SCROLL_LOCK || keyCode == KeyEvent.VK_KANA_LOCK)) {
  1184. throw new IllegalArgumentException("invalid key for Toolkit.setLockingKeyState");
  1185. }
  1186. throw new UnsupportedOperationException("Toolkit.setLockingKeyState");
  1187. }
  1188. /**
  1189. * Give native peers the ability to query the native container
  1190. * given a native component (eg the direct parent may be lightweight).
  1191. */
  1192. protected static Container getNativeContainer(Component c) {
  1193. return c.getNativeContainer();
  1194. }
  1195. /**
  1196. * Creates a new custom cursor object.
  1197. * If the image to display is invalid, the cursor will be hidden (made
  1198. * completely transparent), and the hotspot will be set to (0, 0).
  1199. *
  1200. * <p>Note that multi-frame images are invalid and may cause this
  1201. * method to hang.
  1202. *
  1203. * @param cursor the image to display when the cursor is actived
  1204. * @param hotSpot the X and Y of the large cursor's hot spot; the
  1205. * hotSpot values must be less than the Dimension returned by
  1206. * <code>getBestCursorSize</code>
  1207. * @param name a localized description of the cursor, for Java Accessibility use
  1208. * @exception IndexOutOfBoundsException if the hotSpot values are outside
  1209. * the bounds of the cursor
  1210. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  1211. * returns true
  1212. * @see java.awt.GraphicsEnvironment#isHeadless
  1213. * @since 1.2
  1214. */
  1215. public Cursor createCustomCursor(Image cursor, Point hotSpot, String name)
  1216. throws IndexOutOfBoundsException, HeadlessException
  1217. {
  1218. // Override to implement custom cursor support.
  1219. if (this != Toolkit.getDefaultToolkit()) {
  1220. return Toolkit.getDefaultToolkit().
  1221. createCustomCursor(cursor, hotSpot, name);
  1222. } else {
  1223. return new Cursor(Cursor.DEFAULT_CURSOR);
  1224. }
  1225. }
  1226. /**
  1227. * Returns the supported cursor dimension which is closest to the desired
  1228. * sizes. Systems which only support a single cursor size will return that
  1229. * size regardless of the desired sizes. Systems which don't support custom
  1230. * cursors will return a dimension of 0, 0. <p>
  1231. * Note: if an image is used whose dimensions don't match a supported size
  1232. * (as returned by this method), the Toolkit implementation will attempt to
  1233. * resize the image to a supported size.
  1234. * Since converting low-resolution images is difficult,
  1235. * no guarantees are made as to the quality of a cursor image which isn't a
  1236. * supported size. It is therefore recommended that this method
  1237. * be called and an appropriate image used so no image conversion is made.
  1238. *
  1239. * @param preferredWidth the preferred cursor width the component would like
  1240. * to use.
  1241. * @param preferredHeight the preferred cursor height the component would like
  1242. * to use.
  1243. * @return the closest matching supported cursor size, or a dimension of 0,0 if
  1244. * the Toolkit implementation doesn't support custom cursors.
  1245. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  1246. * returns true
  1247. * @see java.awt.GraphicsEnvironment#isHeadless
  1248. * @since 1.2
  1249. */
  1250. public Dimension getBestCursorSize(int preferredWidth,
  1251. int preferredHeight) throws HeadlessException {
  1252. // Override to implement custom cursor support.
  1253. if (this != Toolkit.getDefaultToolkit()) {
  1254. return Toolkit.getDefaultToolkit().
  1255. getBestCursorSize(preferredWidth, preferredHeight);
  1256. } else {
  1257. return new Dimension(0, 0);
  1258. }
  1259. }
  1260. /**
  1261. * Returns the maximum number of colors the Toolkit supports in a custom cursor
  1262. * palette.<p>
  1263. * Note: if an image is used which has more colors in its palette than
  1264. * the supported maximum, the Toolkit implementation will attempt to flatten the
  1265. * palette to the maximum. Since converting low-resolution images is difficult,
  1266. * no guarantees are made as to the quality of a cursor image which has more
  1267. * colors than the system supports. It is therefore recommended that this method
  1268. * be called and an appropriate image used so no image conversion is made.
  1269. *
  1270. * @return the maximum number of colors, or zero if custom cursors are not
  1271. * supported by this Toolkit implementation.
  1272. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  1273. * returns true
  1274. * @see java.awt.GraphicsEnvironment#isHeadless
  1275. * @since 1.2
  1276. */
  1277. public int getMaximumCursorColors() throws HeadlessException {
  1278. // Override to implement custom cursor support.
  1279. if (this != Toolkit.getDefaultToolkit()) {
  1280. return Toolkit.getDefaultToolkit().getMaximumCursorColors();
  1281. } else {
  1282. return 0;
  1283. }
  1284. }
  1285. /**
  1286. * Returns whether Toolkit supports this state for
  1287. * <code>Frame</code>s. This method tells whether the <em>UI
  1288. * concept</em> of, say, maximization or iconification is
  1289. * supported. It will always return false for "compound" states
  1290. * like <code>Frame.ICONIFIED|Frame.MAXIMIZED_VERT</code>.
  1291. * In other words, the rule of thumb is that only queries with a
  1292. * single frame state constant as an argument are meaningful.
  1293. *
  1294. * @param state one of named frame state constants.
  1295. * @return <code>true</code> is this frame state is supported by
  1296. * this Toolkit implementation, <code>false</code> otherwise.
  1297. * @exception HeadlessException
  1298. * if <code>GraphicsEnvironment.isHeadless()</code>
  1299. * returns <code>true</code>.
  1300. * @see java.awt.Frame#setExtendedState
  1301. * @since 1.4
  1302. */
  1303. public boolean isFrameStateSupported(int state)
  1304. throws HeadlessException
  1305. {
  1306. if (this != Toolkit.getDefaultToolkit()) {
  1307. return Toolkit.getDefaultToolkit().
  1308. isFrameStateSupported(state);
  1309. } else {
  1310. return (state == Frame.NORMAL); // others are not guaranteed
  1311. }
  1312. }
  1313. /**
  1314. * Support for I18N: any visible strings should be stored in
  1315. * sun.awt.resources.awt.properties. The ResourceBundle is stored
  1316. * here, so that only one copy is maintained.
  1317. */
  1318. private static ResourceBundle resources;
  1319. /**
  1320. * Initialize JNI field and method ids
  1321. */
  1322. private static native void initIDs();
  1323. /**
  1324. * WARNING: This is a temporary workaround for a problem in the
  1325. * way the AWT loads native libraries. A number of classes in the
  1326. * AWT package have a native method, initIDs(), which initializes
  1327. * the JNI field and method ids used in the native portion of
  1328. * their implementation.
  1329. *
  1330. * Since the use and storage of these ids is done by the
  1331. * implementation libraries, the implementation of these method is
  1332. * provided by the particular AWT implementations (for example,
  1333. * "Toolkit"s/Peer), such as Motif, Microsoft Windows, or Tiny. The
  1334. * problem is that this means that the native libraries must be
  1335. * loaded by the java.* classes, which do not necessarily know the
  1336. * names of the libraries to load. A better way of doing this
  1337. * would be to provide a separate library which defines java.awt.*
  1338. * initIDs, and exports the relevant symbols out to the
  1339. * implementation libraries.
  1340. *
  1341. * For now, we know it's done by the implementation, and we assume
  1342. * that the name of the library is "awt". -br.
  1343. *
  1344. * If you change loadLibraries(), please add the change to
  1345. * java.awt.image.ColorModel.loadLibraries(). Unfortunately,
  1346. * classes can be loaded in java.awt.image that depend on
  1347. * libawt and there is no way to call Toolkit.loadLibraries()
  1348. * directly. -hung
  1349. */
  1350. private static boolean loaded = false;
  1351. static void loadLibraries() {
  1352. if (!loaded) {
  1353. java.security.AccessController.doPrivileged(
  1354. new sun.security.action.LoadLibraryAction("awt"));
  1355. loaded = true;
  1356. }
  1357. }
  1358. static {
  1359. java.security.AccessController.doPrivileged(
  1360. new java.security.PrivilegedAction() {
  1361. public Object run() {
  1362. try {
  1363. resources =
  1364. ResourceBundle.getBundle("sun.awt.resources.awt");
  1365. } catch (MissingResourceException e) {
  1366. // No resource file; defaults will be used.
  1367. }
  1368. return null;
  1369. }
  1370. });
  1371. // ensure that the proper libraries are loaded
  1372. loadLibraries();
  1373. if (!GraphicsEnvironment.isHeadless()) {
  1374. initIDs();
  1375. }
  1376. }
  1377. /**
  1378. * Gets a property with the specified key and default.
  1379. * This method returns defaultValue if the property is not found.
  1380. */
  1381. public static String getProperty(String key, String defaultValue) {
  1382. if (resources != null) {
  1383. try {
  1384. return resources.getString(key);
  1385. }
  1386. catch (MissingResourceException e) {}
  1387. }
  1388. return defaultValue;
  1389. }
  1390. /**
  1391. * Get the application's or applet's EventQueue instance.
  1392. * Depending on the Toolkit implementation, different EventQueues
  1393. * may be returned for different applets. Applets should
  1394. * therefore not assume that the EventQueue instance returned
  1395. * by this method will be shared by other applets or the system.
  1396. *
  1397. * <p>First, if there is a security manager, its
  1398. * <code>checkAwtEventQueueAccess</code>
  1399. * method is called.
  1400. * If the default implementation of <code>checkAwtEventQueueAccess</code>
  1401. * is used (that is, that method is not overriden), then this results in
  1402. * a call to the security manager's <code>checkPermission</code> method
  1403. * with an <code>AWTPermission("accessEventQueue")</code> permission.
  1404. *
  1405. * @return the <code>EventQueue</code> object.
  1406. * @throws SecurityException
  1407. * if a security manager exists and its <code>{@link
  1408. * java.lang.SecurityManager#checkAwtEventQueueAccess}</code> method denies
  1409. * access to the EventQueue.
  1410. * @see java.awt.AWTPermission
  1411. */
  1412. public final EventQueue getSystemEventQueue() {
  1413. SecurityManager security = System.getSecurityManager();
  1414. if (security != null) {
  1415. security.checkAwtEventQueueAccess();
  1416. }
  1417. return getSystemEventQueueImpl();
  1418. }
  1419. /*
  1420. * Get the application's or applet's EventQueue instance, without
  1421. * checking access. For security reasons, this can only be called
  1422. * from a Toolkit subclass. Implementations wishing to modify
  1423. * the default EventQueue support should subclass this method.
  1424. */
  1425. protected abstract EventQueue getSystemEventQueueImpl();
  1426. /* Accessor method for use by AWT package routines. */
  1427. static EventQueue getEventQueue() {
  1428. return getDefaultToolkit().getSystemEventQueueImpl();
  1429. }
  1430. /**
  1431. * Creates the peer for a DragSourceContext.
  1432. * Always throws InvalidDndOperationException if
  1433. * GraphicsEnvironment.isHeadless() returns true.
  1434. * @see java.awt.GraphicsEnvironment#isHeadless
  1435. */
  1436. public abstract DragSourceContextPeer createDragSourceContextPeer(DragGestureEvent dge) throws InvalidDnDOperationException;
  1437. /**
  1438. * Creates a concrete, platform dependent, subclass of the abstract
  1439. * DragGestureRecognizer class requested, and associates it with the
  1440. * DragSource, Component and DragGestureListener specified.
  1441. *
  1442. * subclasses should override this to provide their own implementation
  1443. *
  1444. * @param abstractRecognizerClass The abstract class of the required recognizer
  1445. * @param ds The DragSource
  1446. * @param c The Component target for the DragGestureRecognizer
  1447. * @param srcActions The actions permitted for the gesture
  1448. * @param dgl The DragGestureListener
  1449. *
  1450. * @return the new object or null. Always returns null if
  1451. * GraphicsEnvironment.isHeadless() returns true.
  1452. * @see java.awt.GraphicsEnvironment#isHeadless
  1453. */
  1454. public DragGestureRecognizer createDragGestureRecognizer(Class abstractRecognizerClass, DragSource ds, Component c, int srcActions, DragGestureListener dgl) {
  1455. return null;
  1456. }
  1457. /**
  1458. * Obtains a value for the specified desktop property.
  1459. *
  1460. * A desktop property is a uniquely named value for a resource that
  1461. * is Toolkit global in nature. Usually it also is an abstract
  1462. * representation for an underlying platform dependent desktop setting.
  1463. */
  1464. public final synchronized Object getDesktopProperty(String propertyName) {
  1465. // This is a workaround for headless toolkits. It would be
  1466. // better to override this method but it is declared final.
  1467. // "this instanceof" syntax defeats polymorphism.
  1468. // --mm, 03/03/00
  1469. if (this instanceof HeadlessToolkit) {
  1470. return ((HeadlessToolkit)this).getUnderlyingToolkit()
  1471. .getDesktopProperty(propertyName);
  1472. }
  1473. if (desktopProperties.isEmpty()) {
  1474. initializeDesktopProperties();
  1475. }
  1476. Object value;
  1477. // This property should never be cached
  1478. if (propertyName.equals("awt.dynamicLayoutSupported")) {
  1479. value = lazilyLoadDesktopProperty(propertyName);
  1480. return value;
  1481. }
  1482. value = desktopProperties.get(propertyName);
  1483. if (value == null) {
  1484. value = lazilyLoadDesktopProperty(propertyName);
  1485. if (value != null) {
  1486. setDesktopProperty(propertyName, value);
  1487. }
  1488. }
  1489. return value;
  1490. }
  1491. /**
  1492. * Sets the named desktop property to the specified value and fires a
  1493. * property change event to notify any listeners that the value has changed.
  1494. */
  1495. protected final void setDesktopProperty(String name, Object newValue) {
  1496. // This is a workaround for headless toolkits. It would be
  1497. // better to override this method but it is declared final.
  1498. // "this instanceof" syntax defeats polymorphism.
  1499. // --mm, 03/03/00
  1500. if (this instanceof HeadlessToolkit) {
  1501. ((HeadlessToolkit)this).getUnderlyingToolkit()
  1502. .setDesktopProperty(name, newValue);
  1503. return;
  1504. }
  1505. Object oldValue;
  1506. synchronized (this) {
  1507. oldValue = desktopProperties.get(name);
  1508. desktopProperties.put(name, newValue);
  1509. }
  1510. desktopPropsSupport.firePropertyChange(name, oldValue, newValue);
  1511. }
  1512. /**
  1513. * an opportunity to lazily evaluate desktop property values.
  1514. */
  1515. protected Object lazilyLoadDesktopProperty(String name) {
  1516. return null;
  1517. }
  1518. /**
  1519. * initializeDesktopProperties
  1520. */
  1521. protected void initializeDesktopProperties() {
  1522. }
  1523. /**
  1524. * Adds the specified property change listener for the named desktop
  1525. * property.
  1526. * If pcl is null, no exception is thrown and no action is performed.
  1527. *
  1528. * @param name The name of the property to listen for
  1529. * @param pcl The property change listener
  1530. * @since 1.2
  1531. */
  1532. public synchronized void addPropertyChangeListener(String name, PropertyChangeListener pcl) {
  1533. if (pcl == null) {
  1534. return;
  1535. }
  1536. desktopPropsSupport.addPropertyChangeListener(name, pcl);
  1537. }
  1538. /**
  1539. * Removes the specified property change listener for the named
  1540. * desktop property.
  1541. * If pcl is null, no exception is thrown and no action is performed.
  1542. *
  1543. * @param name The name of the property to remove
  1544. * @param pcl The property change listener
  1545. * @since 1.2
  1546. */
  1547. public synchronized void removePropertyChangeListener(String name, PropertyChangeListener pcl) {
  1548. if (pcl == null) {
  1549. return;
  1550. }
  1551. desktopPropsSupport.removePropertyChangeListener(name, pcl);
  1552. }
  1553. /**
  1554. * Returns an array of all the property change listeners
  1555. * registered on this toolkit.
  1556. *
  1557. * @return all of this toolkit's <code>PropertyChangeListener</code>s
  1558. * or an empty array if no property change
  1559. * listeners are currently registered
  1560. *
  1561. * @since 1.4
  1562. */
  1563. public PropertyChangeListener[] getPropertyChangeListeners() {
  1564. return desktopPropsSupport.getPropertyChangeListeners();
  1565. }
  1566. /**
  1567. * Returns an array of all the <code>PropertyChangeListener</code>s
  1568. * associated with the named property.
  1569. *
  1570. * @param propertyName the named property
  1571. * @return all of the <code>PropertyChangeListener</code>s associated with
  1572. * the named property or an empty array if no such listeners have
  1573. * been added
  1574. * @since 1.4
  1575. */
  1576. public synchronized PropertyChangeListener[] getPropertyChangeListeners(String propertyName) {
  1577. return desktopPropsSupport.getPropertyChangeListeners(propertyName);
  1578. }
  1579. protected final Map desktopProperties = new HashMap();
  1580. protected final PropertyChangeSupport desktopPropsSupport = new PropertyChangeSupport(this);
  1581. private static final DebugHelper dbg = DebugHelper.create(Toolkit.class);
  1582. private static final int LONG_BITS = 64;
  1583. private int[] calls = new int[LONG_BITS];
  1584. private AWTEventListener eventListener = null;
  1585. private WeakHashMap listener2SelectiveListener = new WeakHashMap();
  1586. /*
  1587. * Extracts a "pure" AWTEventListener from a AWTEventListenerProxy,
  1588. * if the listener is proxied.
  1589. */
  1590. static private AWTEventListener deProxyAWTEventListener(AWTEventListener l)
  1591. {
  1592. AWTEventListener localL = l;
  1593. if (localL == null) {
  1594. return null;
  1595. }
  1596. // if user passed in a AWTEventListenerProxy object, extract
  1597. // the listener
  1598. if (l instanceof AWTEventListenerProxy) {
  1599. localL = (AWTEventListener)((AWTEventListenerProxy)l).getListener();
  1600. }
  1601. return localL;
  1602. }
  1603. /**
  1604. * Adds an AWTEventListener to receive all AWTEvents dispatched
  1605. * system-wide that conform to the given <code>eventMask</code>.
  1606. * <p>
  1607. * First, if there is a security manager, its <code>checkPermission</code>
  1608. * method is called with an
  1609. * <code>AWTPermission("listenToAllAWTEvents")</code> permission.
  1610. * This may result in a SecurityException.
  1611. * <p>
  1612. * <code>eventMask</code> is a bitmask of event types to receive.
  1613. * It is constructed by bitwise OR-ing together the event masks
  1614. * defined in <code>AWTEvent</code>.
  1615. * <p>
  1616. * Note: event listener use is not recommended for normal
  1617. * application use, but are intended solely to support special
  1618. * purpose facilities including support for accessibility,
  1619. * event record/playback, and diagnostic tracing.
  1620. *
  1621. * If listener is null, no exception is thrown and no action is performed.
  1622. *
  1623. * @param listener the event listener.
  1624. * @param eventMask the bitmask of event types to receive
  1625. * @throws SecurityException
  1626. * if a security manager exists and its
  1627. * <code>checkPermission</code> method doesn't allow the operation.
  1628. * @see #removeAWTEventListener
  1629. * @see #getAWTEventListeners
  1630. * @see SecurityManager#checkPermission
  1631. * @see java.awt.AWTEvent
  1632. * @see java.awt.AWTPermission
  1633. * @see java.awt.event.AWTEventListener
  1634. * @see java.awt.event.AWTEventListenerProxy
  1635. * @since 1.2
  1636. */
  1637. public void addAWTEventListener(AWTEventListener listener, long eventMask) {
  1638. AWTEventListener localL = deProxyAWTEventListener(listener);
  1639. if (localL == null) {
  1640. return;
  1641. }
  1642. SecurityManager security = System.getSecurityManager();
  1643. if (security != null) {
  1644. security.checkPermission(SecurityConstants.ALL_AWT_EVENTS_PERMISSION);
  1645. }
  1646. synchronized (this) {
  1647. SelectiveAWTEventListener selectiveListener =
  1648. (SelectiveAWTEventListener)listener2SelectiveListener.get(localL);
  1649. if (selectiveListener == null) {
  1650. // Create a new selectiveListener.
  1651. selectiveListener = new SelectiveAWTEventListener(localL,
  1652. eventMask);
  1653. listener2SelectiveListener.put(localL, selectiveListener);
  1654. eventListener = ToolkitEventMulticaster.add(eventListener,
  1655. selectiveListener);
  1656. }
  1657. // OR the eventMask into the selectiveListener's event mask.
  1658. selectiveListener.orEventMasks(eventMask);
  1659. long mask = eventMask;
  1660. for (int i=0; i<LONG_BITS; i++) {
  1661. // If no bits are set, break out of loop.
  1662. if (mask == 0) {
  1663. break;
  1664. }
  1665. if ((mask & 1L) != 0) { // Always test bit 0.
  1666. calls[i]++;
  1667. }
  1668. mask >>>= 1; // Right shift, fill with zeros on left.
  1669. }
  1670. }
  1671. }
  1672. /**
  1673. * Removes an AWTEventListener from receiving dispatched AWTEvents.
  1674. * <p>
  1675. * First, if there is a security manager, its <code>checkPermission</code>
  1676. * method is called with an
  1677. * <code>AWTPermission("listenToAllAWTEvents")</code> permission.
  1678. * This may result in a SecurityException.
  1679. * <p>
  1680. * Note: event listener use is not recommended for normal
  1681. * application use, but are intended solely to support special
  1682. * purpose facilities including support for accessibility,
  1683. * event record/playback, and diagnostic tracing.
  1684. *
  1685. * If listener is null, no exception is thrown and no action is performed.
  1686. *
  1687. * @param listener the event listener.
  1688. * @throws SecurityException
  1689. * if a security manager exists and its
  1690. * <code>checkPermission</code> method doesn't allow the operation.
  1691. * @see #addAWTEventListener
  1692. * @see #getAWTEventListeners
  1693. * @see SecurityManager#checkPermission
  1694. * @see java.awt.AWTEvent
  1695. * @see java.awt.AWTPermission
  1696. * @see java.awt.event.AWTEventListener
  1697. * @see java.awt.event.AWTEventListenerProxy
  1698. * @since 1.2
  1699. */
  1700. public void removeAWTEventListener(AWTEventListener listener) {
  1701. AWTEventListener localL = deProxyAWTEventListener(listener);
  1702. if (listener == null) {
  1703. return;
  1704. }
  1705. SecurityManager security = System.getSecurityManager();
  1706. if (security != null) {
  1707. security.checkPermission(SecurityConstants.ALL_AWT_EVENTS_PERMISSION);
  1708. }
  1709. synchronized (this) {
  1710. SelectiveAWTEventListener selectiveListener =
  1711. (SelectiveAWTEventListener)listener2SelectiveListener.get(localL);
  1712. if (selectiveListener != null) {
  1713. listener2SelectiveListener.remove(localL);
  1714. int[] listenerCalls = selectiveListener.getCalls();
  1715. for (int i=0; i<LONG_BITS; i++) {
  1716. calls[i] -= listenerCalls[i];
  1717. if (dbg.on) {
  1718. dbg.assertion(calls[i] >= 0);
  1719. }
  1720. }
  1721. }
  1722. eventListener = ToolkitEventMulticaster.remove(eventListener,
  1723. (selectiveListener == null) ? localL : selectiveListener);
  1724. }
  1725. }
  1726. static boolean enabledOnToolkit(long eventMask) {
  1727. // Wonderfully disgusting hack for Solaris 9
  1728. //
  1729. // Solaris 9 makes unreasonable assumptions about the point at which
  1730. // the AWT is initialized. The fix for 4460376 broke one such
  1731. // assumption, preventing the Solaris 9 install from working in a
  1732. // headless environment. We (AWT) suggested that Solaris 9 use Headless
  1733. // AWT to avoid these sorts of problems, but they cannot because they
  1734. // use AWT peered Components (e.g., Checkbox) as models. (!)
  1735. Toolkit defaultToolkit;
  1736. synchronized (Toolkit.class) {
  1737. defaultToolkit = toolkit;
  1738. }
  1739. return (defaultToolkit != null)
  1740. ? (defaultToolkit.countAWTEventListeners(eventMask) > 0)
  1741. : false;
  1742. }
  1743. synchronized int countAWTEventListeners(long eventMask) {
  1744. if (dbg.on) {
  1745. dbg.assertion(eventMask != 0);
  1746. }
  1747. int ci = 0;
  1748. for (; eventMask != 0; eventMask >>>= 1, ci++) {
  1749. }
  1750. ci--;
  1751. return calls[ci];
  1752. }
  1753. /**
  1754. * Returns an array of all the <code>AWTEventListener</code>s
  1755. * registered on this toolkit. Listeners can be returned
  1756. * within <code>AWTEventListenerProxy</code> objects, which also contain
  1757. * the event mask for the given listener.
  1758. * Note that listener objects
  1759. * added multiple times appear only once in the returned array.
  1760. *
  1761. * @return all of the <code>AWTEventListener</code>s or an empty
  1762. * array if no listeners are currently registered
  1763. * @throws SecurityException
  1764. * if a security manager exists and its
  1765. * <code>checkPermission</code> method doesn't allow the operation.
  1766. * @see #addAWTEventListener
  1767. * @see #removeAWTEventListener
  1768. * @see SecurityManager#checkPermission
  1769. * @see java.awt.AWTEvent
  1770. * @see java.awt.AWTPermission
  1771. * @see java.awt.event.AWTEventListener
  1772. * @see java.awt.event.AWTEventListenerProxy
  1773. * @since 1.4
  1774. */
  1775. public AWTEventListener[] getAWTEventListeners() {
  1776. SecurityManager security = System.getSecurityManager();
  1777. if (security != null) {
  1778. security.checkPermission(SecurityConstants.ALL_AWT_EVENTS_PERMISSION);
  1779. }
  1780. synchronized (this) {
  1781. EventListener[] la = ToolkitEventMulticaster.getListeners(eventListener,AWTEventListener.class);
  1782. AWTEventListener[] ret = new AWTEventListener[la.length];
  1783. for (int i = 0; i < la.length; i++) {
  1784. SelectiveAWTEventListener sael = (SelectiveAWTEventListener)la[i];
  1785. AWTEventListener tempL = sael.getListener();
  1786. //assert tempL is not an AWTEventListenerProxy - we should
  1787. // have weeded them all out
  1788. // don't want to wrap a proxy inside a proxy
  1789. ret[i] = new AWTEventListenerProxy(sael.getEventMask(), tempL);
  1790. }
  1791. return ret;
  1792. }
  1793. }
  1794. /**
  1795. * Returns an array of all the <code>AWTEventListener</code>s
  1796. * registered on this toolkit which listen to all of the event
  1797. * types indicates in the <code>eventMask</code> argument.
  1798. * Listeners can be returned
  1799. * within <code>AWTEventListenerProxy</code> objects, which also contain
  1800. * the event mask for the given listener.
  1801. * Note that listener objects
  1802. * added multiple times appear only once in the returned array.
  1803. *
  1804. * @param eventMask the bitmask of event types to listen for
  1805. * @return all of the <code>AWTEventListener</code>s registered
  1806. * on this toolkit for the specified
  1807. * event types, or an empty array if no such listeners
  1808. * are currently registered
  1809. * @throws SecurityException
  1810. * if a security manager exists and its
  1811. * <code>checkPermission</code> method doesn't allow the operation.
  1812. * @see #addAWTEventListener
  1813. * @see #removeAWTEventListener
  1814. * @see SecurityManager#checkPermission
  1815. * @see java.awt.AWTEvent
  1816. * @see java.awt.AWTPermission
  1817. * @see java.awt.event.AWTEventListener
  1818. * @see java.awt.event.AWTEventListenerProxy
  1819. * @since 1.4
  1820. */
  1821. public AWTEventListener[] getAWTEventListeners(long eventMask) {
  1822. SecurityManager security = System.getSecurityManager();
  1823. if (security != null) {
  1824. security.checkPermission(SecurityConstants.ALL_AWT_EVENTS_PERMISSION);
  1825. }
  1826. synchronized (this) {
  1827. EventListener[] la = ToolkitEventMulticaster.getListeners(eventListener,AWTEventListener.class);
  1828. java.util.List list = new ArrayList(la.length);
  1829. for (int i = 0; i < la.length; i++) {
  1830. SelectiveAWTEventListener sael = (SelectiveAWTEventListener)la[i];
  1831. if ((sael.getEventMask() & eventMask) == eventMask) {
  1832. //AWTEventListener tempL = sael.getListener();
  1833. list.add(new AWTEventListenerProxy(sael.getEventMask(),
  1834. sael.getListener()));
  1835. }
  1836. }
  1837. return (AWTEventListener[])list.toArray(new AWTEventListener[0]);
  1838. }
  1839. }
  1840. /*
  1841. * This method notifies any AWTEventListeners that an event
  1842. * is about to be dispatched.
  1843. *
  1844. * @param theEvent the event which will be dispatched.
  1845. */
  1846. void notifyAWTEventListeners(AWTEvent theEvent) {
  1847. // This is a workaround for headless toolkits. It would be
  1848. // better to override this method but it is declared package private.
  1849. // "this instanceof" syntax defeats polymorphism.
  1850. // --mm, 03/03/00
  1851. if (this instanceof HeadlessToolkit) {
  1852. ((HeadlessToolkit)this).getUnderlyingToolkit()
  1853. .notifyAWTEventListeners(theEvent);
  1854. return;
  1855. }
  1856. AWTEventListener eventListener = this.eventListener;
  1857. if (eventListener != null) {
  1858. eventListener.eventDispatched(theEvent);
  1859. }
  1860. }
  1861. static private class ToolkitEventMulticaster extends AWTEventMulticaster
  1862. implements AWTEventListener {
  1863. // Implementation cloned from AWTEventMulticaster.
  1864. ToolkitEventMulticaster(AWTEventListener a, AWTEventListener b) {
  1865. super(a, b);
  1866. }
  1867. static AWTEventListener add(AWTEventListener a,
  1868. AWTEventListener b) {
  1869. if (a == null) return b;
  1870. if (b == null) return a;
  1871. return new ToolkitEventMulticaster(a, b);
  1872. }
  1873. static AWTEventListener remove(AWTEventListener l,
  1874. AWTEventListener oldl) {
  1875. return (AWTEventListener) removeInternal(l, oldl);
  1876. }
  1877. // #4178589: must overload remove(EventListener) to call our add()
  1878. // instead of the static addInternal() so we allocate a
  1879. // ToolkitEventMulticaster instead of an AWTEventMulticaster.
  1880. // Note: this method is called by AWTEventListener.removeInternal(),
  1881. // so its method signature must match AWTEventListener.remove().
  1882. protected EventListener remove(EventListener oldl) {
  1883. if (oldl == a) return b;
  1884. if (oldl == b) return a;
  1885. AWTEventListener a2 = (AWTEventListener)removeInternal(a, oldl);
  1886. AWTEventListener b2 = (AWTEventListener)removeInternal(b, oldl);
  1887. if (a2 == a && b2 == b) {
  1888. return this; // it's not here
  1889. }
  1890. return add(a2, b2);
  1891. }
  1892. public void eventDispatched(AWTEvent event) {
  1893. ((AWTEventListener)a).eventDispatched(event);
  1894. ((AWTEventListener)b).eventDispatched(event);
  1895. }
  1896. }
  1897. private class SelectiveAWTEventListener implements AWTEventListener {
  1898. AWTEventListener listener;
  1899. private long eventMask;
  1900. // This array contains the number of times to call the eventlistener
  1901. // for each event type.
  1902. int[] calls = new int[Toolkit.LONG_BITS];
  1903. public AWTEventListener getListener() {return listener;}
  1904. public long getEventMask() {return eventMask;}
  1905. public int[] getCalls() {return calls;}
  1906. public void orEventMasks(long mask) {
  1907. eventMask |= mask;
  1908. // For each event bit set in mask, increment its call count.
  1909. for (int i=0; i<Toolkit.LONG_BITS; i++) {
  1910. // If no bits are set, break out of loop.
  1911. if (mask == 0) {
  1912. break;
  1913. }
  1914. if ((mask & 1L) != 0) { // Always test bit 0.
  1915. calls[i]++;
  1916. }
  1917. mask >>>= 1; // Right shift, fill with zeros on left.
  1918. }
  1919. }
  1920. SelectiveAWTEventListener(AWTEventListener l, long mask) {
  1921. listener = l;
  1922. eventMask = mask;
  1923. }
  1924. public void eventDispatched(AWTEvent event) {
  1925. long eventBit = 0; // Used to save the bit of the event type.
  1926. if (((eventBit = eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0 &&
  1927. event.id >= ComponentEvent.COMPONENT_FIRST &&
  1928. event.id <= ComponentEvent.COMPONENT_LAST)
  1929. || ((eventBit = eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0 &&
  1930. event.id >= ContainerEvent.CONTAINER_FIRST &&
  1931. event.id <= ContainerEvent.CONTAINER_LAST)
  1932. || ((eventBit = eventMask & AWTEvent.FOCUS_EVENT_MASK) != 0 &&
  1933. event.id >= FocusEvent.FOCUS_FIRST &&
  1934. event.id <= FocusEvent.FOCUS_LAST)
  1935. || ((eventBit = eventMask & AWTEvent.KEY_EVENT_MASK) != 0 &&
  1936. event.id >= KeyEvent.KEY_FIRST &&
  1937. event.id <= KeyEvent.KEY_LAST)
  1938. || ((eventBit = eventMask & AWTEvent.MOUSE_WHEEL_EVENT_MASK) != 0 &&
  1939. event.id == MouseEvent.MOUSE_WHEEL)
  1940. || ((eventBit = eventMask & AWTEvent.MOUSE_MOTION_EVENT_MASK) != 0 &&
  1941. (event.id == MouseEvent.MOUSE_MOVED ||
  1942. event.id == MouseEvent.MOUSE_DRAGGED))
  1943. || ((eventBit = eventMask & AWTEvent.MOUSE_EVENT_MASK) != 0 &&
  1944. event.id != MouseEvent.MOUSE_MOVED &&
  1945. event.id != MouseEvent.MOUSE_DRAGGED &&
  1946. event.id != MouseEvent.MOUSE_WHEEL &&
  1947. event.id >= MouseEvent.MOUSE_FIRST &&
  1948. event.id <= MouseEvent.MOUSE_LAST)
  1949. || ((eventBit = eventMask & AWTEvent.WINDOW_EVENT_MASK) != 0 &&
  1950. event.id >= WindowEvent.WINDOW_FIRST &&
  1951. event.id <= WindowEvent.WINDOW_LAST)
  1952. || ((eventBit = eventMask & AWTEvent.ACTION_EVENT_MASK) != 0 &&
  1953. event.id >= ActionEvent.ACTION_FIRST &&
  1954. event.id <= ActionEvent.ACTION_LAST)
  1955. || ((eventBit = eventMask & AWTEvent.ADJUSTMENT_EVENT_MASK) != 0 &&
  1956. event.id >= AdjustmentEvent.ADJUSTMENT_FIRST &&
  1957. event.id <= AdjustmentEvent.ADJUSTMENT_LAST)
  1958. || ((eventBit = eventMask & AWTEvent.ITEM_EVENT_MASK) != 0 &&
  1959. event.id >= ItemEvent.ITEM_FIRST &&
  1960. event.id <= ItemEvent.ITEM_LAST)
  1961. || ((eventBit = eventMask & AWTEvent.TEXT_EVENT_MASK) != 0 &&
  1962. event.id >= TextEvent.TEXT_FIRST &&
  1963. event.id <= TextEvent.TEXT_LAST)
  1964. || ((eventBit = eventMask & AWTEvent.INPUT_METHOD_EVENT_MASK) != 0 &&
  1965. event.id >= InputMethodEvent.INPUT_METHOD_FIRST &&
  1966. event.id <= InputMethodEvent.INPUT_METHOD_LAST)
  1967. || ((eventBit = eventMask & AWTEvent.PAINT_EVENT_MASK) != 0 &&
  1968. event.id >= PaintEvent.PAINT_FIRST &&
  1969. event.id <= PaintEvent.PAINT_LAST)
  1970. || ((eventBit = eventMask & AWTEvent.INVOCATION_EVENT_MASK) != 0 &&
  1971. event.id >= InvocationEvent.INVOCATION_FIRST &&
  1972. event.id <= InvocationEvent.INVOCATION_LAST)
  1973. || ((eventBit = eventMask & AWTEvent.HIERARCHY_EVENT_MASK) != 0 &&
  1974. event.id == HierarchyEvent.HIERARCHY_CHANGED)
  1975. || ((eventBit = eventMask & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) != 0 &&
  1976. (event.id == HierarchyEvent.ANCESTOR_MOVED ||
  1977. event.id == HierarchyEvent.ANCESTOR_RESIZED))
  1978. || ((eventBit = eventMask & AWTEvent.WINDOW_STATE_EVENT_MASK) != 0 &&
  1979. event.id == WindowEvent.WINDOW_STATE_CHANGED)
  1980. || ((eventBit = eventMask & AWTEvent.WINDOW_FOCUS_EVENT_MASK) != 0 &&
  1981. (event.id == WindowEvent.WINDOW_GAINED_FOCUS ||
  1982. event.id == WindowEvent.WINDOW_LOST_FOCUS))) {
  1983. // Get the index of the call count for this event type.
  1984. // Instead of using Math.log(...) we will calculate it with
  1985. // bit shifts. That's what previous implementation looked like:
  1986. //
  1987. // int ci = (int) (Math.log(eventBit)/Math.log(2));
  1988. int ci = 0;
  1989. for (long eMask = eventBit; eMask != 0; eMask >>>= 1, ci++) {
  1990. }
  1991. ci--;
  1992. // Call the listener as many times as it was added for this
  1993. // event type.
  1994. for (int i=0; i<calls[ci]; i++) {
  1995. listener.eventDispatched(event);
  1996. }
  1997. }
  1998. }
  1999. }
  2000. /**
  2001. * Returns a map of visual attributes for the abstract level description
  2002. * of the given input method highlight, or null if no mapping is found.
  2003. * The style field of the input method highlight is ignored. The map
  2004. * returned is unmodifiable.
  2005. * @param highlight input method highlight
  2006. * @return style attribute map, or <code>null</code>
  2007. * @exception HeadlessException if
  2008. * <code>GraphicsEnvironment.isHeadless</code> returns true
  2009. * @see java.awt.GraphicsEnvironment#isHeadless
  2010. * @since 1.3
  2011. */
  2012. public abstract Map mapInputMethodHighlight(
  2013. InputMethodHighlight highlight) throws HeadlessException;
  2014. }