< prev index next >

src/java.desktop/share/classes/java/awt/Component.java

Print this page


   1 /*
   2  * Copyright (c) 1995, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.awt;
  26 
  27 import java.io.PrintStream;
  28 import java.io.PrintWriter;
  29 import java.util.Objects;
  30 import java.util.Vector;
  31 import java.util.Locale;
  32 import java.util.EventListener;
  33 import java.util.HashSet;
  34 import java.util.Map;
  35 import java.util.Set;
  36 import java.util.Collections;
  37 import java.awt.peer.ComponentPeer;
  38 import java.awt.peer.ContainerPeer;
  39 import java.awt.peer.LightweightPeer;













  40 import java.awt.image.BufferStrategy;

  41 import java.awt.image.ImageObserver;
  42 import java.awt.image.ImageProducer;
  43 import java.awt.image.ColorModel;
  44 import java.awt.image.VolatileImage;
  45 import java.awt.event.*;
  46 import java.io.Serializable;
  47 import java.io.ObjectOutputStream;
  48 import java.io.ObjectInputStream;
  49 import java.io.IOException;
  50 import java.beans.PropertyChangeListener;
  51 import java.beans.PropertyChangeSupport;
  52 import java.beans.Transient;
  53 import java.awt.im.InputContext;
  54 import java.awt.im.InputMethodRequests;
  55 import java.awt.dnd.DropTarget;
  56 import java.security.AccessController;


  57 import java.security.AccessControlContext;
  58 import javax.accessibility.*;
  59 import java.applet.Applet;















  60 import javax.swing.JComponent;
  61 import javax.swing.JRootPane;
  62 
  63 import sun.awt.ComponentFactory;
  64 import sun.security.action.GetPropertyAction;
  65 import sun.awt.AppContext;
  66 import sun.awt.AWTAccessor;


  67 import sun.awt.ConstrainableGraphics;


  68 import sun.awt.SubRegionShowable;
  69 import sun.awt.SunToolkit;
  70 import sun.awt.EmbeddedFrame;
  71 import sun.awt.dnd.SunDropTargetEvent;
  72 import sun.awt.im.CompositionArea;

  73 import sun.font.FontManager;
  74 import sun.font.FontManagerFactory;
  75 import sun.font.SunFontManager;
  76 import sun.java2d.SunGraphics2D;

  77 import sun.java2d.pipe.Region;
  78 import sun.awt.image.VSyncedBSManager;
  79 import sun.java2d.pipe.hw.ExtendedBufferCapabilities;
  80 import static sun.java2d.pipe.hw.ExtendedBufferCapabilities.VSyncType.*;
  81 import sun.awt.RequestFocusController;
  82 import sun.java2d.SunGraphicsEnvironment;
  83 import sun.swing.SwingAccessor;
  84 import sun.util.logging.PlatformLogger;
  85 




  86 /**
  87  * A <em>component</em> is an object having a graphical representation
  88  * that can be displayed on the screen and that can interact with the
  89  * user. Examples of components are the buttons, checkboxes, and scrollbars
  90  * of a typical graphical user interface. <p>
  91  * The {@code Component} class is the abstract superclass of
  92  * the nonmenu-related Abstract Window Toolkit components. Class
  93  * {@code Component} can also be extended directly to create a
  94  * lightweight component. A lightweight component is a component that is
  95  * not associated with a native window. On the contrary, a heavyweight
  96  * component is associated with a native window. The {@link #isLightweight()}
  97  * method may be used to distinguish between the two kinds of the components.
  98  * <p>
  99  * Lightweight and heavyweight components may be mixed in a single component
 100  * hierarchy. However, for correct operating of such a mixed hierarchy of
 101  * components, the whole hierarchy must be valid. When the hierarchy gets
 102  * invalidated, like after changing the bounds of components, or
 103  * adding/removing components to/from containers, the whole hierarchy must be
 104  * validated afterwards by means of the {@link Container#validate()} method
 105  * invoked on the top-most invalid container of the hierarchy.


1112      *          {@code Component} or {@code null}
1113      * @since 1.3
1114      */
1115     public GraphicsConfiguration getGraphicsConfiguration() {
1116         return getGraphicsConfiguration_NoClientCode();
1117     }
1118 
1119     final GraphicsConfiguration getGraphicsConfiguration_NoClientCode() {
1120         return graphicsConfig;
1121     }
1122 
1123     void setGraphicsConfiguration(GraphicsConfiguration gc) {
1124         synchronized(getTreeLock()) {
1125             if (updateGraphicsData(gc)) {
1126                 removeNotify();
1127                 addNotify();
1128             }
1129         }
1130     }
1131 
1132     boolean updateGraphicsData(GraphicsConfiguration gc) {
1133         checkTreeLock();
1134 
1135         if (graphicsConfig == gc) {
1136             return false;
1137         }
1138         GraphicsConfiguration oldConfig = graphicsConfig;
1139         graphicsConfig = gc;
1140 




1141         /*
1142          * If component is moved from one screen to another sceeen
1143          * graphicsConfiguration property is fired to enable the component
1144          * to recalculate any rendering data, if needed
1145          */
1146         if (oldConfig != null && gc != null) {
1147             firePropertyChange("graphicsConfiguration", oldConfig, gc);
1148         }









1149 
1150         ComponentPeer peer = this.peer;
1151         if (peer != null) {
1152             return peer.updateGraphicsData(gc);
1153         }
1154         return false;
1155     }
1156 




1157     /**
1158      * Checks that this component's {@code GraphicsDevice}
1159      * {@code idString} matches the string argument.
1160      */
1161     void checkGD(String stringID) {
1162         if (graphicsConfig != null) {
1163             if (!graphicsConfig.getDevice().getIDstring().equals(stringID)) {
1164                 throw new IllegalArgumentException(
1165                                                    "adding a container to a container on a different GraphicsDevice");
1166             }
1167         }
1168     }
1169 
1170     /**
1171      * Gets this component's locking object (the object that owns the thread
1172      * synchronization monitor) for AWT component-tree and layout
1173      * operations.
1174      * @return this component's locking object
1175      */
1176     public final Object getTreeLock() {


   1 /*
   2  * Copyright (c) 1995, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.awt;
  26 
  27 import java.applet.Applet;
  28 import java.awt.dnd.DropTarget;
  29 import java.awt.event.ActionEvent;
  30 import java.awt.event.AdjustmentEvent;
  31 import java.awt.event.ComponentEvent;
  32 import java.awt.event.ComponentListener;
  33 import java.awt.event.FocusEvent;
  34 import java.awt.event.FocusListener;
  35 import java.awt.event.HierarchyBoundsListener;
  36 import java.awt.event.HierarchyEvent;
  37 import java.awt.event.HierarchyListener;
  38 import java.awt.event.InputEvent;
  39 import java.awt.event.InputMethodEvent;
  40 import java.awt.event.InputMethodListener;
  41 import java.awt.event.ItemEvent;
  42 import java.awt.event.KeyEvent;
  43 import java.awt.event.KeyListener;
  44 import java.awt.event.MouseEvent;
  45 import java.awt.event.MouseListener;
  46 import java.awt.event.MouseMotionListener;
  47 import java.awt.event.MouseWheelEvent;
  48 import java.awt.event.MouseWheelListener;
  49 import java.awt.event.PaintEvent;
  50 import java.awt.event.TextEvent;
  51 import java.awt.im.InputContext;
  52 import java.awt.im.InputMethodRequests;
  53 import java.awt.image.BufferStrategy;
  54 import java.awt.image.ColorModel;
  55 import java.awt.image.ImageObserver;
  56 import java.awt.image.ImageProducer;

  57 import java.awt.image.VolatileImage;
  58 import java.awt.peer.ComponentPeer;
  59 import java.awt.peer.ContainerPeer;
  60 import java.awt.peer.LightweightPeer;


  61 import java.beans.PropertyChangeListener;
  62 import java.beans.PropertyChangeSupport;
  63 import java.beans.Transient;
  64 import java.io.IOException;
  65 import java.io.ObjectInputStream;
  66 import java.io.ObjectOutputStream;
  67 import java.io.PrintStream;
  68 import java.io.PrintWriter;
  69 import java.io.Serializable;
  70 import java.security.AccessControlContext;
  71 import java.security.AccessController;
  72 import java.util.Collections;
  73 import java.util.EventListener;
  74 import java.util.HashSet;
  75 import java.util.Locale;
  76 import java.util.Map;
  77 import java.util.Objects;
  78 import java.util.Set;
  79 import java.util.Vector;
  80 
  81 import javax.accessibility.Accessible;
  82 import javax.accessibility.AccessibleComponent;
  83 import javax.accessibility.AccessibleContext;
  84 import javax.accessibility.AccessibleRole;
  85 import javax.accessibility.AccessibleSelection;
  86 import javax.accessibility.AccessibleState;
  87 import javax.accessibility.AccessibleStateSet;
  88 import javax.swing.JComponent;
  89 import javax.swing.JRootPane;
  90 



  91 import sun.awt.AWTAccessor;
  92 import sun.awt.AppContext;
  93 import sun.awt.ComponentFactory;
  94 import sun.awt.ConstrainableGraphics;
  95 import sun.awt.EmbeddedFrame;
  96 import sun.awt.RequestFocusController;
  97 import sun.awt.SubRegionShowable;
  98 import sun.awt.SunToolkit;

  99 import sun.awt.dnd.SunDropTargetEvent;
 100 import sun.awt.im.CompositionArea;
 101 import sun.awt.image.VSyncedBSManager;
 102 import sun.font.FontManager;
 103 import sun.font.FontManagerFactory;
 104 import sun.font.SunFontManager;
 105 import sun.java2d.SunGraphics2D;
 106 import sun.java2d.SunGraphicsEnvironment;
 107 import sun.java2d.pipe.Region;

 108 import sun.java2d.pipe.hw.ExtendedBufferCapabilities;
 109 import sun.security.action.GetPropertyAction;


 110 import sun.swing.SwingAccessor;
 111 import sun.util.logging.PlatformLogger;
 112 
 113 import static sun.java2d.pipe.hw.ExtendedBufferCapabilities.VSyncType
 114         .VSYNC_DEFAULT;
 115 import static sun.java2d.pipe.hw.ExtendedBufferCapabilities.VSyncType.VSYNC_ON;
 116 
 117 /**
 118  * A <em>component</em> is an object having a graphical representation
 119  * that can be displayed on the screen and that can interact with the
 120  * user. Examples of components are the buttons, checkboxes, and scrollbars
 121  * of a typical graphical user interface. <p>
 122  * The {@code Component} class is the abstract superclass of
 123  * the nonmenu-related Abstract Window Toolkit components. Class
 124  * {@code Component} can also be extended directly to create a
 125  * lightweight component. A lightweight component is a component that is
 126  * not associated with a native window. On the contrary, a heavyweight
 127  * component is associated with a native window. The {@link #isLightweight()}
 128  * method may be used to distinguish between the two kinds of the components.
 129  * <p>
 130  * Lightweight and heavyweight components may be mixed in a single component
 131  * hierarchy. However, for correct operating of such a mixed hierarchy of
 132  * components, the whole hierarchy must be valid. When the hierarchy gets
 133  * invalidated, like after changing the bounds of components, or
 134  * adding/removing components to/from containers, the whole hierarchy must be
 135  * validated afterwards by means of the {@link Container#validate()} method
 136  * invoked on the top-most invalid container of the hierarchy.


1143      *          {@code Component} or {@code null}
1144      * @since 1.3
1145      */
1146     public GraphicsConfiguration getGraphicsConfiguration() {
1147         return getGraphicsConfiguration_NoClientCode();
1148     }
1149 
1150     final GraphicsConfiguration getGraphicsConfiguration_NoClientCode() {
1151         return graphicsConfig;
1152     }
1153 
1154     void setGraphicsConfiguration(GraphicsConfiguration gc) {
1155         synchronized(getTreeLock()) {
1156             if (updateGraphicsData(gc)) {
1157                 removeNotify();
1158                 addNotify();
1159             }
1160         }
1161     }
1162 
1163     final boolean updateGraphicsData(GraphicsConfiguration gc) {





1164         GraphicsConfiguration oldConfig = graphicsConfig;
1165         // First, update own graphics configuration
1166         boolean ret = updateSelfGraphicsData(gc);
1167         // Second, update children graphics configurations
1168         ret |= updateChildGraphicsData(gc);
1169         // Third, fire PropertyChange if needed
1170         if (oldConfig != gc) {
1171             /*
1172              * If component is moved from one screen to another screen or shown
1173              * for the first time graphicsConfiguration property is fired to
1174              * enable the component to recalculate any rendering data, if needed
1175              */

1176             firePropertyChange("graphicsConfiguration", oldConfig, gc);
1177         }
1178         return ret;
1179     }
1180 
1181     private boolean updateSelfGraphicsData(GraphicsConfiguration gc) {
1182         checkTreeLock();
1183         if (graphicsConfig == gc) {
1184             return false;
1185         }
1186         graphicsConfig = gc;
1187 
1188         ComponentPeer peer = this.peer;
1189         if (peer != null) {
1190             return peer.updateGraphicsData(gc);
1191         }
1192         return false;
1193     }
1194 
1195     boolean updateChildGraphicsData(GraphicsConfiguration gc) {
1196         return false;
1197     }
1198 
1199     /**
1200      * Checks that this component's {@code GraphicsDevice}
1201      * {@code idString} matches the string argument.
1202      */
1203     void checkGD(String stringID) {
1204         if (graphicsConfig != null) {
1205             if (!graphicsConfig.getDevice().getIDstring().equals(stringID)) {
1206                 throw new IllegalArgumentException(
1207                                                    "adding a container to a container on a different GraphicsDevice");
1208             }
1209         }
1210     }
1211 
1212     /**
1213      * Gets this component's locking object (the object that owns the thread
1214      * synchronization monitor) for AWT component-tree and layout
1215      * operations.
1216      * @return this component's locking object
1217      */
1218     public final Object getTreeLock() {


< prev index next >