- /*
- * @(#)JComponent.java 2.113 01/11/29
- *
- * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
- * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
- */
- package javax.swing;
- import java.util.Hashtable;
- import java.util.Dictionary;
- import java.util.Enumeration;
- import java.util.Locale;
- import java.util.Vector;
- import java.awt.*;
- import java.awt.event.*;
- import java.beans.*;
- import java.applet.Applet;
- import java.io.Serializable;
- import java.io.ObjectOutputStream;
- import java.io.ObjectInputStream;
- import java.io.IOException;
- import java.io.ObjectInputValidation;
- import java.io.InvalidObjectException;
- import javax.swing.border.*;
- import javax.swing.event.*;
- import javax.swing.plaf.*;
- import javax.accessibility.*;
- import java.awt.Graphics2D;
- /**
- * The base class for the Swing components. JComponent provides:
- * <ul>
- * <li>A "pluggable look and feel" (l&f) that can be specified by the
- * programmer or (optionally) selected by the user at runtime.
- * <li>Components that are designed to be combined and extended in order
- * to create custom components.
- * <li>Comprehensive keystroke-handling that works with nested components.
- * <li>Action objects, for single-point control of program actions initiated
- * by multiple components.
- * <li>A border property that implicitly defines the component's insets.
- * <li>The ability to set the preferred, minimim, and maximum size for a
- * component.
- * <li>ToolTips -- short descriptions that pop up when the cursor lingers
- * over a component.
- * <li>Autoscrolling -- automatic scrolling in a list, table, or tree that
- * occurs when the user is dragging the mouse.
- * <li>Simple, easy dialog construction using static methods in the JOptionPane
- * class that let you display information and query the user.
- * <li>Slow-motion graphics rendering using debugGraphics so you can see
- * what is being displayed on screen and whether or not it is being
- * overwritten.
- * <li>Support for Accessibility.
- * <li>Support for international Localization.
- * </ul>
- * For more information on these subjects, see the
- * <a href="package-summary.html#package_description">Swing package description</a>
- * <p>
- * <strong>Warning:</strong>
- * Serialized objects of this class will not be compatible with
- * future Swing releases. The current serialization support is appropriate
- * for short term storage or RMI between applications running the same
- * version of Swing. A future release of Swing will provide support for
- * long term persistence.
- *
- * @see KeyStroke
- * @see Action
- * @see #setBorder
- * @see #registerKeyboardAction
- * @see JOptionPane
- * @see #setDebugGraphicsOptions
- * @see #setToolTipText
- * @see #setAutoscrolls
- *
- * @version 2.113 11/29/01
- * @author Hans Muller
- * @author Arnaud Weber
- */
- public abstract class JComponent extends Container implements Serializable
- {
- /**
- * @see #getUIClassID
- * @see #writeObject
- */
- private static final String uiClassID = "ComponentUI";
- /**
- * @see ReadObjectCallback
- * @se #readObject
- */
- private static final Hashtable readObjectCallbacks = new Hashtable(1);
- /* The following fields support set methods for the corresponding
- * java.awt.Component properties.
- */
- private Dimension preferredSize;
- private Dimension minimumSize;
- private Dimension maximumSize;
- private Float alignmentX;
- private Float alignmentY;
- private AncestorNotifier ancestorNotifier;
- Rectangle _bounds = new Rectangle();
- /* Backing store for JComponent properties and listeners
- */
- protected transient ComponentUI ui;
- protected EventListenerList listenerList = new EventListenerList();
- private Hashtable clientProperties;
- private VetoableChangeSupport vetoableChangeSupport;
- private Autoscroller autoscroller;
- private Border border;
- private int flags;
- /* A "scratch pad" rectangle used by the painting code.
- */
- private transient Rectangle tmpRect;
- /** Set in _paintImmediately. Will indicate the child that initiated
- * the painting operation. If paintingChild is opaque, no need to paint
- * any child components after paintingChild. Test used in paintChildren. */
- transient Component paintingChild;
- /**
- * Constant used for registerKeyboardAction() which
- * means that the command should be invoked when
- * the component has the focus.
- */
- public static final int WHEN_FOCUSED = 0;
- /**
- * Constant used for registerKeyboardAction() which
- * means that the comand should be invoked when the receiving
- * component is an ancestor of the focused component or is
- * itself the focused component.
- */
- public static final int WHEN_ANCESTOR_OF_FOCUSED_COMPONENT = 1;
- /**
- * Constant used for registerKeyboardAction() which
- * means that the command should be invoked when
- * the receiving component is in the window that has the focus
- * or is itself the focused component.
- */
- public static final int WHEN_IN_FOCUSED_WINDOW = 2;
- /**
- * Constant used by some of the apis to mean that no condition is defined.
- */
- public static final int UNDEFINED_CONDITION = -1;
- /**
- * The key used by JComponent to access keyboard bindings.
- */
- private static final String KEYBOARD_BINDINGS_KEY = "_KeyboardBindings";
- /**
- * The comment to display when the cursor is over the component,
- * also known as a "value tip", "flyover help", or "flyover label".
- */
- public static final String TOOL_TIP_TEXT_KEY = "ToolTipText";
- private static final String NEXT_FOCUS = "nextFocus";
- /** Private flags **/
- private static final int REQUEST_FOCUS_DISABLED = 0;
- private static final int IS_DOUBLE_BUFFERED = 1;
- private static final int ANCESTOR_USING_BUFFER = 2;
- private static final int IS_PAINTING_TILE = 3;
- private static final int HAS_FOCUS = 4;
- private static final int IS_OPAQUE = 5;
- private static final int IS_PRINTING = 12;
- private static final int IS_PRINTING_ALL = 13;
- /**
- * Default JComponent constructor. This constructor does
- * no initialization beyond calling the Container constructor,
- * e.g. the initial layout manager is null.
- */
- public JComponent() {
- super();
- enableEvents(AWTEvent.FOCUS_EVENT_MASK);
- enableSerialization();
- }
- /**
- * Resets the UI property to a value from the current look and feel.
- * JComponent subclasses must override this method like this:
- * <pre>
- * public void updateUI() {
- * setUI((SliderUI)UIManager.getUI(this);
- * }
- * </pre>
- *
- * @see #setUI
- * @see UIManager#getLookAndFeel
- * @see UIManager#getUI
- */
- public void updateUI() {}
- /**
- * Set the look and feel delegate for this component.
- * JComponent subclasses generally override this method
- * to narrow the argument type, e.g. in JSlider:
- * <pre>
- * public void setUI(SliderUI newUI) {
- * super.setUI(newUI);
- * }
- * </pre>
- * <p>
- * Additionaly JComponent subclasses must provide a getUI
- * method that returns the correct type, e.g.
- * <pre>
- * public SliderUI getUI() {
- * return (SliderUI)ui;
- * }
- * </pre>
- *
- * @see #updateUI
- * @see UIManager#getLookAndFeel
- * @see UIManager#getUI
- * @beaninfo
- * bound: true
- * attribute: visualUpdate true
- * description: The component's look and feel delegate
- */
- protected void setUI(ComponentUI newUI) {
- /* We do not check that the UI instance is different
- * before allowing the switch in order to enable the
- * same UI instance *with different default settings*
- * to be installed.
- */
- if (ui != null) {
- ui.uninstallUI(this);
- }
- ComponentUI oldUI = ui;
- ui = newUI;
- if (ui != null) {
- ui.installUI(this);
- }
- firePropertyChange("UI", oldUI, newUI);
- revalidate();
- repaint();
- }
- /**
- * Return the UIDefaults key used to look up the name of the
- * swing.plaf.ComponentUI class that defines the look and feel
- * for this component. Most applications will never need to
- * call this method. Subclasses of JComponent that support
- * pluggable look and feel should override this method to
- * return a UIDefaults key that maps to the ComponentUI subclass
- * that defines their look and feel.
- *
- * @return The UIDefaults key for a ComponentUI subclass.
- * @see UIDefaults#getUI
- * @beaninfo
- * expert: true
- * description: UIClassID
- */
- public String getUIClassID() {
- return uiClassID;
- }
- /**
- * Returns the graphics object used to paint this component.
- * If DebugGraphics is turned on we create a new DebugGraphics
- * object if neccessary otherwise we just configure the
- * specified graphics objects foreground and font.
- *
- * @return A Graphics object configured for this component
- */
- protected Graphics getComponentGraphics(Graphics g) {
- Graphics componentGraphics = g;
- if (ui != null) {
- if ((DebugGraphics.debugComponentCount() != 0) &&
- (shouldDebugGraphics() != 0) &&
- !(g instanceof DebugGraphics)) {
- if(g instanceof SwingGraphics) {
- if(!(((SwingGraphics)g).subGraphics() instanceof DebugGraphics)) {
- componentGraphics = new DebugGraphics(((SwingGraphics)g).subGraphics(),this);
- componentGraphics = SwingGraphics.createSwingGraphics(componentGraphics);
- }
- } else {
- componentGraphics = new DebugGraphics(g,this);
- }
- }
- }
- componentGraphics.setColor(getForeground());
- componentGraphics.setFont(getFont());
- return componentGraphics;
- }
- /**
- * If the UI delegate is non-null, call its paint
- * method. We pass the delegate a copy of the Graphics
- * object to protect the rest of the paint code from
- * irrevocable changes (e.g. Graphics.translate()).
- *
- * @see #paint
- */
- protected void paintComponent(Graphics g) {
- if (ui != null) {
- Graphics scratchGraphics = SwingGraphics.createSwingGraphics(g);
- try {
- ui.update(scratchGraphics, this);
- }
- finally {
- scratchGraphics.dispose();
- }
- }
- }
- /**
- * Paint this component's children.
- * If shouldUseBuffer is true, no component ancestor has a buffer and
- * the component children can use a buffer if they have one.
- * Otherwise, one ancestor has a buffer currently in use and children
- * should not use a buffer to paint.
- * @see #paint
- * @see java.awt.Container#paint
- */
- protected void paintChildren(Graphics g) {
- boolean isJComponent;
- Graphics sg = null;
- try {
- synchronized(getTreeLock()) {
- boolean printing = getFlag(IS_PRINTING);
- int i = getComponentCount() - 1;
- if (i < 0) {
- return;
- }
- sg = SwingGraphics.createSwingGraphics(g);
- // If we are only to paint to a specific child, determine
- // its index.
- if (paintingChild != null &&
- (paintingChild instanceof JComponent) &&
- ((JComponent)paintingChild).isOpaque()) {
- for (; i >= 0; i--) {
- if (getComponent(i) == paintingChild){
- break;
- }
- }
- }
- if(tmpRect == null) {
- tmpRect = new Rectangle();
- }
- boolean checkSiblings = (!isOptimizedDrawingEnabled() &&
- checkIfChildObscuredBySibling());
- Rectangle clipBounds = null;
- if (checkSiblings) {
- clipBounds = sg.getClipBounds();
- if (clipBounds == null) {
- clipBounds = new Rectangle(0, 0, _bounds.width,
- _bounds.height);
- }
- }
- for (; i >= 0 ; i--) {
- Component comp = getComponent(i);
- if (comp != null && isLightweightComponent(comp) &&
- (comp.isVisible() == true)) {
- Rectangle cr;
- isJComponent = (comp instanceof JComponent);
- if(isJComponent) {
- cr = tmpRect;
- ((JComponent)comp).getBounds(cr);
- } else {
- cr = comp.getBounds();
- }
- boolean hitClip =
- g.hitClip(cr.x, cr.y, cr.width, cr.height);
- if (hitClip) {
- if (checkSiblings && i > 0) {
- int x = cr.x;
- int y = cr.y;
- int width = cr.width;
- int height = cr.height;
- SwingUtilities.computeIntersection
- (clipBounds.x, clipBounds.y,
- clipBounds.width, clipBounds.height, cr);
- if(rectangleIsObscuredBySibling(i, cr.x, cr.y,
- cr.width, cr.height)) {
- continue;
- }
- cr.x = x;
- cr.y = y;
- cr.width = width;
- cr.height = height;
- }
- Graphics cg = SwingGraphics.createSwingGraphics(
- sg, cr.x, cr.y, cr.width, cr.height);
- cg.setColor(comp.getForeground());
- cg.setFont(comp.getFont());
- boolean shouldSetFlagBack = false;
- try {
- if(isJComponent) {
- if(getFlag(ANCESTOR_USING_BUFFER)) {
- ((JComponent)comp).setFlag(ANCESTOR_USING_BUFFER,true);
- shouldSetFlagBack = true;
- }
- if(getFlag(IS_PAINTING_TILE)) {
- ((JComponent)comp).setFlag(IS_PAINTING_TILE,true);
- shouldSetFlagBack = true;
- }
- if(!printing) {
- ((JComponent)comp).paint(cg);
- }
- else {
- if (!getFlag(IS_PRINTING_ALL)) {
- comp.print(cg);
- }
- else {
- comp.printAll(cg);
- }
- }
- } else {
- if (!printing) {
- comp.paint(cg);
- }
- else {
- if (!getFlag(IS_PRINTING_ALL)) {
- comp.print(cg);
- }
- else {
- comp.printAll(cg);
- }
- }
- }
- } finally {
- cg.dispose();
- if(shouldSetFlagBack) {
- ((JComponent)comp).setFlag(ANCESTOR_USING_BUFFER,false);
- ((JComponent)comp).setFlag(IS_PAINTING_TILE,false);
- }
- }
- }
- }
- }
- }
- } finally {
- if (sg != null) {
- sg.dispose();
- }
- }
- }
- /**
- * Paint the component's border.
- *
- * @see #paint
- * @see #setBorder
- */
- protected void paintBorder(Graphics g) {
- Border border = getBorder();
- if (border != null) {
- border.paintBorder(this, g, 0, 0, getWidth(), getHeight());
- }
- }
- /**
- * Calls paint(g). Doesn't clear the background but see
- * ComponentUI.update() which is called by paintComponent.
- *
- * @see #paint
- * @see #paintComponent
- * @see javax.swing.plaf.ComponentUI
- */
- public void update(Graphics g) {
- paint(g);
- }
- /**
- * This method is invoked by Swing to draw components.
- * Applications should not invoke paint directly,
- * but should instead use the <code>repaint</code> method to
- * schedule the component for redrawing.
- * <p>
- * This method actually delegates the work of painting to three
- * protected methods: <code>paintComponent</code>, <code>paintBorder</code>,
- * and <code>paintChildren</code>. They're called in the order
- * listed to ensure that children appear on top of component itself.
- * Generally speaking, the component and its children should not
- * paint in the insets area allocated to the border. Subclasses can
- * just override this method, as always. A subclass that just
- * wants to specialize the UI (look and feel) delegates paint
- * method should just override <code>paintComponent</code>.
- *
- * @see #paintComponent
- * @see #paintBorder
- * @see #paintChildren
- * @see #getComponentGraphics
- * @see #repaint
- */
- public void paint(Graphics g) {
- boolean shouldClearPaintFlags = false;
- if ((getWidth() <= 0) || (getHeight() <= 0)) {
- return;
- }
- Graphics componentGraphics = getComponentGraphics(g);
- Graphics co = SwingGraphics.createSwingGraphics(componentGraphics);
- try {
- Image offscr = null;
- RepaintManager repaintManager = RepaintManager.currentManager(this);
- Rectangle clipRect = co.getClipBounds();
- int clipX;
- int clipY;
- int clipW;
- int clipH;
- if (clipRect == null) {
- clipX = clipY = 0;
- clipW = _bounds.width;
- clipH = _bounds.height;
- }
- else {
- clipX = clipRect.x;
- clipY = clipRect.y;
- clipW = clipRect.width;
- clipH = clipRect.height;
- }
- if(clipW > getWidth()) {
- clipW = getWidth();
- }
- if(clipH > getHeight()) {
- clipH = getHeight();
- }
- if(getParent() != null && !(getParent() instanceof JComponent)) {
- adjustPaintFlags();
- shouldClearPaintFlags = true;
- }
- int bw,bh;
- boolean printing = getFlag(IS_PRINTING);
- if(!printing && repaintManager.isDoubleBufferingEnabled() &&
- !getFlag(ANCESTOR_USING_BUFFER) && isDoubleBuffered() &&
- (offscr = repaintManager.getOffscreenBuffer
- (this,clipW,clipH)) != null &&
- (bw = offscr.getWidth(null)) > 0 &&
- (bh = offscr.getHeight(null)) > 0) {
- int x,y,maxx,maxy;
- Graphics sg =
- SwingGraphics.createSwingGraphics(offscr.getGraphics());
- try {
- sg.translate(-clipX,-clipY);
- bw = offscr.getWidth(null);
- bh = offscr.getHeight(null);
- if (bw > clipW) {
- bw = clipW;
- }
- if (bh > clipH) {
- bh = clipH;
- }
- setFlag(ANCESTOR_USING_BUFFER,true);
- setFlag(IS_PAINTING_TILE,true);
- for(x = 0, maxx = clipW; x < maxx ; x += bw ) {
- for(y=0, maxy = clipH; y < maxy ; y += bh) {
- if((y+bh) >= maxy && (x+bw) >= maxx)
- setFlag(IS_PAINTING_TILE,false);
- sg.translate(-x,-y);
- sg.setClip(clipX+x,clipY + y,bw,bh);
- if(!rectangleIsObscured(clipX,clipY,bw,bh)) {
- paintComponent(sg);
- paintBorder(sg);
- }
- paintChildren(sg);
- co.drawImage(offscr,clipX + x,clipY + y,this);
- sg.translate(x,y);
- }
- }
- } finally {
- setFlag(ANCESTOR_USING_BUFFER,false);
- setFlag(IS_PAINTING_TILE,false);
- sg.dispose();
- }
- } else {
- if (!rectangleIsObscured(clipX,clipY,clipW,clipH)) {
- paintComponent(co);
- paintBorder(co);
- }
- paintChildren(co);
- }
- } finally {
- co.dispose();
- if(shouldClearPaintFlags) {
- setFlag(ANCESTOR_USING_BUFFER,false);
- setFlag(IS_PAINTING_TILE,false);
- setFlag(IS_PRINTING,false);
- setFlag(IS_PRINTING_ALL,false);
- }
- }
- }
- private void adjustPaintFlags() {
- JComponent jparent = null;
- Container parent;
- for(parent = getParent() ; parent != null ; parent =
- parent.getParent()) {
- if(parent instanceof JComponent) {
- jparent = (JComponent) parent;
- if(jparent.getFlag(ANCESTOR_USING_BUFFER))
- setFlag(ANCESTOR_USING_BUFFER, true);
- if(jparent.getFlag(IS_PAINTING_TILE))
- setFlag(IS_PAINTING_TILE, true);
- if(jparent.getFlag(IS_PRINTING))
- setFlag(IS_PRINTING, true);
- if(jparent.getFlag(IS_PRINTING_ALL))
- setFlag(IS_PRINTING_ALL, true);
- break;
- }
- }
- }
- public void printAll(Graphics g) {
- setFlag(IS_PRINTING_ALL, true);
- try {
- print(g);
- }
- finally {
- setFlag(IS_PRINTING_ALL, false);
- }
- }
- public void print(Graphics g) {
- setFlag(IS_PRINTING, true);
- try {
- paint(g);
- }
- finally {
- setFlag(IS_PRINTING, false);
- }
- }
- /**
- * Returns true if the receiving component is currently painting a tile.
- * If this method returns true, paint will be called again for another
- * tile. This method returns false if you are not painting a tile or
- * if the last tile is painted.
- * Use this method to keep some state you might need between tiles.
- */
- public boolean isPaintingTile() {
- return getFlag(IS_PAINTING_TILE);
- }
- /**
- * Override this method and return true if your component is the root of
- * of a component tree with its own focus cycle.
- */
- public boolean isFocusCycleRoot() {
- return false;
- }
- /**
- * Override this method and return true if your JComponent manages focus.
- * If your component manages focus, the focus manager will handle your
- * component's children. All key event will be sent to your key listener
- * including TAB and SHIFT+TAB. CONTROL + TAB and CONTROL + SHIFT + TAB
- * will move the focus to the next / previous component.
- */
- public boolean isManagingFocus() {
- return false;
- }
- /**
- * Specifies the next component to get the focus after this one,
- * for example, when the tab key is pressed. Invoke this method
- * to override the default focus-change sequence.
- * @beaninfo
- * expert: true
- * description: The next component to get focus after this one.
- */
- public void setNextFocusableComponent(Component aComponent) {
- putClientProperty(NEXT_FOCUS,aComponent);
- }
- /**
- * Return the next focusable component or null if the focus manager
- * should choose the next focusable component automatically
- */
- public Component getNextFocusableComponent() {
- return (Component) getClientProperty(NEXT_FOCUS);
- }
- /**
- * Set whether the receiving component can obtain the focus by
- * calling requestFocus. The default value is true.
- * Note: Setting this property to false will not prevent the focus
- * manager from setting the focus to this component, it will prevent
- * the component from getting the focus when the focus is requested
- * explicitly. Override isFocusTraversable and return false if the
- * component should never get the focus.
- * @beaninfo
- * expert: true
- * description: Whether the component can obtain the focus by calling requestFocus.
- */
- public void setRequestFocusEnabled(boolean aFlag) {
- setFlag(REQUEST_FOCUS_DISABLED,(aFlag ? false:true));
- }
- /** Return whether the receiving component can obtain the focus by
- * calling requestFocus
- * @see #setRequestFocusEnabled
- */
- public boolean isRequestFocusEnabled() {
- return (getFlag(REQUEST_FOCUS_DISABLED) ? false : true);
- }
- /** Set focus on the receiving component if isRequestFocusEnabled returns true **/
- public void requestFocus() {
- /* someone other then the focus manager is requesting focus,
- so we clear the focus manager's idea of focus history */
- FocusManager focusManager = FocusManager.getCurrentManager();
- if (focusManager instanceof DefaultFocusManager)
- ((DefaultFocusManager)focusManager).clearHistory();
- if(isRequestFocusEnabled()) {
- super.requestFocus();
- }
- }
- /** Set the focus on the receiving component. This method is for focus managers, you
- * rarely want to call this method, use requestFocus() enstead.
- */
- public void grabFocus() {
- super.requestFocus();
- }
- /**
- * Set the preferred size of the receiving component.
- * if <code>preferredSize</code> is null, the UI will
- * be asked for the preferred size
- * @beaninfo
- * preferred: true
- * bound: true
- * description: The preferred size of the component.
- */
- public void setPreferredSize(Dimension preferredSize) {
- Dimension old = this.preferredSize;
- this.preferredSize = preferredSize;
- firePropertyChange("preferredSize", old, preferredSize);
- }
- /**
- * If the preferredSize has been set to a non-null value
- * just return it. If the UI delegates getPreferredSize()
- * method returns a non null then value return that, otherwise
- * defer to the components layout manager.
- *
- * @return the value of the preferredSize property.
- * @see #setPreferredSize
- */
- public Dimension getPreferredSize() {
- if (preferredSize != null) {
- return preferredSize;
- }
- Dimension size = null;
- if (ui != null) {
- size = ui.getPreferredSize(this);
- }
- return (size != null) ? size : super.getPreferredSize();
- }
- /**
- * Sets the maximumSize of this component to a constant
- * value. Subsequent calls to getMaximumSize will always
- * return this value, the components UI will not be asked
- * to compute it. Setting the maximumSize to null
- * restores the default behavior.
- *
- * @see #getMaximumSize
- * @beaninfo
- * bound: true
- * description: The maximum size of the component.
- */
- public void setMaximumSize(Dimension maximumSize) {
- Dimension old = this.maximumSize;
- this.maximumSize = maximumSize;
- firePropertyChange("maximumSize", old, maximumSize);
- }
- /**
- * If the maximumSize has been set to a non-null value
- * just return it. If the UI delegates getMaximumSize()
- * method returns a non null value then return that, otherwise
- * defer to the components layout manager.
- *
- * @return the value of the maximumSize property.
- * @see #setMaximumSize
- */
- public Dimension getMaximumSize() {
- if (maximumSize != null) {
- return maximumSize;
- }
- Dimension size = null;
- if (ui != null) {
- size = ui.getMaximumSize(this);
- }
- return (size != null) ? size : super.getMaximumSize();
- }
- /**
- * Sets the minimumSize of this component to a constant
- * value. Subsequent calls to getMinimumSize will always
- * return this value, the components UI will not be asked
- * to compute it. Setting the minimumSize to null
- * restores the default behavior.
- *
- * @see #getMinimumSize
- * @beaninfo
- * bound: true
- * description: The minimum size of the component.
- */
- public void setMinimumSize(Dimension minimumSize) {
- Dimension old = this.minimumSize;
- this.minimumSize = minimumSize;
- firePropertyChange("minimumSize", old, minimumSize);
- }
- /**
- * If the minimumSize has been set to a non-null value
- * just return it. If the UI delegates getMinimumSize()
- * method returns a non null value then return that, otherwise
- * defer to the components layout manager.
- *
- * @return the value of the minimumSize property.
- * @see #setMinimumSize
- */
- public Dimension getMinimumSize() {
- if (minimumSize != null) {
- return minimumSize;
- }
- Dimension size = null;
- if (ui != null) {
- size = ui.getMinimumSize(this);
- }
- return (size != null) ? size : super.getMinimumSize();
- }
- /**
- * Give the UI delegate an opportunity to define the precise
- * shape of this component for the sake of mouse processing.
- *
- * @return true if this component logically contains x,y.
- * @see java.awt.Component#contains(int, int)
- */
- public boolean contains(int x, int y) {
- return (ui != null) ? ui.contains(this, x, y) : super.contains(x, y);
- }
- /**
- * Sets the border of this component. The Border object is
- * responsible for defining the insets for the component
- * (overriding any insets set directly on the component) and
- * for optionally rendering any border decorations within the
- * bounds of those insets. Borders should be used (rather
- * than insets) for creating both decorative and non-decorative
- * (e.g. margins and padding) regions for a swing component.
- * Compound borders can be used to nest multiple borders within a
- * single component.
- * <p>
- * This is a bound property.
- *
- * @param border the border to be rendered for this component
- * @see Border
- * @see CompoundBorder
- * @beaninfo
- * bound: true
- * preferred: true
- * attribute: visualUpdate true
- * description: The component's border.
- */
- public void setBorder(Border border) {
- Border oldBorder = this.border;
- this.border = border;
- firePropertyChange("border", oldBorder, border);
- if (border != oldBorder) {
- if (border == null || oldBorder == null ||
- !(border.getBorderInsets(this).equals(oldBorder.getBorderInsets(this)))) {
- revalidate();
- }
- repaint();
- }
- }
- /**
- * Returns the border of this component or null if no border is
- * currently set.
- *
- * @return the border object for this component
- * @see #setBorder
- */
- public Border getBorder() {
- return border;
- }
- /**
- * If a border has been set on this component, returns the
- * border's insets, else calls super.getInsets.
- *
- * @return the value of the insets property.
- * @see #setBorder
- */
- public Insets getInsets() {
- if (border != null) {
- return border.getBorderInsets(this);
- }
- return super.getInsets();
- }
- /**
- * Returns an Insets object containing this component's inset
- * values. The passed-in Insets object will be reused if possible.
- * Calling methods cannot assume that the same object will be returned,
- * however. All existing values within this object are overwritten.
- *
- * @param insets the Insets object which can be reused.
- * @see #getInsets
- * @beaninfo
- * expert: true
- */
- public Insets getInsets(Insets insets) {
- if (border != null) {
- if (border instanceof AbstractBorder) {
- return ((AbstractBorder)border).getBorderInsets(this, insets);
- } else {
- // Can't reuse border insets because the Border interface
- // can't be enhanced.
- return border.getBorderInsets(this);
- }
- } else {
- // super.getInsets() always returns an Insets object with
- // all of its value zeroed. No need for a new object here.
- insets.left = insets.top = insets.right = insets.bottom = 0;
- return insets;
- }
- }
- /**
- * Overrides <code>Container.getAlignmentY</code> to return
- * the horizontal alignment.
- *
- * @return the value of the alignmentY property.
- * @see #setAlignmentY
- * @see java.awt.Component#getAlignmentY
- */
- public float getAlignmentY() {
- return (alignmentY != null) ? alignmentY.floatValue() : super.getAlignmentY();
- }
- /**
- * Set the the horizontal alignment.
- *
- * @see #getAlignmentY
- * @beaninfo
- * description: The preferred vertical alignment of the component
- */
- public void setAlignmentY(float alignmentY) {
- this.alignmentY = new Float(alignmentY > 1.0f ? 1.0f : alignmentY < 0.0f ? 0.0f : alignmentY);
- }
- /**
- * Overrides <code>Container.getAlignmentX</code> to return
- * the vertical alignment.
- *
- * @return the value of the alignmentX property.
- * @see #setAlignmentX
- * @see java.awt.Component#getAlignmentX
- */
- public float getAlignmentX() {
- return (alignmentX != null) ? alignmentX.floatValue() : super.getAlignmentX();
- }
- /**
- * Set the the vertical alignment.
- *
- * @see #getAlignmentX
- * @beaninfo
- * description: The preferred horizontal alignment of the component
- */
- public void setAlignmentX(float alignmentX) {
- this.alignmentX = new Float(alignmentX > 1.0f ? 1.0f : alignmentX < 0.0f ? 0.0f : alignmentX);
- }
- /**
- * Returns this component's graphics context, which lets you draw
- * on a component. Use this method get a Graphics object and
- * then invoke oeprations on that object to draw on the component.
- */
- public Graphics getGraphics() {
- if (shouldDebugGraphics() != 0) {
- DebugGraphics graphics = new DebugGraphics(super.getGraphics(),
- this);
- return graphics;
- }
- return super.getGraphics();
- }
- /** Enables or disables diagnostic information about every graphics
- * operation performed within the component or one of its children. The
- * value of <b>debugOptions</b> determines how the component should
- * display this information:
- * <ul>
- * <li>DebugGraphics.LOG_OPTION - causes a text message to be printed.
- * <li>DebugGraphics.FLASH_OPTION - causes the drawing to flash several
- * times.
- * <li>DebugGraphics.BUFFERED_OPTION - creates an ExternalWindow that
- * displays the operations performed on the View's offscreen buffer.
- * </ul>
- * <b>debug</b> is bitwise OR'd into the current value.
- * DebugGraphics.NONE_OPTION disables debugging.
- * A value of 0 causes no changes to the debugging options.
- * @beaninfo
- * preferred: true
- * description: Diagnostic options for graphics operations.
- */
- public void setDebugGraphicsOptions(int debugOptions) {
- DebugGraphics.setDebugOptions(this, debugOptions);
- }
- /** Returns the state of graphics debugging.
- * @see #setDebugGraphicsOptions
- */
- public int getDebugGraphicsOptions() {
- return DebugGraphics.getDebugOptions(this);
- }
- /**
- * Returns <b>true</b> if debug information is enabled for this JComponent
- * or one if its parents.
- */
- int shouldDebugGraphics() {
- return DebugGraphics.shouldComponentDebug(this);
- }
- /**
- * Register a new keyboard action.
- * <b>anAction</b> will be invoked if a key event matching <b>aKeyStroke</b> occurs
- * and <b>aCondition</b> is verified. The KeyStroke object defines a
- * particular combination of a keyboard key and one or more modifiers
- * (alt, shift, ctrl, meta).
- * <p>
- * The <b>aCommand</b> will be set in the delivered event if specified.
- * <p>
- * The Condition can be one of:
- * <blockquote>
- * <DL>
- * <DT>WHEN_FOCUSED
- * <DD>The action will be invoked only when the keystroke occurs
- * while the component has the focus.
- * <DT>WHEN_IN_FOCUSED_WINDOW
- * <DD>The action will be invoked when the keystroke occurs while
- * the component has the focus or if the component is in the
- * window that has the focus. Note that the component need not
- * be an immediate descendent of the window -- it can be
- * anywhere in the window's containment hierarchy. In other
- * words, whenever <em>any</em> component in the window has the focus,
- * the action registered with this component is invoked.
- * <DT>WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
- * <DD>The action will be invoked when the keystroke occurs while the
- * component has the focus or if the component is an ancestor of
- * the component that has the focus.
- * </DL>
- * </blockquote>
- * <p>
- * The combination of keystrokes and conditions lets you define high
- * level (semantic) action events for a specified keystroke+modifier
- * combination (using the KeyStroke class) and direct to a parent or
- * child of a component that has the focus, or to the component itself.
- * In other words, in any hierarchical structure of components, an
- * arbitrary key-combination can be immediately directed to the
- * appropriate component in the hierarchy, and cause a specific method
- * to be invoked (usually by way of adapter objects).
- * <p>
- * If an action has already been registered for the receiving
- * container, with the same charCode and the same modifiers,
- * <b>anAction</b> will replace the action.
- *
- * @see KeyStroke
- */
- public void registerKeyboardAction(ActionListener anAction,String aCommand,KeyStroke aKeyStroke,int aCondition) {
- Hashtable bindings;
- boolean firstKeyboardAction = false;
- synchronized(this) {
- bindings = (Hashtable) getClientProperty(KEYBOARD_BINDINGS_KEY);
- if(bindings == null) {
- bindings = new Hashtable();
- putClientProperty(KEYBOARD_BINDINGS_KEY,bindings);
- firstKeyboardAction = true;
- }
- }
- synchronized(bindings) {
- bindings.put(aKeyStroke,new KeyboardBinding(anAction,aCommand,aKeyStroke,aCondition));
- }
- /* This is the first time a keyboard binding is added, let's order
- * keyboard events...
- * ALERT: we need to enable events. Adding a listener will not work since
- * we want our listener to be after all other listeners.
- */
- if(firstKeyboardAction) {
- enableEvents(AWTEvent.KEY_EVENT_MASK);
- }
- if (getParent() != null && aCondition == WHEN_IN_FOCUSED_WINDOW) {
- registerWithKeyboardManager(aKeyStroke);
- }
- }
- void registerWithKeyboardManager(KeyStroke aKeyStroke) {
- KeyboardManager.getCurrentManager().registerKeyStroke(aKeyStroke, this);
- }
- void unregisterWithKeyboardManager(KeyStroke aKeyStroke) {
- KeyboardManager.getCurrentManager().unregisterKeyStroke(aKeyStroke, this);
- }
- /**
- * Calls registerKeyboardAction(ActionListener,String,KeyStroke,condition) with a null command.
- */
- public void registerKeyboardAction(ActionListener anAction,KeyStroke aKeyStroke,int aCondition) {
- registerKeyboardAction(anAction,null,aKeyStroke,aCondition);
- }
- private Hashtable keyboardBindings() {
- Hashtable bindings;
- synchronized(this) {
- bindings = (Hashtable) getClientProperty(KEYBOARD_BINDINGS_KEY);
- }
- return bindings;
- }
- /**
- * Unregister a keyboard action.
- *
- * @see #registerKeyboardAction
- */
- public void unregisterKeyboardAction(KeyStroke aKeyStroke) {
- Hashtable bindings = keyboardBindings();
- KeyboardBinding aBinding;
- if(bindings == null)
- return;
- synchronized(bindings) {
- aBinding = (KeyboardBinding)bindings.remove(aKeyStroke);
- }
- if(bindings.size() == 0) {
- /** ALERT. We need a way to disable keyboard events only if there is no
- * keyboard listener.
- */
- }
- if ( aBinding != null && aBinding.condition == WHEN_IN_FOCUSED_WINDOW) {
- unregisterWithKeyboardManager(aKeyStroke);
- }
- }
- /**
- * Return the KeyStrokes that will initiate registered actions.
- *
- * @return an array of KeyStroke objects
- * @see #registerKeyboardAction
- */
- public KeyStroke[] getRegisteredKeyStrokes() {
- Hashtable bindings = keyboardBindings();
- KeyStroke result[];
- int i;
- Enumeration keys;
- if(bindings == null)
- return new KeyStroke[0];
- synchronized(bindings) {
- result = new KeyStroke[bindings.size()];
- i = 0;
- keys = bindings.keys();
- while(keys.hasMoreElements())
- result[i++] = (KeyStroke) keys.nextElement();
- }
- return result;
- }
- /**
- * Return the condition that determines whether a registered action
- * occurs in response to the specified keystroke.
- *
- * @return the action-keystroke condition
- * @see #registerKeyboardAction
- */
- public int getConditionForKeyStroke(KeyStroke aKeyStroke) {
- Hashtable bindings = keyboardBindings();
- if(bindings == null)
- return UNDEFINED_CONDITION;
- synchronized(bindings) {
- KeyboardBinding kb = (KeyboardBinding) bindings.get(aKeyStroke);
- if(kb != null) {
- return kb.getCondition();
- }
- }
- return UNDEFINED_CONDITION;
- }
- /**
- * Return the object that will perform the action registered for a
- * given keystroke.
- *
- * @return the ActionListener object invoked when the keystroke occurs
- * @see #registerKeyboardAction
- */
- public ActionListener getActionForKeyStroke(KeyStroke aKeyStroke) {
- Hashtable bindings = keyboardBindings();
- if(bindings == null)
- return null;
- synchronized(bindings) {
- KeyboardBinding kb = (KeyboardBinding) bindings.get(aKeyStroke);
- if(kb != null) {
- return kb.getAction();
- }
- }
- return null;
- }
- /**
- * Unregister all keyboard actions
- *
- * @see #registerKeyboardAction
- */
- public void resetKeyboardActions() {
- synchronized(this) {
- Hashtable bindings = (Hashtable) getClientProperty(KEYBOARD_BINDINGS_KEY);
- if(bindings != null) {
- bindings.clear();
- }
- }
- /* ALERT. We need a way to disable keyboard events only if there is no
- * keyboard listener.
- */
- }
- /**
- * Request the focus for the component that should have the focus
- * by default. The default implementation will recursively request
- * the focus on the first component that is focus-traversable.
- *
- * @return false if the focus has not been set, otherwise
- * return true
- */
- public boolean requestDefaultFocus() {
- Component ca[] = getComponents();
- int i;
- for(i=0 ; i < ca.length ; i++) {
- if(ca[i].isFocusTraversable()) {
- if(ca[i] instanceof JComponent) {
- ((JComponent)ca[i]).grabFocus();
- } else {
- ca[i].requestFocus();
- }
- return true;
- }
- if(ca[i] instanceof JComponent && !((JComponent)ca[i]).isManagingFocus()) {
- if(((JComponent)(ca[i])).requestDefaultFocus()) {
- return true;
- }
- }
- }
- return false;
- }
- /**
- * Makes the component visible or invisible.
- * Overrides <code>Component.setVisible</code>.
- *
- * @param aFlag true to make the component visible
- *
- * @beaninfo
- * attribute: visualUpdate true
- */
- public void setVisible(boolean aFlag) {
- if(aFlag != isVisible()) {
- super.setVisible(aFlag);
- Container parent = getParent();
- if(parent != null) {
- Rectangle r = getBounds();
- parent.repaint(r.x,r.y,r.width,r.height);
- }
- // Some (all should) LayoutManagers do not consider components
- // that are not visible. As such we need to revalidate when the
- // visible bit changes.
- revalidate();
- if (accessibleContext != null) {
- if (aFlag) {
- accessibleContext.firePropertyChange(
- AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
- null, AccessibleState.VISIBLE);
- } else {
- accessibleContext.firePropertyChange(
- AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
- AccessibleState.VISIBLE, null);
- }
- }
- }
- }
- /**
- * Sets whether or not this component is enabled.
- * A component which is enabled may respond to user input,
- * while a component which is not enabled cannot respond to
- * user input. Some components may alter their visual
- * representation when they are disabled in order to
- * provide feedback to the user that they cannot take input.
- *
- * @see java.awt.Component#isEnabled
- *
- * @beaninfo
- * preferred: true
- * bound: true
- * attribute: visualUpdate true
- * description: The enabled state of the component
- */
- public void setEnabled(boolean enabled) {
- boolean oldEnabled = isEnabled();
- super.setEnabled(enabled);
- if (!enabled && hasFocus()) {
- FocusManager.getCurrentManager().focusPreviousComponent(this);
- }
- firePropertyChange("enabled", oldEnabled, enabled);
- if (enabled != oldEnabled) {
- repaint();
- }
- }
- /**
- * Sets the foreground color of this component.
- *
- * @see java.awt.Component#getForeground
- *
- * @beaninfo
- * preferred: true
- * bound: true
- * attribute: visualUpdate true
- * description: The foreground color of the component.
- */
- public void setForeground(Color fg) {
- Color oldFg = getForeground();
- super.setForeground(fg);
- if ((oldFg != null) ? !oldFg.equals(fg) : ((fg != null) && !fg.equals(oldFg))) {
- // foreground already bound in AWT1.2
- if (!SwingUtilities.is1dot2) {
- firePropertyChange("foreground", oldFg, fg);
- }
- repaint();
- }
- }
- /**
- * Sets the background color of this component.
- *
- * @see java.awt.Component#getBackground
- *
- * @beaninfo
- * preferred: true
- * bound: true
- * attribute: visualUpdate true
- * description: The background color of the component.
- */
- public void setBackground(Color bg) {
- Color oldBg = getBackground();
- super.setBackground(bg);
- if ((oldBg != null) ? !oldBg.equals(bg) : ((bg != null) && !bg.equals(oldBg))) {
- // background already bound in AWT1.2
- if (!SwingUtilities.is1dot2) {
- firePropertyChange("background", oldBg, bg);
- }
- repaint();
- }
- }
- /**
- * Sets the font for this component.
- *
- * @see java.awt.Component#getFont
- *
- * @beaninfo
- * preferred: true
- * bound: true
- * attribute: visualUpdate true
- * description: The font for the component.
- */
- public void setFont(Font font) {
- Font oldFont = getFont();
- super.setFont(font);
- // font already bound in AWT1.2
- if (!SwingUtilities.is1dot2) {
- firePropertyChange("font", oldFont, font);
- }
- if (font != oldFont) {
- revalidate();
- repaint();
- }
- }
- /**
- * Identifies whether or not this component can receive the focus.
- * A disabled button, for example, would return false.
- *
- * @return true if this component can receive the focus
- */
- public boolean isFocusTraversable() {
- boolean result = false;
- Hashtable bindings;
- synchronized(this) {
- bindings = (Hashtable) getClientProperty(KEYBOARD_BINDINGS_KEY);
- }
- if(bindings != null) {
- synchronized(bindings) {
- Enumeration keys = bindings.keys();
- KeyboardBinding b;
- while(keys.hasMoreElements()) {
- b = (KeyboardBinding) bindings.get(keys.nextElement());
- if(b.getCondition() == WHEN_FOCUSED) {
- result = true;
- break;
- }
- }
- }
- }
- return result;
- }
- protected void processFocusEvent(FocusEvent e) {
- switch(e.getID()) {
- case FocusEvent.FOCUS_GAINED:
- setFlag(HAS_FOCUS, true);
- break;
- case FocusEvent.FOCUS_LOST:
- setFlag(HAS_FOCUS, false);
- break;
- }
- // Call super *after* setting flag, in case listener calls paint.
- super.processFocusEvent(e);
- }
- /**
- * Process any key events that the component itself
- * recognizes. This will be called after the focus
- * manager and any interested listeners have been
- * given a chance to steal away the event. This
- * method will only be called is the event has not
- * yet been consumed. This method is called prior
- * to the keyboard UI logic.
- * <p>
- * This is implemented to do nothing. Subclasses would
- * normally override this method if they process some
- * key events themselves. If the event is processed,
- * it should be consumed.
- */
- protected void processComponentKeyEvent(KeyEvent e) {
- }
- /** Override processKeyEvent to process events **/
- protected void processKeyEvent(KeyEvent e) {
- // focus manager gets to steal the event if it wants it.
- boolean result;
- boolean shouldProcessKey = false;
- if(FocusManager.isFocusManagerEnabled()) {
- FocusManager focusManager = FocusManager.getCurrentManager();
- focusManager.processKeyEvent(this,e);
- if(e.isConsumed()) {
- return;
- }
- }
- // This gives the key event listeners a crack at the event
- super.processKeyEvent(e);
- // give the component itself a crack at the event
- if (! e.isConsumed()) {
- processComponentKeyEvent(e);
- }
- if(e.getID() == KeyEvent.KEY_PRESSED) {
- shouldProcessKey = true;
- if(!KeyboardState.keyIsPressed(e.getKeyCode()))
- KeyboardState.registerKeyPressed(e.getKeyCode());
- } else if(e.getID() == KeyEvent.KEY_RELEASED) {
- if(KeyboardState.keyIsPressed(e.getKeyCode())) {
- shouldProcessKey = true;
- KeyboardState.registerKeyReleased(e.getKeyCode());
- }
- } else if(e.getID() == KeyEvent.KEY_TYPED) {
- shouldProcessKey = true;
- }
- if(e.isConsumed()) {
- return;
- }
- // (PENDING) Hania & Steve - take out this block? Do we need to do this pressed stuff?
- // And, shouldProcessKey, do we need it?
- if(shouldProcessKey && e.getID() == KeyEvent.KEY_PRESSED) {
- result = processKeyBindings(e,true);
- if(result)
- e.consume();
- } else if(shouldProcessKey && e.getID() == KeyEvent.KEY_RELEASED) {
- result = processKeyBindings(e,false);
- if(result) {
- e.consume();
- }
- } else if(shouldProcessKey && e.getID() == KeyEvent.KEY_TYPED) {
- result = processKeyBindings(e,false);
- if(result) {
- e.consume();
- }
- }
- }
- KeyboardBinding bindingForKeyStroke(KeyStroke ks,int condition) {
- Hashtable bindings;
- KeyboardBinding b;
- KeyboardBinding result = null;
- // synchronized(this) {
- bindings = (Hashtable) getClientProperty(KEYBOARD_BINDINGS_KEY);
- // }
- if(bindings != null) {
- // synchronized(bindings) {
- b = (KeyboardBinding) bindings.get(ks);
- // System.out.println("Bindings are " + bindings);
- if(b != null) {
- ActionListener action = b.getAction();
- if((action instanceof Action) && !(((Action)action).isEnabled()))
- action = null;
- if(action != null) {
- switch(b.getCondition()) {
- case WHEN_FOCUSED:
- if(condition == WHEN_FOCUSED)
- result = b;
- break;
- case WHEN_ANCESTOR_OF_FOCUSED_COMPONENT:
- if(condition == WHEN_FOCUSED ||
- condition == WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
- result = b;
- break;
- case WHEN_IN_FOCUSED_WINDOW:
- if(condition == WHEN_FOCUSED ||
- conditio