1. /*
  2. * @(#)Toolkit.java 1.189 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt;
  8. import java.util.MissingResourceException;
  9. import java.util.Properties;
  10. import java.util.ResourceBundle;
  11. import java.util.StringTokenizer;
  12. import java.awt.event.*;
  13. import java.awt.peer.*;
  14. import java.awt.*;
  15. import java.awt.im.InputMethodHighlight;
  16. import java.awt.image.ImageObserver;
  17. import java.awt.image.ImageProducer;
  18. import java.awt.image.ColorModel;
  19. import java.awt.datatransfer.Clipboard;
  20. import java.awt.dnd.DnDConstants;
  21. import java.awt.dnd.DragSource;
  22. import java.awt.dnd.DragGestureRecognizer;
  23. import java.awt.dnd.DragGestureEvent;
  24. import java.awt.dnd.DragGestureListener;
  25. import java.awt.dnd.InvalidDnDOperationException;
  26. import java.awt.dnd.peer.DragSourceContextPeer;
  27. import java.net.URL;
  28. import java.io.BufferedInputStream;
  29. import java.io.File;
  30. import java.io.FileInputStream;
  31. import java.security.AccessController;
  32. import java.security.PrivilegedAction;
  33. import java.util.EventListener;
  34. import java.util.Map;
  35. import java.util.HashMap;
  36. import java.util.WeakHashMap;
  37. import java.util.List;
  38. import java.util.ArrayList;
  39. import java.beans.PropertyChangeListener;
  40. import java.beans.PropertyChangeSupport;
  41. import sun.awt.DebugHelper;
  42. import sun.awt.HeadlessToolkit;
  43. import sun.awt.NullComponentPeer;
  44. import sun.security.util.SecurityConstants;
  45. /**
  46. * This class is the abstract superclass of all actual
  47. * implementations of the Abstract Window Toolkit. Subclasses of
  48. * <code>Toolkit</code> are used to bind the various components
  49. * to particular native toolkit implementations.
  50. * <p>
  51. * Most applications should not call any of the methods in this
  52. * class directly. The methods defined by <code>Toolkit</code> are
  53. * the "glue" that joins the platform-independent classes in the
  54. * <code>java.awt</code> package with their counterparts in
  55. * <code>java.awt.peer</code>. Some methods defined by
  56. * <code>Toolkit</code> query the native operating system directly.
  57. *
  58. * @version 1.189, 01/23/03
  59. * @author Sami Shaio
  60. * @author Arthur van Hoff
  61. * @author Fred Ecks
  62. * @since JDK1.0
  63. */
  64. public abstract class Toolkit {
  65. /**
  66. * Creates this toolkit's implementation of <code>Button</code> using
  67. * the specified peer interface.
  68. * @param target the button to be implemented.
  69. * @return this toolkit's implementation of <code>Button</code>.
  70. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  71. * returns true
  72. * @see java.awt.GraphicsEnvironment#isHeadless
  73. * @see java.awt.Button
  74. * @see java.awt.peer.ButtonPeer
  75. */
  76. protected abstract ButtonPeer createButton(Button target)
  77. throws HeadlessException;
  78. /**
  79. * Creates this toolkit's implementation of <code>TextField</code> using
  80. * the specified peer interface.
  81. * @param target the text field to be implemented.
  82. * @return this toolkit's implementation of <code>TextField</code>.
  83. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  84. * returns true
  85. * @see java.awt.GraphicsEnvironment#isHeadless
  86. * @see java.awt.TextField
  87. * @see java.awt.peer.TextFieldPeer
  88. */
  89. protected abstract TextFieldPeer createTextField(TextField target)
  90. throws HeadlessException;
  91. /**
  92. * Creates this toolkit's implementation of <code>Label</code> using
  93. * the specified peer interface.
  94. * @param target the label to be implemented.
  95. * @return this toolkit's implementation of <code>Label</code>.
  96. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  97. * returns true
  98. * @see java.awt.GraphicsEnvironment#isHeadless
  99. * @see java.awt.Label
  100. * @see java.awt.peer.LabelPeer
  101. */
  102. protected abstract LabelPeer createLabel(Label target)
  103. throws HeadlessException;
  104. /**
  105. * Creates this toolkit's implementation of <code>List</code> using
  106. * the specified peer interface.
  107. * @param target the list to be implemented.
  108. * @return this toolkit's implementation of <code>List</code>.
  109. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  110. * returns true
  111. * @see java.awt.GraphicsEnvironment#isHeadless
  112. * @see java.awt.List
  113. * @see java.awt.peer.ListPeer
  114. */
  115. protected abstract ListPeer createList(java.awt.List target)
  116. throws HeadlessException;
  117. /**
  118. * Creates this toolkit's implementation of <code>Checkbox</code> using
  119. * the specified peer interface.
  120. * @param target the check box to be implemented.
  121. * @return this toolkit's implementation of <code>Checkbox</code>.
  122. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  123. * returns true
  124. * @see java.awt.GraphicsEnvironment#isHeadless
  125. * @see java.awt.Checkbox
  126. * @see java.awt.peer.CheckboxPeer
  127. */
  128. protected abstract CheckboxPeer createCheckbox(Checkbox target)
  129. throws HeadlessException;
  130. /**
  131. * Creates this toolkit's implementation of <code>Scrollbar</code> using
  132. * the specified peer interface.
  133. * @param target the scroll bar to be implemented.
  134. * @return this toolkit's implementation of <code>Scrollbar</code>.
  135. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  136. * returns true
  137. * @see java.awt.GraphicsEnvironment#isHeadless
  138. * @see java.awt.Scrollbar
  139. * @see java.awt.peer.ScrollbarPeer
  140. */
  141. protected abstract ScrollbarPeer createScrollbar(Scrollbar target)
  142. throws HeadlessException;
  143. /**
  144. * Creates this toolkit's implementation of <code>ScrollPane</code> using
  145. * the specified peer interface.
  146. * @param target the scroll pane to be implemented.
  147. * @return this toolkit's implementation of <code>ScrollPane</code>.
  148. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  149. * returns true
  150. * @see java.awt.GraphicsEnvironment#isHeadless
  151. * @see java.awt.ScrollPane
  152. * @see java.awt.peer.ScrollPanePeer
  153. * @since JDK1.1
  154. */
  155. protected abstract ScrollPanePeer createScrollPane(ScrollPane target)
  156. throws HeadlessException;
  157. /**
  158. * Creates this toolkit's implementation of <code>TextArea</code> using
  159. * the specified peer interface.
  160. * @param target the text area to be implemented.
  161. * @return this toolkit's implementation of <code>TextArea</code>.
  162. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  163. * returns true
  164. * @see java.awt.GraphicsEnvironment#isHeadless
  165. * @see java.awt.TextArea
  166. * @see java.awt.peer.TextAreaPeer
  167. */
  168. protected abstract TextAreaPeer createTextArea(TextArea target)
  169. throws HeadlessException;
  170. /**
  171. * Creates this toolkit's implementation of <code>Choice</code> using
  172. * the specified peer interface.
  173. * @param target the choice to be implemented.
  174. * @return this toolkit's implementation of <code>Choice</code>.
  175. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  176. * returns true
  177. * @see java.awt.GraphicsEnvironment#isHeadless
  178. * @see java.awt.Choice
  179. * @see java.awt.peer.ChoicePeer
  180. */
  181. protected abstract ChoicePeer createChoice(Choice target)
  182. throws HeadlessException;
  183. /**
  184. * Creates this toolkit's implementation of <code>Frame</code> using
  185. * the specified peer interface.
  186. * @param target the frame to be implemented.
  187. * @return this toolkit's implementation of <code>Frame</code>.
  188. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  189. * returns true
  190. * @see java.awt.GraphicsEnvironment#isHeadless
  191. * @see java.awt.Frame
  192. * @see java.awt.peer.FramePeer
  193. */
  194. protected abstract FramePeer createFrame(Frame target)
  195. throws HeadlessException;
  196. /**
  197. * Creates this toolkit's implementation of <code>Canvas</code> using
  198. * the specified peer interface.
  199. * @param target the canvas to be implemented.
  200. * @return this toolkit's implementation of <code>Canvas</code>.
  201. * @see java.awt.Canvas
  202. * @see java.awt.peer.CanvasPeer
  203. */
  204. protected abstract CanvasPeer createCanvas(Canvas target);
  205. /**
  206. * Creates this toolkit's implementation of <code>Panel</code> using
  207. * the specified peer interface.
  208. * @param target the panel to be implemented.
  209. * @return this toolkit's implementation of <code>Panel</code>.
  210. * @see java.awt.Panel
  211. * @see java.awt.peer.PanelPeer
  212. */
  213. protected abstract PanelPeer createPanel(Panel target);
  214. /**
  215. * Creates this toolkit's implementation of <code>Window</code> using
  216. * the specified peer interface.
  217. * @param target the window to be implemented.
  218. * @return this toolkit's implementation of <code>Window</code>.
  219. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  220. * returns true
  221. * @see java.awt.GraphicsEnvironment#isHeadless
  222. * @see java.awt.Window
  223. * @see java.awt.peer.WindowPeer
  224. */
  225. protected abstract WindowPeer createWindow(Window target)
  226. throws HeadlessException;
  227. /**
  228. * Creates this toolkit's implementation of <code>Dialog</code> using
  229. * the specified peer interface.
  230. * @param target the dialog to be implemented.
  231. * @return this toolkit's implementation of <code>Dialog</code>.
  232. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  233. * returns true
  234. * @see java.awt.GraphicsEnvironment#isHeadless
  235. * @see java.awt.Dialog
  236. * @see java.awt.peer.DialogPeer
  237. */
  238. protected abstract DialogPeer createDialog(Dialog target)
  239. throws HeadlessException;
  240. /**
  241. * Creates this toolkit's implementation of <code>MenuBar</code> using
  242. * the specified peer interface.
  243. * @param target the menu bar to be implemented.
  244. * @return this toolkit's implementation of <code>MenuBar</code>.
  245. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  246. * returns true
  247. * @see java.awt.GraphicsEnvironment#isHeadless
  248. * @see java.awt.MenuBar
  249. * @see java.awt.peer.MenuBarPeer
  250. */
  251. protected abstract MenuBarPeer createMenuBar(MenuBar target)
  252. throws HeadlessException;
  253. /**
  254. * Creates this toolkit's implementation of <code>Menu</code> using
  255. * the specified peer interface.
  256. * @param target the menu to be implemented.
  257. * @return this toolkit's implementation of <code>Menu</code>.
  258. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  259. * returns true
  260. * @see java.awt.GraphicsEnvironment#isHeadless
  261. * @see java.awt.Menu
  262. * @see java.awt.peer.MenuPeer
  263. */
  264. protected abstract MenuPeer createMenu(Menu target)
  265. throws HeadlessException;
  266. /**
  267. * Creates this toolkit's implementation of <code>PopupMenu</code> using
  268. * the specified peer interface.
  269. * @param target the popup menu to be implemented.
  270. * @return this toolkit's implementation of <code>PopupMenu</code>.
  271. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  272. * returns true
  273. * @see java.awt.GraphicsEnvironment#isHeadless
  274. * @see java.awt.PopupMenu
  275. * @see java.awt.peer.PopupMenuPeer
  276. * @since JDK1.1
  277. */
  278. protected abstract PopupMenuPeer createPopupMenu(PopupMenu target)
  279. throws HeadlessException;
  280. /**
  281. * Creates this toolkit's implementation of <code>MenuItem</code> using
  282. * the specified peer interface.
  283. * @param target the menu item to be implemented.
  284. * @return this toolkit's implementation of <code>MenuItem</code>.
  285. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  286. * returns true
  287. * @see java.awt.GraphicsEnvironment#isHeadless
  288. * @see java.awt.MenuItem
  289. * @see java.awt.peer.MenuItemPeer
  290. */
  291. protected abstract MenuItemPeer createMenuItem(MenuItem target)
  292. throws HeadlessException;
  293. /**
  294. * Creates this toolkit's implementation of <code>FileDialog</code> using
  295. * the specified peer interface.
  296. * @param target the file dialog to be implemented.
  297. * @return this toolkit's implementation of <code>FileDialog</code>.
  298. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  299. * returns true
  300. * @see java.awt.GraphicsEnvironment#isHeadless
  301. * @see java.awt.FileDialog
  302. * @see java.awt.peer.FileDialogPeer
  303. */
  304. protected abstract FileDialogPeer createFileDialog(FileDialog target)
  305. throws HeadlessException;
  306. /**
  307. * Creates this toolkit's implementation of <code>CheckboxMenuItem</code> using
  308. * the specified peer interface.
  309. * @param target the checkbox menu item to be implemented.
  310. * @return this toolkit's implementation of <code>CheckboxMenuItem</code>.
  311. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  312. * returns true
  313. * @see java.awt.GraphicsEnvironment#isHeadless
  314. * @see java.awt.CheckboxMenuItem
  315. * @see java.awt.peer.CheckboxMenuItemPeer
  316. */
  317. protected abstract CheckboxMenuItemPeer createCheckboxMenuItem(
  318. CheckboxMenuItem target) throws HeadlessException;
  319. private static LightweightPeer lightweightMarker;
  320. /**
  321. * Creates a peer for a component or container. This peer is windowless
  322. * and allows the Component and Container classes to be extended directly
  323. * to create windowless components that are defined entirely in java.
  324. *
  325. * @param target The Component to be created.
  326. */
  327. protected LightweightPeer createComponent(Component target) {
  328. if (lightweightMarker == null) {
  329. lightweightMarker = new NullComponentPeer();
  330. }
  331. return lightweightMarker;
  332. }
  333. /**
  334. * Creates this toolkit's implementation of <code>Font</code> using
  335. * the specified peer interface.
  336. * @param name the font to be implemented
  337. * @param style the style of the font, such as <code>PLAIN</code>,
  338. * <code>BOLD</code>, <code>ITALIC</code>, or a combination
  339. * @return this toolkit's implementation of <code>Font</code>
  340. * @see java.awt.Font
  341. * @see java.awt.peer.FontPeer
  342. * @see java.awt.GraphicsEnvironment#getAllFonts
  343. * @deprecated see java.awt.GraphicsEnvironment#getAllFonts
  344. */
  345. protected abstract FontPeer getFontPeer(String name, int style);
  346. // The following method is called by the private method
  347. // <code>updateSystemColors</code> in <code>SystemColor</code>.
  348. /**
  349. * Fills in the integer array that is supplied as an argument
  350. * with the current system color values.
  351. *
  352. * @param systemColors an integer array.
  353. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  354. * returns true
  355. * @see java.awt.GraphicsEnvironment#isHeadless
  356. * @since JDK1.1
  357. */
  358. protected void loadSystemColors(int[] systemColors)
  359. throws HeadlessException {
  360. }
  361. /**
  362. * Controls whether the layout of Containers is validated dynamically
  363. * during resizing, or statically, after resizing is complete.
  364. * Note that this feature is not supported on all platforms, and
  365. * conversely, that this feature cannot be turned off on some platforms.
  366. * On platforms where dynamic layout during resize is not supported
  367. * (or is always supported), setting this property has no effect.
  368. * Note that this feature can be set or unset as a property of the
  369. * operating system or window manager on some platforms. On such
  370. * platforms, the dynamic resize property must be set at the operating
  371. * system or window manager level before this method can take effect.
  372. * This method does not change the underlying operating system or
  373. * window manager support or settings. The OS/WM support can be
  374. * queried using getDesktopProperty("awt.dynamicLayoutSupported").
  375. *
  376. * @param dynamic If true, Containers should re-layout their
  377. * components as the Container is being resized. If false,
  378. * the layout will be validated after resizing is finished.
  379. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  380. * returns true
  381. * @see #isDynamicLayoutSet()
  382. * @see #isDynamicLayoutActive()
  383. * @see #getDesktopProperty(String propertyName)
  384. * @see java.awt.GraphicsEnvironment#isHeadless
  385. * @since 1.4
  386. */
  387. public void setDynamicLayout(boolean dynamic)
  388. throws HeadlessException {
  389. }
  390. /**
  391. * Returns whether the layout of Containers is validated dynamically
  392. * during resizing, or statically, after resizing is complete.
  393. * Note: this method returns the value that was set programmatically;
  394. * it does not reflect support at the level of the operating system
  395. * or window manager for dynamic layout on resizing, or the current
  396. * operating system or window manager settings. The OS/WM support can
  397. * be queried using getDesktopProperty("awt.dynamicLayoutSupported").
  398. *
  399. * @return true if validation of Containers is done dynamically,
  400. * false if validation is done after resizing is finished.
  401. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  402. * returns true
  403. * @see #setDynamicLayout(boolean dynamic)
  404. * @see #isDynamicLayoutActive()
  405. * @see #getDesktopProperty(String propertyName)
  406. * @see java.awt.GraphicsEnvironment#isHeadless
  407. * @since 1.4
  408. */
  409. protected boolean isDynamicLayoutSet()
  410. throws HeadlessException {
  411. if (this != Toolkit.getDefaultToolkit()) {
  412. return Toolkit.getDefaultToolkit().isDynamicLayoutSet();
  413. } else {
  414. return false;
  415. }
  416. }
  417. /**
  418. * Returns whether dynamic layout of Containers on resize is
  419. * currently active (both set programmatically, and supported
  420. * by the underlying operating system and/or window manager).
  421. * The OS/WM support can be queried using
  422. * getDesktopProperty("awt.dynamicLayoutSupported").
  423. *
  424. * @return true if dynamic layout of Containers on resize is
  425. * currently active, false otherwise.
  426. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  427. * returns true
  428. * @see #setDynamicLayout(boolean dynamic)
  429. * @see #isDynamicLayoutSet()
  430. * @see #getDesktopProperty(String propertyName)
  431. * @see java.awt.GraphicsEnvironment#isHeadless
  432. * @since 1.4
  433. */
  434. public boolean isDynamicLayoutActive()
  435. throws HeadlessException {
  436. if (this != Toolkit.getDefaultToolkit()) {
  437. return Toolkit.getDefaultToolkit().isDynamicLayoutActive();
  438. } else {
  439. return false;
  440. }
  441. }
  442. /**
  443. * Gets the size of the screen. On systems with multiple displays, the
  444. * primary display is used. Multi-screen aware display dimensions are
  445. * available from <code>GraphicsConfiguration</code> and
  446. * <code>GraphicsDevice</code>.
  447. * @return the size of this toolkit's screen, in pixels.
  448. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  449. * returns true
  450. * @see java.awt.GraphicsConfiguration#getBounds
  451. * @see java.awt.GraphicsDevice#getDisplayMode
  452. * @see java.awt.GraphicsEnvironment#isHeadless
  453. */
  454. public abstract Dimension getScreenSize()
  455. throws HeadlessException;
  456. /**
  457. * Returns the screen resolution in dots-per-inch.
  458. * @return this toolkit's screen resolution, in dots-per-inch.
  459. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  460. * returns true
  461. * @see java.awt.GraphicsEnvironment#isHeadless
  462. */
  463. public abstract int getScreenResolution()
  464. throws HeadlessException;
  465. /**
  466. * Gets the insets of the screen.
  467. * @param gc a <code>GraphicsConfiguration</code>
  468. * @return the insets of this toolkit's screen, in pixels.
  469. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  470. * returns true
  471. * @see java.awt.GraphicsEnvironment#isHeadless
  472. * @since 1.4
  473. */
  474. public Insets getScreenInsets(GraphicsConfiguration gc)
  475. throws HeadlessException {
  476. if (this != Toolkit.getDefaultToolkit()) {
  477. return Toolkit.getDefaultToolkit().getScreenInsets(gc);
  478. } else {
  479. return new Insets(0, 0, 0, 0);
  480. }
  481. }
  482. /**
  483. * Determines the color model of this toolkit's screen.
  484. * <p>
  485. * <code>ColorModel</code> is an abstract class that
  486. * encapsulates the ability to translate between the
  487. * pixel values of an image and its red, green, blue,
  488. * and alpha components.
  489. * <p>
  490. * This toolkit method is called by the
  491. * <code>getColorModel</code> method
  492. * of the <code>Component</code> class.
  493. * @return the color model of this toolkit's screen.
  494. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  495. * returns true
  496. * @see java.awt.GraphicsEnvironment#isHeadless
  497. * @see java.awt.image.ColorModel
  498. * @see java.awt.Component#getColorModel
  499. */
  500. public abstract ColorModel getColorModel()
  501. throws HeadlessException;
  502. /**
  503. * Returns the names of the available fonts in this toolkit.<p>
  504. * For 1.1, the following font names are deprecated (the replacement
  505. * name follows):
  506. * <ul>
  507. * <li>TimesRoman (use Serif)
  508. * <li>Helvetica (use SansSerif)
  509. * <li>Courier (use Monospaced)
  510. * </ul><p>
  511. * The ZapfDingbats fontname is also deprecated in 1.1 but the characters
  512. * are defined in Unicode starting at 0x2700, and as of 1.1 Java supports
  513. * those characters.
  514. * @return the names of the available fonts in this toolkit.
  515. * @deprecated see {@link java.awt.GraphicsEnvironment#getAvailableFontFamilyNames()}
  516. * @see java.awt.GraphicsEnvironment#getAvailableFontFamilyNames()
  517. */
  518. public abstract String[] getFontList();
  519. /**
  520. * Gets the screen device metrics for rendering of the font.
  521. * @param font a font
  522. * @return the screen metrics of the specified font in this toolkit
  523. * @deprecated As of JDK version 1.2, replaced by the <code>Font</code>
  524. * method <code>getLineMetrics</code>.
  525. * @see java.awt.font.LineMetrics
  526. * @see java.awt.Font#getLineMetrics
  527. * @see java.awt.GraphicsEnvironment#getScreenDevices
  528. */
  529. public abstract FontMetrics getFontMetrics(Font font);
  530. /**
  531. * Synchronizes this toolkit's graphics state. Some window systems
  532. * may do buffering of graphics events.
  533. * <p>
  534. * This method ensures that the display is up-to-date. It is useful
  535. * for animation.
  536. */
  537. public abstract void sync();
  538. /**
  539. * The default toolkit.
  540. */
  541. private static Toolkit toolkit;
  542. /**
  543. * Loads additional classes into the VM, using the property
  544. * 'assistive_technologies' specified in the Sun reference
  545. * implementation by a line in the 'accessibility.properties'
  546. * file. The form is "assistive_technologies=..." where
  547. * the "..." is a comma-separated list of assistive technology
  548. * classes to load. Each class is loaded in the order given
  549. * and a single instance of each is created using
  550. * Class.forName(class).newInstance(). All errors are handled
  551. * via an AWTError exception.
  552. *
  553. * <p>The assumption is made that assistive technology classes are supplied
  554. * as part of INSTALLED (as opposed to: BUNDLED) extensions or specified
  555. * on the class path
  556. * (and therefore can be loaded using the class loader returned by
  557. * a call to <code>ClassLoader.getSystemClassLoader</code>, whose
  558. * delegation parent is the extension class loader for installed
  559. * extensions).
  560. */
  561. private static void loadAssistiveTechnologies() {
  562. // Get accessibility properties
  563. final String sep = File.separator;
  564. final Properties properties = new Properties();
  565. String atNames = (String)java.security.AccessController.doPrivileged(
  566. new java.security.PrivilegedAction() {
  567. public Object run() {
  568. // Try loading the per-user accessibility properties file.
  569. try {
  570. File propsFile = new File(
  571. System.getProperty("user.home") +
  572. sep + ".accessibility.properties");
  573. FileInputStream in =
  574. new FileInputStream(propsFile);
  575. // Inputstream has been buffered in Properties class
  576. properties.load(in);
  577. in.close();
  578. } catch (Exception e) {
  579. // Per-user accessibility properties file does not exist
  580. }
  581. // Try loading the system-wide accessibility properties
  582. // file only if a per-user accessibility properties
  583. // file does not exist or is empty.
  584. if (properties.size() == 0) {
  585. try {
  586. File propsFile = new File(
  587. System.getProperty("java.home") + sep + "lib" +
  588. sep + "accessibility.properties");
  589. FileInputStream in =
  590. new FileInputStream(propsFile);
  591. // Inputstream has been buffered in Properties class
  592. properties.load(in);
  593. in.close();
  594. } catch (Exception e) {
  595. // System-wide accessibility properties file does
  596. // not exist;
  597. }
  598. }
  599. // Get whether a screen magnifier is present. First check
  600. // the system property and then check the properties file.
  601. String magPresent = System.getProperty("javax.accessibility.screen_magnifier_present");
  602. if (magPresent == null) {
  603. magPresent = properties.getProperty("screen_magnifier_present", null);
  604. if (magPresent != null) {
  605. System.setProperty("javax.accessibility.screen_magnifier_present", magPresent);
  606. }
  607. }
  608. // Get the names of any assistive technolgies to load. First
  609. // check the system property and then check the properties
  610. // file.
  611. String classNames = System.getProperty("javax.accessibility.assistive_technologies");
  612. if (classNames == null) {
  613. classNames = properties.getProperty("assistive_technologies", null);
  614. if (classNames != null) {
  615. System.setProperty("javax.accessibility.assistive_technologies", classNames);
  616. }
  617. }
  618. return classNames;
  619. }
  620. });
  621. // Load any assistive technologies
  622. if (atNames != null) {
  623. ClassLoader cl = ClassLoader.getSystemClassLoader();
  624. StringTokenizer parser = new StringTokenizer(atNames," ,");
  625. String atName;
  626. while (parser.hasMoreTokens()) {
  627. atName = parser.nextToken();
  628. try {
  629. Class clazz;
  630. if (cl != null) {
  631. clazz = cl.loadClass(atName);
  632. } else {
  633. clazz = Class.forName(atName);
  634. }
  635. clazz.newInstance();
  636. } catch (ClassNotFoundException e) {
  637. throw new AWTError("Assistive Technology not found: "
  638. + atName);
  639. } catch (InstantiationException e) {
  640. throw new AWTError("Could not instantiate Assistive"
  641. + " Technology: " + atName);
  642. } catch (IllegalAccessException e) {
  643. throw new AWTError("Could not access Assistive"
  644. + " Technology: " + atName);
  645. } catch (Exception e) {
  646. throw new AWTError("Error trying to install Assistive"
  647. + " Technology: " + atName + " " + e);
  648. }
  649. }
  650. }
  651. }
  652. /**
  653. * Gets the default toolkit.
  654. * <p>
  655. * If there is a system property named <code>"awt.toolkit"</code>,
  656. * that property is treated as the name of a class that is a subclass
  657. * of <code>Toolkit</code>.
  658. * <p>
  659. * If the system property does not exist, then the default toolkit
  660. * used is the class named <code>"sun.awt.motif.MToolkit"</code>,
  661. * which is a motif implementation of the Abstract Window Toolkit.
  662. * <p>
  663. * Also loads additional classes into the VM, using the property
  664. * 'assistive_technologies' specified in the Sun reference
  665. * implementation by a line in the 'accessibility.properties'
  666. * file. The form is "assistive_technologies=..." where
  667. * the "..." is a comma-separated list of assistive technology
  668. * classes to load. Each class is loaded in the order given
  669. * and a single instance of each is created using
  670. * Class.forName(class).newInstance(). This is done just after
  671. * the AWT toolkit is created. All errors are handled via an
  672. * AWTError exception.
  673. * @return the default toolkit.
  674. * @exception AWTError if a toolkit could not be found, or
  675. * if one could not be accessed or instantiated.
  676. */
  677. public static synchronized Toolkit getDefaultToolkit() {
  678. if (toolkit == null) {
  679. try {
  680. // We disable the JIT during toolkit initialization. This
  681. // tends to touch lots of classes that aren't needed again
  682. // later and therefore JITing is counter-productiive.
  683. java.lang.Compiler.disable();
  684. java.security.AccessController.doPrivileged(
  685. new java.security.PrivilegedAction() {
  686. public Object run() {
  687. String nm = null;
  688. Class cls = null;
  689. try {
  690. nm = System.getProperty("awt.toolkit",
  691. "sun.awt.motif.MToolkit");
  692. try {
  693. cls = Class.forName(nm);
  694. } catch (ClassNotFoundException e) {
  695. ClassLoader cl = ClassLoader.getSystemClassLoader();
  696. if (cl != null) {
  697. try {
  698. cls = cl.loadClass(nm);
  699. } catch (ClassNotFoundException ee) {
  700. throw new AWTError("Toolkit not found: " + nm);
  701. }
  702. }
  703. }
  704. if (cls != null) {
  705. toolkit = (Toolkit)cls.newInstance();
  706. if (GraphicsEnvironment.isHeadless()) {
  707. toolkit = new HeadlessToolkit(toolkit);
  708. }
  709. }
  710. } catch (InstantiationException e) {
  711. throw new AWTError("Could not instantiate Toolkit: " +
  712. nm);
  713. } catch (IllegalAccessException e) {
  714. throw new AWTError("Could not access Toolkit: " + nm);
  715. }
  716. return null;
  717. }
  718. });
  719. loadAssistiveTechnologies();
  720. } finally {
  721. // Make sure to always re-enable the JIT.
  722. java.lang.Compiler.enable();
  723. }
  724. }
  725. return toolkit;
  726. }
  727. /**
  728. * Returns an image which gets pixel data from the specified file,
  729. * whose format can be either GIF, JPEG or PNG.
  730. * The underlying toolkit attempts to resolve multiple requests
  731. * with the same filename to the same returned Image.
  732. * Since the mechanism required to facilitate this sharing of
  733. * Image objects may continue to hold onto images that are no
  734. * longer of use for an indefinite period of time, developers
  735. * are encouraged to implement their own caching of images by
  736. * using the createImage variant wherever available.
  737. * @param filename the name of a file containing pixel data
  738. * in a recognized file format.
  739. * @return an image which gets its pixel data from
  740. * the specified file.
  741. * @see #createImage(java.lang.String)
  742. */
  743. public abstract Image getImage(String filename);
  744. /**
  745. * Returns an image which gets pixel data from the specified URL.
  746. * The pixel data referenced by the specified URL must be in one
  747. * of the following formats: GIF, JPEG or PNG.
  748. * The underlying toolkit attempts to resolve multiple requests
  749. * with the same URL to the same returned Image.
  750. * Since the mechanism required to facilitate this sharing of
  751. * Image objects may continue to hold onto images that are no
  752. * longer of use for an indefinite period of time, developers
  753. * are encouraged to implement their own caching of images by
  754. * using the createImage variant wherever available.
  755. * @param url the URL to use in fetching the pixel data.
  756. * @return an image which gets its pixel data from
  757. * the specified URL.
  758. * @see #createImage(java.net.URL)
  759. */
  760. public abstract Image getImage(URL url);
  761. /**
  762. * Returns an image which gets pixel data from the specified file.
  763. * The returned Image is a new object which will not be shared
  764. * with any other caller of this method or its getImage variant.
  765. * @param filename the name of a file containing pixel data
  766. * in a recognized file format.
  767. * @return an image which gets its pixel data from
  768. * the specified file.
  769. * @see #getImage(java.lang.String)
  770. */
  771. public abstract Image createImage(String filename);
  772. /**
  773. * Returns an image which gets pixel data from the specified URL.
  774. * The returned Image is a new object which will not be shared
  775. * with any other caller of this method or its getImage variant.
  776. * @param url the URL to use in fetching the pixel data.
  777. * @return an image which gets its pixel data from
  778. * the specified URL.
  779. * @see #getImage(java.net.URL)
  780. */
  781. public abstract Image createImage(URL url);
  782. /**
  783. * Prepares an image for rendering.
  784. * <p>
  785. * If the values of the width and height arguments are both
  786. * <code>-1</code>, this method prepares the image for rendering
  787. * on the default screen; otherwise, this method prepares an image
  788. * for rendering on the default screen at the specified width and height.
  789. * <p>
  790. * The image data is downloaded asynchronously in another thread,
  791. * and an appropriately scaled screen representation of the image is
  792. * generated.
  793. * <p>
  794. * This method is called by components <code>prepareImage</code>
  795. * methods.
  796. * <p>
  797. * Information on the flags returned by this method can be found
  798. * with the definition of the <code>ImageObserver</code> interface.
  799. * @param image the image for which to prepare a
  800. * screen representation.
  801. * @param width the width of the desired screen
  802. * representation, or <code>-1</code>.
  803. * @param height the height of the desired screen
  804. * representation, or <code>-1</code>.
  805. * @param observer the <code>ImageObserver</code>
  806. * object to be notified as the
  807. * image is being prepared.
  808. * @return <code>true</code> if the image has already been
  809. * fully prepared; <code>false</code> otherwise.
  810. * @see java.awt.Component#prepareImage(java.awt.Image,
  811. * java.awt.image.ImageObserver)
  812. * @see java.awt.Component#prepareImage(java.awt.Image,
  813. * int, int, java.awt.image.ImageObserver)
  814. * @see java.awt.image.ImageObserver
  815. */
  816. public abstract boolean prepareImage(Image image, int width, int height,
  817. ImageObserver observer);
  818. /**
  819. * Indicates the construction status of a specified image that is
  820. * being prepared for display.
  821. * <p>
  822. * If the values of the width and height arguments are both
  823. * <code>-1</code>, this method returns the construction status of
  824. * a screen representation of the specified image in this toolkit.
  825. * Otherwise, this method returns the construction status of a
  826. * scaled representation of the image at the specified width
  827. * and height.
  828. * <p>
  829. * This method does not cause the image to begin loading.
  830. * An application must call <code>prepareImage</code> to force
  831. * the loading of an image.
  832. * <p>
  833. * This method is called by the component's <code>checkImage</code>
  834. * methods.
  835. * <p>
  836. * Information on the flags returned by this method can be found
  837. * with the definition of the <code>ImageObserver</code> interface.
  838. * @param image the image whose status is being checked.
  839. * @param width the width of the scaled version whose status is
  840. * being checked, or <code>-1</code>.
  841. * @param height the height of the scaled version whose status
  842. * is being checked, or <code>-1</code>.
  843. * @param observer the <code>ImageObserver</code> object to be
  844. * notified as the image is being prepared.
  845. * @return the bitwise inclusive <strong>OR</strong> of the
  846. * <code>ImageObserver</code> flags for the
  847. * image data that is currently available.
  848. * @see java.awt.Toolkit#prepareImage(java.awt.Image,
  849. * int, int, java.awt.image.ImageObserver)
  850. * @see java.awt.Component#checkImage(java.awt.Image,
  851. * java.awt.image.ImageObserver)
  852. * @see java.awt.Component#checkImage(java.awt.Image,
  853. * int, int, java.awt.image.ImageObserver)
  854. * @see java.awt.image.ImageObserver
  855. */
  856. public abstract int checkImage(Image image, int width, int height,
  857. ImageObserver observer);
  858. /**
  859. * Creates an image with the specified image producer.
  860. * @param producer the image producer to be used.
  861. * @return an image with the specified image producer.
  862. * @see java.awt.Image
  863. * @see java.awt.image.ImageProducer
  864. * @see java.awt.Component#createImage(java.awt.image.ImageProducer)
  865. */
  866. public abstract Image createImage(ImageProducer producer);
  867. /**
  868. * Creates an image which decodes the image stored in the specified
  869. * byte array.
  870. * <p>
  871. * The data must be in some image format, such as GIF or JPEG,
  872. * that is supported by this toolkit.
  873. * @param imagedata an array of bytes, representing
  874. * image data in a supported image format.
  875. * @return an image.
  876. * @since JDK1.1
  877. */
  878. public Image createImage(byte[] imagedata) {
  879. return createImage(imagedata, 0, imagedata.length);
  880. }
  881. /**
  882. * Creates an image which decodes the image stored in the specified
  883. * byte array, and at the specified offset and length.
  884. * The data must be in some image format, such as GIF or JPEG,
  885. * that is supported by this toolkit.
  886. * @param imagedata an array of bytes, representing
  887. * image data in a supported image format.
  888. * @param imageoffset the offset of the beginning
  889. * of the data in the array.
  890. * @param imagelength the length of the data in the array.
  891. * @return an image.
  892. * @since JDK1.1
  893. */
  894. public abstract Image createImage(byte[] imagedata,
  895. int imageoffset,
  896. int imagelength);
  897. /**
  898. * Gets a <code>PrintJob</code> object which is the result of initiating
  899. * a print operation on the toolkit's platform.
  900. * <p>
  901. * Each actual implementation of this method should first check if there
  902. * is a security manager installed. If there is, the method should call
  903. * the security manager's <code>checkPrintJobAccess</code> method to
  904. * ensure initiation of a print operation is allowed. If the default
  905. * implementation of <code>checkPrintJobAccess</code> is used (that is,
  906. * that method is not overriden), then this results in a call to the
  907. * security manager's <code>checkPermission</code> method with a <code>
  908. * RuntimePermission("queuePrintJob")</code> permission.
  909. *
  910. * @param frame the parent of the print dialog. May not be null.
  911. * @param jobtitle the title of the PrintJob. A null title is equivalent
  912. * to "".
  913. * @param props a Properties object containing zero or more properties.
  914. * Properties are not standardized and are not consistent across
  915. * implementations. Because of this, PrintJobs which require job
  916. * and page control should use the version of this function which
  917. * takes JobAttributes and PageAttributes objects. This object
  918. * may be updated to reflect the user's job choices on exit. May
  919. * be null.
  920. *
  921. * @return a <code>PrintJob</code> object, or <code>null</code> if the
  922. * user cancelled the print job.
  923. * @throws NullPointerException if frame is null. This exception is
  924. * always thrown when GraphicsEnvironment.isHeadless() returns
  925. * true.
  926. * @throws SecurityException if this thread is not allowed to initiate a
  927. * print job request
  928. * @see java.awt.GraphicsEnvironment#isHeadless
  929. * @see java.awt.PrintJob
  930. * @see java.lang.RuntimePermission
  931. * @since JDK1.1
  932. */
  933. public abstract PrintJob getPrintJob(Frame frame, String jobtitle,
  934. Properties props);
  935. /**
  936. * Gets a <code>PrintJob</code> object which is the result of initiating
  937. * a print operation on the toolkit's platform.
  938. * <p>
  939. * Each actual implementation of this method should first check if there
  940. * is a security manager installed. If there is, the method should call
  941. * the security manager's <code>checkPrintJobAccess</code> method to
  942. * ensure initiation of a print operation is allowed. If the default
  943. * implementation of <code>checkPrintJobAccess</code> is used (that is,
  944. * that method is not overriden), then this results in a call to the
  945. * security manager's <code>checkPermission</code> method with a <code>
  946. * RuntimePermission("queuePrintJob")</code> permission.
  947. *
  948. * @param frame the parent of the print dialog. May be null if and only
  949. * if jobAttributes is not null and jobAttributes.getDialog()
  950. * returns JobAttributes.DialogType.NONE or
  951. * JobAttributes.DialogType.COMMON.
  952. * @param jobtitle the title of the PrintJob. A null title is equivalent
  953. * to "".
  954. * @param jobAttributes a set of job attributes which will control the
  955. * PrintJob. The attributes will be updated to reflect the user's
  956. * choices as outlined in the JobAttributes documentation. May be
  957. * null.
  958. * @param pageAttributes a set of page attributes which will control the
  959. * PrintJob. The attributes will be applied to every page in the
  960. * job. The attributes will be updated to reflect the user's
  961. * choices as outlined in the PageAttributes documentation. May be
  962. * null.
  963. *
  964. * @return a <code>PrintJob</code> object, or <code>null</code> if the
  965. * user cancelled the print job.
  966. * @throws NullPointerException if frame is null and either jobAttributes
  967. * is null or jobAttributes.getDialog() returns
  968. * JobAttributes.DialogType.NATIVE.
  969. * @throws IllegalArgumentException if pageAttributes specifies differing
  970. * cross feed and feed resolutions. This exception is always
  971. * thrown when GraphicsEnvironment.isHeadless() returns true.
  972. * @throws SecurityException if this thread is not allowed to initiate a
  973. * print job request, or if jobAttributes specifies print to file,
  974. * and this thread is not allowed to access the file system
  975. * @see java.awt.PrintJob
  976. * @see java.awt.GraphicsEnvironment#isHeadless
  977. * @see java.lang.RuntimePermission
  978. * @see java.awt.JobAttributes
  979. * @see java.awt.PageAttributes
  980. * @since 1.3
  981. */
  982. public PrintJob getPrintJob(Frame frame, String jobtitle,
  983. JobAttributes jobAttributes,
  984. PageAttributes pageAttributes) {
  985. // Override to add printing support with new job/page control classes
  986. if (GraphicsEnvironment.isHeadless()) {
  987. throw new IllegalArgumentException();
  988. }
  989. if (this != Toolkit.getDefaultToolkit()) {
  990. return Toolkit.getDefaultToolkit().getPrintJob(frame, jobtitle,
  991. jobAttributes,
  992. pageAttributes);
  993. } else {
  994. return getPrintJob(frame, jobtitle, null);
  995. }
  996. }
  997. /**
  998. * Emits an audio beep.
  999. * @since JDK1.1
  1000. */
  1001. public abstract void beep();
  1002. /**
  1003. * Gets the singleton instance of the system Clipboard which interfaces
  1004. * with clipboard facilities provided by the native platform. This
  1005. * clipboard enables data transfer between Java programs and native
  1006. * applications which use native clipboard facilities.
  1007. * <p>
  1008. * In addition to any and all formats specified in the flavormap.properties
  1009. * file, or other file specified by the <code>AWT.DnD.flavorMapFileURL
  1010. * </code> Toolkit property, text returned by the system Clipboard's <code>
  1011. * getTransferData()</code> method is available in the following flavors:
  1012. * <ul>
  1013. * <li>DataFlavor.stringFlavor</li>
  1014. * <li>DataFlavor.plainTextFlavor (<b>deprecated</b>)</li>
  1015. * </ul>
  1016. * As with <code>java.awt.datatransfer.StringSelection</code>, if the
  1017. * requested flavor is <code>DataFlavor.plainTextFlavor</code>, or an
  1018. * equivalent flavor, a Reader is returned. <b>Note:</b> The behavior of
  1019. * the system Clipboard's <code>getTransferData()</code> method for <code>
  1020. * DataFlavor.plainTextFlavor</code>, and equivalent DataFlavors, is
  1021. * inconsistent with the definition of <code>DataFlavor.plainTextFlavor
  1022. * </code>. Because of this, support for <code>
  1023. * DataFlavor.plainTextFlavor</code>, and equivalent flavors, is
  1024. * <b>deprecated</b>.
  1025. * <p>
  1026. * Each actual implementation of this method should first check if there
  1027. * is a security manager installed. If there is, the method should call
  1028. * the security manager's <code>checkSystemClipboardAccess</code> method
  1029. * to ensure it's ok to to access the system clipboard. If the default
  1030. * implementation of <code>checkSystemClipboardAccess</code> is used (that
  1031. * is, that method is not overriden), then this results in a call to the
  1032. * security manager's <code>checkPermission</code> method with an <code>
  1033. * AWTPermission("accessClipboard")</code> permission.
  1034. *
  1035. * @return the system Clipboard
  1036. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  1037. * returns true
  1038. * @see java.awt.GraphicsEnvironment#isHeadless
  1039. * @see java.awt.datatransfer.Clipboard
  1040. * @see java.awt.datatransfer.StringSelection
  1041. * @see java.awt.datatransfer.DataFlavor#stringFlavor
  1042. * @see java.awt.datatransfer.DataFlavor#plainTextFlavor
  1043. * @see java.io.Reader
  1044. * @see java.awt.AWTPermission
  1045. * @since JDK1.1
  1046. */
  1047. public abstract Clipboard getSystemClipboard()
  1048. throws HeadlessException;
  1049. /**
  1050. * Gets the singleton instance of the system selection as a
  1051. * <code>Clipboard</code> object. This allows an application to read and
  1052. * modify the current, system-wide selection.
  1053. * <p>
  1054. * An application is responsible for updating the system selection whenever
  1055. * the user selects text, using either the mouse or the keyboard.
  1056. * Typically, this is implemented by installing a
  1057. * <code>FocusListener</code> on all <code>Component</code>s which support
  1058. * text selection, and, between <code>FOCUS_GAINED</code> and
  1059. * <code>FOCUS_LOST</code> events delivered to that <code>Component</code>,
  1060. * updating the system selection <code>Clipboard</code> when the selection
  1061. * changes inside the <code>Component</code>. Properly updating the system
  1062. * selection ensures that a Java application will interact correctly with
  1063. * native applications and other Java applications running simultaneously
  1064. * on the system. Note that <code>java.awt.TextComponent</code> and
  1065. * <code>javax.swing.text.JTextComponent</code> already adhere to this
  1066. * policy. When using these classes, and their subclasses, developers need
  1067. * not write any additional code.
  1068. * <p>
  1069. * Some platforms do not support a system selection <code>Clipboard</code>.
  1070. * On those platforms, this method will return <code>null</code>. In such a
  1071. * case, an application is absolved from its responsibility to update the
  1072. * system selection <code>Clipboard</code> as described above.