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