- /*
- * @(#)Component.java 1.357 03/04/14
- *
- * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
- * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
- */
- package java.awt;
- import java.io.PrintStream;
- import java.io.PrintWriter;
- import java.util.Vector;
- import java.util.Locale;
- import java.util.EventListener;
- import java.util.Iterator;
- import java.util.HashSet;
- import java.util.Set;
- import java.util.Collections;
- import java.awt.peer.ComponentPeer;
- import java.awt.peer.ContainerPeer;
- import java.awt.peer.LightweightPeer;
- import java.awt.image.BufferStrategy;
- import java.awt.image.ImageObserver;
- import java.awt.image.ImageProducer;
- import java.awt.image.ColorModel;
- import java.awt.image.VolatileImage;
- import java.awt.event.*;
- import java.awt.datatransfer.Transferable;
- import java.awt.dnd.DnDConstants;
- import java.awt.dnd.DragSource;
- import java.awt.dnd.DragSourceContext;
- import java.awt.dnd.DragSourceListener;
- import java.awt.dnd.InvalidDnDOperationException;
- import java.io.Serializable;
- import java.io.ObjectOutputStream;
- import java.io.ObjectInputStream;
- import java.io.IOException;
- import java.beans.PropertyChangeListener;
- import java.awt.event.InputMethodListener;
- import java.awt.event.InputMethodEvent;
- import java.awt.im.InputContext;
- import java.awt.im.InputMethodRequests;
- import java.awt.dnd.DropTarget;
- import javax.accessibility.*;
- import java.awt.GraphicsConfiguration;
- import javax.accessibility.*;
- import sun.security.action.GetPropertyAction;
- import sun.awt.AppContext;
- import sun.awt.SunToolkit;
- import sun.awt.ConstrainableGraphics;
- import sun.awt.DebugHelper;
- import sun.awt.WindowClosingListener;
- import sun.awt.WindowClosingSupport;
- import sun.awt.GlobalCursorManager;
- import sun.awt.dnd.SunDropTargetEvent;
- import sun.awt.im.CompositionArea;
- /**
- * A <em>component</em> is an object having a graphical representation
- * that can be displayed on the screen and that can interact with the
- * user. Examples of components are the buttons, checkboxes, and scrollbars
- * of a typical graphical user interface. <p>
- * The <code>Component</code> class is the abstract superclass of
- * the nonmenu-related Abstract Window Toolkit components. Class
- * <code>Component</code> can also be extended directly to create a
- * lightweight component. A lightweight component is a component that is
- * not associated with a native opaque window.
- * <p>
- * <h3>Serialization</h3>
- * It is important to note that only AWT listeners which conform
- * to the <code>Serializable</code> protocol will be saved when
- * the object is stored. If an AWT object has listeners that
- * aren't marked serializable, they will be dropped at
- * <code>writeObject</code> time. Developers will need, as always,
- * to consider the implications of making an object serializable.
- * One situation to watch out for is this:
- * <pre>
- * import java.awt.*;
- * import java.awt.event.*;
- * import java.io.Serializable;
- *
- * class MyApp implements ActionListener, Serializable
- * {
- * BigObjectThatShouldNotBeSerializedWithAButton bigOne;
- * Button aButton = new Button();
- *
- * MyApp()
- * {
- * // Oops, now aButton has a listener with a reference
- * // to bigOne!
- * aButton.addActionListener(this);
- * }
- *
- * public void actionPerformed(ActionEvent e)
- * {
- * System.out.println("Hello There");
- * }
- * }
- * </pre>
- * In this example, serializing <code>aButton</code> by itself
- * will cause <code>MyApp</code> and everything it refers to
- * to be serialized as well. The problem is that the listener
- * is serializable by coincidence, not by design. To separate
- * the decisions about <code>MyApp</code> and the
- * <code>ActionListener</code> being serializable one can use a
- * nested class, as in the following example:
- * <pre>
- * import java.awt.*;
- * import java.awt.event.*;
- * import java.io.Serializable;
- *
- * class MyApp java.io.Serializable
- * {
- * BigObjectThatShouldNotBeSerializedWithAButton bigOne;
- * Button aButton = new Button();
- *
- * class MyActionListener implements ActionListener
- * {
- * public void actionPerformed(ActionEvent e)
- * {
- * System.out.println("Hello There");
- * }
- * }
- *
- * MyApp()
- * {
- * aButton.addActionListener(new MyActionListener());
- * }
- * }
- * </pre>
- * <p>
- * <b>Note</b>: For more information on the paint mechanisms utilitized
- * by AWT and Swing, including information on how to write the most
- * efficient painting code, see
- * <a href="http://java.sun.com/products/jfc/tsc/articles/painting/index.html">Painting in AWT and Swing</a>.
- *
- * @version 1.357, 04/14/03
- * @author Arthur van Hoff
- * @author Sami Shaio
- */
- public abstract class Component implements ImageObserver, MenuContainer,
- Serializable
- {
- /**
- * The peer of the component. The peer implements the component's
- * behavior. The peer is set when the <code>Component</code> is
- * added to a container that also is a peer.
- * @see #addNotify
- * @see #removeNotify
- */
- transient ComponentPeer peer;
- /**
- * The parent of the object. It may be <code>null</code>
- * for top-level components.
- * @see #getParent
- */
- transient Container parent;
- /**
- * The <code>AppContext</code> of the component. This is set in
- * the constructor and never changes.
- */
- transient AppContext appContext;
- /**
- * The x position of the component in the parent's coordinate system.
- *
- * @serial
- * @see #getLocation
- */
- int x;
- /**
- * The y position of the component in the parent's coordinate system.
- *
- * @serial
- * @see #getLocation
- */
- int y;
- /**
- * The width of the component.
- *
- * @serial
- * @see #getSize
- */
- int width;
- /**
- * The height of the component.
- *
- * @serial
- * @see #getSize
- */
- int height;
- /**
- * The foreground color for this component.
- * <code>foreground</code> can be <code>null</code>.
- *
- * @serial
- * @see #getForeground
- * @see #setForeground
- */
- Color foreground;
- /**
- * The background color for this component.
- * <code>background</code> can be <code>null</code>.
- *
- * @serial
- * @see #getBackground
- * @see #setBackground
- */
- Color background;
- /**
- * The font used by this component.
- * The <code>font</code> can be <code>null</code>.
- *
- * @serial
- * @see #getFont
- * @see #setFont
- */
- Font font;
- /**
- * The font which the peer is currently using.
- * (<code>null</code> if no peer exists.)
- */
- Font peerFont;
- /**
- * The cursor displayed when pointer is over this component.
- * This value can be <code>null</code>.
- *
- * @serial
- * @see #getCursor
- * @see #setCursor
- */
- Cursor cursor;
- /**
- * The locale for the component.
- *
- * @serial
- * @see #getLocale
- * @see #setLocale
- */
- Locale locale;
- /**
- * A reference to a <code>GraphicsConfiguration</code> object
- * used to describe the characteristics of a graphics
- * destination.
- * This value can be <code>null</code>.
- *
- * @since 1.3
- * @serial
- * @see GraphicsConfiguration
- * @see #getGraphicsConfiguration
- */
- transient GraphicsConfiguration graphicsConfig = null;
- /**
- * A reference to a <code>BufferStrategy</code> object
- * used to manipulate the buffers on this component.
- *
- * @since 1.4
- * @see java.awt.image.BufferStrategy
- * @see #getBufferStrategy()
- */
- transient BufferStrategy bufferStrategy = null;
- /**
- * True when the object should ignore all repaint events.
- *
- * @since 1.4
- * @serial
- * @see #setIgnoreRepaint
- * @see #getIgnoreRepaint
- */
- boolean ignoreRepaint = false;
- /**
- * True when the object is visible. An object that is not
- * visible is not drawn on the screen.
- *
- * @serial
- * @see #isVisible
- * @see #setVisible
- */
- boolean visible = true;
- /**
- * True when the object is enabled. An object that is not
- * enabled does not interact with the user.
- *
- * @serial
- * @see #isEnabled
- * @see #setEnabled
- */
- boolean enabled = true;
- /**
- * True when the object is valid. An invalid object needs to
- * be layed out. This flag is set to false when the object
- * size is changed.
- *
- * @serial
- * @see #isValid
- * @see #validate
- * @see #invalidate
- */
- boolean valid = false;
- /**
- * The <code>DropTarget</code> associated with this component.
- *
- * @since 1.2
- * @serial
- * @see #setDropTarget
- * @see #getDropTarget
- */
- DropTarget dropTarget;
- /**
- * @serial
- * @see #add
- */
- Vector popups;
- /**
- * A component's name.
- * This field can be <code>null</code>.
- *
- * @serial
- * @see #getName
- * @see #setName(String)
- */
- private String name;
- /**
- * A bool to determine whether the name has
- * been set explicitly. <code>nameExplicitlySet</code> will
- * be false if the name has not been set and
- * true if it has.
- *
- * @serial
- * @see #getName
- * @see #setName(String)
- */
- private boolean nameExplicitlySet = false;
- /**
- * Indicates whether this Component can be focused.
- *
- * @serial
- * @see #setFocusable
- * @see #isFocusable
- * @since 1.4
- */
- private boolean focusable = true;
- private static final int FOCUS_TRAVERSABLE_UNKNOWN = 0;
- private static final int FOCUS_TRAVERSABLE_DEFAULT = 1;
- private static final int FOCUS_TRAVERSABLE_SET = 2;
- /**
- * Tracks whether this Component is relying on default focus travesability.
- *
- * @serial
- * @since 1.4
- */
- private int isFocusTraversableOverridden = FOCUS_TRAVERSABLE_UNKNOWN;
- /**
- * The focus traversal keys. These keys will generate focus traversal
- * behavior for Components for which focus traversal keys are enabled. If a
- * value of null is specified for a traversal key, this Component inherits
- * that traversal key from its parent. If all ancestors of this Component
- * have null specified for that traversal key, then the current
- * KeyboardFocusManager's default traversal key is used.
- *
- * @serial
- * @see #setFocusTraversalKeys
- * @see #getFocusTraversalKeys
- * @since 1.4
- */
- Set[] focusTraversalKeys;
- private static final String[] focusTraversalKeyPropertyNames = {
- "forwardFocusTraversalKeys",
- "backwardFocusTraversalKeys",
- "upCycleFocusTraversalKeys",
- "downCycleFocusTraversalKeys"
- };
- /**
- * Indicates whether focus traversal keys are enabled for this Component.
- * Components for which focus traversal keys are disabled receive key
- * events for focus traversal keys. Components for which focus traversal
- * keys are enabled do not see these events; instead, the events are
- * automatically converted to traversal operations.
- *
- * @serial
- * @see #setFocusTraversalKeysEnabled
- * @see #getFocusTraversalKeysEnabled
- * @since 1.4
- */
- private boolean focusTraversalKeysEnabled = true;
- /**
- * The locking object for AWT component-tree and layout operations.
- *
- * @see #getTreeLock
- */
- static final Object LOCK = new AWTTreeLock();
- static class AWTTreeLock {}
- /**
- * Internal, cached size information.
- * (This field perhaps should have been transient).
- *
- * @serial
- */
- Dimension minSize;
- /**
- * Internal, cached size information
- * (This field perhaps should have been transient).
- *
- * @serial
- */
- Dimension prefSize;
- /**
- * The orientation for this component.
- * @see #getComponentOrientation
- * @see #setComponentOrientation
- */
- transient ComponentOrientation componentOrientation
- = ComponentOrientation.UNKNOWN;
- /**
- * <code>newEventsOnly</code> will be true if the event is
- * one of the event types enabled for the component.
- * It will then allow for normal processing to
- * continue. If it is false the event is passed
- * to the component's parent and up the ancestor
- * tree until the event has been consumed.
- *
- * @serial
- * @see #dispatchEvent
- */
- boolean newEventsOnly = false;
- transient ComponentListener componentListener;
- transient FocusListener focusListener;
- transient HierarchyListener hierarchyListener;
- transient HierarchyBoundsListener hierarchyBoundsListener;
- transient KeyListener keyListener;
- transient MouseListener mouseListener;
- transient MouseMotionListener mouseMotionListener;
- transient MouseWheelListener mouseWheelListener;
- transient InputMethodListener inputMethodListener;
- transient RuntimeException windowClosingException = null;
- /** Internal, constants for serialization */
- final static String actionListenerK = "actionL";
- final static String adjustmentListenerK = "adjustmentL";
- final static String componentListenerK = "componentL";
- final static String containerListenerK = "containerL";
- final static String focusListenerK = "focusL";
- final static String itemListenerK = "itemL";
- final static String keyListenerK = "keyL";
- final static String mouseListenerK = "mouseL";
- final static String mouseMotionListenerK = "mouseMotionL";
- final static String mouseWheelListenerK = "mouseWheelL";
- final static String textListenerK = "textL";
- final static String ownedWindowK = "ownedL";
- final static String windowListenerK = "windowL";
- final static String inputMethodListenerK = "inputMethodL";
- final static String hierarchyListenerK = "hierarchyL";
- final static String hierarchyBoundsListenerK = "hierarchyBoundsL";
- final static String windowStateListenerK = "windowStateL";
- final static String windowFocusListenerK = "windowFocusL";
- /**
- * The <code>eventMask</code> is ONLY set by subclasses via
- * <code>enableEvents</code>.
- * The mask should NOT be set when listeners are registered
- * so that we can distinguish the difference between when
- * listeners request events and subclasses request them.
- * One bit is used to indicate whether input methods are
- * enabled; this bit is set by <code>enableInputMethods</code> and is
- * on by default.
- *
- * @serial
- * @see #enableInputMethods
- * @see AWTEvent
- */
- long eventMask = AWTEvent.INPUT_METHODS_ENABLED_MASK;
- private static final DebugHelper dbg = DebugHelper.create(Component.class);
- /**
- * Static properties for incremental drawing.
- * @see #imageUpdate
- */
- static boolean isInc;
- static int incRate;
- static {
- /* ensure that the necessary native libraries are loaded */
- Toolkit.loadLibraries();
- /* initialize JNI field and method ids */
- if (!GraphicsEnvironment.isHeadless()) {
- initIDs();
- }
- String s = (String) java.security.AccessController.doPrivileged(
- new GetPropertyAction("awt.image.incrementaldraw"));
- isInc = (s == null || s.equals("true"));
- s = (String) java.security.AccessController.doPrivileged(
- new GetPropertyAction("awt.image.redrawrate"));
- incRate = (s != null) ? Integer.parseInt(s) : 100;
- }
- /**
- * Ease-of-use constant for <code>getAlignmentY()</code>.
- * Specifies an alignment to the top of the component.
- * @see #getAlignmentY
- */
- public static final float TOP_ALIGNMENT = 0.0f;
- /**
- * Ease-of-use constant for <code>getAlignmentY</code> and
- * <code>getAlignmentX</code>. Specifies an alignment to
- * the center of the component
- * @see #getAlignmentX
- * @see #getAlignmentY
- */
- public static final float CENTER_ALIGNMENT = 0.5f;
- /**
- * Ease-of-use constant for <code>getAlignmentY</code>.
- * Specifies an alignment to the bottom of the component.
- * @see #getAlignmentY
- */
- public static final float BOTTOM_ALIGNMENT = 1.0f;
- /**
- * Ease-of-use constant for <code>getAlignmentX</code>.
- * Specifies an alignment to the left side of the component.
- * @see #getAlignmentX
- */
- public static final float LEFT_ALIGNMENT = 0.0f;
- /**
- * Ease-of-use constant for <code>getAlignmentX</code>.
- * Specifies an alignment to the right side of the component.
- * @see #getAlignmentX
- */
- public static final float RIGHT_ALIGNMENT = 1.0f;
- /*
- * JDK 1.1 serialVersionUID
- */
- private static final long serialVersionUID = -7644114512714619750L;
- /**
- * If any <code>PropertyChangeListeners</code> have been registered,
- * the <code>changeSupport</code> field describes them.
- *
- * @serial
- * @since 1.2
- * @see #addPropertyChangeListener
- * @see #removePropertyChangeListener
- * @see #firePropertyChange
- */
- private java.beans.PropertyChangeSupport changeSupport;
- boolean isPacked = false;
- /**
- * This object is used as a key for internal hashtables.
- */
- transient private Object privateKey = new Object();
- /**
- * Constructs a new component. Class <code>Component</code> can be
- * extended directly to create a lightweight component that does not
- * utilize an opaque native window. A lightweight component must be
- * hosted by a native container somewhere higher up in the component
- * tree (for example, by a <code>Frame</code> object).
- */
- protected Component() {
- appContext = AppContext.getAppContext();
- SunToolkit.insertTargetMapping(this, appContext);
- }
- void initializeFocusTraversalKeys() {
- focusTraversalKeys = new Set[3];
- }
- /**
- * Constructs a name for this component. Called by <code>getName</code>
- * when the name is <code>null</code>.
- */
- String constructComponentName() {
- return null; // For strict compliance with prior platform versions, a Component
- // that doesn't set its name should return null from
- // getName()
- }
- /**
- * Gets the name of the component.
- * @return this component's name
- * @see #setName
- * @since JDK1.1
- */
- public String getName() {
- if (name == null && !nameExplicitlySet) {
- synchronized(this) {
- if (name == null && !nameExplicitlySet)
- name = constructComponentName();
- }
- }
- return name;
- }
- /**
- * Sets the name of the component to the specified string.
- * @param name the string that is to be this
- * component's name
- * @see #getName
- * @since JDK1.1
- */
- public void setName(String name) {
- synchronized(this) {
- this.name = name;
- nameExplicitlySet = true;
- }
- }
- /**
- * Gets the parent of this component.
- * @return the parent container of this component
- * @since JDK1.0
- */
- public Container getParent() {
- return getParent_NoClientCode();
- }
- // NOTE: This method may be called by privileged threads.
- // This functionality is implemented in a package-private method
- // to insure that it cannot be overridden by client subclasses.
- // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
- final Container getParent_NoClientCode() {
- return parent;
- }
- /**
- * @deprecated As of JDK version 1.1,
- * programs should not directly manipulate peers;
- * replaced by <code>boolean isDisplayable()</code>.
- */
- public ComponentPeer getPeer() {
- return peer;
- }
- /**
- * Associate a <code>DropTarget</code> with this component.
- * The <code>Component</code> will receive drops only if it
- * is enabled.
- *
- * @see #isEnabled
- * @param dt The DropTarget
- */
- public synchronized void setDropTarget(DropTarget dt) {
- if (dt == dropTarget || (dropTarget != null && dropTarget.equals(dt)))
- return;
- DropTarget old;
- if ((old = dropTarget) != null) {
- if (peer != null) dropTarget.removeNotify(peer);
- DropTarget t = dropTarget;
- dropTarget = null;
- try {
- t.setComponent(null);
- } catch (IllegalArgumentException iae) {
- // ignore it.
- }
- }
- // if we have a new one, and we have a peer, add it!
- if ((dropTarget = dt) != null) {
- try {
- dropTarget.setComponent(this);
- if (peer != null) dropTarget.addNotify(peer);
- } catch (IllegalArgumentException iae) {
- if (old != null) {
- try {
- old.setComponent(this);
- if (peer != null) dropTarget.addNotify(peer);
- } catch (IllegalArgumentException iae1) {
- // ignore it!
- }
- }
- }
- }
- }
- /**
- * Gets the <code>DropTarget</code> associated with this
- * <code>Component</code>.
- */
- public synchronized DropTarget getDropTarget() { return dropTarget; }
- /**
- * Gets the <code>GraphicsConfiguration</code> associated with this
- * <code>Component</code>.
- * If the <code>Component</code> has not been assigned a specific
- * <code>GraphicsConfiguration</code>,
- * the <code>GraphicsConfiguration</code> of the
- * <code>Component</code> object's top-level container is
- * returned.
- * If the <code>Component</code> has been created, but not yet added
- * to a <code>Container</code>, this method returns <code>null</code>.
- *
- * @return the <code>GraphicsConfiguration</code> used by this
- * <code>Component</code> or <code>null</code>
- * @since 1.3
- */
- public GraphicsConfiguration getGraphicsConfiguration() {
- synchronized(getTreeLock()) {
- if (graphicsConfig != null) {
- return graphicsConfig;
- } else if (getParent() != null) {
- return getParent().getGraphicsConfiguration();
- } else {
- return null;
- }
- }
- }
- /**
- * Resets this <code>Component</code>'s
- * <code>GraphicsConfiguration</code> back to a default
- * value. For most componenets, this is <code>null</code>.
- * Called from the Toolkit thread, so NO CLIENT CODE.
- */
- void resetGC() {
- synchronized(getTreeLock()) {
- graphicsConfig = null;
- }
- }
- /*
- * Not called on Component, but needed for Canvas and Window
- */
- void setGCFromPeer() {
- synchronized(getTreeLock()) {
- if (peer != null) { // can't imagine how this will be false,
- // but just in case
- graphicsConfig = peer.getGraphicsConfiguration();
- } else {
- graphicsConfig = null;
- }
- }
- }
- /**
- * Checks that this component's <code>GraphicsDevice</code>
- * <code>idString</code> matches the string argument.
- */
- void checkGD(String stringID) {
- if (graphicsConfig != null) {
- if (!graphicsConfig.getDevice().getIDstring().equals(stringID)) {
- throw new IllegalArgumentException(
- "adding a container to a container on a different GraphicsDevice");
- }
- }
- }
- /**
- * Gets this component's locking object (the object that owns the thread
- * sychronization monitor) for AWT component-tree and layout
- * operations.
- * @return this component's locking object
- */
- public final Object getTreeLock() {
- return LOCK;
- }
- /**
- * Gets the toolkit of this component. Note that
- * the frame that contains a component controls which
- * toolkit is used by that component. Therefore if the component
- * is moved from one frame to another, the toolkit it uses may change.
- * @return the toolkit of this component
- * @since JDK1.0
- */
- public Toolkit getToolkit() {
- return getToolkitImpl();
- }
- /*
- * This is called by the native code, so client code can't
- * be called on the toolkit thread.
- */
- final Toolkit getToolkitImpl() {
- ComponentPeer peer = this.peer;
- if ((peer != null) && ! (peer instanceof LightweightPeer)){
- return peer.getToolkit();
- }
- Container parent = this.parent;
- if (parent != null) {
- return parent.getToolkitImpl();
- }
- return Toolkit.getDefaultToolkit();
- }
- /**
- * Determines whether this component is valid. A component is valid
- * when it is correctly sized and positioned within its parent
- * container and all its children are also valid. Components are
- * invalidated when they are first shown on the screen.
- * @return <code>true</code> if the component is valid, <code>false</code>
- * otherwise
- * @see #validate
- * @see #invalidate
- * @since JDK1.0
- */
- public boolean isValid() {
- return (peer != null) && valid;
- }
- /**
- * Determines whether this component is displayable. A component is
- * displayable when it is connected to a native screen resource.
- * <p>
- * A component is made displayable either when it is added to
- * a displayable containment hierarchy or when its containment
- * hierarchy is made displayable.
- * A containment hierarchy is made displayable when its ancestor
- * window is either packed or made visible.
- * <p>
- * A component is made undisplayable either when it is removed from
- * a displayable containment hierarchy or when its containment hierarchy
- * is made undisplayable. A containment hierarchy is made
- * undisplayable when its ancestor window is disposed.
- *
- * @return <code>true</code> if the component is displayable,
- * <code>false</code> otherwise
- * @see Container#add(Component)
- * @see Window#pack
- * @see Window#show
- * @see Container#remove(Component)
- * @see Window#dispose
- * @since 1.2
- */
- public boolean isDisplayable() {
- return getPeer() != null;
- }
- /**
- * Determines whether this component should be visible when its
- * parent is visible. Components are
- * initially visible, with the exception of top level components such
- * as <code>Frame</code> objects.
- * @return <code>true</code> if the component is visible,
- * <code>false</code> otherwise
- * @see #setVisible
- * @since JDK1.0
- */
- public boolean isVisible() {
- return visible;
- }
- /**
- * Determines whether this component will be displayed on the screen
- * if it's displayable.
- * @return <code>true</code> if the component and all of its ancestors
- * are visible, <code>false</code> otherwise
- */
- boolean isRecursivelyVisible() {
- return visible && (parent == null || parent.isRecursivelyVisible());
- }
- /**
- * Determines whether this component is showing on screen. This means
- * that the component must be visible, and it must be in a container
- * that is visible and showing.
- * @return <code>true</code> if the component is showing,
- * <code>false</code> otherwise
- * @see #setVisible
- * @since JDK1.0
- */
- public boolean isShowing() {
- if (visible && (peer != null)) {
- Container parent = this.parent;
- return (parent == null) || parent.isShowing();
- }
- return false;
- }
- /**
- * Determines whether this component is enabled. An enabled component
- * can respond to user input and generate events. Components are
- * enabled initially by default. A component may be enabled or disabled by
- * calling its <code>setEnabled</code> method.
- * @return <code>true</code> if the component is enabled,
- * <code>false</code> otherwise
- * @see #setEnabled
- * @since JDK1.0
- */
- public boolean isEnabled() {
- return isEnabledImpl();
- }
- /*
- * This is called by the native code, so client code can't
- * be called on the toolkit thread.
- */
- final boolean isEnabledImpl() {
- return enabled;
- }
- /**
- * Enables or disables this component, depending on the value of the
- * parameter <code>b</code>. An enabled component can respond to user
- * input and generate events. Components are enabled initially by default.
- *
- * <p>Note: Disabling a lightweight component does not prevent it from
- * receiving MouseEvents.
- *
- * @param b If <code>true</code>, this component is
- * enabled; otherwise this component is disabled
- * @see #isEnabled
- * @see #isLightweight
- * @since JDK1.1
- */
- public void setEnabled(boolean b) {
- enable(b);
- }
- /**
- * @deprecated As of JDK version 1.1,
- * replaced by <code>setEnabled(boolean)</code>.
- */
- public void enable() {
- if (!enabled) {
- synchronized (getTreeLock()) {
- enabled = true;
- ComponentPeer peer = this.peer;
- if (peer != null) {
- peer.enable();
- if (visible) {
- updateCursorImmediately();
- }
- }
- }
- if (accessibleContext != null) {
- accessibleContext.firePropertyChange(
- AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
- null, AccessibleState.ENABLED);
- }
- }
- }
- /**
- * @deprecated As of JDK version 1.1,
- * replaced by <code>setEnabled(boolean)</code>.
- */
- public void enable(boolean b) {
- if (b) {
- enable();
- } else {
- disable();
- }
- }
- /**
- * @deprecated As of JDK version 1.1,
- * replaced by <code>setEnabled(boolean)</code>.
- */
- public void disable() {
- if (enabled) {
- KeyboardFocusManager.clearMostRecentFocusOwner(this);
- synchronized (getTreeLock()) {
- enabled = false;
- if (isFocusOwner()) {
- // Don't clear the global focus owner. If transferFocus
- // fails, we want the focus to stay on the disabled
- // Component so that keyboard traversal, et. al. still
- // makes sense to the user.
- autoTransferFocus(false);
- }
- ComponentPeer peer = this.peer;
- if (peer != null) {
- peer.disable();
- if (visible) {
- updateCursorImmediately();
- }
- }
- }
- if (accessibleContext != null) {
- accessibleContext.firePropertyChange(
- AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
- null, AccessibleState.ENABLED);
- }
- }
- }
- /**
- * Returns true if this component is painted to an offscreen image
- * ("buffer") that's copied to the screen later. Component
- * subclasses that support double buffering should override this
- * method to return true if double buffering is enabled.
- *
- * @return false by default
- */
- public boolean isDoubleBuffered() {
- return false;
- }
- /**
- * Enables or disables input method support for this component. If input
- * method support is enabled and the component also processes key events,
- * incoming events are offered to
- * the current input method and will only be processed by the component or
- * dispatched to its listeners if the input method does not consume them.
- * By default, input method support is enabled.
- *
- * @param enable true to enable, false to disable
- * @see #processKeyEvent
- * @since 1.2
- */
- public void enableInputMethods(boolean enable) {
- if (enable) {
- if ((eventMask & AWTEvent.INPUT_METHODS_ENABLED_MASK) != 0)
- return;
- // If this component already has focus, then activate the
- // input method by dispatching a synthesized focus gained
- // event.
- if (isFocusOwner()) {
- InputContext inputContext = getInputContext();
- if (inputContext != null) {
- FocusEvent focusGainedEvent =
- new FocusEvent(this, FocusEvent.FOCUS_GAINED);
- inputContext.dispatchEvent(focusGainedEvent);
- }
- }
- eventMask |= AWTEvent.INPUT_METHODS_ENABLED_MASK;
- } else {
- if (areInputMethodsEnabled()) {
- InputContext inputContext = getInputContext();
- if (inputContext != null) {
- inputContext.endComposition();
- inputContext.removeNotify(this);
- }
- }
- eventMask &= ~AWTEvent.INPUT_METHODS_ENABLED_MASK;
- }
- }
- /**
- * Shows or hides this component depending on the value of parameter
- * <code>b</code>.
- * @param b if <code>true</code>, shows this component;
- * otherwise, hides this component
- * @see #isVisible
- * @since JDK1.1
- */
- public void setVisible(boolean b) {
- show(b);
- }
- /**
- * @deprecated As of JDK version 1.1,
- * replaced by <code>setVisible(boolean)</code>.
- */
- public void show() {
- if (!visible) {
- synchronized (getTreeLock()) {
- visible = true;
- ComponentPeer peer = this.peer;
- if (peer != null) {
- peer.show();
- createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED,
- this, parent,
- HierarchyEvent.SHOWING_CHANGED,
- Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK));
- if (peer instanceof LightweightPeer) {
- repaint();
- }
- updateCursorImmediately();
- }
- if (componentListener != null ||
- (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0 ||
- Toolkit.enabledOnToolkit(AWTEvent.COMPONENT_EVENT_MASK)) {
- ComponentEvent e = new ComponentEvent(this,
- ComponentEvent.COMPONENT_SHOWN);
- Toolkit.getEventQueue().postEvent(e);
- }
- }
- Container parent = this.parent;
- if (parent != null) {
- parent.invalidate();
- }
- }
- }
- /**
- * @deprecated As of JDK version 1.1,
- * replaced by <code>setVisible(boolean)</code>.
- */
- public void show(boolean b) {
- if (b) {
- show();
- } else {
- isPacked = false;
- hide();
- }
- }
- boolean containsFocus() {
- return isFocusOwner();
- }
- void clearMostRecentFocusOwnerOnHide() {
- KeyboardFocusManager.clearMostRecentFocusOwner(this);
- }
- void clearCurrentFocusCycleRootOnHide() {
- /* do nothing */
- }
- /**
- * @deprecated As of JDK version 1.1,
- * replaced by <code>setVisible(boolean)</code>.
- */
- public void hide() {
- if (visible) {
- clearCurrentFocusCycleRootOnHide();
- clearMostRecentFocusOwnerOnHide();
- synchronized (getTreeLock()) {
- visible = false;
- if (containsFocus()) {
- autoTransferFocus(true);
- }
- ComponentPeer peer = this.peer;
- if (peer != null) {
- peer.hide();
- createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED,
- this, parent,
- HierarchyEvent.SHOWING_CHANGED,
- Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK));
- if (peer instanceof LightweightPeer) {
- repaint();
- }
- updateCursorImmediately();
- }
- if (componentListener != null ||
- (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0 ||
- Toolkit.enabledOnToolkit(AWTEvent.COMPONENT_EVENT_MASK)) {
- ComponentEvent e = new ComponentEvent(this,
- ComponentEvent.COMPONENT_HIDDEN);
- Toolkit.getEventQueue().postEvent(e);
- }
- }
- Container parent = this.parent;
- if (parent != null) {
- parent.invalidate();
- }
- }
- }
- /**
- * Gets the foreground color of this component.
- * @return this component's foreground color; if this component does
- * not have a foreground color, the foreground color of its parent
- * is returned
- * @see #setForeground
- * @since JDK1.0
- * @beaninfo
- * bound: true
- */
- public Color getForeground() {
- Color foreground = this.foreground;
- if (foreground != null) {
- return foreground;
- }
- Container parent = this.parent;
- return (parent != null) ? parent.getForeground() : null;
- }
- /**
- * Sets the foreground color of this component.
- * @param c the color to become this component's
- * foreground color; if this parameter is <code>null</code>
- * then this component will inherit
- * the foreground color of its parent
- * @see #getForeground
- * @since JDK1.0
- */
- public void setForeground(Color c) {
- Color oldColor = foreground;
- ComponentPeer peer = this.peer;
- foreground = c;
- if (peer != null) {
- c = getForeground();
- if (c != null) {
- peer.setForeground(c);
- }
- }
- // This is a bound property, so report the change to
- // any registered listeners. (Cheap if there are none.)
- firePropertyChange("foreground", oldColor, c);
- }
- /**
- * Returns whether the foreground color has been explicitly set for this
- * Component. If this method returns <code>false</code>, this Component is
- * inheriting its foreground color from an ancestor.
- *
- * @return <code>true</code> if the foreground color has been explicitly
- * set for this Component; <code>false</code> otherwise.
- * @since 1.4
- */
- public boolean isForegroundSet() {
- return (foreground != null);
- }
- /**
- * Gets the background color of this component.
- * @return this component's background color; if this component does
- * not have a background color,
- * the background color of its parent is returned
- * @see #setBackground
- * @since JDK1.0
- */
- public Color getBackground() {
- Color background = this.background;
- if (background != null) {
- return background;
- }
- Container parent = this.parent;
- return (parent != null) ? parent.getBackground() : null;
- }
- /**
- * Sets the background color of this component.
- * <p>
- * The background color affects each component differently and the
- * parts of the component that are affected by the background color
- * may differ between operating systems.
- *
- * @param c the color to become this component's color;
- * if this parameter is <code>null</code>, then this
- * component will inherit the background color of its parent
- * @see #getBackground
- * @since JDK1.0
- * @beaninfo
- * bound: true
- */
- public void setBackground(Color c) {
- Color oldColor = background;
- ComponentPeer peer = this.peer;
- background = c;
- if (peer != null) {
- c = getBackground();
- if (c != null) {
- peer.setBackground(c);
- }
- }
- // This is a bound property, so report the change to
- // any registered listeners. (Cheap if there are none.)
- firePropertyChange("background", oldColor, c);
- }
- /**
- * Returns whether the background color has been explicitly set for this
- * Component. If this method returns <code>false</code>, this Component is
- * inheriting its background color from an ancestor.
- *
- * @return <code>true</code> if the background color has been explicitly
- * set for this Component; <code>false</code> otherwise.
- * @since 1.4
- */
- public boolean isBackgroundSet() {
- return (background != null);
- }
- /**
- * Gets the font of this component.
- * @return this component's font; if a font has not been set
- * for this component, the font of its parent is returned
- * @see #setFont
- * @since JDK1.0
- */
- public Font getFont() {
- return getFont_NoClientCode();
- }
- // NOTE: This method may be called by privileged threads.
- // This functionality is implemented in a package-private method
- // to insure that it cannot be overridden by client subclasses.
- // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
- final Font getFont_NoClientCode() {
- Font font = this.font;
- if (font != null) {
- return font;
- }
- Container parent = this.parent;
- return (parent != null) ? parent.getFont_NoClientCode() : null;
- }
- /**
- * Sets the font of this component.
- * @param f the font to become this component's font;
- * if this parameter is <code>null</code> then this
- * component will inherit the font of its parent
- * @see #getFont
- * @since JDK1.0
- * @beaninfo
- * bound: true
- */
- public void setFont(Font f) {
- Font oldFont, newFont;
- synchronized (this) {
- oldFont = font;
- ComponentPeer peer = this.peer;
- newFont = font = f;
- if (peer != null) {
- f = getFont();
- if (f != null) {
- peer.setFont(f);
- peerFont = f;
- }
- }
- }
- // This is a bound property, so report the change to
- // any registered listeners. (Cheap if there are none.)
- firePropertyChange("font", oldFont, newFont);
- // This could change the preferred size of the Component.
- if (valid) {
- invalidate();
- }
- }
- /**
- * Returns whether the font has been explicitly set for this Component. If
- * this method returns <code>false</code>, this Component is inheriting its
- * font from an ancestor.
- *
- * @return <code>true</code> if the font has been explicitly set for this
- * Component; <code>false</code> otherwise.
- * @since 1.4
- */
- public boolean isFontSet() {
- return (font != null);
- }
- /**
- * Gets the locale of this component.
- * @return this component's locale; if this component does not
- * have a locale, the locale of its parent is returned
- * @see #setLocale
- * @exception IllegalComponentStateException if the <code>Component</code>
- * does not have its own locale and has not yet been added to
- * a containment hierarchy such that the locale can be determined
- * from the containing parent
- * @since JDK1.1
- */
- public Locale getLocale() {
- Locale locale = this.locale;
- if (locale != null) {
- return locale;
- }
- Container parent = this.parent;
- if (parent == null) {
- throw new IllegalComponentStateException("This component must have a parent in order to determine its locale");
- } else {
- return parent.getLocale();
- }
- }
- /**
- * Sets the locale of this component. This is a bound property.
- * @param l the locale to become this component's locale
- * @see #getLocale
- * @since JDK1.1
- */
- public void setLocale(Locale l) {
- Locale oldValue = locale;
- locale = l;
- // This is a bound property, so report the change to
- // any registered listeners. (Cheap if there are none.)
- firePropertyChange("locale", oldValue, l);
- // This could change the preferred size of the Component.
- if (valid) {
- invalidate();
- }
- }
- /**
- * Gets the instance of <code>ColorModel</code> used to display
- * the component on the output device.
- * @return the color model used by this component
- * @see java.awt.image.ColorModel
- * @see java.awt.peer.ComponentPeer#getColorModel()
- * @see Toolkit#getColorModel()
- * @since JDK1.0
- */
- public ColorModel getColorModel() {
- ComponentPeer peer = this.peer;
- if ((peer != null) && ! (peer instanceof LightweightPeer)) {
- return peer.getColorModel();
- } else if (GraphicsEnvironment.isHeadless()) {
- return ColorModel.getRGBdefault();
- } // else
- return getToolkit().getColorModel();
- }
- /**
- * Gets the location of this component in the form of a
- * point specifying the component's top-left corner.
- * The location will be relative to the parent's coordinate space.
- * <p>
- * Due to the asynchronous nature of native event handling, this
- * method can return outdated values (for instance, after several calls
- * of <code>setLocation()</code> in rapid succession). For this
- * reason, the recommended method of obtaining a component's position is
- * within <code>java.awt.event.ComponentListener.componentMoved()</code>,
- * which is called after the operating system has finished moving the
- * component.
- * </p>
- * @return an instance of <code>Point</code> representing
- * the top-left corner of the component's bounds in
- * the coordinate space of the component's parent
- * @see #setLocation
- * @see #getLocationOnScreen
- * @since JDK1.1
- */
- public Point getLocation() {
- return location();
- }
- /**
- * Gets the location of this component in the form of a point
- * specifying the component's top-left corner in the screen's
- * coordinate space.
- * @return an instance of <code>Point</code> representing
- * the top-left corner of the component's bounds in the
- * coordinate space of the screen
- * @throws <code>IllegalComponentStateException</code> if the
- * component is not showing on the screen
- * @see #setLocation
- * @see #getLocation
- */
- public Point getLocationOnScreen() {
- synchronized (getTreeLock()) {
- return getLocationOnScreen_NoTreeLock();
- }
- }
- /*
- * a package private version of getLocationOnScreen
- * used by GlobalCursormanager to update cursor
- */
- final Point getLocationOnScreen_NoTreeLock() {
- if (peer != null && isShowing()) {
- if (peer instanceof LightweightPeer) {
- // lightweight component location needs to be translated
- // relative to a native component.
- Container host = getNativeContainer();
- Point pt = host.peer.getLocationOnScreen();
- for(Component c = this; c != host; c = c.getParent()) {
- pt.x += c.x;
- pt.y += c.y;
- }
- return pt;
- } else {
- Point pt = peer.getLocationOnScreen();
- return pt;
- }
- } else {
- throw new IllegalComponentStateException("component must be showing on the screen to determine its location");
- }
- }
- /**
- * @deprecated As of JDK version 1.1,
- * replaced by <code>getLocation()</code>.
- */
- public Point location() {
- return new Point(x, y);
- }
- /**
- * Moves this component to a new location. The top-left corner of
- * the new location is specified by the <code>x</code> and <code>y</code>
- * parameters in the coordinate space of this component's parent.
- * @param x the <i>x</i>-coordinate of the new location's
- * top-left corner in the parent's coordinate space
- * @param y the <i>y</i>-coordinate of the new location's
- * top-left corner in the parent's coordinate space
- * @see #getLocation
- * @see #setBounds
- * @since JDK1.1
- */
- public void setLocation(int x, int y) {
- move(x, y);
- }
- /**
- * @deprecated As of JDK version 1.1,
- * replaced by <code>setLocation(int, int)</code>.
- */
- public void move(int x, int y) {
- setBounds(x, y, width, height);
- }
- /**
- * Moves this component to a new location. The top-left corner of
- * the new location is specified by point <code>p</code>. Point
- * <code>p</code> is given in the parent's coordinate space.
- * @param p the point defining the top-left corner
- * of the new location, given in the coordinate space of this
- * component's parent
- * @see #getLocation
- * @see #setBounds
- * @since JDK1.1
- */
- public void setLocation(Point p) {
- setLocation(p.x, p.y);
- }
- /**
- * Returns the size of this component in the form of a
- * <code>Dimension</code> object. The <code>height</code>
- * field of the <code>Dimension</code> object contains
- * this component's height, and the <code>width</code>
- * field of the <code>Dimension</code> object contains
- * this component's width.
- * @return a <code>Dimension</code> object that indicates the
- * size of this component
- * @see #setSize
- * @since JDK1.1
- */
- public Dimension getSize() {
- return size();
- }
- /**
- * @deprecated As of JDK version 1.1,
- * replaced by <code>getSize()</code>.
- */
- public Dimension size() {
- return new Dimension(width, height);
- }
- /**
- * Resizes this component so that it has width <code>width</code>
- * and height <code>height</code>.
- * @param width the new width of this component in pixels