- /*
- * @(#)Component.java 1.265 00/02/28
- *
- * Copyright 1995-2000 Sun Microsystems, Inc. All Rights Reserved.
- *
- * This software is the proprietary information of Sun Microsystems, Inc.
- * 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.awt.peer.ComponentPeer;
- import java.awt.image.ImageObserver;
- import java.awt.image.ImageProducer;
- import java.awt.image.ColorModel;
- 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.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.
- *
- * @version 1.265, 02/28/00
- * @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
- * behaviour. The peer is set when the Component 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 null for top-level components.
- * @see #getParent
- */
- transient Container parent;
- /**
- * The AppContext 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.
- * foreground color can be null.
- *
- * @serial
- * @see #getForeground
- * @see #setForeground
- */
- Color foreground;
- /**
- * The background color for this component.
- * background can be null.
- *
- * @serial
- * @see #getBackground
- * @see #setBackground
- */
- Color background;
- /**
- * The font used by this component.
- * The font can be null.
- *
- * @serial
- * @see #getFont
- * @see #setFont
- */
- Font font;
- /**
- * The font which the peer is currently using. (null if no peer exists.)
- */
- Font peerFont;
- /**
- * The cursor displayed when pointer is over this component.
- * This value can be null.
- *
- * @serial
- * @see #getCursor
- * @see #setCursor
- */
- Cursor cursor;
- /**
- * The locale for the component.
- *
- * @serial
- * @see #getLocale
- * @see #setLocale
- */
- Locale locale;
- /**
- * A reference to a GraphicsConfiguration object
- * used to describe the characteristics of a graphics
- * destination.
- * This value can be null.
- *
- * @since 1.3
- * @serial
- * @see java.awt.GraphicsConfiguration
- * @see #getGraphicsConfiguration
- */
- transient GraphicsConfiguration graphicsConfig = null;
- /**
- * 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 DropTarget associated with this Component.
- *
- * @since 1.2
- * @serial
- * @see #setDropTarget
- * @see #getDropTarget
- */
- DropTarget dropTarget;
- /**
- * True if this component has enabled focus events and currently
- * has the focus.
- *
- * @serial
- * @see #hasFocus
- * @see #processFocusEvent
- */
- boolean hasFocus = false;
- /**
- * @serial
- * @see add()
- */
- Vector popups;
- /**
- * A components name.
- * This field can be null.
- *
- * @serial
- * @see getName()
- * @see setName(String)
- */
- private String name;
- /**
- * A bool to determine whether the name has
- * been set explicitly. nameExplicitlySet 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;
- /**
- * 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;
- /**
- * newEventsOnly 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 components 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 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 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";
- /**
- * The eventMask is ONLY set by subclasses via enableEvents.
- * 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 enableInputMethods and is
- * on by default.
- *
- * @serial
- * @see enableInputMethods()
- */
- 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 */
- 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 PropertyChangeListeners have been registered, the
- * changeSupport 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);
- }
- /**
- * Construct a name for this component. Called by getName() when the
- * name is null.
- */
- 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 DropTarget with this Component. The Component 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!
- }
- }
- }
- }
- }
- /**
- * Get the DropTarget associated with this Component
- */
- public synchronized DropTarget getDropTarget() { return dropTarget; }
- /**
- * Get 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;
- }
- }
- }
- /**
- * Reset this Componenet's GraphicsConfiguration back to a default
- * value. For most Componenets, this is null.
- * Called from the Toolkit thread, so NO CLIENT CODE.
- */
- void resetGC() {
- synchronized(getTreeLock()) {
- graphicsConfig = null;
- }
- }
- /**
- * Checks that this Component's GraphicsDevice idString 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 the locking object for AWT component-tree and layout
- * 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 java.awt.peer.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 java.awt.Container#add(java.awt.Component)
- * @see java.awt.Window#pack
- * @see java.awt.Window#show
- * @see java.awt.Container#remove(java.awt.Component)
- * @see java.awt.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.
- * @param b If <code>true</code>, this component is
- * enabled; otherwise this component is disabled.
- * @see #isEnabled
- * @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 != true) {
- synchronized (getTreeLock()) {
- enabled = true;
- ComponentPeer peer = this.peer;
- if (peer != null) {
- peer.enable();
- if (visible) {
- GlobalCursorManager.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 != false) {
- synchronized (getTreeLock()) {
- enabled = false;
- ComponentPeer peer = this.peer;
- if (peer != null) {
- peer.disable();
- if (visible) {
- GlobalCursorManager.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 java.awt.Component#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 (hasFocus() == true) {
- 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);
- if (peer instanceof java.awt.peer.LightweightPeer) {
- repaint();
- }
- GlobalCursorManager.updateCursorImmediately();
- }
- if (componentListener != null ||
- (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0) {
- 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 {
- hide();
- }
- }
- /**
- * @deprecated As of JDK version 1.1,
- * replaced by <code>setVisible(boolean)</code>.
- */
- public void hide() {
- if (visible) {
- synchronized (getTreeLock()) {
- visible = false;
- ComponentPeer peer = this.peer;
- if (peer != null) {
- peer.hide();
- createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED,
- this, parent,
- HierarchyEvent.SHOWING_CHANGED);
- if (peer instanceof java.awt.peer.LightweightPeer) {
- repaint();
- }
- GlobalCursorManager.updateCursorImmediately();
- }
- if (componentListener != null ||
- (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0) {
- 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
- */
- 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 null 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);
- }
- /**
- * 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 java.awt.Component#setBackground(java.awt.Color)
- * @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.
- * @param c The color to become this component's color.
- * If this parameter is null then this component will inherit
- * the background color of its parent.
- * background color.
- * @see #getBackground
- * @since JDK1.0
- */
- 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);
- }
- /**
- * 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 null then this component will inherit
- * the font of its parent.
- * @see #getFont
- * @since JDK1.0
- */
- 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();
- }
- }
- /**
- * 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 Component
- * 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.
- * @param l The locale to become this component's locale.
- * @see #getLocale
- * @since JDK1.1
- */
- public void setLocale(Locale l) {
- locale = 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 java.awt.Toolkit#getColorModel()
- * @since JDK1.0
- */
- public ColorModel getColorModel() {
- ComponentPeer peer = this.peer;
- if ((peer != null) && ! (peer instanceof java.awt.peer.LightweightPeer)) {
- return peer.getColorModel();
- }
- 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.
- * @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 java.awt.peer.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 <code>height</code>.
- * @param width The new width of this component in pixels.
- * @param height The new height of this component in pixels.
- * @see #getSize
- * @see #setBounds
- * @since JDK1.1
- */
- public void setSize(int width, int height) {
- resize(width, height);
- }
- /**
- * @deprecated As of JDK version 1.1,
- * replaced by <code>setSize(int, int)</code>.
- */
- public void resize(int width, int height) {
- setBounds(x, y, width, height);
- }
- /**
- * Resizes this component so that it has width <code>d.width</code>
- * and height <code>d.height</code>.
- * @param d The dimension specifying the new size
- * of this component.
- * @see #setSize
- * @see #setBounds
- * @since JDK1.1
- */
- public void setSize(Dimension d) {
- resize(d);
- }
- /**
- * @deprecated As of JDK version 1.1,
- * replaced by <code>setSize(Dimension)</code>.
- */
- public void resize(Dimension d) {
- setSize(d.width, d.height);
- }
- /**
- * Gets the bounds of this component in the form of a
- * <code>Rectangle</code> object. The bounds specify this
- * component's width, height, and location relative to
- * its parent.
- * @return A rectangle indicating this component's bounds.
- * @see #setBounds
- * @see #getLocation
- * @see #getSize
- */
- public Rectangle getBounds() {
- return bounds();
- }
- /**
- * @deprecated As of JDK version 1.1,
- * replaced by <code>getBounds()</code>.
- */
- public Rectangle bounds() {
- return new Rectangle(x, y, width, height);
- }
- /**
- * Moves and resizes this component. The new location of the top-left
- * corner is specified by <code>x</code> and <code>y</code>, and the
- * new size is specified by <code>width</code> and <code>height</code>.
- * @param x The new <i>x</i>-coordinate of this component.
- * @param y The new <i>y</i>-coordinate of this component.
- * @param width The new <code>width</code> of this component.
- * @param height The new <code>height</code> of this
- * component.
- * @see java.awt.Component#getBounds
- * @see java.awt.Component#setLocation(int, int)
- * @see java.awt.Component#setLocation(java.awt.Point)
- * @see java.awt.Component#setSize(int, int)
- * @see java.awt.Component#setSize(java.awt.Dimension)
- * @JDK1.1
- */
- public void setBounds(int x, int y, int width, int height) {
- reshape(x, y, width, height);
- }
- /**
- * @deprecated As of JDK version 1.1,
- * replaced by <code>setBounds(int, int, int, int)</code>.
- */
- public void reshape(int x, int y, int width, int height) {
- synchronized (getTreeLock()) {
- boolean resized = (this.width != width) || (this.height != height);
- boolean moved = (this.x != x) || (this.y != y);
- boolean isLightweight = peer instanceof java.awt.peer.LightweightPeer;
- if (resized) {
- isPacked = false;
- }
- if (resized || moved) {
- if (isLightweight && visible) {
- // Have the parent redraw the area this component occupied.
- repaint();
- }
- this.x = x;
- this.y = y;
- this.width = width;
- this.height = height;
- if (peer != null) {
- if (isLightweight) {
- peer.setBounds(x, y, width, height);
- } else {
- // native peer might be offset by more than direct
- // parent since parent might be lightweight.
- int nativeX = x;
- int nativeY = y;
- for(Component c = parent; (c != null) &&
- (c.peer instanceof java.awt.peer.LightweightPeer);
- c = c.parent) {
- nativeX += c.x;
- nativeY += c.y;
- }
- peer.setBounds(nativeX, nativeY, width, height);
- }
- if (resized) {
- invalidate();
- }
- if (parent != null && parent.valid) {
- parent.invalidate();
- }
- }
- if (resized) {
- if (componentListener != null ||
- (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0) {
- ComponentEvent e = new ComponentEvent(this,
- ComponentEvent.COMPONENT_RESIZED);
- Toolkit.getEventQueue().postEvent(e);
- // Container.dispatchEventImpl will create
- // HierarchyEvents
- } else {
- createChildHierarchyEvents(
- HierarchyEvent.ANCESTOR_RESIZED,
- 0);
- }
- }
- if (moved) {
- if (componentListener != null ||
- (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0){
- ComponentEvent e = new ComponentEvent(this,
- ComponentEvent.COMPONENT_MOVED);
- Toolkit.getEventQueue().postEvent(e);
- // Container.dispatchEventImpl will create
- // HierarchyEvents
- } else {
- createChildHierarchyEvents(
- HierarchyEvent.ANCESTOR_MOVED,
- 0);
- }
- }
- if (isLightweight && visible) {
- // Have the parent redraw the area this component *now* occupies.
- repaint();
- }
- }
- }
- }
- /**
- * Moves and resizes this component to conform to the new
- * bounding rectangle <code>r</code>. This component's new
- * position is specified by <code>r.x</code> and <code>r.y</code>,
- * and its new size is specified by <code>r.width</code> and
- * <code>r.height</code>
- * @param r The new bounding rectangle for this component.
- * @see java.awt.Component#getBounds
- * @see java.awt.Component#setLocation(int, int)
- * @see java.awt.Component#setLocation(java.awt.Point)
- * @see java.awt.Component#setSize(int, int)
- * @see java.awt.Component#setSize(java.awt.Dimension)
- * @since JDK1.1
- */
- public void setBounds(Rectangle r) {
- setBounds(r.x, r.y, r.width, r.height);
- }
- /**
- * Return the current x coordinate of the components origin.
- * This method is preferable to writing component.getBounds().x,
- * or component.getLocation().x because it doesn't cause any
- * heap allocations.
- *
- * @return the current x coordinate of the components origin.
- * @since 1.2
- */
- public int getX() {
- return x;
- }
- /**
- * Return the current y coordinate of the components origin.
- * This method is preferable to writing component.getBounds().y,
- * or component.getLocation().y because it doesn't cause any
- * heap allocations.
- *
- * @return the current y coordinate of the components origin.
- * @since 1.2
- */
- public int getY() {
- return y;
- }
- /**
- * Return the current width of this component.
- * This method is preferable to writing component.getBounds().width,
- * or component.getSize().width because it doesn't cause any
- * heap allocations.
- *
- * @return the current width of this component.
- * @since 1.2
- */
- public int getWidth() {
- return width;
- }
- /**
- * Return the current height of this component.
- * This method is preferable to writing component.getBounds().height,
- * or component.getSize().height because it doesn't cause any
- * heap allocations.
- *
- * @return the current height of this component.
- * @since 1.2
- */
- public int getHeight() {
- return height;
- }
- /**
- * Store the bounds of this component into "return value" <b>rv</b> and
- * return <b>rv</b>. If rv is null a new Rectangle is allocated.
- * This version of getBounds() is useful if the caller
- * wants to avoid allocating a new Rectangle object on the heap.
- *
- * @param rv the return value, modified to the components bounds
- * @return rv
- */
- public Rectangle getBounds(Rectangle rv) {
- if (rv == null) {
- return new Rectangle(getX(), getY(), getWidth(), getHeight());
- }
- else {
- rv.setBounds(getX(), getY(), getWidth(), getHeight());
- return rv;
- }
- }
- /**
- * Store the width/height of this component into "return value" <b>rv</b>
- * and return <b>rv</b>. If rv is null a new Dimension object is
- * allocated. This version of getSize() is useful if the
- * caller wants to avoid allocating a new Dimension object on the heap.
- *
- * @param rv the return value, modified to the components size
- * @return rv
- */
- public Dimension getSize(Dimension rv) {
- if (rv == null) {
- return new Dimension(getWidth(), getHeight());
- }
- else {
- rv.setSize(getWidth(), getHeight());
- return rv;
- }
- }
- /**
- * Store the x,y origin of this component into "return value" <b>rv</b>
- * and return <b>rv</b>. If rv is null a new Point is allocated.
- * This version of getLocation() is useful if the
- * caller wants to avoid allocating a new Point object on the heap.
- *
- * @param rv the return value, modified to the components location
- * @return rv
- */
- public Point getLocation(Point rv) {
- if (rv == null) {
- return new Point(getX(), getY());
- }
- else {
- rv.setLocation(getX(), getY());
- return rv;
- }
- }
- /**
- * Returns true if this component is completely opaque, returns
- * false by default.
- * <p>
- * An opaque component paints every pixel within its
- * rectangular region. A non-opaque component paints only some of
- * its pixels, allowing the pixels underneath it to "show through".
- * A component that does not fully paint its pixels therefore
- * provides a degree of transparency. Only lightweight
- * components can be transparent.
- * <p>
- * Subclasses that guarantee to always completely paint their
- * contents should override this method and return true. All
- * of the "heavyweight" AWT components are opaque.
- *
- * @return true if this component is completely opaque.