Merge from mainline.
[official-gcc.git] / libjava / classpath / java / awt / Toolkit.java
blob16f1caf85df656113a01809e5dba61a59e218e0c
1 /* Toolkit.java -- AWT Toolkit superclass
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA.
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
25 combination.
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
40 package java.awt;
42 import gnu.classpath.SystemProperties;
43 import gnu.java.awt.peer.GLightweightPeer;
45 import java.awt.datatransfer.Clipboard;
46 import java.awt.dnd.DragGestureEvent;
47 import java.awt.dnd.DragGestureListener;
48 import java.awt.dnd.DragGestureRecognizer;
49 import java.awt.dnd.DragSource;
50 import java.awt.dnd.peer.DragSourceContextPeer;
51 import java.awt.event.AWTEventListener;
52 import java.awt.event.AWTEventListenerProxy;
53 import java.awt.event.KeyEvent;
54 import java.awt.im.InputMethodHighlight;
55 import java.awt.image.ColorModel;
56 import java.awt.image.ImageObserver;
57 import java.awt.image.ImageProducer;
58 import java.awt.peer.ButtonPeer;
59 import java.awt.peer.CanvasPeer;
60 import java.awt.peer.CheckboxMenuItemPeer;
61 import java.awt.peer.CheckboxPeer;
62 import java.awt.peer.ChoicePeer;
63 import java.awt.peer.DialogPeer;
64 import java.awt.peer.FileDialogPeer;
65 import java.awt.peer.FontPeer;
66 import java.awt.peer.FramePeer;
67 import java.awt.peer.LabelPeer;
68 import java.awt.peer.LightweightPeer;
69 import java.awt.peer.ListPeer;
70 import java.awt.peer.MenuBarPeer;
71 import java.awt.peer.MenuItemPeer;
72 import java.awt.peer.MenuPeer;
73 import java.awt.peer.PanelPeer;
74 import java.awt.peer.PopupMenuPeer;
75 import java.awt.peer.ScrollPanePeer;
76 import java.awt.peer.ScrollbarPeer;
77 import java.awt.peer.TextAreaPeer;
78 import java.awt.peer.TextFieldPeer;
79 import java.awt.peer.WindowPeer;
80 import java.beans.PropertyChangeListener;
81 import java.beans.PropertyChangeSupport;
82 import java.io.File;
83 import java.io.FileInputStream;
84 import java.net.URL;
85 import java.security.AccessController;
86 import java.security.PrivilegedAction;
87 import java.util.ArrayList;
88 import java.util.Map;
89 import java.util.Properties;
90 import java.util.StringTokenizer;
92 /**
93 * The AWT system uses a set of native peer objects to implement its
94 * widgets. These peers are provided by a peer toolkit, that is accessed
95 * via a subclass of this superclass. The system toolkit is retrieved
96 * by the static methods <code>getDefaultToolkit</code>. This method
97 * determines the system toolkit by examining the system property
98 * <code>awt.toolkit</code>. That property is set to the name of the
99 * <code>Toolkit</code> subclass for the specified peer set. If the
100 * <code>awt.toolkit</code> property is not set, then the default
101 * toolkit <code>gnu.java.awt.peer.gtk.GtkToolkit</code> is used. This
102 * toolkit creates its peers using the GTK+ toolkit.
104 * @author Aaron M. Renn (arenn@urbanophile.com)
106 public abstract class Toolkit
108 /** The default toolkit name. */
109 private static String default_toolkit_name
110 = gnu.classpath.Configuration.default_awt_peer_toolkit;
113 * The toolkit in use. Once we load it, we don't ever change it
114 * if the awt.toolkit property is set.
116 private static Toolkit toolkit;
118 /** The toolkit properties. */
119 private static Properties props = new Properties();
121 protected final Map desktopProperties = new Properties();
123 protected final PropertyChangeSupport desktopPropsSupport
124 = new PropertyChangeSupport(this);
127 * All registered AWTEventListener objects. This is package private, so the
128 * event queue can efficiently access this list.
130 AWTEventListenerProxy[] awtEventListeners;
133 * Default constructor for subclasses.
135 public Toolkit()
137 awtEventListeners = new AWTEventListenerProxy[0];
141 * Creates a peer object for the specified <code>Button</code>.
143 * @param target The <code>Button</code> to create the peer for.
145 * @return The peer for the specified <code>Button</code> object.
147 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
149 protected abstract ButtonPeer createButton(Button target);
152 * Creates a peer object for the specified <code>TextField</code>.
154 * @param target The <code>TextField</code> to create the peer for.
156 * @return The peer for the specified <code>TextField</code> object.
158 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
160 protected abstract TextFieldPeer createTextField(TextField target);
163 * Creates a peer object for the specified <code>Label</code>.
165 * @param target The <code>Label</code> to create the peer for.
167 * @return The peer for the specified <code>Label</code> object.
169 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
171 protected abstract LabelPeer createLabel(Label target);
174 * Creates a peer object for the specified <code>List</code>.
176 * @param target The <code>List</code> to create the peer for.
178 * @return The peer for the specified <code>List</code> object.
180 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
182 protected abstract ListPeer createList(List target);
185 * Creates a peer object for the specified <code>Checkbox</code>.
187 * @param target The <code>Checkbox</code> to create the peer for.
189 * @return The peer for the specified <code>Checkbox</code> object.
191 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
193 protected abstract CheckboxPeer createCheckbox(Checkbox target);
196 * Creates a peer object for the specified <code>Scrollbar</code>.
198 * @param target The <code>Scrollbar</code> to create the peer for.
200 * @return The peer for the specified <code>Scrollbar</code> object.
202 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
204 protected abstract ScrollbarPeer createScrollbar(Scrollbar target);
207 * Creates a peer object for the specified <code>ScrollPane</code>.
209 * @param target The <code>ScrollPane</code> to create the peer for.
211 * @return The peer for the specified <code>ScrollPane</code> object.
213 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
215 protected abstract ScrollPanePeer createScrollPane(ScrollPane target);
218 * Creates a peer object for the specified <code>TextArea</code>.
220 * @param target The <code>TextArea</code> to create the peer for.
222 * @return The peer for the specified <code>TextArea</code> object.
224 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
226 protected abstract TextAreaPeer createTextArea(TextArea target);
229 * Creates a peer object for the specified <code>Choice</code>.
231 * @param target The <code>Choice</code> to create the peer for.
233 * @return The peer for the specified <code>Choice</code> object.
235 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
237 protected abstract ChoicePeer createChoice(Choice target);
240 * Creates a peer object for the specified <code>Frame</code>.
242 * @param target The <code>Frame</code> to create the peer for.
244 * @return The peer for the specified <code>Frame</code> object.
246 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
248 protected abstract FramePeer createFrame(Frame target);
251 * Creates a peer object for the specified <code>Canvas</code>.
253 * @param target The <code>Canvas</code> to create the peer for.
255 * @return The peer for the specified <code>Canvas</code> object.
257 protected abstract CanvasPeer createCanvas(Canvas target);
260 * Creates a peer object for the specified <code>Panel</code>.
262 * @param target The <code>Panel</code> to create the peer for.
264 * @return The peer for the specified <code>Panel</code> object.
266 protected abstract PanelPeer createPanel(Panel target);
269 * Creates a peer object for the specified <code>Window</code>.
271 * @param target The <code>Window</code> to create the peer for.
273 * @return The peer for the specified <code>Window</code> object.
275 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
277 protected abstract WindowPeer createWindow(Window target);
280 * Creates a peer object for the specified <code>Dialog</code>.
282 * @param target The dialog to create the peer for
284 * @return The peer for the specified font name.
286 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
288 protected abstract DialogPeer createDialog(Dialog target);
291 * Creates a peer object for the specified <code>MenuBar</code>.
293 * @param target The <code>MenuBar</code> to create the peer for.
295 * @return The peer for the specified <code>MenuBar</code> object.
297 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
299 protected abstract MenuBarPeer createMenuBar(MenuBar target);
302 * Creates a peer object for the specified <code>Menu</code>.
304 * @param target The <code>Menu</code> to create the peer for.
306 * @return The peer for the specified <code>Menu</code> object.
308 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
310 protected abstract MenuPeer createMenu(Menu target);
313 * Creates a peer object for the specified <code>PopupMenu</code>.
315 * @param target The <code>PopupMenu</code> to create the peer for.
317 * @return The peer for the specified <code>PopupMenu</code> object.
319 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
321 protected abstract PopupMenuPeer createPopupMenu(PopupMenu target);
324 * Creates a peer object for the specified <code>MenuItem</code>.
326 * @param target The <code>MenuItem</code> to create the peer for.
328 * @return The peer for the specified <code>MenuItem</code> object.
330 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
332 protected abstract MenuItemPeer createMenuItem(MenuItem target);
335 * Creates a peer object for the specified <code>FileDialog</code>.
337 * @param target The <code>FileDialog</code> to create the peer for.
339 * @return The peer for the specified <code>FileDialog</code> object.
341 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
343 protected abstract FileDialogPeer createFileDialog(FileDialog target);
346 * Creates a peer object for the specified <code>CheckboxMenuItem</code>.
348 * @param target The <code>CheckboxMenuItem</code> to create the peer for.
350 * @return The peer for the specified <code>CheckboxMenuItem</code> object.
352 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
354 protected abstract CheckboxMenuItemPeer
355 createCheckboxMenuItem(CheckboxMenuItem target);
358 * Creates a peer object for the specified <code>Component</code>. The
359 * peer returned by this method is not a native windowing system peer
360 * with its own native window. Instead, this method allows the component
361 * to draw on its parent window as a "lightweight" widget.
363 * @param target The <code>Component</code> to create the peer for.
365 * @return The peer for the specified <code>Component</code> object.
367 protected LightweightPeer createComponent(Component target)
369 return new GLightweightPeer(target);
373 * Creates a peer object for the specified font name.
375 * @param name The font to create the peer for.
376 * @param style The font style to create the peer for.
378 * @return The peer for the specified font name.
380 * @deprecated
382 protected abstract FontPeer getFontPeer(String name, int style);
385 * Copies the current system colors into the specified array. This is
386 * the interface used by the <code>SystemColor</code> class. Although
387 * this method fills in the array with some default colors a real Toolkit
388 * should override this method and provide real system colors for the
389 * native GUI platform.
391 * @param systemColors The array to copy the system colors into.
392 * It must be at least 26 elements.
394 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
396 * @see java.awt.SystemColor
398 protected void loadSystemColors(int systemColors[])
400 systemColors[SystemColor.DESKTOP] = 0xFF005C5C;
401 systemColors[SystemColor.ACTIVE_CAPTION] = 0xFF000080;
402 systemColors[SystemColor.ACTIVE_CAPTION_TEXT] = 0xFFFFFFFF;
403 systemColors[SystemColor.ACTIVE_CAPTION_BORDER] = 0xFFC0C0C0;
404 systemColors[SystemColor.INACTIVE_CAPTION] = 0xFF808080;
405 systemColors[SystemColor.INACTIVE_CAPTION_TEXT] = 0xFFC0C0C0;
406 systemColors[SystemColor.INACTIVE_CAPTION_BORDER] = 0xFFC0C0C0;
407 systemColors[SystemColor.WINDOW] = 0xFFFFFFFF;
408 systemColors[SystemColor.WINDOW_BORDER] = 0xFF000000;
409 systemColors[SystemColor.WINDOW_TEXT] = 0xFF000000;
410 systemColors[SystemColor.MENU] = 0xFFC0C0C0;
411 systemColors[SystemColor.MENU_TEXT] = 0xFF000000;
412 systemColors[SystemColor.TEXT] = 0xFFC0C0C0;
413 systemColors[SystemColor.TEXT_TEXT] = 0xFF000000;
414 systemColors[SystemColor.TEXT_HIGHLIGHT] = 0xFF000090;
415 systemColors[SystemColor.TEXT_HIGHLIGHT_TEXT] = 0xFFFFFFFF;
416 systemColors[SystemColor.TEXT_INACTIVE_TEXT] = 0xFF808080;
417 systemColors[SystemColor.CONTROL] = 0xFFC0C0C0;
418 systemColors[SystemColor.CONTROL_TEXT] = 0xFF000000;
419 systemColors[SystemColor.CONTROL_HIGHLIGHT] = 0xFFFFFFFF;
420 systemColors[SystemColor.CONTROL_LT_HIGHLIGHT] = 0xFFE0E0E0;
421 systemColors[SystemColor.CONTROL_SHADOW] = 0xFF808080;
422 systemColors[SystemColor.CONTROL_DK_SHADOW] = 0xFF000000;
423 systemColors[SystemColor.SCROLLBAR] = 0xFFE0E0E0;
424 systemColors[SystemColor.INFO] = 0xFFE0E000;
425 systemColors[SystemColor.INFO_TEXT] = 0xFF000000;
429 * @since 1.4
431 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
433 public void setDynamicLayout(boolean dynamic)
438 * @since 1.4
440 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
442 protected boolean isDynamicLayoutSet()
444 return false;
448 * @since 1.4
450 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
452 public boolean isDynamicLayoutActive()
454 return false;
458 * Returns the dimensions of the screen in pixels.
460 * @return The dimensions of the screen in pixels.
462 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
464 public abstract Dimension getScreenSize();
467 * Returns the screen resolution in dots per square inch.
469 * @return The screen resolution in dots per square inch.
471 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
473 public abstract int getScreenResolution();
476 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
478 * @since 1.4
480 public Insets getScreenInsets(GraphicsConfiguration gc)
482 return new Insets(0, 0, 0, 0);
486 * Returns the color model of the screen.
488 * @return The color model of the screen.
490 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
492 public abstract ColorModel getColorModel();
495 * Returns the names of the available fonts.
497 * @return The names of the available fonts.
499 * @deprecated
501 public abstract String[] getFontList();
504 * Return the font metrics for the specified font
506 * @param name The name of the font to return metrics for.
508 * @return The requested font metrics.
510 * @deprecated
512 public abstract FontMetrics getFontMetrics(Font name);
515 * Flushes any buffered data to the screen so that it is in sync with
516 * what the AWT system has drawn to it.
518 public abstract void sync();
521 * Returns an instance of the default toolkit. The default toolkit is
522 * the subclass of <code>Toolkit</code> specified in the system property
523 * <code>awt.toolkit</code>, or <code>gnu.java.awt.peer.gtk.GtkToolkit</code>
524 * if the property is not set.
526 * @return An instance of the system default toolkit.
528 * @throws AWTError If the toolkit cannot be loaded.
530 public static Toolkit getDefaultToolkit()
532 if (toolkit != null)
533 return toolkit;
534 String toolkit_name = SystemProperties.getProperty("awt.toolkit",
535 default_toolkit_name);
538 ClassLoader cl;
539 cl = (ClassLoader) AccessController.doPrivileged
540 (new PrivilegedAction()
542 public Object run()
544 return ClassLoader.getSystemClassLoader();
547 Class cls = cl.loadClass(toolkit_name);
548 Object obj = cls.newInstance();
549 if (!(obj instanceof Toolkit))
550 throw new AWTError(toolkit_name + " is not a subclass of " +
551 "java.awt.Toolkit");
552 toolkit = (Toolkit) obj;
554 initAccessibility();
555 return toolkit;
557 catch (ThreadDeath death)
559 throw death;
561 catch (Throwable t)
563 AWTError e = new AWTError("Cannot load AWT toolkit: " + toolkit_name);
564 throw (AWTError) e.initCause(t);
569 * Returns an image from the specified file, which must be in a
570 * recognized format. Supported formats vary from toolkit to toolkit.
572 * @return name The name of the file to read the image from.
574 public abstract Image getImage(String name);
577 * Returns an image from the specified URL, which must be in a
578 * recognized format. Supported formats vary from toolkit to toolkit.
580 * @return url The URl to read the image from.
582 public abstract Image getImage(URL url);
584 public abstract Image createImage(String filename);
586 public abstract Image createImage(URL url);
589 * Readies an image to be rendered on the screen. The width and height
590 * values can be set to the default sizes for the image by passing -1
591 * in those parameters.
593 * @param image The image to prepare for rendering.
594 * @param width The width of the image.
595 * @param height The height of the image.
596 * @param observer The observer to receive events about the preparation
597 * process.
599 * @return <code>true</code> if the image is already prepared for rendering,
600 * <code>false</code> otherwise.
602 public abstract boolean prepareImage(Image image, int width, int height,
603 ImageObserver observer);
606 * Checks the status of specified image as it is being readied for
607 * rendering.
609 * @param image The image to prepare for rendering.
610 * @param width The width of the image.
611 * @param height The height of the image.
612 * @param observer The observer to receive events about the preparation
613 * process.
615 * @return A union of the bitmasks from
616 * <code>java.awt.image.ImageObserver</code> that indicates the current
617 * state of the imaging readying process.
619 public abstract int checkImage(Image image, int width, int height,
620 ImageObserver observer);
623 * Creates an image using the specified <code>ImageProducer</code>
625 * @param producer The <code>ImageProducer</code> to create the image from.
627 * @return The created image.
629 public abstract Image createImage(ImageProducer producer);
632 * Creates an image from the specified byte array. The array must be in
633 * a recognized format. Supported formats vary from toolkit to toolkit.
635 * @param data The raw image data.
637 * @return The created image.
639 public Image createImage(byte[] data)
641 return createImage(data, 0, data.length);
645 * Creates an image from the specified portion of the byte array passed.
646 * The array must be in a recognized format. Supported formats vary from
647 * toolkit to toolkit.
649 * @param data The raw image data.
650 * @param offset The offset into the data where the image data starts.
651 * @param len The length of the image data.
653 * @return The created image.
655 public abstract Image createImage(byte[] data, int offset, int len);
658 * Returns a instance of <code>PrintJob</code> for the specified
659 * arguments.
661 * @param frame The window initiating the print job.
662 * @param title The print job title.
663 * @param props The print job properties.
665 * @return The requested print job, or <code>null</code> if the job
666 * was cancelled.
668 * @exception NullPointerException If frame is null,
669 * or GraphicsEnvironment.isHeadless() returns true.
670 * @exception SecurityException If this thread is not allowed to initiate
671 * a print job request.
673 public abstract PrintJob getPrintJob(Frame frame, String title,
674 Properties props);
677 * Returns a instance of <code>PrintJob</code> for the specified
678 * arguments.
680 * @param frame The window initiating the print job.
681 * @param title The print job title.
682 * @param jobAttr A set of job attributes which will control the print job.
683 * @param pageAttr A set of page attributes which will control the print job.
685 * @exception NullPointerException If frame is null, and either jobAttr is null
686 * or jobAttr.getDialog() returns JobAttributes.DialogType.NATIVE.
687 * @exception IllegalArgumentException If pageAttrspecifies differing cross
688 * feed and feed resolutions, or when GraphicsEnvironment.isHeadless() returns
689 * true.
690 * @exception SecurityException If this thread is not allowed to initiate
691 * a print job request.
693 * @since 1.3
695 public PrintJob getPrintJob(Frame frame, String title,
696 JobAttributes jobAttr, PageAttributes pageAttr)
698 return null;
702 * Causes a "beep" tone to be generated.
704 public abstract void beep();
707 * Returns the system clipboard.
709 * @return THe system clipboard.
711 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
713 public abstract Clipboard getSystemClipboard();
716 * Gets the singleton instance of the system selection as a
717 * Clipboard object. The system selection contains the selected text
718 * of the last component/widget that had focus and a text selection.
719 * The default implementation returns null.
721 * @return The Clipboard holding the system (text) selection or null
722 * if the Toolkit or system doesn't support a selection clipboard.
724 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
725 * is true.
726 * @exception SecurityException If the current security manager
727 * checkSystemClipboardAccess() doesn't allow access.
729 * @since 1.4
731 public Clipboard getSystemSelection()
733 return null;
737 * Returns the accelerator key mask for menu shortcuts. The default is
738 * <code>Event.CTRL_MASK</code>. A toolkit must override this method
739 * to change the default.
741 * @return The key mask for the menu accelerator key.
743 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
745 public int getMenuShortcutKeyMask()
747 return Event.CTRL_MASK;
751 * Returns whether the given locking key on the keyboard is currently in its
752 * "on" state.
754 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
755 * @exception IllegalArgumentException If keyCode is not one of the valid keys.
756 * @exception UnsupportedOperationException If the host system doesn't allow
757 * getting the state of this key programmatically, or if the keyboard doesn't
758 * have this key.
760 public boolean getLockingKeyState(int keyCode)
762 if (keyCode != KeyEvent.VK_CAPS_LOCK
763 && keyCode != KeyEvent.VK_NUM_LOCK
764 && keyCode != KeyEvent.VK_SCROLL_LOCK)
765 throw new IllegalArgumentException();
767 throw new UnsupportedOperationException();
771 * Sets the state of the given locking key on the keyboard.
773 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
774 * @exception IllegalArgumentException If keyCode is not one of the valid keys.
775 * @exception UnsupportedOperationException If the host system doesn't allow
776 * getting the state of this key programmatically, or if the keyboard doesn't
777 * have this key.
779 public void setLockingKeyState(int keyCode, boolean on)
781 if (keyCode != KeyEvent.VK_CAPS_LOCK
782 && keyCode != KeyEvent.VK_NUM_LOCK
783 && keyCode != KeyEvent.VK_SCROLL_LOCK)
784 throw new IllegalArgumentException();
786 throw new UnsupportedOperationException();
790 * Returns the native container object of the specified component. This
791 * method is necessary because the parent component might be a lightweight
792 * component.
794 * @param component The component to fetch the native container for.
796 * @return The native container object for this component.
798 protected static Container getNativeContainer(Component component)
800 component = component.getParent();
801 while (true)
803 if (component == null)
804 return null;
805 if (! (component instanceof Container))
807 component = component.getParent();
808 continue;
810 if (component.getPeer() instanceof LightweightPeer)
812 component = component.getParent();
813 continue;
815 return (Container) component;
820 * Creates a new custom cursor object.
822 * @exception IndexOutOfBoundsException If the hotSpot values are outside
823 * the bounds of the cursor.
824 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
826 public Cursor createCustomCursor(Image cursor, Point hotSpot, String name)
828 // Presumably the only reason this isn't abstract is for backwards
829 // compatibility? FIXME?
830 if (GraphicsEnvironment.isHeadless())
831 throw new HeadlessException("No custom cursor in an headless graphics "
832 + "environment.");
833 return null;
837 * Returns the supported cursor dimension which is closest to the
838 * desired sizes.
840 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
842 public Dimension getBestCursorSize(int preferredWidth, int preferredHeight)
844 if (GraphicsEnvironment.isHeadless())
845 throw new HeadlessException("No best cursor size in an headless "
846 + "graphics environment.");
847 return new Dimension (0,0);
851 * Returns the maximum number of colors the Toolkit supports in a custom
852 * cursor palette.
854 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
856 public int getMaximumCursorColors()
858 return 0;
862 * Returns whether Toolkit supports this state for Frames.
864 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
866 * @since 1.4
868 public boolean isFrameStateSupported(int state)
870 return false;
874 * Returns the value of the property with the specified name, or the
875 * default value if the property does not exist.
877 * @param key The name of the property to retrieve.
878 * @param def The default value of the property.
880 public static String getProperty(String key, String def)
882 return props.getProperty(key, def);
887 * Returns the event queue that is suitable for the calling context.
889 * <p>Despite the word &#x201c;System&#x201d; in the name of this
890 * method, a toolkit may provide different event queues for each
891 * applet. There is no guarantee that the same queue is shared
892 * system-wide.
894 * <p>The implementation first checks whether a
895 * SecurityManager has been installed. If so, its {@link
896 * java.lang.SecurityManager#checkAwtEventQueueAccess()} method gets
897 * called. The security manager will throw a SecurityException if it
898 * does not grant the permission to access the event queue.
900 * <p>Next, the call is delegated to {@link
901 * #getSystemEventQueueImpl()}.
903 * @return The event queue for this applet (or application).
905 * @throws SecurityException if a security manager has been
906 * installed, and it does not grant the permission to access the
907 * event queue.
909 public final EventQueue getSystemEventQueue()
911 SecurityManager sm;
913 sm = System.getSecurityManager();
914 if (sm != null)
915 sm.checkAwtEventQueueAccess();
917 return getSystemEventQueueImpl();
922 * Returns the event queue that is suitable for the calling context.
924 * <p>Despite the word &#x201c;System&#x201d; in the name of this
925 * method, a toolkit may provide different event queues for each
926 * applet. There is no guarantee that the same queue is shared
927 * system-wide.
929 * <p>No security checks are performed, which is why this method
930 * may only be called by Toolkits.
932 * @see #getSystemEventQueue()
934 protected abstract EventQueue getSystemEventQueueImpl();
938 * @since 1.3
940 public abstract DragSourceContextPeer
941 createDragSourceContextPeer(DragGestureEvent e);
944 * @since 1.3
946 public DragGestureRecognizer
947 createDragGestureRecognizer(Class recognizer, DragSource ds,
948 Component comp, int actions,
949 DragGestureListener l)
951 return null;
954 public final Object getDesktopProperty(String propertyName)
956 return desktopProperties.get(propertyName);
959 protected final void setDesktopProperty(String name, Object newValue)
961 Object oldValue = getDesktopProperty(name);
962 desktopProperties.put(name, newValue);
963 desktopPropsSupport.firePropertyChange(name, oldValue, newValue);
966 protected Object lazilyLoadDesktopProperty(String name)
968 // FIXME - what is this??
969 return null;
972 protected void initializeDesktopProperties()
974 // Overridden by toolkit implementation?
977 public void addPropertyChangeListener(String name,
978 PropertyChangeListener pcl)
980 desktopPropsSupport.addPropertyChangeListener(name, pcl);
983 public void removePropertyChangeListener(String name,
984 PropertyChangeListener pcl)
986 desktopPropsSupport.removePropertyChangeListener(name, pcl);
990 * @since 1.4
992 public PropertyChangeListener[] getPropertyChangeListeners()
994 return desktopPropsSupport.getPropertyChangeListeners();
998 * @since 1.4
1000 public PropertyChangeListener[] getPropertyChangeListeners(String name)
1002 return desktopPropsSupport.getPropertyChangeListeners(name);
1006 * Adds an AWTEventListener to this toolkit. This listener is informed about
1007 * all events that pass the eventqueue that match the specified
1008 * <code>evenMask</code>. The <code>eventMask</code> is an ORed combination
1009 * of event masks as defined in {@link AWTEvent}.
1011 * If a security manager is installed, it is asked first if an
1012 * <code>AWTPermission(&quot;listenToAllAWTEvents&quot;)</code> is allowed.
1013 * This may result in a <code>SecurityException</code> beeing thrown.
1015 * It is not recommended to use this kind of notification for normal
1016 * applications. It is intended solely for the purpose of debugging and to
1017 * support special facilities.
1019 * @param listener the listener to add
1020 * @param eventMask the event mask of event types which the listener is
1021 * interested in
1023 * @since 1.2
1025 * @throws SecurityException if there is a <code>SecurityManager</code> that
1026 * doesn't grant
1027 * <code>AWTPermission(&quot;listenToAllAWTEvents&quot;)</code>
1029 * @see #getAWTEventListeners()
1030 * @see #getAWTEventListeners(long)
1031 * @see #removeAWTEventListener(AWTEventListener)
1033 public void addAWTEventListener(AWTEventListener listener, long eventMask)
1035 // First we must check the security permissions.
1036 SecurityManager s = System.getSecurityManager();
1037 if (s != null)
1038 s.checkPermission(new AWTPermission("listenToAllAWTEvents"));
1040 // Go through the list and check if the requested listener is already
1041 // registered.
1042 boolean found = false;
1043 for (int i = 0; i < awtEventListeners.length; ++i)
1045 AWTEventListenerProxy proxy = awtEventListeners[i];
1046 if (proxy.getListener() == listener)
1048 found = true;
1049 // Modify the proxies event mask to include the new event mask.
1050 AWTEventListenerProxy newProxy =
1051 new AWTEventListenerProxy(proxy.getEventMask() | eventMask,
1052 listener);
1053 awtEventListeners[i] = newProxy;
1054 break;
1058 // If that listener was not found, then add it.
1059 if (! found)
1061 AWTEventListenerProxy proxy =
1062 new AWTEventListenerProxy(eventMask, listener);
1063 AWTEventListenerProxy[] newArray =
1064 new AWTEventListenerProxy[awtEventListeners.length + 1];
1065 System.arraycopy(awtEventListeners, 0, newArray, 0,
1066 awtEventListeners.length);
1067 newArray[newArray.length - 1] = proxy;
1068 awtEventListeners = newArray;
1073 * Removes an AWT event listener from this toolkit. This listener is no
1074 * longer informed of any event types it was registered in.
1076 * If a security manager is installed, it is asked first if an
1077 * <code>AWTPermission(&quot;listenToAllAWTEvents&quot;)</code> is allowed.
1078 * This may result in a <code>SecurityException</code> beeing thrown.
1080 * It is not recommended to use this kind of notification for normal
1081 * applications. It is intended solely for the purpose of debugging and to
1082 * support special facilities.
1084 * @param listener the listener to remove
1086 * @throws SecurityException if there is a <code>SecurityManager</code> that
1087 * doesn't grant
1088 * <code>AWTPermission(&quot;listenToAllAWTEvents&quot;)</code>
1090 * @since 1.2
1092 * @see #addAWTEventListener(AWTEventListener, long)
1093 * @see #getAWTEventListeners()
1094 * @see #getAWTEventListeners(long)
1096 public void removeAWTEventListener(AWTEventListener listener)
1098 // First we must check the security permissions.
1099 SecurityManager s = System.getSecurityManager();
1100 if (s != null)
1101 s.checkPermission(new AWTPermission("listenToAllAWTEvents"));
1104 // Find the index of the listener.
1105 int index = -1;
1106 for (int i = 0; i < awtEventListeners.length; ++i)
1108 AWTEventListenerProxy proxy = awtEventListeners[i];
1109 if (proxy.getListener() == listener)
1111 index = i;
1112 break;
1116 // Copy over the arrays and leave out the removed element.
1117 if (index != -1)
1119 AWTEventListenerProxy[] newArray =
1120 new AWTEventListenerProxy[awtEventListeners.length - 1];
1121 if (index > 0)
1122 System.arraycopy(awtEventListeners, 0, newArray, 0, index);
1123 if (index < awtEventListeners.length - 1)
1124 System.arraycopy(awtEventListeners, index + 1, newArray, index,
1125 awtEventListeners.length - index - 1);
1126 awtEventListeners = newArray;
1131 * Returns all registered AWT event listeners. This method returns a copy of
1132 * the listener array, so that application cannot trash the listener list.
1134 * If a security manager is installed, it is asked first if an
1135 * <code>AWTPermission(&quot;listenToAllAWTEvents&quot;)</code> is allowed.
1136 * This may result in a <code>SecurityException</code> beeing thrown.
1138 * It is not recommended to use this kind of notification for normal
1139 * applications. It is intended solely for the purpose of debugging and to
1140 * support special facilities.
1142 * @return all registered AWT event listeners
1144 * @throws SecurityException if there is a <code>SecurityManager</code> that
1145 * doesn't grant
1146 * <code>AWTPermission(&quot;listenToAllAWTEvents&quot;)</code>
1148 * @since 1.4
1150 * @see #addAWTEventListener(AWTEventListener, long)
1151 * @see #removeAWTEventListener(AWTEventListener)
1152 * @see #getAWTEventListeners(long)
1154 public AWTEventListener[] getAWTEventListeners()
1156 // First we must check the security permissions.
1157 SecurityManager s = System.getSecurityManager();
1158 if (s != null)
1159 s.checkPermission(new AWTPermission("listenToAllAWTEvents"));
1161 // Create a copy of the array.
1162 AWTEventListener[] copy = new AWTEventListener[awtEventListeners.length];
1163 System.arraycopy(awtEventListeners, 0, copy, 0, awtEventListeners.length);
1164 return copy;
1168 * Returns all registered AWT event listeners that listen for events with
1169 * the specified <code>eventMask</code>. This method returns a copy of
1170 * the listener array, so that application cannot trash the listener list.
1172 * If a security manager is installed, it is asked first if an
1173 * <code>AWTPermission(&quot;listenToAllAWTEvents&quot;)</code> is allowed.
1174 * This may result in a <code>SecurityException</code> beeing thrown.
1176 * It is not recommended to use this kind of notification for normal
1177 * applications. It is intended solely for the purpose of debugging and to
1178 * support special facilities.
1180 * @param mask the event mask
1182 * @throws SecurityException if there is a <code>SecurityManager</code> that
1183 * doesn't grant
1184 * <code>AWTPermission(&quot;listenToAllAWTEvents&quot;)</code>
1187 * @since 1.4
1189 * @see #addAWTEventListener(AWTEventListener, long)
1190 * @see #removeAWTEventListener(AWTEventListener)
1191 * @see #getAWTEventListeners()
1193 public AWTEventListener[] getAWTEventListeners(long mask)
1195 // First we must check the security permissions.
1196 SecurityManager s = System.getSecurityManager();
1197 if (s != null)
1198 s.checkPermission(new AWTPermission("listenToAllAWTEvents"));
1200 // Create a copy of the array with only the requested listeners in it.
1201 ArrayList l = new ArrayList(awtEventListeners.length);
1202 for (int i = 0; i < awtEventListeners.length; ++i)
1204 if ((awtEventListeners[i].getEventMask() & mask) != 0)
1205 l.add(awtEventListeners[i]);
1208 return (AWTEventListener[] ) l.toArray(new AWTEventListener[l.size()]);
1213 * Dispatches events to listeners registered to this Toolkit. This is called
1214 * by {@link Component#dispatchEventImpl(AWTEvent)} in order to dispatch
1215 * events globally.
1217 * @param ev the event to dispatch
1219 void globalDispatchEvent(AWTEvent ev)
1221 // We do not use the accessor methods here because they create new
1222 // arrays each time. We must be very efficient, so we access this directly.
1223 for (int i = 0; i < awtEventListeners.length; ++i)
1225 AWTEventListenerProxy proxy = awtEventListeners[i];
1226 if ((proxy.getEventMask() & AWTEvent.eventIdToMask(ev.getID())) != 0)
1227 proxy.eventDispatched(ev);
1232 * @since 1.3
1234 public abstract Map mapInputMethodHighlight(InputMethodHighlight highlight);
1237 * Initializes the accessibility framework. In particular, this loads the
1238 * properties javax.accessibility.screen_magnifier_present and
1239 * javax.accessibility.screen_reader_present and loads
1240 * the classes specified in javax.accessibility.assistive_technologies.
1242 private static void initAccessibility()
1244 AccessController.doPrivileged
1245 (new PrivilegedAction()
1247 public Object run()
1249 Properties props = new Properties();
1250 String sep = File.separator;
1252 // Try the user configuration.
1255 File propsFile = new File(System.getProperty("user.home") + sep
1256 + ".accessibility.properties");
1257 FileInputStream in = new FileInputStream(propsFile);
1258 props.load(in);
1259 in.close();
1261 catch (Exception ex)
1263 // User configuration not present, ignore.
1266 // Try the system configuration if there was no user configuration.
1267 if (props.size() == 0)
1271 File propsFile =
1272 new File(System.getProperty("gnu.classpath.home.url")
1273 + sep + "accessibility.properties");
1274 FileInputStream in = new FileInputStream(propsFile);
1275 props.load(in);
1276 in.close();
1278 catch (Exception ex)
1280 // System configuration not present, ignore.
1284 // Fetch the screen_magnifier_present property. Check systen properties
1285 // first, then fallback to the configuration file.
1286 String magPresent = SystemProperties.getProperty
1287 ("javax.accessibility.screen_magnifier_present");
1288 if (magPresent == null)
1290 magPresent = props.getProperty("screen_magnifier_present");
1291 if (magPresent != null)
1293 SystemProperties.setProperty
1294 ("javax.accessibility.screen_magnifier_present", magPresent);
1298 // Fetch the screen_reader_present property. Check systen properties
1299 // first, then fallback to the configuration file.
1300 String readerPresent = SystemProperties.getProperty
1301 ("javax.accessibility.screen_reader_present");
1302 if (readerPresent == null)
1304 readerPresent = props.getProperty("screen_reader_present");
1305 if (readerPresent != null)
1307 SystemProperties.setProperty
1308 ("javax.accessibility.screen_reader_present", readerPresent);
1312 // Fetch the list of classes to be loaded.
1313 String classes = SystemProperties.getProperty
1314 ("javax.accessibility.assistive_technologies");
1315 if (classes == null)
1317 classes = props.getProperty("assistive_technologies");
1318 if (classes != null)
1320 SystemProperties.setProperty
1321 ("javax.accessibility.assistive_technologies", classes);
1325 // Try to load the assisitive_technologies classes.
1326 if (classes != null)
1328 ClassLoader cl = ClassLoader.getSystemClassLoader();
1329 StringTokenizer tokenizer = new StringTokenizer(classes, ",");
1330 while (tokenizer.hasMoreTokens())
1332 String className = tokenizer.nextToken();
1335 Class atClass = cl.loadClass(className);
1336 atClass.newInstance();
1338 catch (ClassNotFoundException ex)
1340 AWTError err = new AWTError("Assistive Technology class not"
1341 + " found: " + className);
1342 err.initCause(ex);
1343 throw err;
1345 catch (InstantiationException ex)
1347 AWTError err =
1348 new AWTError("Assistive Technology class cannot be "
1349 + "instantiated: " + className);
1350 err.initCause(ex);
1351 throw err;
1353 catch (IllegalAccessException ex)
1355 AWTError err =
1356 new AWTError("Assistive Technology class cannot be "
1357 + "accessed: " + className);
1358 err.initCause(err);
1359 throw err;
1363 return null;
1369 } // class Toolkit