0001: /*
0002: * @(#)Toolkit.java 1.114 06/10/10
0003: *
0004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
0005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
0006: *
0007: * This program is free software; you can redistribute it and/or
0008: * modify it under the terms of the GNU General Public License version
0009: * 2 only, as published by the Free Software Foundation.
0010: *
0011: * This program is distributed in the hope that it will be useful, but
0012: * WITHOUT ANY WARRANTY; without even the implied warranty of
0013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0014: * General Public License version 2 for more details (a copy is
0015: * included at /legal/license.txt).
0016: *
0017: * You should have received a copy of the GNU General Public License
0018: * version 2 along with this work; if not, write to the Free Software
0019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
0020: * 02110-1301 USA
0021: *
0022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
0023: * Clara, CA 95054 or visit www.sun.com if you need additional
0024: * information or have any questions.
0025: *
0026: */
0027:
0028: /*
0029: * Warning :
0030: * Two versions of this file exist in this workspace.
0031: * One for Personal Basis, and one for Personal Profile.
0032: * Don't edit the wrong one !!!
0033: */
0034:
0035: package java.awt;
0036:
0037: import java.util.Properties;
0038: import java.util.Map;
0039: import java.util.WeakHashMap;
0040: import java.util.EventListener;
0041: import java.awt.im.InputMethodHighlight;
0042: import java.awt.image.ImageObserver;
0043: import java.awt.image.ImageProducer;
0044: import java.awt.image.ColorModel;
0045: import java.awt.datatransfer.Clipboard;
0046: import java.awt.event.*;
0047: import java.net.URL;
0048: import java.io.BufferedInputStream;
0049: import java.io.InputStream;
0050: import java.security.AccessController;
0051: import sun.security.action.GetPropertyAction;
0052: import sun.io.FileIO;
0053: import sun.io.FileIOFactory;
0054: import java.util.ArrayList;
0055:
0056: /* NOTE: It is no longer a requirement for <code>Toolkit</code> and AWT implementations to be peer based. However,
0057: the implementation of the <code>java.awt</code> components provided assume the toolkit is
0058: peer based. To create a toolkit to work with this implementation it is necessary to sub-class
0059: the <code>sun.awt.PeerBasedToolkit<code> class.*/
0060:
0061: /**
0062: * This class is the abstract superclass of all actual
0063: * implementations of the Abstract Window Toolkit. Subclasses of
0064: * <code>Toolkit</code> are used to bind the various components
0065: * to particular native toolkit implementations.
0066: * <p>
0067: * Most applications should not call any of the methods in this
0068: * class directly. The methods defined by <code>Toolkit</code> are
0069: * the "glue" that joins the platform-independent classes in the
0070: * <code>java.awt</code> package with their counterparts in
0071: * <code>java.awt.peer</code>. Some methods defined by
0072: * <code>Toolkit</code> query the native operating system directly.
0073: *
0074: * @version 1.102, 08/19/02
0075: * @author Sami Shaio
0076: * @author Arthur van Hoff
0077: * @author Fred Ecks
0078: * @since JDK1.0
0079: */
0080:
0081: public abstract class Toolkit {
0082: private AWTEventListener eventListener = null;
0083: private Map listener2SelectiveListener = new WeakHashMap();
0084: private AWTPermission listenToAllAWTEventsPermission = null;
0085:
0086: /**
0087: * Fills in the integer array that is supplied as an argument
0088: * with the current system color values.
0089: * <p>
0090: * This method is called by the method <code>updateSystemColors</code>
0091: * in the <code>SystemColor</code> class.
0092: * @param systemColors an integer array.
0093: * @since JDK1.1
0094: */
0095: protected void loadSystemColors(int[] systemColors) {
0096: }
0097:
0098: /**
0099: * Gets the size of the screen.
0100: * @return the size of this toolkit's screen, in pixels.
0101: * @since JDK1.0
0102: */
0103: public abstract Dimension getScreenSize();
0104:
0105: /**
0106: * Returns the screen resolution in dots-per-inch.
0107: * @return this toolkit's screen resolution, in dots-per-inch.
0108: * @since JDK1.0
0109: */
0110: public abstract int getScreenResolution();
0111:
0112: /**
0113: * Gets the insets of the screen.
0114: * @param gc a <code>GraphicsConfiguration</code>
0115: * @return the insets of this toolkit's screen, in pixels.
0116: * @exception HeadlessException if GraphicsEnvironment.isHeadless()
0117: * returns true
0118: * @see java.awt.GraphicsEnvironment#isHeadless
0119: * @since 1.4
0120: */
0121: public Insets getScreenInsets(GraphicsConfiguration gc)
0122: throws HeadlessException {
0123: if (this != Toolkit.getDefaultToolkit()) {
0124: return Toolkit.getDefaultToolkit().getScreenInsets(gc);
0125: } else {
0126: return new Insets(0, 0, 0, 0);
0127: }
0128: }
0129:
0130: /**
0131: * Determines the color model of this toolkit's screen.
0132: * <p>
0133: * <code>ColorModel</code> is an abstract class that
0134: * encapsulates the ability to translate between the
0135: * pixel values of an image and its red, green, blue,
0136: * and alpha components.
0137: * <p>
0138: * This toolkit method is called by the
0139: * <code>getColorModel</code> method
0140: * of the <code>Component</code> class.
0141: * @return the color model of this toolkit's screen.
0142: * @see java.awt.image.ColorModel
0143: * @see java.awt.Component#getColorModel
0144: * @since JDK1.0
0145: */
0146: public abstract ColorModel getColorModel();
0147:
0148: /**
0149: * Returns the names of the available fonts in this toolkit.<p>
0150: * For 1.1, the following font names are deprecated (the replacement
0151: * name follows):
0152: * <ul>
0153: * <li>TimesRoman (use Serif)
0154: * <li>Helvetica (use SansSerif)
0155: * <li>Courier (use Monospaced)
0156: * </ul><p>
0157: * The ZapfDingbats font is also deprecated in 1.1, but only as a
0158: * separate fontname. Unicode defines the ZapfDingbat characters
0159: * starting at \u2700, and as of 1.1 Java supports those characters.
0160: * @return the names of the available fonts in this toolkit.
0161: * @since JDK1.0
0162: */
0163: public abstract String[] getFontList();
0164:
0165: /**
0166: * Gets the screen metrics of the font.
0167: * @param font a font.
0168: * @return the screen metrics of the specified font in this toolkit.
0169: * @since JDK1.0
0170: */
0171: public abstract FontMetrics getFontMetrics(Font font);
0172:
0173: /**
0174: * Synchronizes this toolkit's graphics state. Some window systems
0175: * may do buffering of graphics events.
0176: * <p>
0177: * This method ensures that the display is up-to-date. It is useful
0178: * for animation.
0179: * @since JDK1.0
0180: */
0181: public abstract void sync();
0182:
0183: /**
0184: * The default toolkit.
0185: */
0186: private static Toolkit toolkit;
0187: // fix for 4187686 Several class objects are used for synchronization
0188: private static Object classLock = new Object();
0189:
0190: /**
0191: * Gets the default toolkit.
0192: * <p>
0193: * If there is a system property named <code>"awt.toolkit"</code>,
0194: * that property is treated as the name of a class that is a subclass
0195: * of <code>Toolkit</code>.
0196: * <p>
0197: * If the system property does not exist, then the default toolkit
0198: * used is <code>"sun.awt.motif.MToolkit"</code> for Solaris, and
0199: *<code>"sun.awt.gtk.GToolkit"</code> for Linux. Both of which are
0200: * implementations of Abstract Window Toolkits.
0201: * @return the default toolkit.
0202: * @exception AWTError if a toolkit could not be found, or
0203: * if one could not be accessed or instantiated.
0204: * @since JDK1.0
0205: */
0206: public static Toolkit getDefaultToolkit() {
0207: // fix for 4187686 Several class objects are used for synchronization
0208: synchronized (classLock) {
0209: if (toolkit == null) {
0210: // JDK 1.2 wraps this block of code with a Compiler.disable/enable
0211: // to turn off a JIT because toolkit initilization touches many
0212: // classes that are only touched once. If Personal Java uses a
0213: // JIT in the future that kind of wrapping would make sense here.
0214: java.security.AccessController
0215: .doPrivileged(new java.security.PrivilegedAction() {
0216: public Object run() {
0217: String nm = null;
0218: try {
0219: nm = System
0220: .getProperty("awt.toolkit");
0221: toolkit = (Toolkit) Class.forName(
0222: nm).newInstance();
0223: } catch (ClassNotFoundException e) {
0224: throw new AWTError(
0225: "Toolkit not found: " + nm);
0226: } catch (InstantiationException e) {
0227: throw new AWTError(
0228: "Could not instantiate Toolkit: "
0229: + nm);
0230: } catch (IllegalAccessException e) {
0231: throw new AWTError(
0232: "Could not access Toolkit: "
0233: + nm);
0234: }
0235: return null;
0236: }
0237: });
0238: }
0239: }
0240: return toolkit;
0241: }
0242:
0243: /**
0244: * Gets a Graphics object for the supplied Window. The graphics object
0245: * should be initialised with the foreground, background and font of
0246: * the window and should draw to the GraphicsDevice the Window's
0247: * GraphicsConfiguration belongs to. This method is package protected so
0248: * as to be spec compatiable. It should be abstract but this would cause
0249: * spec issues.
0250: */
0251: Graphics getGraphics(Window window) {
0252: throw new UnsupportedOperationException();
0253: }
0254:
0255: /**
0256: * Gets the GraphicsEnvironment for this toolkit. This is called from
0257: * GraphicsEnvironment.getLocalGraphicsEnvironment.
0258: */
0259: GraphicsEnvironment getLocalGraphicsEnvironment() {
0260: throw new UnsupportedOperationException();
0261: }
0262:
0263: /**
0264: * Returns an image which gets pixel data from the specified file,
0265: * whose format can be either GIF, JPEG or PNG.
0266: * The underlying toolkit attempts to resolve multiple requests
0267: * with the same filename to the same returned Image.
0268: * Since the mechanism required to facilitate this sharing of
0269: * Image objects may continue to hold onto images that are no
0270: * longer of use for an indefinate period of time, developers
0271: * are encouraged to implement their own caching of images by
0272: * using the createImage variant wherever available.
0273: * @param filename the name of a file containing pixel data
0274: * in a recognized file format.
0275: * @return an image which gets its pixel data from
0276: * the specified file.
0277: * @see #createImage(java.lang.String)
0278: */
0279: public abstract Image getImage(String filename);
0280:
0281: // PBP/PP [6262540]
0282: // Fix for J2SE bug 6262530
0283: /**
0284: * Returns an image which gets pixel data from the specified URL.
0285: * The pixel data referenced by the specified URL must be in one
0286: * of the following formats: GIF, JPEG or PNG.
0287: * The underlying toolkit attempts to resolve multiple requests
0288: * with the same URL to the same returned Image.
0289: * Since the mechanism required to facilitate this sharing of
0290: * Image objects may continue to hold onto images that are no
0291: * longer of use for an indefinate period of time, developers
0292: * are encouraged to implement their own caching of images by
0293: * using the createImage variant wherever available.
0294: *
0295: * <p>
0296: * <em>
0297: * This method will throw {@link SecurityException} if the
0298: * caller does not have the permission obtained from
0299: * <code>url.openConnection.getPermission()</code>.
0300: * For compatibility with pre-1.2 security managers, if the permission
0301: * is a {@link java.io.FilePermission} or a
0302: * {@link java.net.SocketPermission}, then the 1.1-style
0303: * <code>SecurityManager.checkXXX</code> methods are called instead of
0304: * {@link SecurityManager#checkPermission}.
0305: * </em>
0306: *
0307: * <!-- PBP/PP -->
0308: * @throws SecurityException If the caller does not have permission to
0309: * access this URL.
0310: *
0311: * @param url the URL to use in fetching the pixel data.
0312: * @return an image which gets its pixel data from
0313: * the specified URL.
0314: * @see #createImage(java.net.URL)
0315: */
0316: public abstract Image getImage(URL url);
0317:
0318: /**
0319: * Returns an image which gets pixel data from the specified file.
0320: * The returned Image is a new object which will not be shared
0321: * with any other caller of this method or its getImage variant.
0322: * @param filename the name of a file containing pixel data
0323: * in a recognized file format.
0324: * @return an image which gets its pixel data from
0325: * the specified file.
0326: * @see java.awt.Toolkit#getImage(java.lang.String)
0327: */
0328:
0329: public abstract Image createImage(String filename);
0330:
0331: // PBP/PP [6262540]
0332: // Fix for J2SE bug 6262530
0333: /**
0334: * Returns an image which gets pixel data from the specified URL.
0335: * The returned Image is a new object which will not be shared
0336: * with any other caller of this method or its getImage variant.
0337: *
0338: * <p>
0339: * <em>
0340: * This method will throw {@link SecurityException} if the
0341: * caller does not have the permission obtained from
0342: * <code>url.openConnection.getPermission()</code>.
0343: * For compatibility with pre-1.2 security managers, if the permission
0344: * is a {@link java.io.FilePermission} or a
0345: * {@link java.net.SocketPermission}, then the 1.1-style
0346: * <code>SecurityManager.checkXXX</code> methods are called instead of
0347: * {@link SecurityManager#checkPermission}.
0348: * </em>
0349: *
0350: * <!-- PBP/PP -->
0351: * @throws SecurityException If the caller does not have permission to
0352: * access this URL.
0353: *
0354: * @param url the URL to use in fetching the pixel data.
0355: * @return an image which gets its pixel data from
0356: * the specified URL.
0357: * @see java.awt.Toolkit#getImage(java.net.URL)
0358: */
0359:
0360: public abstract Image createImage(URL url);
0361:
0362: /**
0363: * Prepares an image for rendering.
0364: * <p>
0365: * If the values of the width and height arguments are both
0366: * <code>-1</code>, this method prepares the image for rendering
0367: * on the default screen; otherwise, this method prepares an image
0368: * for rendering on the default screen at the specified width and height.
0369: * <p>
0370: * The image data is downloaded asynchronously in another thread,
0371: * and an appropriately scaled screen representation of the image is
0372: * generated.
0373: * <p>
0374: * This method is called by components <code>prepareImage</code>
0375: * methods.
0376: * <p>
0377: * Information on the flags returned by this method can be found
0378: * with the definition of the <code>ImageObserver</code> interface.
0379:
0380: * @param image the image for which to prepare a
0381: * screen representation.
0382: * @param width the width of the desired screen
0383: * representation, or <code>-1</code>.
0384: * @param height the height of the desired screen
0385: * representation, or <code>-1</code>.
0386: * @param observer the <code>ImageObserver</code>
0387: * object to be notified as the
0388: * image is being prepared.
0389: * @return <code>true</code> if the image has already been
0390: * fully prepared; <code>false</code> otherwise.
0391: * @see java.awt.Component#prepareImage(java.awt.Image,
0392: * java.awt.image.ImageObserver)
0393: * @see java.awt.Component#prepareImage(java.awt.Image,
0394: * int, int, java.awt.image.ImageObserver)
0395: * @see java.awt.image.ImageObserver
0396: * @since JDK1.0
0397: */
0398: public abstract boolean prepareImage(Image image, int width,
0399: int height, ImageObserver observer);
0400:
0401: /**
0402: * Indicates the construction status of a specified image that is
0403: * being prepared for display.
0404: * <p>
0405: * If the values of the width and height arguments are both
0406: * <code>-1</code>, this method returns the construction status of
0407: * a screen representation of the specified image in this toolkit.
0408: * Otherwise, this method returns the construction status of a
0409: * scaled representation of the image at the specified width
0410: * and height.
0411: * <p>
0412: * This method does not cause the image to begin loading.
0413: * An application must call <code>prepareImage</code> to force
0414: * the loading of an image.
0415: * <p>
0416: * This method is called by the component's <code>checkImage</code>
0417: * methods.
0418: * <p>
0419: * Information on the flags returned by this method can be found
0420: * with the definition of the <code>ImageObserver</code> interface.
0421: * @param image the image whose status is being checked.
0422: * @param width the width of the scaled version whose status is
0423: * being checked, or <code>-1</code>.
0424: * @param height the height of the scaled version whose status
0425: * is being checked, or <code>-1</code>.
0426: * @param observer the <code>ImageObserver</code> object to be
0427: * notified as the image is being prepared.
0428: * @return the bitwise inclusive <strong>OR</strong> of the
0429: * <code>ImageObserver</code> flags for the
0430: * image data that is currently available.
0431: * @see java.awt.Toolkit#prepareImage(java.awt.Image,
0432: * int, int, java.awt.image.ImageObserver)
0433: * @see java.awt.Component#checkImage(java.awt.Image,
0434: * java.awt.image.ImageObserver)
0435: * @see java.awt.Component#checkImage(java.awt.Image,
0436: * int, int, java.awt.image.ImageObserver)
0437: * @see java.awt.image.ImageObserver
0438: * @since JDK1.0
0439: */
0440: public abstract int checkImage(Image image, int width, int height,
0441: ImageObserver observer);
0442:
0443: /**
0444: * Creates an image with the specified image producer.
0445: * @param producer the image producer to be used.
0446: * @return an image with the specified image producer.
0447: * @see java.awt.Image
0448: * @see java.awt.image.ImageProducer
0449: * @see java.awt.Component#createImage(java.awt.image.ImageProducer)
0450: * @since JDK1.0
0451: */
0452: public abstract Image createImage(ImageProducer producer);
0453:
0454: /**
0455: * Creates an image which decodes the image stored in the specified
0456: * byte array.
0457: * <p>
0458: * The data must be in some image format, such as GIF or JPEG,
0459: * that is supported by this toolkit.
0460: * @param imagedata an array of bytes, representing
0461: * image data in a supported image format.
0462: * @return an image.
0463: * @since JDK1.1
0464: */
0465: public Image createImage(byte[] imagedata) {
0466: return createImage(imagedata, 0, imagedata.length);
0467: }
0468:
0469: /**
0470: * Creates an image which decodes the image stored in the specified
0471: * byte array, and at the specified offset and length.
0472: * The data must be in some image format, such as GIF or JPEG,
0473: * that is supported by this toolkit.
0474: * @param imagedata an array of bytes, representing
0475: * image data in a supported image format.
0476: * @param imageoffset the offset of the beginning
0477: * of the data in the array.
0478: * @param imagelength the length of the data in the array.
0479: * @return an image.
0480: * @since JDK1.1
0481: */
0482: public abstract Image createImage(byte[] imagedata,
0483: int imageoffset, int imagelength);
0484:
0485: /**
0486: * Emits an audio beep.
0487: * @since JDK1.1
0488: */
0489: public abstract void beep();
0490:
0491: /**
0492: * Gets an instance of the system clipboard which interfaces
0493: * with clipboard facilities provided by the native platform.
0494: * <p>
0495: * This clipboard enables data transfer between Java programs
0496: * and native applications which use native clipboard facilities.
0497: * @return an instance of the system clipboard.
0498: * @see java.awt.datatransfer.Clipboard
0499: * @since JDK1.1
0500: */
0501: public abstract Clipboard getSystemClipboard();
0502:
0503: /**
0504: * Determines which modifier key is the appropriate accelerator
0505: * key for menu shortcuts.
0506: * <p>
0507: * Menu shortcuts, which are embodied in the
0508: * <code>MenuShortcut</code> class, are handled by the
0509: * <code>MenuBar</code> class.
0510: * <p>
0511: * By default, this method returns <code>Event.CTRL_MASK</code>.
0512: * Toolkit implementations should override this method if the
0513: * <b>Control</b> key isn't the correct key for accelerators.
0514: * @return the modifier mask on the <code>Event</code> class
0515: * that is used for menu shortcuts on this toolkit.
0516: * @see java.awt.MenuBar
0517: * @see java.awt.MenuShortcut
0518: * @since JDK1.1
0519: */
0520: public int getMenuShortcutKeyMask() {
0521: return Event.CTRL_MASK;
0522: }
0523:
0524: /* Support for I18N: any visible strings should be stored in
0525: * lib/awt.properties. The Properties list is stored here, so
0526: * that only one copy is maintained.
0527: */
0528: private static Properties properties;
0529: static {
0530: String sep = FileIO.separator;
0531: String filename = (String) AccessController
0532: .doPrivileged(new GetPropertyAction("java.home"))
0533: + sep + "lib" + sep + "awt.properties";
0534: FileIO propsFile = FileIOFactory.newInstance(filename);
0535: properties = new Properties();
0536: try {
0537: InputStream in = propsFile.getInputStream();
0538: properties.load(new BufferedInputStream(in));
0539: in.close();
0540: } catch (Exception e) {// No properties, defaults will be used.
0541: }
0542: }
0543:
0544: /**
0545: * Gets a property with the specified key and default.
0546: * This method returns defaultValue if the property is not found.
0547: */
0548: public static String getProperty(String key, String defaultValue) {
0549: String val = properties.getProperty(key);
0550: return (val == null) ? defaultValue : val;
0551: }
0552:
0553: /**
0554: * Get the application's or applet's EventQueue instance.
0555: * Depending on the Toolkit implementation, different EventQueues
0556: * may be returned for different applets. Applets should
0557: * therefore not assume that the EventQueue instance returned
0558: * by this method will be shared by other applets or the system.
0559: */
0560: public final EventQueue getSystemEventQueue() {
0561: SecurityManager security = System.getSecurityManager();
0562: if (security != null) {
0563: security.checkAwtEventQueueAccess();
0564: }
0565: return getSystemEventQueueImpl();
0566: }
0567:
0568: /*
0569: * Get the application's or applet's EventQueue instance, without
0570: * checking access. For security reasons, this can only be called
0571: * from a Toolkit subclass. Implementations wishing to modify
0572: * the default EventQueue support should subclass this method.
0573: */
0574: protected abstract EventQueue getSystemEventQueueImpl();
0575:
0576: /* Accessor method for use by AWT package routines. */
0577: static EventQueue getEventQueue() {
0578: return getDefaultToolkit().getSystemEventQueueImpl();
0579: }
0580:
0581: /**
0582: * Adds an AWTEventListener to receive all AWTEvents dispatched
0583: * system-wide that conform to the given <code>eventMask</code>.
0584: * <p>
0585: * First, if there is a security manager, its <code>checkPermission</code>
0586: * method is called with an
0587: * <code>AWTPermission("listenToAllAWTEvents")</code> permission.
0588: * This may result in a SecurityException.
0589: * <p>
0590: * <code>eventMask</code> is a bitmask of event types to receive.
0591: * It is constructed by bitwise OR-ing together the event masks
0592: * defined in <code>AWTEvent</code>.
0593: * <p>
0594: * Note: event listener use is not recommended for normal
0595: * application use, but are intended solely to support special
0596: * purpose facilities including support for accessibility,
0597: * event record/playback, and diagnostic tracing.
0598: *
0599: * If listener is null, no exception is thrown and no action is performed.
0600: *
0601: * @param listener the event listener.
0602: * @param eventMask the bitmask of event types to receive
0603: * @throws SecurityException
0604: * if a security manager exists and its
0605: * <code>checkPermission</code> method doesn't allow the operation.
0606: * @see java.awt.event.AWTEventListener
0607: * @see java.awt.Toolkit#addAWTEventListener
0608: * @see java.awt.AWTEvent
0609: * @see SecurityManager#checkPermission
0610: * @see java.awt.AWTPermission
0611: * @since 1.2
0612: */
0613: public void addAWTEventListener(AWTEventListener listener,
0614: long eventMask) {
0615: AWTEventListener localL = deProxyAWTEventListener(listener);
0616: if (localL == null) {
0617: return;
0618: }
0619: SecurityManager security = System.getSecurityManager();
0620: if (security != null) {
0621: if (listenToAllAWTEventsPermission == null) {
0622: listenToAllAWTEventsPermission = new AWTPermission(
0623: "listenToAllAWTEvents");
0624: }
0625: security.checkPermission(listenToAllAWTEventsPermission);
0626: }
0627: synchronized (this ) {
0628: SelectiveAWTEventListener selectiveListener = (SelectiveAWTEventListener) listener2SelectiveListener
0629: .get(localL);
0630: if (selectiveListener == null) {
0631: // Create a new selectiveListener.
0632: selectiveListener = new SelectiveAWTEventListener(
0633: localL, eventMask);
0634: listener2SelectiveListener.put(localL,
0635: selectiveListener);
0636: eventListener = ToolkitEventMulticaster.add(
0637: eventListener, selectiveListener);
0638: }
0639: // OR the eventMask into the selectiveListener's event mask.
0640: selectiveListener.orEventMasks(eventMask);
0641: }
0642: }
0643:
0644: /**
0645: * Removes an AWTEventListener from receiving dispatched AWTEvents.
0646: * <p>
0647: * First, if there is a security manager, its <code>checkPermission</code>
0648: * method is called with an
0649: * <code>AWTPermission("listenToAllAWTEvents")</code> permission.
0650: * This may result in a SecurityException.
0651: * <p>
0652: * Note: event listener use is not recommended for normal
0653: * application use, but are intended solely to support special
0654: * purpose facilities including support for accessibility,
0655: * event record/playback, and diagnostic tracing.
0656: *
0657: * If listener is null, no exception is thrown and no action is performed.
0658: *
0659: * @param listener the event listener.
0660: * @throws SecurityException
0661: * if a security manager exists and its
0662: * <code>checkPermission</code> method doesn't allow the operation.
0663: * @see java.awt.event.AWTEventListener
0664: * @see java.awt.Toolkit#addAWTEventListener
0665: * @see java.awt.AWTEvent
0666: * @see SecurityManager#checkPermission
0667: * @see java.awt.AWTPermission
0668: * @since 1.2
0669: */
0670: public void removeAWTEventListener(AWTEventListener listener) {
0671: AWTEventListener localL = deProxyAWTEventListener(listener);
0672:
0673: if (listener == null) {
0674: return;
0675: }
0676: SecurityManager security = System.getSecurityManager();
0677: if (security != null) {
0678: if (listenToAllAWTEventsPermission == null) {
0679: listenToAllAWTEventsPermission = new AWTPermission(
0680: "listenToAllAWTEvents");
0681: }
0682: security.checkPermission(listenToAllAWTEventsPermission);
0683: }
0684: synchronized (this ) {
0685: SelectiveAWTEventListener selectiveListener = (SelectiveAWTEventListener) listener2SelectiveListener
0686: .get(localL);
0687: if (selectiveListener != null)
0688: listener2SelectiveListener.remove(localL);
0689: eventListener = ToolkitEventMulticaster.remove(
0690: eventListener, (selectiveListener == null) ? localL
0691: : selectiveListener);
0692: }
0693: }
0694:
0695: /*
0696: * Extracts a "pure" AWTEventListener from a AWTEventListenerProxy,
0697: * if the listener is proxied.
0698: */
0699: static private AWTEventListener deProxyAWTEventListener(
0700: AWTEventListener l) {
0701: AWTEventListener localL = l;
0702:
0703: if (localL == null) {
0704: return null;
0705: }
0706: // if user passed in a AWTEventListenerProxy object, extract
0707: // the listener
0708: if (l instanceof AWTEventListenerProxy) {
0709: localL = (AWTEventListener) ((AWTEventListenerProxy) l)
0710: .getListener();
0711: }
0712: return localL;
0713: }
0714:
0715: /**
0716: * Returns an array of all the <code>AWTEventListener</code>s
0717: * registered on this toolkit. Listeners can be returned
0718: * within <code>AWTEventListenerProxy</code> objects, which also contain
0719: * the event mask for the given listener.
0720: * Note that listener objects
0721: * added multiple times appear only once in the returned array.
0722: *
0723: * @return all of the <code>AWTEventListener</code>s or an empty
0724: * array if no listeners are currently registered
0725: * @throws SecurityException
0726: * if a security manager exists and its
0727: * <code>checkPermission</code> method doesn't allow the operation.
0728: * @see #addAWTEventListener
0729: * @see #removeAWTEventListener
0730: * @see SecurityManager#checkPermission
0731: * @see java.awt.AWTEvent
0732: * @see java.awt.AWTPermission
0733: * @see java.awt.event.AWTEventListener
0734: * @see java.awt.event.AWTEventListenerProxy
0735: * @since 1.4
0736: */
0737: public AWTEventListener[] getAWTEventListeners() {
0738: //6242260
0739: SecurityManager security = System.getSecurityManager();
0740: if (security != null) {
0741: if (listenToAllAWTEventsPermission == null) {
0742: listenToAllAWTEventsPermission = new AWTPermission(
0743: "listenToAllAWTEvents");
0744: }
0745: security.checkPermission(listenToAllAWTEventsPermission);
0746: }
0747: //6242260
0748:
0749: synchronized (this ) {
0750: EventListener[] la = ToolkitEventMulticaster.getListeners(
0751: eventListener, AWTEventListener.class);
0752:
0753: AWTEventListener[] ret = new AWTEventListener[la.length];
0754: for (int i = 0; i < la.length; i++) {
0755: SelectiveAWTEventListener sael = (SelectiveAWTEventListener) la[i];
0756: AWTEventListener tempL = sael.getListener();
0757: //assert tempL is not an AWTEventListenerProxy - we should
0758: // have weeded them all out
0759: // don't want to wrap a proxy inside a proxy
0760: ret[i] = new AWTEventListenerProxy(sael.getEventMask(),
0761: tempL);
0762: }
0763: return ret;
0764: }
0765: }
0766:
0767: /**
0768: * Returns an array of all the <code>AWTEventListener</code>s
0769: * registered on this toolkit which listen to all of the event
0770: * types indicates in the <code>eventMask</code> argument.
0771: * Listeners can be returned
0772: * within <code>AWTEventListenerProxy</code> objects, which also contain
0773: * the event mask for the given listener.
0774: * Note that listener objects
0775: * added multiple times appear only once in the returned array.
0776: *
0777: * @param eventMask the bitmask of event types to listen for
0778: * @return all of the <code>AWTEventListener</code>s registered
0779: * on this toolkit for the specified
0780: * event types, or an empty array if no such listeners
0781: * are currently registered
0782: * @throws SecurityException
0783: * if a security manager exists and its
0784: * <code>checkPermission</code> method doesn't allow the operation.
0785: * @see #addAWTEventListener
0786: * @see #removeAWTEventListener
0787: * @see SecurityManager#checkPermission
0788: * @see java.awt.AWTEvent
0789: * @see java.awt.AWTPermission
0790: * @see java.awt.event.AWTEventListener
0791: * @see java.awt.event.AWTEventListenerProxy
0792: * @since 1.4
0793: */
0794: public AWTEventListener[] getAWTEventListeners(long eventMask) {
0795: //6242260
0796: SecurityManager security = System.getSecurityManager();
0797: if (security != null) {
0798: if (listenToAllAWTEventsPermission == null) {
0799: listenToAllAWTEventsPermission = new AWTPermission(
0800: "listenToAllAWTEvents");
0801: }
0802: security.checkPermission(listenToAllAWTEventsPermission);
0803: }
0804: //6242260
0805:
0806: synchronized (this ) {
0807: EventListener[] la = ToolkitEventMulticaster.getListeners(
0808: eventListener, AWTEventListener.class);
0809:
0810: java.util.List list = new ArrayList(la.length);
0811:
0812: for (int i = 0; i < la.length; i++) {
0813: SelectiveAWTEventListener sael = (SelectiveAWTEventListener) la[i];
0814: if ((sael.getEventMask() & eventMask) == eventMask) {
0815: //AWTEventListener tempL = sael.getListener();
0816: list.add(new AWTEventListenerProxy(sael
0817: .getEventMask(), sael.getListener()));
0818: }
0819: }
0820: return (AWTEventListener[]) list
0821: .toArray(new AWTEventListener[0]);
0822: }
0823: }
0824:
0825: /*
0826: * This method notifies any AWTEventListeners that an event
0827: * is about to be dispatched.
0828: *
0829: * @param theEvent the event which will be dispatched.
0830: */
0831: void notifyAWTEventListeners(AWTEvent theEvent) {
0832: if (eventListener != null) {
0833: synchronized (this ) {
0834: if (eventListener != null)
0835: eventListener.eventDispatched(theEvent);
0836: }
0837: }
0838: }
0839:
0840: static private class ToolkitEventMulticaster extends
0841: AWTEventMulticaster implements AWTEventListener {
0842: // Implementation cloned from AWTEventMulticaster.
0843:
0844: ToolkitEventMulticaster(AWTEventListener a, AWTEventListener b) {
0845: super (a, b);
0846: }
0847:
0848: static AWTEventListener add(AWTEventListener a,
0849: AWTEventListener b) {
0850: if (a == null)
0851: return b;
0852: if (b == null)
0853: return a;
0854: return new ToolkitEventMulticaster(a, b);
0855: }
0856:
0857: static AWTEventListener remove(AWTEventListener l,
0858: AWTEventListener oldl) {
0859: return (AWTEventListener) removeInternal(l, oldl);
0860: }
0861:
0862: // #4178589: must overload remove(EventListener) to call our add()
0863: // instead of the static addInternal() so we allocate a
0864: // ToolkitEventMulticaster instead of an AWTEventMulticaster.
0865: // Note: this method is called by AWTEventListener.removeInternal(),
0866: // so its method signature must match AWTEventListener.remove().
0867: protected EventListener remove(EventListener oldl) {
0868: if (oldl == a)
0869: return b;
0870: if (oldl == b)
0871: return a;
0872: AWTEventListener a2 = (AWTEventListener) removeInternal(a,
0873: oldl);
0874: AWTEventListener b2 = (AWTEventListener) removeInternal(b,
0875: oldl);
0876: if (a2 == a && b2 == b) {
0877: return this ; // it's not here
0878: }
0879: return add(a2, b2);
0880: }
0881:
0882: public void eventDispatched(AWTEvent event) {
0883: ((AWTEventListener) a).eventDispatched(event);
0884: ((AWTEventListener) b).eventDispatched(event);
0885: }
0886: }
0887:
0888: private class SelectiveAWTEventListener implements AWTEventListener {
0889: AWTEventListener listener;
0890: private long eventMask;
0891: static final int LONG_BITS = 64;
0892: // This array contains the number of times to call the eventlistener
0893: // for each event type.
0894: int[] calls = new int[LONG_BITS];
0895:
0896: public AWTEventListener getListener() {
0897: return listener;
0898: }
0899:
0900: public long getEventMask() {
0901: return eventMask;
0902: }
0903:
0904: public int[] getCalls() {
0905: return calls;
0906: }
0907:
0908: public void orEventMasks(long mask) {
0909: eventMask |= mask;
0910: // For each event bit set in mask, increment its call count.
0911: for (int i = 0; i < LONG_BITS; i++) {
0912: // If no bits are set, break out of loop.
0913: if (mask == 0) {
0914: break;
0915: }
0916: if ((mask & 1L) != 0) { // Always test bit 0.
0917: calls[i]++;
0918: }
0919: mask >>>= 1; // Right shift, fill with zeros on left.
0920: }
0921: }
0922:
0923: SelectiveAWTEventListener(AWTEventListener l, long mask) {
0924: listener = l;
0925: eventMask = mask;
0926: }
0927:
0928: public void eventDispatched(AWTEvent event) {
0929: long eventBit = 0; // Used to save the bit of the event type.
0930: if (((eventBit = eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0
0931: && event.id >= ComponentEvent.COMPONENT_FIRST && event.id <= ComponentEvent.COMPONENT_LAST)
0932: || ((eventBit = eventMask
0933: & AWTEvent.CONTAINER_EVENT_MASK) != 0
0934: && event.id >= ContainerEvent.CONTAINER_FIRST && event.id <= ContainerEvent.CONTAINER_LAST)
0935: || ((eventBit = eventMask
0936: & AWTEvent.FOCUS_EVENT_MASK) != 0
0937: && event.id >= FocusEvent.FOCUS_FIRST && event.id <= FocusEvent.FOCUS_LAST)
0938: || ((eventBit = eventMask & AWTEvent.KEY_EVENT_MASK) != 0
0939: && event.id >= KeyEvent.KEY_FIRST && event.id <= KeyEvent.KEY_LAST)
0940: || ((eventBit = eventMask
0941: & AWTEvent.MOUSE_MOTION_EVENT_MASK) != 0 && (event.id == MouseEvent.MOUSE_MOVED || event.id == MouseEvent.MOUSE_DRAGGED))
0942: || ((eventBit = eventMask
0943: & AWTEvent.MOUSE_EVENT_MASK) != 0
0944: && event.id != MouseEvent.MOUSE_MOVED
0945: && event.id != MouseEvent.MOUSE_DRAGGED
0946: && event.id >= MouseEvent.MOUSE_FIRST && event.id <= MouseEvent.MOUSE_LAST)
0947: || ((eventBit = eventMask
0948: & AWTEvent.WINDOW_EVENT_MASK) != 0
0949: && event.id >= WindowEvent.WINDOW_FIRST && event.id <= WindowEvent.WINDOW_LAST)
0950: || ((eventBit = eventMask
0951: & AWTEvent.ACTION_EVENT_MASK) != 0
0952: && event.id >= ActionEvent.ACTION_FIRST && event.id <= ActionEvent.ACTION_LAST)
0953: || ((eventBit = eventMask
0954: & AWTEvent.ADJUSTMENT_EVENT_MASK) != 0
0955: && event.id >= AdjustmentEvent.ADJUSTMENT_FIRST && event.id <= AdjustmentEvent.ADJUSTMENT_LAST)
0956: || ((eventBit = eventMask
0957: & AWTEvent.ITEM_EVENT_MASK) != 0
0958: && event.id >= ItemEvent.ITEM_FIRST && event.id <= ItemEvent.ITEM_LAST)
0959: || ((eventBit = eventMask
0960: & AWTEvent.TEXT_EVENT_MASK) != 0
0961: && event.id >= TextEvent.TEXT_FIRST && event.id <= TextEvent.TEXT_LAST)
0962:
0963: /*|| ((eventBit = eventMask & AWTEvent.INPUT_METHOD_EVENT_MASK) != 0 &&
0964: event.id >= InputMethodEvent.INPUT_METHOD_FIRST &&
0965: event.id <= InputMethodEvent.INPUT_METHOD_LAST)
0966: || ((eventBit = eventMask & AWTEvent.PAINT_EVENT_MASK) != 0 &&
0967: event.id >= PaintEvent.PAINT_FIRST &&
0968: event.id <= PaintEvent.PAINT_LAST)
0969: || ((eventBit = eventMask & AWTEvent.INVOCATION_EVENT_MASK) != 0 &&
0970: event.id >= InvocationEvent.INVOCATION_FIRST &&
0971: event.id <= InvocationEvent.INVOCATION_LAST)
0972: || ((eventBit = eventMask & AWTEvent.HIERARCHY_EVENT_MASK) != 0 &&
0973: event.id == HierarchyEvent.HIERARCHY_CHANGED)
0974: || ((eventBit = eventMask & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) != 0 &&
0975: (event.id == HierarchyEvent.ANCESTOR_MOVED ||
0976: event.id == HierarchyEvent.ANCESTOR_RESIZED))*/) {
0977: // Get the index of the call count for this event type.
0978: int ci = (int) (Math.log(eventBit) / Math.log(2));
0979: // Call the listener as many times as it was added for this
0980: // event type.
0981: for (int i = 0; i < calls[ci]; i++) {
0982: listener.eventDispatched(event);
0983: }
0984: }
0985: }
0986: }
0987:
0988: /**
0989: * Returns a map of visual attributes for the abstract level description
0990: * of the given input method highlight, or null if no mapping is found.
0991: * The style field of the input method highlight is ignored. The map
0992: * returned is unmodifiable.
0993: * @param highlight input method highlight
0994: * @return style attribute map, or <code>null</code>
0995: * @exception HeadlessException if
0996: * <code>GraphicsEnvironment.isHeadless</code> returns true
0997: * @see java.awt.GraphicsEnvironment#isHeadless
0998: * @since 1.3
0999: */
1000: /*
1001: public abstract Map mapInputMethodHighlight(
1002: InputMethodHighlight highlight) throws HeadlessException;
1003: */
1004:
1005: /**
1006: * Returns whether Toolkit supports this state for
1007: * <code>Frame</code>s. This method tells whether the <em>UI
1008: * concept</em> of iconification is
1009: * supported. It will always return false for any state other than
1010: * like <code>Frame.NORMAL or Frame.ICONIFIED</code>.
1011: *
1012: * @param state one of named frame state constants.
1013: * @return <code>true</code> is this frame state is supported by
1014: * this Toolkit implementation, <code>false</code> otherwise.
1015: * @exception HeadlessException
1016: * if <code>GraphicsEnvironment.isHeadless()</code>
1017: * returns <code>true</code>.
1018: */
1019: public boolean isFrameStateSupported(int state)
1020: throws HeadlessException {
1021: if (this != Toolkit.getDefaultToolkit()) {
1022: return Toolkit.getDefaultToolkit().isFrameStateSupported(
1023: state);
1024: } else {
1025: return (state == Frame.NORMAL); // others are not guaranteed
1026: }
1027: }
1028: }
|