1001
1002 @SuppressWarnings({"rawtypes", "unchecked"})
1003 void initializeFocusTraversalKeys() {
1004 focusTraversalKeys = new Set[3];
1005 }
1006
1007 /**
1008 * Constructs a name for this component. Called by <code>getName</code>
1009 * when the name is <code>null</code>.
1010 */
1011 String constructComponentName() {
1012 return null; // For strict compliance with prior platform versions, a Component
1013 // that doesn't set its name should return null from
1014 // getName()
1015 }
1016
1017 /**
1018 * Gets the name of the component.
1019 * @return this component's name
1020 * @see #setName
1021 * @since JDK1.1
1022 */
1023 public String getName() {
1024 if (name == null && !nameExplicitlySet) {
1025 synchronized(getObjectLock()) {
1026 if (name == null && !nameExplicitlySet)
1027 name = constructComponentName();
1028 }
1029 }
1030 return name;
1031 }
1032
1033 /**
1034 * Sets the name of the component to the specified string.
1035 * @param name the string that is to be this
1036 * component's name
1037 * @see #getName
1038 * @since JDK1.1
1039 */
1040 public void setName(String name) {
1041 String oldName;
1042 synchronized(getObjectLock()) {
1043 oldName = this.name;
1044 this.name = name;
1045 nameExplicitlySet = true;
1046 }
1047 firePropertyChange("name", oldName, name);
1048 }
1049
1050 /**
1051 * Gets the parent of this component.
1052 * @return the parent container of this component
1053 * @since JDK1.0
1054 */
1055 public Container getParent() {
1056 return getParent_NoClientCode();
1057 }
1058
1059 // NOTE: This method may be called by privileged threads.
1060 // This functionality is implemented in a package-private method
1061 // to insure that it cannot be overridden by client subclasses.
1062 // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
1063 final Container getParent_NoClientCode() {
1064 return parent;
1065 }
1066
1067 // This method is overridden in the Window class to return null,
1068 // because the parent field of the Window object contains
1069 // the owner of the window, not its parent.
1070 Container getContainer() {
1071 return getParent_NoClientCode();
1072 }
1073
1204 * synchronization monitor) for AWT component-tree and layout
1205 * operations.
1206 * @return this component's locking object
1207 */
1208 public final Object getTreeLock() {
1209 return LOCK;
1210 }
1211
1212 final void checkTreeLock() {
1213 if (!Thread.holdsLock(getTreeLock())) {
1214 throw new IllegalStateException("This function should be called while holding treeLock");
1215 }
1216 }
1217
1218 /**
1219 * Gets the toolkit of this component. Note that
1220 * the frame that contains a component controls which
1221 * toolkit is used by that component. Therefore if the component
1222 * is moved from one frame to another, the toolkit it uses may change.
1223 * @return the toolkit of this component
1224 * @since JDK1.0
1225 */
1226 public Toolkit getToolkit() {
1227 return getToolkitImpl();
1228 }
1229
1230 /*
1231 * This is called by the native code, so client code can't
1232 * be called on the toolkit thread.
1233 */
1234 final Toolkit getToolkitImpl() {
1235 Container parent = this.parent;
1236 if (parent != null) {
1237 return parent.getToolkitImpl();
1238 }
1239 return Toolkit.getDefaultToolkit();
1240 }
1241
1242 /**
1243 * Determines whether this component is valid. A component is valid
1244 * when it is correctly sized and positioned within its parent
1245 * container and all its children are also valid.
1246 * In order to account for peers' size requirements, components are invalidated
1247 * before they are first shown on the screen. By the time the parent container
1248 * is fully realized, all its components will be valid.
1249 * @return <code>true</code> if the component is valid, <code>false</code>
1250 * otherwise
1251 * @see #validate
1252 * @see #invalidate
1253 * @since JDK1.0
1254 */
1255 public boolean isValid() {
1256 return (peer != null) && valid;
1257 }
1258
1259 /**
1260 * Determines whether this component is displayable. A component is
1261 * displayable when it is connected to a native screen resource.
1262 * <p>
1263 * A component is made displayable either when it is added to
1264 * a displayable containment hierarchy or when its containment
1265 * hierarchy is made displayable.
1266 * A containment hierarchy is made displayable when its ancestor
1267 * window is either packed or made visible.
1268 * <p>
1269 * A component is made undisplayable either when it is removed from
1270 * a displayable containment hierarchy or when its containment hierarchy
1271 * is made undisplayable. A containment hierarchy is made
1272 * undisplayable when its ancestor window is disposed.
1273 *
1275 * <code>false</code> otherwise
1276 * @see Container#add(Component)
1277 * @see Window#pack
1278 * @see Window#show
1279 * @see Container#remove(Component)
1280 * @see Window#dispose
1281 * @since 1.2
1282 */
1283 public boolean isDisplayable() {
1284 return getPeer() != null;
1285 }
1286
1287 /**
1288 * Determines whether this component should be visible when its
1289 * parent is visible. Components are
1290 * initially visible, with the exception of top level components such
1291 * as <code>Frame</code> objects.
1292 * @return <code>true</code> if the component is visible,
1293 * <code>false</code> otherwise
1294 * @see #setVisible
1295 * @since JDK1.0
1296 */
1297 @Transient
1298 public boolean isVisible() {
1299 return isVisible_NoClientCode();
1300 }
1301 final boolean isVisible_NoClientCode() {
1302 return visible;
1303 }
1304
1305 /**
1306 * Determines whether this component will be displayed on the screen.
1307 * @return <code>true</code> if the component and all of its ancestors
1308 * until a toplevel window or null parent are visible,
1309 * <code>false</code> otherwise
1310 */
1311 boolean isRecursivelyVisible() {
1312 return visible && (parent == null || parent.isRecursivelyVisible());
1313 }
1314
1315 /**
1402 boolean isSameOrAncestorOf(Component comp, boolean allowChildren) {
1403 return comp == this;
1404 }
1405
1406 /**
1407 * Determines whether this component is showing on screen. This means
1408 * that the component must be visible, and it must be in a container
1409 * that is visible and showing.
1410 * <p>
1411 * <strong>Note:</strong> sometimes there is no way to detect whether the
1412 * {@code Component} is actually visible to the user. This can happen when:
1413 * <ul>
1414 * <li>the component has been added to a visible {@code ScrollPane} but
1415 * the {@code Component} is not currently in the scroll pane's view port.
1416 * <li>the {@code Component} is obscured by another {@code Component} or
1417 * {@code Container}.
1418 * </ul>
1419 * @return <code>true</code> if the component is showing,
1420 * <code>false</code> otherwise
1421 * @see #setVisible
1422 * @since JDK1.0
1423 */
1424 public boolean isShowing() {
1425 if (visible && (peer != null)) {
1426 Container parent = this.parent;
1427 return (parent == null) || parent.isShowing();
1428 }
1429 return false;
1430 }
1431
1432 /**
1433 * Determines whether this component is enabled. An enabled component
1434 * can respond to user input and generate events. Components are
1435 * enabled initially by default. A component may be enabled or disabled by
1436 * calling its <code>setEnabled</code> method.
1437 * @return <code>true</code> if the component is enabled,
1438 * <code>false</code> otherwise
1439 * @see #setEnabled
1440 * @since JDK1.0
1441 */
1442 public boolean isEnabled() {
1443 return isEnabledImpl();
1444 }
1445
1446 /*
1447 * This is called by the native code, so client code can't
1448 * be called on the toolkit thread.
1449 */
1450 final boolean isEnabledImpl() {
1451 return enabled;
1452 }
1453
1454 /**
1455 * Enables or disables this component, depending on the value of the
1456 * parameter <code>b</code>. An enabled component can respond to user
1457 * input and generate events. Components are enabled initially by default.
1458 *
1459 * <p>Note: Disabling a lightweight component does not prevent it from
1460 * receiving MouseEvents.
1461 * <p>Note: Disabling a heavyweight container prevents all components
1462 * in this container from receiving any input events. But disabling a
1463 * lightweight container affects only this container.
1464 *
1465 * @param b If <code>true</code>, this component is
1466 * enabled; otherwise this component is disabled
1467 * @see #isEnabled
1468 * @see #isLightweight
1469 * @since JDK1.1
1470 */
1471 public void setEnabled(boolean b) {
1472 enable(b);
1473 }
1474
1475 /**
1476 * @deprecated As of JDK version 1.1,
1477 * replaced by <code>setEnabled(boolean)</code>.
1478 */
1479 @Deprecated
1480 public void enable() {
1481 if (!enabled) {
1482 synchronized (getTreeLock()) {
1483 enabled = true;
1484 ComponentPeer peer = this.peer;
1485 if (peer != null) {
1486 peer.setEnabled(true);
1487 if (visible) {
1488 updateCursorImmediately();
1489 }
1594 if (inputContext != null) {
1595 inputContext.endComposition();
1596 inputContext.removeNotify(this);
1597 }
1598 }
1599 eventMask &= ~AWTEvent.INPUT_METHODS_ENABLED_MASK;
1600 }
1601 }
1602
1603 /**
1604 * Shows or hides this component depending on the value of parameter
1605 * <code>b</code>.
1606 * <p>
1607 * This method changes layout-related information, and therefore,
1608 * invalidates the component hierarchy.
1609 *
1610 * @param b if <code>true</code>, shows this component;
1611 * otherwise, hides this component
1612 * @see #isVisible
1613 * @see #invalidate
1614 * @since JDK1.1
1615 */
1616 public void setVisible(boolean b) {
1617 show(b);
1618 }
1619
1620 /**
1621 * @deprecated As of JDK version 1.1,
1622 * replaced by <code>setVisible(boolean)</code>.
1623 */
1624 @Deprecated
1625 public void show() {
1626 if (!visible) {
1627 synchronized (getTreeLock()) {
1628 visible = true;
1629 mixOnShowing();
1630 ComponentPeer peer = this.peer;
1631 if (peer != null) {
1632 peer.setVisible(true);
1633 createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED,
1634 this, parent,
1722 (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0 ||
1723 Toolkit.enabledOnToolkit(AWTEvent.COMPONENT_EVENT_MASK)) {
1724 ComponentEvent e = new ComponentEvent(this,
1725 ComponentEvent.COMPONENT_HIDDEN);
1726 Toolkit.getEventQueue().postEvent(e);
1727 }
1728 }
1729 Container parent = this.parent;
1730 if (parent != null) {
1731 parent.invalidate();
1732 }
1733 }
1734 }
1735
1736 /**
1737 * Gets the foreground color of this component.
1738 * @return this component's foreground color; if this component does
1739 * not have a foreground color, the foreground color of its parent
1740 * is returned
1741 * @see #setForeground
1742 * @since JDK1.0
1743 * @beaninfo
1744 * bound: true
1745 */
1746 @Transient
1747 public Color getForeground() {
1748 Color foreground = this.foreground;
1749 if (foreground != null) {
1750 return foreground;
1751 }
1752 Container parent = this.parent;
1753 return (parent != null) ? parent.getForeground() : null;
1754 }
1755
1756 /**
1757 * Sets the foreground color of this component.
1758 * @param c the color to become this component's
1759 * foreground color; if this parameter is <code>null</code>
1760 * then this component will inherit
1761 * the foreground color of its parent
1762 * @see #getForeground
1763 * @since JDK1.0
1764 */
1765 public void setForeground(Color c) {
1766 Color oldColor = foreground;
1767 ComponentPeer peer = this.peer;
1768 foreground = c;
1769 if (peer != null) {
1770 c = getForeground();
1771 if (c != null) {
1772 peer.setForeground(c);
1773 }
1774 }
1775 // This is a bound property, so report the change to
1776 // any registered listeners. (Cheap if there are none.)
1777 firePropertyChange("foreground", oldColor, c);
1778 }
1779
1780 /**
1781 * Returns whether the foreground color has been explicitly set for this
1782 * Component. If this method returns <code>false</code>, this Component is
1783 * inheriting its foreground color from an ancestor.
1784 *
1785 * @return <code>true</code> if the foreground color has been explicitly
1786 * set for this Component; <code>false</code> otherwise.
1787 * @since 1.4
1788 */
1789 public boolean isForegroundSet() {
1790 return (foreground != null);
1791 }
1792
1793 /**
1794 * Gets the background color of this component.
1795 * @return this component's background color; if this component does
1796 * not have a background color,
1797 * the background color of its parent is returned
1798 * @see #setBackground
1799 * @since JDK1.0
1800 */
1801 @Transient
1802 public Color getBackground() {
1803 Color background = this.background;
1804 if (background != null) {
1805 return background;
1806 }
1807 Container parent = this.parent;
1808 return (parent != null) ? parent.getBackground() : null;
1809 }
1810
1811 /**
1812 * Sets the background color of this component.
1813 * <p>
1814 * The background color affects each component differently and the
1815 * parts of the component that are affected by the background color
1816 * may differ between operating systems.
1817 *
1818 * @param c the color to become this component's color;
1819 * if this parameter is <code>null</code>, then this
1820 * component will inherit the background color of its parent
1821 * @see #getBackground
1822 * @since JDK1.0
1823 * @beaninfo
1824 * bound: true
1825 */
1826 public void setBackground(Color c) {
1827 Color oldColor = background;
1828 ComponentPeer peer = this.peer;
1829 background = c;
1830 if (peer != null) {
1831 c = getBackground();
1832 if (c != null) {
1833 peer.setBackground(c);
1834 }
1835 }
1836 // This is a bound property, so report the change to
1837 // any registered listeners. (Cheap if there are none.)
1838 firePropertyChange("background", oldColor, c);
1839 }
1840
1841 /**
1842 * Returns whether the background color has been explicitly set for this
1843 * Component. If this method returns <code>false</code>, this Component is
1844 * inheriting its background color from an ancestor.
1845 *
1846 * @return <code>true</code> if the background color has been explicitly
1847 * set for this Component; <code>false</code> otherwise.
1848 * @since 1.4
1849 */
1850 public boolean isBackgroundSet() {
1851 return (background != null);
1852 }
1853
1854 /**
1855 * Gets the font of this component.
1856 * @return this component's font; if a font has not been set
1857 * for this component, the font of its parent is returned
1858 * @see #setFont
1859 * @since JDK1.0
1860 */
1861 @Transient
1862 public Font getFont() {
1863 return getFont_NoClientCode();
1864 }
1865
1866 // NOTE: This method may be called by privileged threads.
1867 // This functionality is implemented in a package-private method
1868 // to insure that it cannot be overridden by client subclasses.
1869 // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
1870 final Font getFont_NoClientCode() {
1871 Font font = this.font;
1872 if (font != null) {
1873 return font;
1874 }
1875 Container parent = this.parent;
1876 return (parent != null) ? parent.getFont_NoClientCode() : null;
1877 }
1878
1879 /**
1880 * Sets the font of this component.
1881 * <p>
1882 * This method changes layout-related information, and therefore,
1883 * invalidates the component hierarchy.
1884 *
1885 * @param f the font to become this component's font;
1886 * if this parameter is <code>null</code> then this
1887 * component will inherit the font of its parent
1888 * @see #getFont
1889 * @see #invalidate
1890 * @since JDK1.0
1891 * @beaninfo
1892 * bound: true
1893 */
1894 public void setFont(Font f) {
1895 Font oldFont, newFont;
1896 synchronized(getTreeLock()) {
1897 oldFont = font;
1898 newFont = font = f;
1899 ComponentPeer peer = this.peer;
1900 if (peer != null) {
1901 f = getFont();
1902 if (f != null) {
1903 peer.setFont(f);
1904 peerFont = f;
1905 }
1906 }
1907 }
1908 // This is a bound property, so report the change to
1909 // any registered listeners. (Cheap if there are none.)
1910 firePropertyChange("font", oldFont, newFont);
1923 * this method returns <code>false</code>, this Component is inheriting its
1924 * font from an ancestor.
1925 *
1926 * @return <code>true</code> if the font has been explicitly set for this
1927 * Component; <code>false</code> otherwise.
1928 * @since 1.4
1929 */
1930 public boolean isFontSet() {
1931 return (font != null);
1932 }
1933
1934 /**
1935 * Gets the locale of this component.
1936 * @return this component's locale; if this component does not
1937 * have a locale, the locale of its parent is returned
1938 * @see #setLocale
1939 * @exception IllegalComponentStateException if the <code>Component</code>
1940 * does not have its own locale and has not yet been added to
1941 * a containment hierarchy such that the locale can be determined
1942 * from the containing parent
1943 * @since JDK1.1
1944 */
1945 public Locale getLocale() {
1946 Locale locale = this.locale;
1947 if (locale != null) {
1948 return locale;
1949 }
1950 Container parent = this.parent;
1951
1952 if (parent == null) {
1953 throw new IllegalComponentStateException("This component must have a parent in order to determine its locale");
1954 } else {
1955 return parent.getLocale();
1956 }
1957 }
1958
1959 /**
1960 * Sets the locale of this component. This is a bound property.
1961 * <p>
1962 * This method changes layout-related information, and therefore,
1963 * invalidates the component hierarchy.
1964 *
1965 * @param l the locale to become this component's locale
1966 * @see #getLocale
1967 * @see #invalidate
1968 * @since JDK1.1
1969 */
1970 public void setLocale(Locale l) {
1971 Locale oldValue = locale;
1972 locale = l;
1973
1974 // This is a bound property, so report the change to
1975 // any registered listeners. (Cheap if there are none.)
1976 firePropertyChange("locale", oldValue, l);
1977
1978 // This could change the preferred size of the Component.
1979 invalidateIfValid();
1980 }
1981
1982 /**
1983 * Gets the instance of <code>ColorModel</code> used to display
1984 * the component on the output device.
1985 * @return the color model used by this component
1986 * @see java.awt.image.ColorModel
1987 * @see java.awt.peer.ComponentPeer#getColorModel()
1988 * @see Toolkit#getColorModel()
1989 * @since JDK1.0
1990 */
1991 public ColorModel getColorModel() {
1992 ComponentPeer peer = this.peer;
1993 if ((peer != null) && ! (peer instanceof LightweightPeer)) {
1994 return peer.getColorModel();
1995 } else if (GraphicsEnvironment.isHeadless()) {
1996 return ColorModel.getRGBdefault();
1997 } // else
1998 return getToolkit().getColorModel();
1999 }
2000
2001 /**
2002 * Gets the location of this component in the form of a
2003 * point specifying the component's top-left corner.
2004 * The location will be relative to the parent's coordinate space.
2005 * <p>
2006 * Due to the asynchronous nature of native event handling, this
2007 * method can return outdated values (for instance, after several calls
2008 * of <code>setLocation()</code> in rapid succession). For this
2009 * reason, the recommended method of obtaining a component's position is
2010 * within <code>java.awt.event.ComponentListener.componentMoved()</code>,
2011 * which is called after the operating system has finished moving the
2012 * component.
2013 * </p>
2014 * @return an instance of <code>Point</code> representing
2015 * the top-left corner of the component's bounds in
2016 * the coordinate space of the component's parent
2017 * @see #setLocation
2018 * @see #getLocationOnScreen
2019 * @since JDK1.1
2020 */
2021 public Point getLocation() {
2022 return location();
2023 }
2024
2025 /**
2026 * Gets the location of this component in the form of a point
2027 * specifying the component's top-left corner in the screen's
2028 * coordinate space.
2029 * @return an instance of <code>Point</code> representing
2030 * the top-left corner of the component's bounds in the
2031 * coordinate space of the screen
2032 * @throws IllegalComponentStateException if the
2033 * component is not showing on the screen
2034 * @see #setLocation
2035 * @see #getLocation
2036 */
2037 public Point getLocationOnScreen() {
2038 synchronized (getTreeLock()) {
2039 return getLocationOnScreen_NoTreeLock();
2078
2079 private Point location_NoClientCode() {
2080 return new Point(x, y);
2081 }
2082
2083 /**
2084 * Moves this component to a new location. The top-left corner of
2085 * the new location is specified by the <code>x</code> and <code>y</code>
2086 * parameters in the coordinate space of this component's parent.
2087 * <p>
2088 * This method changes layout-related information, and therefore,
2089 * invalidates the component hierarchy.
2090 *
2091 * @param x the <i>x</i>-coordinate of the new location's
2092 * top-left corner in the parent's coordinate space
2093 * @param y the <i>y</i>-coordinate of the new location's
2094 * top-left corner in the parent's coordinate space
2095 * @see #getLocation
2096 * @see #setBounds
2097 * @see #invalidate
2098 * @since JDK1.1
2099 */
2100 public void setLocation(int x, int y) {
2101 move(x, y);
2102 }
2103
2104 /**
2105 * @deprecated As of JDK version 1.1,
2106 * replaced by <code>setLocation(int, int)</code>.
2107 */
2108 @Deprecated
2109 public void move(int x, int y) {
2110 synchronized(getTreeLock()) {
2111 setBoundsOp(ComponentPeer.SET_LOCATION);
2112 setBounds(x, y, width, height);
2113 }
2114 }
2115
2116 /**
2117 * Moves this component to a new location. The top-left corner of
2118 * the new location is specified by point <code>p</code>. Point
2119 * <code>p</code> is given in the parent's coordinate space.
2120 * <p>
2121 * This method changes layout-related information, and therefore,
2122 * invalidates the component hierarchy.
2123 *
2124 * @param p the point defining the top-left corner
2125 * of the new location, given in the coordinate space of this
2126 * component's parent
2127 * @see #getLocation
2128 * @see #setBounds
2129 * @see #invalidate
2130 * @since JDK1.1
2131 */
2132 public void setLocation(Point p) {
2133 setLocation(p.x, p.y);
2134 }
2135
2136 /**
2137 * Returns the size of this component in the form of a
2138 * <code>Dimension</code> object. The <code>height</code>
2139 * field of the <code>Dimension</code> object contains
2140 * this component's height, and the <code>width</code>
2141 * field of the <code>Dimension</code> object contains
2142 * this component's width.
2143 * @return a <code>Dimension</code> object that indicates the
2144 * size of this component
2145 * @see #setSize
2146 * @since JDK1.1
2147 */
2148 public Dimension getSize() {
2149 return size();
2150 }
2151
2152 /**
2153 * @deprecated As of JDK version 1.1,
2154 * replaced by <code>getSize()</code>.
2155 */
2156 @Deprecated
2157 public Dimension size() {
2158 return new Dimension(width, height);
2159 }
2160
2161 /**
2162 * Resizes this component so that it has width <code>width</code>
2163 * and height <code>height</code>.
2164 * <p>
2165 * This method changes layout-related information, and therefore,
2166 * invalidates the component hierarchy.
2167 *
2168 * @param width the new width of this component in pixels
2169 * @param height the new height of this component in pixels
2170 * @see #getSize
2171 * @see #setBounds
2172 * @see #invalidate
2173 * @since JDK1.1
2174 */
2175 public void setSize(int width, int height) {
2176 resize(width, height);
2177 }
2178
2179 /**
2180 * @deprecated As of JDK version 1.1,
2181 * replaced by <code>setSize(int, int)</code>.
2182 */
2183 @Deprecated
2184 public void resize(int width, int height) {
2185 synchronized(getTreeLock()) {
2186 setBoundsOp(ComponentPeer.SET_SIZE);
2187 setBounds(x, y, width, height);
2188 }
2189 }
2190
2191 /**
2192 * Resizes this component so that it has width <code>d.width</code>
2193 * and height <code>d.height</code>.
2194 * <p>
2195 * This method changes layout-related information, and therefore,
2196 * invalidates the component hierarchy.
2197 *
2198 * @param d the dimension specifying the new size
2199 * of this component
2200 * @throws NullPointerException if {@code d} is {@code null}
2201 * @see #setSize
2202 * @see #setBounds
2203 * @see #invalidate
2204 * @since JDK1.1
2205 */
2206 public void setSize(Dimension d) {
2207 resize(d);
2208 }
2209
2210 /**
2211 * @deprecated As of JDK version 1.1,
2212 * replaced by <code>setSize(Dimension)</code>.
2213 */
2214 @Deprecated
2215 public void resize(Dimension d) {
2216 setSize(d.width, d.height);
2217 }
2218
2219 /**
2220 * Gets the bounds of this component in the form of a
2221 * <code>Rectangle</code> object. The bounds specify this
2222 * component's width, height, and location relative to
2223 * its parent.
2224 * @return a rectangle indicating this component's bounds
2241
2242 /**
2243 * Moves and resizes this component. The new location of the top-left
2244 * corner is specified by <code>x</code> and <code>y</code>, and the
2245 * new size is specified by <code>width</code> and <code>height</code>.
2246 * <p>
2247 * This method changes layout-related information, and therefore,
2248 * invalidates the component hierarchy.
2249 *
2250 * @param x the new <i>x</i>-coordinate of this component
2251 * @param y the new <i>y</i>-coordinate of this component
2252 * @param width the new <code>width</code> of this component
2253 * @param height the new <code>height</code> of this
2254 * component
2255 * @see #getBounds
2256 * @see #setLocation(int, int)
2257 * @see #setLocation(Point)
2258 * @see #setSize(int, int)
2259 * @see #setSize(Dimension)
2260 * @see #invalidate
2261 * @since JDK1.1
2262 */
2263 public void setBounds(int x, int y, int width, int height) {
2264 reshape(x, y, width, height);
2265 }
2266
2267 /**
2268 * @deprecated As of JDK version 1.1,
2269 * replaced by <code>setBounds(int, int, int, int)</code>.
2270 */
2271 @Deprecated
2272 public void reshape(int x, int y, int width, int height) {
2273 synchronized (getTreeLock()) {
2274 try {
2275 setBoundsOp(ComponentPeer.SET_BOUNDS);
2276 boolean resized = (this.width != width) || (this.height != height);
2277 boolean moved = (this.x != x) || (this.y != y);
2278 if (!resized && !moved) {
2279 return;
2280 }
2281 int oldX = this.x;
2385 }
2386
2387 /**
2388 * Moves and resizes this component to conform to the new
2389 * bounding rectangle <code>r</code>. This component's new
2390 * position is specified by <code>r.x</code> and <code>r.y</code>,
2391 * and its new size is specified by <code>r.width</code> and
2392 * <code>r.height</code>
2393 * <p>
2394 * This method changes layout-related information, and therefore,
2395 * invalidates the component hierarchy.
2396 *
2397 * @param r the new bounding rectangle for this component
2398 * @throws NullPointerException if {@code r} is {@code null}
2399 * @see #getBounds
2400 * @see #setLocation(int, int)
2401 * @see #setLocation(Point)
2402 * @see #setSize(int, int)
2403 * @see #setSize(Dimension)
2404 * @see #invalidate
2405 * @since JDK1.1
2406 */
2407 public void setBounds(Rectangle r) {
2408 setBounds(r.x, r.y, r.width, r.height);
2409 }
2410
2411
2412 /**
2413 * Returns the current x coordinate of the components origin.
2414 * This method is preferable to writing
2415 * <code>component.getBounds().x</code>,
2416 * or <code>component.getLocation().x</code> because it doesn't
2417 * cause any heap allocations.
2418 *
2419 * @return the current x coordinate of the components origin
2420 * @since 1.2
2421 */
2422 public int getX() {
2423 return x;
2424 }
2425
2866 }
2867
2868 /**
2869 * @deprecated As of JDK version 1.1,
2870 * replaced by <code>doLayout()</code>.
2871 */
2872 @Deprecated
2873 public void layout() {
2874 }
2875
2876 /**
2877 * Validates this component.
2878 * <p>
2879 * The meaning of the term <i>validating</i> is defined by the ancestors of
2880 * this class. See {@link Container#validate} for more details.
2881 *
2882 * @see #invalidate
2883 * @see #doLayout()
2884 * @see LayoutManager
2885 * @see Container#validate
2886 * @since JDK1.0
2887 */
2888 public void validate() {
2889 synchronized (getTreeLock()) {
2890 ComponentPeer peer = this.peer;
2891 boolean wasValid = isValid();
2892 if (!wasValid && peer != null) {
2893 Font newfont = getFont();
2894 Font oldfont = peerFont;
2895 if (newfont != oldfont && (oldfont == null
2896 || !oldfont.equals(newfont))) {
2897 peer.setFont(newfont);
2898 peerFont = newfont;
2899 }
2900 peer.layout();
2901 }
2902 valid = true;
2903 if (!wasValid) {
2904 mixOnValidating();
2905 }
2906 }
2909 /**
2910 * Invalidates this component and its ancestors.
2911 * <p>
2912 * By default, all the ancestors of the component up to the top-most
2913 * container of the hierarchy are marked invalid. If the {@code
2914 * java.awt.smartInvalidate} system property is set to {@code true},
2915 * invalidation stops on the nearest validate root of this component.
2916 * Marking a container <i>invalid</i> indicates that the container needs to
2917 * be laid out.
2918 * <p>
2919 * This method is called automatically when any layout-related information
2920 * changes (e.g. setting the bounds of the component, or adding the
2921 * component to a container).
2922 * <p>
2923 * This method might be called often, so it should work fast.
2924 *
2925 * @see #validate
2926 * @see #doLayout
2927 * @see LayoutManager
2928 * @see java.awt.Container#isValidateRoot
2929 * @since JDK1.0
2930 */
2931 public void invalidate() {
2932 synchronized (getTreeLock()) {
2933 /* Nullify cached layout and size information.
2934 * For efficiency, propagate invalidate() upwards only if
2935 * some other component hasn't already done so first.
2936 */
2937 valid = false;
2938 if (!isPreferredSizeSet()) {
2939 prefSize = null;
2940 }
2941 if (!isMinimumSizeSet()) {
2942 minSize = null;
2943 }
2944 if (!isMaximumSizeSet()) {
2945 maxSize = null;
2946 }
2947 invalidateParent();
2948 }
2949 }
3004 // If there's no validate roots, we'll validate the
3005 // topmost container
3006 break;
3007 }
3008
3009 root = root.getContainer();
3010 }
3011
3012 root.validate();
3013 }
3014 }
3015 }
3016
3017 /**
3018 * Creates a graphics context for this component. This method will
3019 * return <code>null</code> if this component is currently not
3020 * displayable.
3021 * @return a graphics context for this component, or <code>null</code>
3022 * if it has none
3023 * @see #paint
3024 * @since JDK1.0
3025 */
3026 public Graphics getGraphics() {
3027 if (peer instanceof LightweightPeer) {
3028 // This is for a lightweight component, need to
3029 // translate coordinate spaces and clip relative
3030 // to the parent.
3031 if (parent == null) return null;
3032 Graphics g = parent.getGraphics();
3033 if (g == null) return null;
3034 if (g instanceof ConstrainableGraphics) {
3035 ((ConstrainableGraphics) g).constrain(x, y, width, height);
3036 } else {
3037 g.translate(x,y);
3038 g.setClip(0, 0, width, height);
3039 }
3040 g.setFont(getFont());
3041 return g;
3042 } else {
3043 ComponentPeer peer = this.peer;
3044 return (peer != null) ? peer.getGraphics() : null;
3068 }
3069 }
3070
3071 /**
3072 * Gets the font metrics for the specified font.
3073 * Warning: Since Font metrics are affected by the
3074 * {@link java.awt.font.FontRenderContext FontRenderContext} and
3075 * this method does not provide one, it can return only metrics for
3076 * the default render context which may not match that used when
3077 * rendering on the Component if {@link Graphics2D} functionality is being
3078 * used. Instead metrics can be obtained at rendering time by calling
3079 * {@link Graphics#getFontMetrics()} or text measurement APIs on the
3080 * {@link Font Font} class.
3081 * @param font the font for which font metrics is to be
3082 * obtained
3083 * @return the font metrics for <code>font</code>
3084 * @see #getFont
3085 * @see #getPeer
3086 * @see java.awt.peer.ComponentPeer#getFontMetrics(Font)
3087 * @see Toolkit#getFontMetrics(Font)
3088 * @since JDK1.0
3089 */
3090 public FontMetrics getFontMetrics(Font font) {
3091 // This is an unsupported hack, but left in for a customer.
3092 // Do not remove.
3093 FontManager fm = FontManagerFactory.getInstance();
3094 if (fm instanceof SunFontManager
3095 && ((SunFontManager) fm).usePlatformFontMetrics()) {
3096
3097 if (peer != null &&
3098 !(peer instanceof LightweightPeer)) {
3099 return peer.getFontMetrics(font);
3100 }
3101 }
3102 return sun.font.FontDesignMetrics.getMetrics(font);
3103 }
3104
3105 /**
3106 * Sets the cursor image to the specified cursor. This cursor
3107 * image is displayed when the <code>contains</code> method for
3108 * this component returns true for the current cursor location, and
3109 * this Component is visible, displayable, and enabled. Setting the
3110 * cursor of a <code>Container</code> causes that cursor to be displayed
3111 * within all of the container's subcomponents, except for those
3112 * that have a non-<code>null</code> cursor.
3113 * <p>
3114 * The method may have no visual effect if the Java platform
3115 * implementation and/or the native system do not support
3116 * changing the mouse cursor shape.
3117 * @param cursor One of the constants defined
3118 * by the <code>Cursor</code> class;
3119 * if this parameter is <code>null</code>
3120 * then this component will inherit
3121 * the cursor of its parent
3122 * @see #isEnabled
3123 * @see #isShowing
3124 * @see #getCursor
3125 * @see #contains
3126 * @see Toolkit#createCustomCursor
3127 * @see Cursor
3128 * @since JDK1.1
3129 */
3130 public void setCursor(Cursor cursor) {
3131 this.cursor = cursor;
3132 updateCursorImmediately();
3133 }
3134
3135 /**
3136 * Updates the cursor. May not be invoked from the native
3137 * message pump.
3138 */
3139 final void updateCursorImmediately() {
3140 if (peer instanceof LightweightPeer) {
3141 Container nativeContainer = getNativeContainer();
3142
3143 if (nativeContainer == null) return;
3144
3145 ComponentPeer cPeer = nativeContainer.getPeer();
3146
3147 if (cPeer != null) {
3148 cPeer.updateCursorImmediately();
3149 }
3150 } else if (peer != null) {
3151 peer.updateCursorImmediately();
3152 }
3153 }
3154
3155 /**
3156 * Gets the cursor set in the component. If the component does
3157 * not have a cursor set, the cursor of its parent is returned.
3158 * If no cursor is set in the entire hierarchy,
3159 * <code>Cursor.DEFAULT_CURSOR</code> is returned.
3160 * @see #setCursor
3161 * @since JDK1.1
3162 */
3163 public Cursor getCursor() {
3164 return getCursor_NoClientCode();
3165 }
3166
3167 final Cursor getCursor_NoClientCode() {
3168 Cursor cursor = this.cursor;
3169 if (cursor != null) {
3170 return cursor;
3171 }
3172 Container parent = this.parent;
3173 if (parent != null) {
3174 return parent.getCursor_NoClientCode();
3175 } else {
3176 return Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
3177 }
3178 }
3179
3180 /**
3181 * Returns whether the cursor has been explicitly set for this Component.
3195 * <p>
3196 * This method is called when the contents of the component should
3197 * be painted; such as when the component is first being shown or
3198 * is damaged and in need of repair. The clip rectangle in the
3199 * <code>Graphics</code> parameter is set to the area
3200 * which needs to be painted.
3201 * Subclasses of <code>Component</code> that override this
3202 * method need not call <code>super.paint(g)</code>.
3203 * <p>
3204 * For performance reasons, <code>Component</code>s with zero width
3205 * or height aren't considered to need painting when they are first shown,
3206 * and also aren't considered to need repair.
3207 * <p>
3208 * <b>Note</b>: For more information on the paint mechanisms utilitized
3209 * by AWT and Swing, including information on how to write the most
3210 * efficient painting code, see
3211 * <a href="http://www.oracle.com/technetwork/java/painting-140037.html">Painting in AWT and Swing</a>.
3212 *
3213 * @param g the graphics context to use for painting
3214 * @see #update
3215 * @since JDK1.0
3216 */
3217 public void paint(Graphics g) {
3218 }
3219
3220 /**
3221 * Updates this component.
3222 * <p>
3223 * If this component is not a lightweight component, the
3224 * AWT calls the <code>update</code> method in response to
3225 * a call to <code>repaint</code>. You can assume that
3226 * the background is not cleared.
3227 * <p>
3228 * The <code>update</code> method of <code>Component</code>
3229 * calls this component's <code>paint</code> method to redraw
3230 * this component. This method is commonly overridden by subclasses
3231 * which need to do additional work in response to a call to
3232 * <code>repaint</code>.
3233 * Subclasses of Component that override this method should either
3234 * call <code>super.update(g)</code>, or call <code>paint(g)</code>
3235 * directly from their <code>update</code> method.
3236 * <p>
3237 * The origin of the graphics context, its
3238 * (<code>0</code>, <code>0</code>) coordinate point, is the
3239 * top-left corner of this component. The clipping region of the
3240 * graphics context is the bounding rectangle of this component.
3241 *
3242 * <p>
3243 * <b>Note</b>: For more information on the paint mechanisms utilitized
3244 * by AWT and Swing, including information on how to write the most
3245 * efficient painting code, see
3246 * <a href="http://www.oracle.com/technetwork/java/painting-140037.html">Painting in AWT and Swing</a>.
3247 *
3248 * @param g the specified context to use for updating
3249 * @see #paint
3250 * @see #repaint()
3251 * @since JDK1.0
3252 */
3253 public void update(Graphics g) {
3254 paint(g);
3255 }
3256
3257 /**
3258 * Paints this component and all of its subcomponents.
3259 * <p>
3260 * The origin of the graphics context, its
3261 * (<code>0</code>, <code>0</code>) coordinate point, is the
3262 * top-left corner of this component. The clipping region of the
3263 * graphics context is the bounding rectangle of this component.
3264 *
3265 * @param g the graphics context to use for painting
3266 * @see #paint
3267 * @since JDK1.0
3268 */
3269 public void paintAll(Graphics g) {
3270 if (isShowing()) {
3271 GraphicsCallback.PeerPaintCallback.getInstance().
3272 runOneComponent(this, new Rectangle(0, 0, width, height),
3273 g, g.getClip(),
3274 GraphicsCallback.LIGHTWEIGHTS |
3275 GraphicsCallback.HEAVYWEIGHTS);
3276 }
3277 }
3278
3279 /**
3280 * Simulates the peer callbacks into java.awt for painting of
3281 * lightweight Components.
3282 * @param g the graphics context to use for painting
3283 * @see #paintAll
3284 */
3285 void lightweightPaint(Graphics g) {
3286 paint(g);
3287 }
3291 */
3292 void paintHeavyweightComponents(Graphics g) {
3293 }
3294
3295 /**
3296 * Repaints this component.
3297 * <p>
3298 * If this component is a lightweight component, this method
3299 * causes a call to this component's <code>paint</code>
3300 * method as soon as possible. Otherwise, this method causes
3301 * a call to this component's <code>update</code> method as soon
3302 * as possible.
3303 * <p>
3304 * <b>Note</b>: For more information on the paint mechanisms utilitized
3305 * by AWT and Swing, including information on how to write the most
3306 * efficient painting code, see
3307 * <a href="http://www.oracle.com/technetwork/java/painting-140037.html">Painting in AWT and Swing</a>.
3308
3309 *
3310 * @see #update(Graphics)
3311 * @since JDK1.0
3312 */
3313 public void repaint() {
3314 repaint(0, 0, 0, width, height);
3315 }
3316
3317 /**
3318 * Repaints the component. If this component is a lightweight
3319 * component, this results in a call to <code>paint</code>
3320 * within <code>tm</code> milliseconds.
3321 * <p>
3322 * <b>Note</b>: For more information on the paint mechanisms utilitized
3323 * by AWT and Swing, including information on how to write the most
3324 * efficient painting code, see
3325 * <a href="http://www.oracle.com/technetwork/java/painting-140037.html">Painting in AWT and Swing</a>.
3326 *
3327 * @param tm maximum time in milliseconds before update
3328 * @see #paint
3329 * @see #update(Graphics)
3330 * @since JDK1.0
3331 */
3332 public void repaint(long tm) {
3333 repaint(tm, 0, 0, width, height);
3334 }
3335
3336 /**
3337 * Repaints the specified rectangle of this component.
3338 * <p>
3339 * If this component is a lightweight component, this method
3340 * causes a call to this component's <code>paint</code> method
3341 * as soon as possible. Otherwise, this method causes a call to
3342 * this component's <code>update</code> method as soon as possible.
3343 * <p>
3344 * <b>Note</b>: For more information on the paint mechanisms utilitized
3345 * by AWT and Swing, including information on how to write the most
3346 * efficient painting code, see
3347 * <a href="http://www.oracle.com/technetwork/java/painting-140037.html">Painting in AWT and Swing</a>.
3348 *
3349 * @param x the <i>x</i> coordinate
3350 * @param y the <i>y</i> coordinate
3351 * @param width the width
3352 * @param height the height
3353 * @see #update(Graphics)
3354 * @since JDK1.0
3355 */
3356 public void repaint(int x, int y, int width, int height) {
3357 repaint(0, x, y, width, height);
3358 }
3359
3360 /**
3361 * Repaints the specified rectangle of this component within
3362 * <code>tm</code> milliseconds.
3363 * <p>
3364 * If this component is a lightweight component, this method causes
3365 * a call to this component's <code>paint</code> method.
3366 * Otherwise, this method causes a call to this component's
3367 * <code>update</code> method.
3368 * <p>
3369 * <b>Note</b>: For more information on the paint mechanisms utilitized
3370 * by AWT and Swing, including information on how to write the most
3371 * efficient painting code, see
3372 * <a href="http://www.oracle.com/technetwork/java/painting-140037.html">Painting in AWT and Swing</a>.
3373 *
3374 * @param tm maximum time in milliseconds before update
3375 * @param x the <i>x</i> coordinate
3376 * @param y the <i>y</i> coordinate
3377 * @param width the width
3378 * @param height the height
3379 * @see #update(Graphics)
3380 * @since JDK1.0
3381 */
3382 public void repaint(long tm, int x, int y, int width, int height) {
3383 if (this.peer instanceof LightweightPeer) {
3384 // Needs to be translated to parent coordinates since
3385 // a parent native container provides the actual repaint
3386 // services. Additionally, the request is restricted to
3387 // the bounds of the component.
3388 if (parent != null) {
3389 if (x < 0) {
3390 width += x;
3391 x = 0;
3392 }
3393 if (y < 0) {
3394 height += y;
3395 y = 0;
3396 }
3397
3398 int pwidth = (width > this.width) ? this.width : width;
3399 int pheight = (height > this.height) ? this.height : height;
3400
3413 new Rectangle(x, y, width, height));
3414 Toolkit.getEventQueue().postEvent(e);
3415 }
3416 }
3417 }
3418
3419 /**
3420 * Prints this component. Applications should override this method
3421 * for components that must do special processing before being
3422 * printed or should be printed differently than they are painted.
3423 * <p>
3424 * The default implementation of this method calls the
3425 * <code>paint</code> method.
3426 * <p>
3427 * The origin of the graphics context, its
3428 * (<code>0</code>, <code>0</code>) coordinate point, is the
3429 * top-left corner of this component. The clipping region of the
3430 * graphics context is the bounding rectangle of this component.
3431 * @param g the graphics context to use for printing
3432 * @see #paint(Graphics)
3433 * @since JDK1.0
3434 */
3435 public void print(Graphics g) {
3436 paint(g);
3437 }
3438
3439 /**
3440 * Prints this component and all of its subcomponents.
3441 * <p>
3442 * The origin of the graphics context, its
3443 * (<code>0</code>, <code>0</code>) coordinate point, is the
3444 * top-left corner of this component. The clipping region of the
3445 * graphics context is the bounding rectangle of this component.
3446 * @param g the graphics context to use for printing
3447 * @see #print(Graphics)
3448 * @since JDK1.0
3449 */
3450 public void printAll(Graphics g) {
3451 if (isShowing()) {
3452 GraphicsCallback.PeerPrintCallback.getInstance().
3453 runOneComponent(this, new Rectangle(0, 0, width, height),
3454 g, g.getClip(),
3455 GraphicsCallback.LIGHTWEIGHTS |
3456 GraphicsCallback.HEAVYWEIGHTS);
3457 }
3458 }
3459
3460 /**
3461 * Simulates the peer callbacks into java.awt for printing of
3462 * lightweight Components.
3463 * @param g the graphics context to use for printing
3464 * @see #printAll
3465 */
3466 void lightweightPrint(Graphics g) {
3467 print(g);
3468 }
3508 * <p>
3509 * The interpretation of the <code>x</code>, <code>y</code>,
3510 * <code>width</code>, and <code>height</code> arguments depends on
3511 * the value of the <code>infoflags</code> argument.
3512 *
3513 * @param img the image being observed
3514 * @param infoflags see <code>imageUpdate</code> for more information
3515 * @param x the <i>x</i> coordinate
3516 * @param y the <i>y</i> coordinate
3517 * @param w the width
3518 * @param h the height
3519 * @return <code>false</code> if the infoflags indicate that the
3520 * image is completely loaded; <code>true</code> otherwise.
3521 *
3522 * @see java.awt.image.ImageObserver
3523 * @see Graphics#drawImage(Image, int, int, Color, java.awt.image.ImageObserver)
3524 * @see Graphics#drawImage(Image, int, int, java.awt.image.ImageObserver)
3525 * @see Graphics#drawImage(Image, int, int, int, int, Color, java.awt.image.ImageObserver)
3526 * @see Graphics#drawImage(Image, int, int, int, int, java.awt.image.ImageObserver)
3527 * @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)
3528 * @since JDK1.0
3529 */
3530 public boolean imageUpdate(Image img, int infoflags,
3531 int x, int y, int w, int h) {
3532 int rate = -1;
3533 if ((infoflags & (FRAMEBITS|ALLBITS)) != 0) {
3534 rate = 0;
3535 } else if ((infoflags & SOMEBITS) != 0) {
3536 if (isInc) {
3537 rate = incRate;
3538 if (rate < 0) {
3539 rate = 0;
3540 }
3541 }
3542 }
3543 if (rate >= 0) {
3544 repaint(rate, 0, 0, width, height);
3545 }
3546 return (infoflags & (ALLBITS|ABORT)) == 0;
3547 }
3548
3549 /**
3550 * Creates an image from the specified image producer.
3551 * @param producer the image producer
3552 * @return the image produced
3553 * @since JDK1.0
3554 */
3555 public Image createImage(ImageProducer producer) {
3556 ComponentPeer peer = this.peer;
3557 if ((peer != null) && ! (peer instanceof LightweightPeer)) {
3558 return peer.createImage(producer);
3559 }
3560 return getToolkit().createImage(producer);
3561 }
3562
3563 /**
3564 * Creates an off-screen drawable image
3565 * to be used for double buffering.
3566 * @param width the specified width
3567 * @param height the specified height
3568 * @return an off-screen drawable image, which can be used for double
3569 * buffering. The return value may be <code>null</code> if the
3570 * component is not displayable. This will always happen if
3571 * <code>GraphicsEnvironment.isHeadless()</code> returns
3572 * <code>true</code>.
3573 * @see #isDisplayable
3574 * @see GraphicsEnvironment#isHeadless
3575 * @since JDK1.0
3576 */
3577 public Image createImage(int width, int height) {
3578 ComponentPeer peer = this.peer;
3579 if (peer instanceof LightweightPeer) {
3580 if (parent != null) { return parent.createImage(width, height); }
3581 else { return null;}
3582 } else {
3583 return (peer != null) ? peer.createImage(width, height) : null;
3584 }
3585 }
3586
3587 /**
3588 * Creates a volatile off-screen drawable image
3589 * to be used for double buffering.
3590 * @param width the specified width.
3591 * @param height the specified height.
3592 * @return an off-screen drawable image, which can be used for double
3593 * buffering. The return value may be <code>null</code> if the
3594 * component is not displayable. This will always happen if
3595 * <code>GraphicsEnvironment.isHeadless()</code> returns
3626 * to manage surface contents loss and capabilities.
3627 * @see java.awt.image.VolatileImage
3628 * @since 1.4
3629 */
3630 public VolatileImage createVolatileImage(int width, int height,
3631 ImageCapabilities caps) throws AWTException {
3632 // REMIND : check caps
3633 return createVolatileImage(width, height);
3634 }
3635
3636 /**
3637 * Prepares an image for rendering on this component. The image
3638 * data is downloaded asynchronously in another thread and the
3639 * appropriate screen representation of the image is generated.
3640 * @param image the <code>Image</code> for which to
3641 * prepare a screen representation
3642 * @param observer the <code>ImageObserver</code> object
3643 * to be notified as the image is being prepared
3644 * @return <code>true</code> if the image has already been fully
3645 * prepared; <code>false</code> otherwise
3646 * @since JDK1.0
3647 */
3648 public boolean prepareImage(Image image, ImageObserver observer) {
3649 return prepareImage(image, -1, -1, observer);
3650 }
3651
3652 /**
3653 * Prepares an image for rendering on this component at the
3654 * specified width and height.
3655 * <p>
3656 * The image data is downloaded asynchronously in another thread,
3657 * and an appropriately scaled screen representation of the image is
3658 * generated.
3659 * @param image the instance of <code>Image</code>
3660 * for which to prepare a screen representation
3661 * @param width the width of the desired screen representation
3662 * @param height the height of the desired screen representation
3663 * @param observer the <code>ImageObserver</code> object
3664 * to be notified as the image is being prepared
3665 * @return <code>true</code> if the image has already been fully
3666 * prepared; <code>false</code> otherwise
3667 * @see java.awt.image.ImageObserver
3668 * @since JDK1.0
3669 */
3670 public boolean prepareImage(Image image, int width, int height,
3671 ImageObserver observer) {
3672 ComponentPeer peer = this.peer;
3673 if (peer instanceof LightweightPeer) {
3674 return (parent != null)
3675 ? parent.prepareImage(image, width, height, observer)
3676 : getToolkit().prepareImage(image, width, height, observer);
3677 } else {
3678 return (peer != null)
3679 ? peer.prepareImage(image, width, height, observer)
3680 : getToolkit().prepareImage(image, width, height, observer);
3681 }
3682 }
3683
3684 /**
3685 * Returns the status of the construction of a screen representation
3686 * of the specified image.
3687 * <p>
3688 * This method does not cause the image to begin loading. An
3689 * application must use the <code>prepareImage</code> method
3690 * to force the loading of an image.
3691 * <p>
3692 * Information on the flags returned by this method can be found
3693 * with the discussion of the <code>ImageObserver</code> interface.
3694 * @param image the <code>Image</code> object whose status
3695 * is being checked
3696 * @param observer the <code>ImageObserver</code>
3697 * object to be notified as the image is being prepared
3698 * @return the bitwise inclusive <b>OR</b> of
3699 * <code>ImageObserver</code> flags indicating what
3700 * information about the image is currently available
3701 * @see #prepareImage(Image, int, int, java.awt.image.ImageObserver)
3702 * @see Toolkit#checkImage(Image, int, int, java.awt.image.ImageObserver)
3703 * @see java.awt.image.ImageObserver
3704 * @since JDK1.0
3705 */
3706 public int checkImage(Image image, ImageObserver observer) {
3707 return checkImage(image, -1, -1, observer);
3708 }
3709
3710 /**
3711 * Returns the status of the construction of a screen representation
3712 * of the specified image.
3713 * <p>
3714 * This method does not cause the image to begin loading. An
3715 * application must use the <code>prepareImage</code> method
3716 * to force the loading of an image.
3717 * <p>
3718 * The <code>checkImage</code> method of <code>Component</code>
3719 * calls its peer's <code>checkImage</code> method to calculate
3720 * the flags. If this component does not yet have a peer, the
3721 * component's toolkit's <code>checkImage</code> method is called
3722 * instead.
3723 * <p>
3724 * Information on the flags returned by this method can be found
3725 * with the discussion of the <code>ImageObserver</code> interface.
3726 * @param image the <code>Image</code> object whose status
3727 * is being checked
3728 * @param width the width of the scaled version
3729 * whose status is to be checked
3730 * @param height the height of the scaled version
3731 * whose status is to be checked
3732 * @param observer the <code>ImageObserver</code> object
3733 * to be notified as the image is being prepared
3734 * @return the bitwise inclusive <b>OR</b> of
3735 * <code>ImageObserver</code> flags indicating what
3736 * information about the image is currently available
3737 * @see #prepareImage(Image, int, int, java.awt.image.ImageObserver)
3738 * @see Toolkit#checkImage(Image, int, int, java.awt.image.ImageObserver)
3739 * @see java.awt.image.ImageObserver
3740 * @since JDK1.0
3741 */
3742 public int checkImage(Image image, int width, int height,
3743 ImageObserver observer) {
3744 ComponentPeer peer = this.peer;
3745 if (peer instanceof LightweightPeer) {
3746 return (parent != null)
3747 ? parent.checkImage(image, width, height, observer)
3748 : getToolkit().checkImage(image, width, height, observer);
3749 } else {
3750 return (peer != null)
3751 ? peer.checkImage(image, width, height, observer)
3752 : getToolkit().checkImage(image, width, height, observer);
3753 }
3754 }
3755
3756 /**
3757 * Creates a new strategy for multi-buffering on this component.
3758 * Multi-buffering is useful for rendering performance. This method
3759 * attempts to create the best strategy available with the number of
3760 * buffers supplied. It will always create a <code>BufferStrategy</code>
4605 }
4606
4607 /**
4608 * @return whether or not paint messages received from the operating system
4609 * should be ignored.
4610 *
4611 * @since 1.4
4612 * @see #setIgnoreRepaint
4613 */
4614 public boolean getIgnoreRepaint() {
4615 return ignoreRepaint;
4616 }
4617
4618 /**
4619 * Checks whether this component "contains" the specified point,
4620 * where <code>x</code> and <code>y</code> are defined to be
4621 * relative to the coordinate system of this component.
4622 * @param x the <i>x</i> coordinate of the point
4623 * @param y the <i>y</i> coordinate of the point
4624 * @see #getComponentAt(int, int)
4625 * @since JDK1.1
4626 */
4627 public boolean contains(int x, int y) {
4628 return inside(x, y);
4629 }
4630
4631 /**
4632 * @deprecated As of JDK version 1.1,
4633 * replaced by contains(int, int).
4634 */
4635 @Deprecated
4636 public boolean inside(int x, int y) {
4637 return (x >= 0) && (x < width) && (y >= 0) && (y < height);
4638 }
4639
4640 /**
4641 * Checks whether this component "contains" the specified point,
4642 * where the point's <i>x</i> and <i>y</i> coordinates are defined
4643 * to be relative to the coordinate system of this component.
4644 * @param p the point
4645 * @throws NullPointerException if {@code p} is {@code null}
4646 * @see #getComponentAt(Point)
4647 * @since JDK1.1
4648 */
4649 public boolean contains(Point p) {
4650 return contains(p.x, p.y);
4651 }
4652
4653 /**
4654 * Determines if this component or one of its immediate
4655 * subcomponents contains the (<i>x</i>, <i>y</i>) location,
4656 * and if so, returns the containing component. This method only
4657 * looks one level deep. If the point (<i>x</i>, <i>y</i>) is
4658 * inside a subcomponent that itself has subcomponents, it does not
4659 * go looking down the subcomponent tree.
4660 * <p>
4661 * The <code>locate</code> method of <code>Component</code> simply
4662 * returns the component itself if the (<i>x</i>, <i>y</i>)
4663 * coordinate location is inside its bounding box, and <code>null</code>
4664 * otherwise.
4665 * @param x the <i>x</i> coordinate
4666 * @param y the <i>y</i> coordinate
4667 * @return the component or subcomponent that contains the
4668 * (<i>x</i>, <i>y</i>) location;
4669 * <code>null</code> if the location
4670 * is outside this component
4671 * @see #contains(int, int)
4672 * @since JDK1.0
4673 */
4674 public Component getComponentAt(int x, int y) {
4675 return locate(x, y);
4676 }
4677
4678 /**
4679 * @deprecated As of JDK version 1.1,
4680 * replaced by getComponentAt(int, int).
4681 */
4682 @Deprecated
4683 public Component locate(int x, int y) {
4684 return contains(x, y) ? this : null;
4685 }
4686
4687 /**
4688 * Returns the component or subcomponent that contains the
4689 * specified point.
4690 * @param p the point
4691 * @see java.awt.Component#contains
4692 * @since JDK1.1
4693 */
4694 public Component getComponentAt(Point p) {
4695 return getComponentAt(p.x, p.y);
4696 }
4697
4698 /**
4699 * @deprecated As of JDK version 1.1,
4700 * replaced by <code>dispatchEvent(AWTEvent e)</code>.
4701 */
4702 @Deprecated
4703 public void deliverEvent(Event e) {
4704 postEvent(e);
4705 }
4706
4707 /**
4708 * Dispatches an event to this component or one of its sub components.
4709 * Calls <code>processEvent</code> before returning for 1.1-style
4710 * events which have been enabled for the <code>Component</code>.
4711 * @param e the event
4712 */
5207 e.y = eventy;
5208 }
5209 return false;
5210 }
5211
5212 // Event source interfaces
5213
5214 /**
5215 * Adds the specified component listener to receive component events from
5216 * this component.
5217 * If listener <code>l</code> is <code>null</code>,
5218 * no exception is thrown and no action is performed.
5219 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
5220 * >AWT Threading Issues</a> for details on AWT's threading model.
5221 *
5222 * @param l the component listener
5223 * @see java.awt.event.ComponentEvent
5224 * @see java.awt.event.ComponentListener
5225 * @see #removeComponentListener
5226 * @see #getComponentListeners
5227 * @since JDK1.1
5228 */
5229 public synchronized void addComponentListener(ComponentListener l) {
5230 if (l == null) {
5231 return;
5232 }
5233 componentListener = AWTEventMulticaster.add(componentListener, l);
5234 newEventsOnly = true;
5235 }
5236
5237 /**
5238 * Removes the specified component listener so that it no longer
5239 * receives component events from this component. This method performs
5240 * no function, nor does it throw an exception, if the listener
5241 * specified by the argument was not previously added to this component.
5242 * If listener <code>l</code> is <code>null</code>,
5243 * no exception is thrown and no action is performed.
5244 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
5245 * >AWT Threading Issues</a> for details on AWT's threading model.
5246 * @param l the component listener
5247 * @see java.awt.event.ComponentEvent
5248 * @see java.awt.event.ComponentListener
5249 * @see #addComponentListener
5250 * @see #getComponentListeners
5251 * @since JDK1.1
5252 */
5253 public synchronized void removeComponentListener(ComponentListener l) {
5254 if (l == null) {
5255 return;
5256 }
5257 componentListener = AWTEventMulticaster.remove(componentListener, l);
5258 }
5259
5260 /**
5261 * Returns an array of all the component listeners
5262 * registered on this component.
5263 *
5264 * @return all <code>ComponentListener</code>s of this component
5265 * or an empty array if no component
5266 * listeners are currently registered
5267 *
5268 * @see #addComponentListener
5269 * @see #removeComponentListener
5270 * @since 1.4
5271 */
5272 public synchronized ComponentListener[] getComponentListeners() {
5273 return getListeners(ComponentListener.class);
5274 }
5275
5276 /**
5277 * Adds the specified focus listener to receive focus events from
5278 * this component when this component gains input focus.
5279 * If listener <code>l</code> is <code>null</code>,
5280 * no exception is thrown and no action is performed.
5281 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
5282 * >AWT Threading Issues</a> for details on AWT's threading model.
5283 *
5284 * @param l the focus listener
5285 * @see java.awt.event.FocusEvent
5286 * @see java.awt.event.FocusListener
5287 * @see #removeFocusListener
5288 * @see #getFocusListeners
5289 * @since JDK1.1
5290 */
5291 public synchronized void addFocusListener(FocusListener l) {
5292 if (l == null) {
5293 return;
5294 }
5295 focusListener = AWTEventMulticaster.add(focusListener, l);
5296 newEventsOnly = true;
5297
5298 // if this is a lightweight component, enable focus events
5299 // in the native container.
5300 if (peer instanceof LightweightPeer) {
5301 parent.proxyEnableEvents(AWTEvent.FOCUS_EVENT_MASK);
5302 }
5303 }
5304
5305 /**
5306 * Removes the specified focus listener so that it no longer
5307 * receives focus events from this component. This method performs
5308 * no function, nor does it throw an exception, if the listener
5309 * specified by the argument was not previously added to this component.
5310 * If listener <code>l</code> is <code>null</code>,
5311 * no exception is thrown and no action is performed.
5312 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
5313 * >AWT Threading Issues</a> for details on AWT's threading model.
5314 *
5315 * @param l the focus listener
5316 * @see java.awt.event.FocusEvent
5317 * @see java.awt.event.FocusListener
5318 * @see #addFocusListener
5319 * @see #getFocusListeners
5320 * @since JDK1.1
5321 */
5322 public synchronized void removeFocusListener(FocusListener l) {
5323 if (l == null) {
5324 return;
5325 }
5326 focusListener = AWTEventMulticaster.remove(focusListener, l);
5327 }
5328
5329 /**
5330 * Returns an array of all the focus listeners
5331 * registered on this component.
5332 *
5333 * @return all of this component's <code>FocusListener</code>s
5334 * or an empty array if no component
5335 * listeners are currently registered
5336 *
5337 * @see #addFocusListener
5338 * @see #removeFocusListener
5339 * @since 1.4
5340 */
5601 * because parent in Window is owner.
5602 */
5603 void adjustListeningChildrenOnParent(long mask, int num) {
5604 if (parent != null) {
5605 parent.adjustListeningChildren(mask, num);
5606 }
5607 }
5608
5609 /**
5610 * Adds the specified key listener to receive key events from
5611 * this component.
5612 * If l is null, no exception is thrown and no action is performed.
5613 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
5614 * >AWT Threading Issues</a> for details on AWT's threading model.
5615 *
5616 * @param l the key listener.
5617 * @see java.awt.event.KeyEvent
5618 * @see java.awt.event.KeyListener
5619 * @see #removeKeyListener
5620 * @see #getKeyListeners
5621 * @since JDK1.1
5622 */
5623 public synchronized void addKeyListener(KeyListener l) {
5624 if (l == null) {
5625 return;
5626 }
5627 keyListener = AWTEventMulticaster.add(keyListener, l);
5628 newEventsOnly = true;
5629
5630 // if this is a lightweight component, enable key events
5631 // in the native container.
5632 if (peer instanceof LightweightPeer) {
5633 parent.proxyEnableEvents(AWTEvent.KEY_EVENT_MASK);
5634 }
5635 }
5636
5637 /**
5638 * Removes the specified key listener so that it no longer
5639 * receives key events from this component. This method performs
5640 * no function, nor does it throw an exception, if the listener
5641 * specified by the argument was not previously added to this component.
5642 * If listener <code>l</code> is <code>null</code>,
5643 * no exception is thrown and no action is performed.
5644 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
5645 * >AWT Threading Issues</a> for details on AWT's threading model.
5646 *
5647 * @param l the key listener
5648 * @see java.awt.event.KeyEvent
5649 * @see java.awt.event.KeyListener
5650 * @see #addKeyListener
5651 * @see #getKeyListeners
5652 * @since JDK1.1
5653 */
5654 public synchronized void removeKeyListener(KeyListener l) {
5655 if (l == null) {
5656 return;
5657 }
5658 keyListener = AWTEventMulticaster.remove(keyListener, l);
5659 }
5660
5661 /**
5662 * Returns an array of all the key listeners
5663 * registered on this component.
5664 *
5665 * @return all of this component's <code>KeyListener</code>s
5666 * or an empty array if no key
5667 * listeners are currently registered
5668 *
5669 * @see #addKeyListener
5670 * @see #removeKeyListener
5671 * @since 1.4
5672 */
5673 public synchronized KeyListener[] getKeyListeners() {
5674 return getListeners(KeyListener.class);
5675 }
5676
5677 /**
5678 * Adds the specified mouse listener to receive mouse events from
5679 * this component.
5680 * If listener <code>l</code> is <code>null</code>,
5681 * no exception is thrown and no action is performed.
5682 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
5683 * >AWT Threading Issues</a> for details on AWT's threading model.
5684 *
5685 * @param l the mouse listener
5686 * @see java.awt.event.MouseEvent
5687 * @see java.awt.event.MouseListener
5688 * @see #removeMouseListener
5689 * @see #getMouseListeners
5690 * @since JDK1.1
5691 */
5692 public synchronized void addMouseListener(MouseListener l) {
5693 if (l == null) {
5694 return;
5695 }
5696 mouseListener = AWTEventMulticaster.add(mouseListener,l);
5697 newEventsOnly = true;
5698
5699 // if this is a lightweight component, enable mouse events
5700 // in the native container.
5701 if (peer instanceof LightweightPeer) {
5702 parent.proxyEnableEvents(AWTEvent.MOUSE_EVENT_MASK);
5703 }
5704 }
5705
5706 /**
5707 * Removes the specified mouse listener so that it no longer
5708 * receives mouse events from this component. This method performs
5709 * no function, nor does it throw an exception, if the listener
5710 * specified by the argument was not previously added to this component.
5711 * If listener <code>l</code> is <code>null</code>,
5712 * no exception is thrown and no action is performed.
5713 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
5714 * >AWT Threading Issues</a> for details on AWT's threading model.
5715 *
5716 * @param l the mouse listener
5717 * @see java.awt.event.MouseEvent
5718 * @see java.awt.event.MouseListener
5719 * @see #addMouseListener
5720 * @see #getMouseListeners
5721 * @since JDK1.1
5722 */
5723 public synchronized void removeMouseListener(MouseListener l) {
5724 if (l == null) {
5725 return;
5726 }
5727 mouseListener = AWTEventMulticaster.remove(mouseListener, l);
5728 }
5729
5730 /**
5731 * Returns an array of all the mouse listeners
5732 * registered on this component.
5733 *
5734 * @return all of this component's <code>MouseListener</code>s
5735 * or an empty array if no mouse
5736 * listeners are currently registered
5737 *
5738 * @see #addMouseListener
5739 * @see #removeMouseListener
5740 * @since 1.4
5741 */
5742 public synchronized MouseListener[] getMouseListeners() {
5743 return getListeners(MouseListener.class);
5744 }
5745
5746 /**
5747 * Adds the specified mouse motion listener to receive mouse motion
5748 * events from this component.
5749 * If listener <code>l</code> is <code>null</code>,
5750 * no exception is thrown and no action is performed.
5751 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
5752 * >AWT Threading Issues</a> for details on AWT's threading model.
5753 *
5754 * @param l the mouse motion listener
5755 * @see java.awt.event.MouseEvent
5756 * @see java.awt.event.MouseMotionListener
5757 * @see #removeMouseMotionListener
5758 * @see #getMouseMotionListeners
5759 * @since JDK1.1
5760 */
5761 public synchronized void addMouseMotionListener(MouseMotionListener l) {
5762 if (l == null) {
5763 return;
5764 }
5765 mouseMotionListener = AWTEventMulticaster.add(mouseMotionListener,l);
5766 newEventsOnly = true;
5767
5768 // if this is a lightweight component, enable mouse events
5769 // in the native container.
5770 if (peer instanceof LightweightPeer) {
5771 parent.proxyEnableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
5772 }
5773 }
5774
5775 /**
5776 * Removes the specified mouse motion listener so that it no longer
5777 * receives mouse motion events from this component. This method performs
5778 * no function, nor does it throw an exception, if the listener
5779 * specified by the argument was not previously added to this component.
5780 * If listener <code>l</code> is <code>null</code>,
5781 * no exception is thrown and no action is performed.
5782 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
5783 * >AWT Threading Issues</a> for details on AWT's threading model.
5784 *
5785 * @param l the mouse motion listener
5786 * @see java.awt.event.MouseEvent
5787 * @see java.awt.event.MouseMotionListener
5788 * @see #addMouseMotionListener
5789 * @see #getMouseMotionListeners
5790 * @since JDK1.1
5791 */
5792 public synchronized void removeMouseMotionListener(MouseMotionListener l) {
5793 if (l == null) {
5794 return;
5795 }
5796 mouseMotionListener = AWTEventMulticaster.remove(mouseMotionListener, l);
5797 }
5798
5799 /**
5800 * Returns an array of all the mouse motion listeners
5801 * registered on this component.
5802 *
5803 * @return all of this component's <code>MouseMotionListener</code>s
5804 * or an empty array if no mouse motion
5805 * listeners are currently registered
5806 *
5807 * @see #addMouseMotionListener
5808 * @see #removeMouseMotionListener
5809 * @since 1.4
5810 */
6055 } else {
6056 return parent.getInputContext();
6057 }
6058 }
6059
6060 /**
6061 * Enables the events defined by the specified event mask parameter
6062 * to be delivered to this component.
6063 * <p>
6064 * Event types are automatically enabled when a listener for
6065 * that event type is added to the component.
6066 * <p>
6067 * This method only needs to be invoked by subclasses of
6068 * <code>Component</code> which desire to have the specified event
6069 * types delivered to <code>processEvent</code> regardless of whether
6070 * or not a listener is registered.
6071 * @param eventsToEnable the event mask defining the event types
6072 * @see #processEvent
6073 * @see #disableEvents
6074 * @see AWTEvent
6075 * @since JDK1.1
6076 */
6077 protected final void enableEvents(long eventsToEnable) {
6078 long notifyAncestors = 0;
6079 synchronized (this) {
6080 if ((eventsToEnable & AWTEvent.HIERARCHY_EVENT_MASK) != 0 &&
6081 hierarchyListener == null &&
6082 (eventMask & AWTEvent.HIERARCHY_EVENT_MASK) == 0) {
6083 notifyAncestors |= AWTEvent.HIERARCHY_EVENT_MASK;
6084 }
6085 if ((eventsToEnable & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) != 0 &&
6086 hierarchyBoundsListener == null &&
6087 (eventMask & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) == 0) {
6088 notifyAncestors |= AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK;
6089 }
6090 eventMask |= eventsToEnable;
6091 newEventsOnly = true;
6092 }
6093
6094 // if this is a lightweight component, enable mouse events
6095 // in the native container.
6096 if (peer instanceof LightweightPeer) {
6097 parent.proxyEnableEvents(eventMask);
6098 }
6099 if (notifyAncestors != 0) {
6100 synchronized (getTreeLock()) {
6101 adjustListeningChildrenOnParent(notifyAncestors, 1);
6102 }
6103 }
6104 }
6105
6106 /**
6107 * Disables the events defined by the specified event mask parameter
6108 * from being delivered to this component.
6109 * @param eventsToDisable the event mask defining the event types
6110 * @see #enableEvents
6111 * @since JDK1.1
6112 */
6113 protected final void disableEvents(long eventsToDisable) {
6114 long notifyAncestors = 0;
6115 synchronized (this) {
6116 if ((eventsToDisable & AWTEvent.HIERARCHY_EVENT_MASK) != 0 &&
6117 hierarchyListener == null &&
6118 (eventMask & AWTEvent.HIERARCHY_EVENT_MASK) != 0) {
6119 notifyAncestors |= AWTEvent.HIERARCHY_EVENT_MASK;
6120 }
6121 if ((eventsToDisable & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK)!=0 &&
6122 hierarchyBoundsListener == null &&
6123 (eventMask & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) != 0) {
6124 notifyAncestors |= AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK;
6125 }
6126 eventMask &= ~eventsToDisable;
6127 }
6128 if (notifyAncestors != 0) {
6129 synchronized (getTreeLock()) {
6130 adjustListeningChildrenOnParent(notifyAncestors, -1);
6131 }
6268 }
6269
6270 /**
6271 * Processes events occurring on this component. By default this
6272 * method calls the appropriate
6273 * <code>process<event type>Event</code>
6274 * method for the given class of event.
6275 * <p>Note that if the event parameter is <code>null</code>
6276 * the behavior is unspecified and may result in an
6277 * exception.
6278 *
6279 * @param e the event
6280 * @see #processComponentEvent
6281 * @see #processFocusEvent
6282 * @see #processKeyEvent
6283 * @see #processMouseEvent
6284 * @see #processMouseMotionEvent
6285 * @see #processInputMethodEvent
6286 * @see #processHierarchyEvent
6287 * @see #processMouseWheelEvent
6288 * @since JDK1.1
6289 */
6290 protected void processEvent(AWTEvent e) {
6291 if (e instanceof FocusEvent) {
6292 processFocusEvent((FocusEvent)e);
6293
6294 } else if (e instanceof MouseEvent) {
6295 switch(e.getID()) {
6296 case MouseEvent.MOUSE_PRESSED:
6297 case MouseEvent.MOUSE_RELEASED:
6298 case MouseEvent.MOUSE_CLICKED:
6299 case MouseEvent.MOUSE_ENTERED:
6300 case MouseEvent.MOUSE_EXITED:
6301 processMouseEvent((MouseEvent)e);
6302 break;
6303 case MouseEvent.MOUSE_MOVED:
6304 case MouseEvent.MOUSE_DRAGGED:
6305 processMouseMotionEvent((MouseEvent)e);
6306 break;
6307 case MouseEvent.MOUSE_WHEEL:
6308 processMouseWheelEvent((MouseWheelEvent)e);
6334 * dispatching them to any registered
6335 * <code>ComponentListener</code> objects.
6336 * <p>
6337 * This method is not called unless component events are
6338 * enabled for this component. Component events are enabled
6339 * when one of the following occurs:
6340 * <ul>
6341 * <li>A <code>ComponentListener</code> object is registered
6342 * via <code>addComponentListener</code>.
6343 * <li>Component events are enabled via <code>enableEvents</code>.
6344 * </ul>
6345 * <p>Note that if the event parameter is <code>null</code>
6346 * the behavior is unspecified and may result in an
6347 * exception.
6348 *
6349 * @param e the component event
6350 * @see java.awt.event.ComponentEvent
6351 * @see java.awt.event.ComponentListener
6352 * @see #addComponentListener
6353 * @see #enableEvents
6354 * @since JDK1.1
6355 */
6356 protected void processComponentEvent(ComponentEvent e) {
6357 ComponentListener listener = componentListener;
6358 if (listener != null) {
6359 int id = e.getID();
6360 switch(id) {
6361 case ComponentEvent.COMPONENT_RESIZED:
6362 listener.componentResized(e);
6363 break;
6364 case ComponentEvent.COMPONENT_MOVED:
6365 listener.componentMoved(e);
6366 break;
6367 case ComponentEvent.COMPONENT_SHOWN:
6368 listener.componentShown(e);
6369 break;
6370 case ComponentEvent.COMPONENT_HIDDEN:
6371 listener.componentHidden(e);
6372 break;
6373 }
6374 }
6397 * method, which results in a call to the <code>Component</code>'s
6398 * <code>processFocusEvent</code> method.
6399 * <p>
6400 * If focus events are enabled for a <code>Component</code>, calling
6401 * the <code>Component</code>'s <code>dispatchEvent</code> method
6402 * with a <code>FocusEvent</code> as the argument will result in a
6403 * call to the <code>Component</code>'s <code>processFocusEvent</code>
6404 * method regardless of the current <code>KeyboardFocusManager</code>.
6405 *
6406 * <p>Note that if the event parameter is <code>null</code>
6407 * the behavior is unspecified and may result in an
6408 * exception.
6409 *
6410 * @param e the focus event
6411 * @see java.awt.event.FocusEvent
6412 * @see java.awt.event.FocusListener
6413 * @see java.awt.KeyboardFocusManager
6414 * @see #addFocusListener
6415 * @see #enableEvents
6416 * @see #dispatchEvent
6417 * @since JDK1.1
6418 */
6419 protected void processFocusEvent(FocusEvent e) {
6420 FocusListener listener = focusListener;
6421 if (listener != null) {
6422 int id = e.getID();
6423 switch(id) {
6424 case FocusEvent.FOCUS_GAINED:
6425 listener.focusGained(e);
6426 break;
6427 case FocusEvent.FOCUS_LOST:
6428 listener.focusLost(e);
6429 break;
6430 }
6431 }
6432 }
6433
6434 /**
6435 * Processes key events occurring on this component by
6436 * dispatching them to any registered
6437 * <code>KeyListener</code> objects.
6463 * method with a <code>KeyEvent</code> as the argument will
6464 * result in a call to the <code>Component</code>'s
6465 * <code>processKeyEvent</code> method regardless of the
6466 * current <code>KeyboardFocusManager</code> as long as the
6467 * component is showing, focused, and enabled, and key events
6468 * are enabled on it.
6469 * <p>If the event parameter is <code>null</code>
6470 * the behavior is unspecified and may result in an
6471 * exception.
6472 *
6473 * @param e the key event
6474 * @see java.awt.event.KeyEvent
6475 * @see java.awt.event.KeyListener
6476 * @see java.awt.KeyboardFocusManager
6477 * @see java.awt.DefaultKeyboardFocusManager
6478 * @see #processEvent
6479 * @see #dispatchEvent
6480 * @see #addKeyListener
6481 * @see #enableEvents
6482 * @see #isShowing
6483 * @since JDK1.1
6484 */
6485 protected void processKeyEvent(KeyEvent e) {
6486 KeyListener listener = keyListener;
6487 if (listener != null) {
6488 int id = e.getID();
6489 switch(id) {
6490 case KeyEvent.KEY_TYPED:
6491 listener.keyTyped(e);
6492 break;
6493 case KeyEvent.KEY_PRESSED:
6494 listener.keyPressed(e);
6495 break;
6496 case KeyEvent.KEY_RELEASED:
6497 listener.keyReleased(e);
6498 break;
6499 }
6500 }
6501 }
6502
6503 /**
6505 * dispatching them to any registered
6506 * <code>MouseListener</code> objects.
6507 * <p>
6508 * This method is not called unless mouse events are
6509 * enabled for this component. Mouse events are enabled
6510 * when one of the following occurs:
6511 * <ul>
6512 * <li>A <code>MouseListener</code> object is registered
6513 * via <code>addMouseListener</code>.
6514 * <li>Mouse events are enabled via <code>enableEvents</code>.
6515 * </ul>
6516 * <p>Note that if the event parameter is <code>null</code>
6517 * the behavior is unspecified and may result in an
6518 * exception.
6519 *
6520 * @param e the mouse event
6521 * @see java.awt.event.MouseEvent
6522 * @see java.awt.event.MouseListener
6523 * @see #addMouseListener
6524 * @see #enableEvents
6525 * @since JDK1.1
6526 */
6527 protected void processMouseEvent(MouseEvent e) {
6528 MouseListener listener = mouseListener;
6529 if (listener != null) {
6530 int id = e.getID();
6531 switch(id) {
6532 case MouseEvent.MOUSE_PRESSED:
6533 listener.mousePressed(e);
6534 break;
6535 case MouseEvent.MOUSE_RELEASED:
6536 listener.mouseReleased(e);
6537 break;
6538 case MouseEvent.MOUSE_CLICKED:
6539 listener.mouseClicked(e);
6540 break;
6541 case MouseEvent.MOUSE_EXITED:
6542 listener.mouseExited(e);
6543 break;
6544 case MouseEvent.MOUSE_ENTERED:
6545 listener.mouseEntered(e);
6553 * dispatching them to any registered
6554 * <code>MouseMotionListener</code> objects.
6555 * <p>
6556 * This method is not called unless mouse motion events are
6557 * enabled for this component. Mouse motion events are enabled
6558 * when one of the following occurs:
6559 * <ul>
6560 * <li>A <code>MouseMotionListener</code> object is registered
6561 * via <code>addMouseMotionListener</code>.
6562 * <li>Mouse motion events are enabled via <code>enableEvents</code>.
6563 * </ul>
6564 * <p>Note that if the event parameter is <code>null</code>
6565 * the behavior is unspecified and may result in an
6566 * exception.
6567 *
6568 * @param e the mouse motion event
6569 * @see java.awt.event.MouseEvent
6570 * @see java.awt.event.MouseMotionListener
6571 * @see #addMouseMotionListener
6572 * @see #enableEvents
6573 * @since JDK1.1
6574 */
6575 protected void processMouseMotionEvent(MouseEvent e) {
6576 MouseMotionListener listener = mouseMotionListener;
6577 if (listener != null) {
6578 int id = e.getID();
6579 switch(id) {
6580 case MouseEvent.MOUSE_MOVED:
6581 listener.mouseMoved(e);
6582 break;
6583 case MouseEvent.MOUSE_DRAGGED:
6584 listener.mouseDragged(e);
6585 break;
6586 }
6587 }
6588 }
6589
6590 /**
6591 * Processes mouse wheel events occurring on this component by
6592 * dispatching them to any registered
6593 * <code>MouseWheelListener</code> objects.
6865 * should register this component as ActionListener on component
6866 * which fires action events.
6867 */
6868 @Deprecated
6869 public boolean action(Event evt, Object what) {
6870 return false;
6871 }
6872
6873 /**
6874 * Makes this <code>Component</code> displayable by connecting it to a
6875 * native screen resource.
6876 * This method is called internally by the toolkit and should
6877 * not be called directly by programs.
6878 * <p>
6879 * This method changes layout-related information, and therefore,
6880 * invalidates the component hierarchy.
6881 *
6882 * @see #isDisplayable
6883 * @see #removeNotify
6884 * @see #invalidate
6885 * @since JDK1.0
6886 */
6887 public void addNotify() {
6888 synchronized (getTreeLock()) {
6889 ComponentPeer peer = this.peer;
6890 if (peer == null || peer instanceof LightweightPeer){
6891 if (peer == null) {
6892 // Update both the Component's peer variable and the local
6893 // variable we use for thread safety.
6894 this.peer = peer = getToolkit().createComponent(this);
6895 }
6896
6897 // This is a lightweight component which means it won't be
6898 // able to get window-related events by itself. If any
6899 // have been enabled, then the nearest native container must
6900 // be enabled.
6901 if (parent != null) {
6902 long mask = 0;
6903 if ((mouseListener != null) || ((eventMask & AWTEvent.MOUSE_EVENT_MASK) != 0)) {
6904 mask |= AWTEvent.MOUSE_EVENT_MASK;
6905 }
6968 HierarchyEvent.DISPLAYABILITY_CHANGED |
6969 ((isRecursivelyVisible())
6970 ? HierarchyEvent.SHOWING_CHANGED
6971 : 0));
6972 dispatchEvent(e);
6973 }
6974 }
6975 }
6976
6977 /**
6978 * Makes this <code>Component</code> undisplayable by destroying it native
6979 * screen resource.
6980 * <p>
6981 * This method is called by the toolkit internally and should
6982 * not be called directly by programs. Code overriding
6983 * this method should call <code>super.removeNotify</code> as
6984 * the first line of the overriding method.
6985 *
6986 * @see #isDisplayable
6987 * @see #addNotify
6988 * @since JDK1.0
6989 */
6990 public void removeNotify() {
6991 KeyboardFocusManager.clearMostRecentFocusOwner(this);
6992 if (KeyboardFocusManager.getCurrentKeyboardFocusManager().
6993 getPermanentFocusOwner() == this)
6994 {
6995 KeyboardFocusManager.getCurrentKeyboardFocusManager().
6996 setGlobalPermanentFocusOwner(null);
6997 }
6998
6999 synchronized (getTreeLock()) {
7000 clearLightweightDispatcherOnRemove(this);
7001
7002 if (isFocusOwner() && KeyboardFocusManager.isAutoFocusTransferEnabledFor(this)) {
7003 transferFocus(true);
7004 }
7005
7006 if (getContainer() != null && isAddNotifyComplete) {
7007 getContainer().decreaseComponentCount(this);
7008 }
7077 public boolean gotFocus(Event evt, Object what) {
7078 return false;
7079 }
7080
7081 /**
7082 * @deprecated As of JDK version 1.1,
7083 * replaced by processFocusEvent(FocusEvent).
7084 */
7085 @Deprecated
7086 public boolean lostFocus(Event evt, Object what) {
7087 return false;
7088 }
7089
7090 /**
7091 * Returns whether this <code>Component</code> can become the focus
7092 * owner.
7093 *
7094 * @return <code>true</code> if this <code>Component</code> is
7095 * focusable; <code>false</code> otherwise
7096 * @see #setFocusable
7097 * @since JDK1.1
7098 * @deprecated As of 1.4, replaced by <code>isFocusable()</code>.
7099 */
7100 @Deprecated
7101 public boolean isFocusTraversable() {
7102 if (isFocusTraversableOverridden == FOCUS_TRAVERSABLE_UNKNOWN) {
7103 isFocusTraversableOverridden = FOCUS_TRAVERSABLE_DEFAULT;
7104 }
7105 return focusable;
7106 }
7107
7108 /**
7109 * Returns whether this Component can be focused.
7110 *
7111 * @return <code>true</code> if this Component is focusable;
7112 * <code>false</code> otherwise.
7113 * @see #setFocusable
7114 * @since 1.4
7115 */
7116 public boolean isFocusable() {
7117 return isFocusTraversable();
7416 * Window is later focused by the user.
7417 * <p>
7418 * This method cannot be used to set the focus owner to no Component at
7419 * all. Use <code>KeyboardFocusManager.clearGlobalFocusOwner()</code>
7420 * instead.
7421 * <p>
7422 * Because the focus behavior of this method is platform-dependent,
7423 * developers are strongly encouraged to use
7424 * <code>requestFocusInWindow</code> when possible.
7425 *
7426 * <p>Note: Not all focus transfers result from invoking this method. As
7427 * such, a component may receive focus without this or any of the other
7428 * {@code requestFocus} methods of {@code Component} being invoked.
7429 *
7430 * @see #requestFocusInWindow
7431 * @see java.awt.event.FocusEvent
7432 * @see #addFocusListener
7433 * @see #isFocusable
7434 * @see #isDisplayable
7435 * @see KeyboardFocusManager#clearGlobalFocusOwner
7436 * @since JDK1.0
7437 */
7438 public void requestFocus() {
7439 requestFocusHelper(false, true);
7440 }
7441
7442 boolean requestFocus(CausedFocusEvent.Cause cause) {
7443 return requestFocusHelper(false, true, cause);
7444 }
7445
7446 /**
7447 * Requests that this <code>Component</code> get the input focus,
7448 * and that this <code>Component</code>'s top-level ancestor
7449 * become the focused <code>Window</code>. This component must be
7450 * displayable, focusable, visible and all of its ancestors (with
7451 * the exception of the top-level Window) must be visible for the
7452 * request to be granted. Every effort will be made to honor the
7453 * request; however, in some cases it may be impossible to do
7454 * so. Developers must never assume that this component is the
7455 * focus owner until this component receives a FOCUS_GAINED
7456 * event. If this request is denied because this component's
7845 *
7846 * @param container the Container to be tested
7847 * @return <code>true</code> if the specified Container is a focus-cycle-
7848 * root of this Component; <code>false</code> otherwise
7849 * @see Container#isFocusCycleRoot()
7850 * @since 1.4
7851 */
7852 public boolean isFocusCycleRoot(Container container) {
7853 Container rootAncestor = getFocusCycleRootAncestor();
7854 return (rootAncestor == container);
7855 }
7856
7857 Container getTraversalRoot() {
7858 return getFocusCycleRootAncestor();
7859 }
7860
7861 /**
7862 * Transfers the focus to the next component, as though this Component were
7863 * the focus owner.
7864 * @see #requestFocus()
7865 * @since JDK1.1
7866 */
7867 public void transferFocus() {
7868 nextFocus();
7869 }
7870
7871 /**
7872 * @deprecated As of JDK version 1.1,
7873 * replaced by transferFocus().
7874 */
7875 @Deprecated
7876 public void nextFocus() {
7877 transferFocus(false);
7878 }
7879
7880 boolean transferFocus(boolean clearOnFailure) {
7881 if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
7882 focusLog.finer("clearOnFailure = " + clearOnFailure);
7883 }
7884 Component toFocus = getNextFocusCandidate();
7885 boolean res = false;
8053
8054 /*
8055 * Used to disallow auto-focus-transfer on disposal of the focus owner
8056 * in the process of disposing its parent container.
8057 */
8058 private boolean autoFocusTransferOnDisposal = true;
8059
8060 void setAutoFocusTransferOnDisposal(boolean value) {
8061 autoFocusTransferOnDisposal = value;
8062 }
8063
8064 boolean isAutoFocusTransferOnDisposal() {
8065 return autoFocusTransferOnDisposal;
8066 }
8067
8068 /**
8069 * Adds the specified popup menu to the component.
8070 * @param popup the popup menu to be added to the component.
8071 * @see #remove(MenuComponent)
8072 * @exception NullPointerException if {@code popup} is {@code null}
8073 * @since JDK1.1
8074 */
8075 public void add(PopupMenu popup) {
8076 synchronized (getTreeLock()) {
8077 if (popup.parent != null) {
8078 popup.parent.remove(popup);
8079 }
8080 if (popups == null) {
8081 popups = new Vector<PopupMenu>();
8082 }
8083 popups.addElement(popup);
8084 popup.parent = this;
8085
8086 if (peer != null) {
8087 if (popup.peer == null) {
8088 popup.addNotify();
8089 }
8090 }
8091 }
8092 }
8093
8094 /**
8095 * Removes the specified popup menu from the component.
8096 * @param popup the popup menu to be removed
8097 * @see #add(PopupMenu)
8098 * @since JDK1.1
8099 */
8100 @SuppressWarnings("unchecked")
8101 public void remove(MenuComponent popup) {
8102 synchronized (getTreeLock()) {
8103 if (popups == null) {
8104 return;
8105 }
8106 int index = popups.indexOf(popup);
8107 if (index >= 0) {
8108 PopupMenu pmenu = (PopupMenu)popup;
8109 if (pmenu.peer != null) {
8110 pmenu.removeNotify();
8111 }
8112 pmenu.parent = null;
8113 popups.removeElementAt(index);
8114 if (popups.size() == 0) {
8115 popups = null;
8116 }
8117 }
8118 }
8119 }
8120
8121 /**
8122 * Returns a string representing the state of this component. This
8123 * method is intended to be used only for debugging purposes, and the
8124 * content and format of the returned string may vary between
8125 * implementations. The returned string may be empty but may not be
8126 * <code>null</code>.
8127 *
8128 * @return a string representation of this component's state
8129 * @since JDK1.0
8130 */
8131 protected String paramString() {
8132 final String thisName = Objects.toString(getName(), "");
8133 final String invalid = isValid() ? "" : ",invalid";
8134 final String hidden = visible ? "" : ",hidden";
8135 final String disabled = enabled ? "" : ",disabled";
8136 return thisName + ',' + x + ',' + y + ',' + width + 'x' + height
8137 + invalid + hidden + disabled;
8138 }
8139
8140 /**
8141 * Returns a string representation of this component and its values.
8142 * @return a string representation of this component
8143 * @since JDK1.0
8144 */
8145 public String toString() {
8146 return getClass().getName() + '[' + paramString() + ']';
8147 }
8148
8149 /**
8150 * Prints a listing of this component to the standard system output
8151 * stream <code>System.out</code>.
8152 * @see java.lang.System#out
8153 * @since JDK1.0
8154 */
8155 public void list() {
8156 list(System.out, 0);
8157 }
8158
8159 /**
8160 * Prints a listing of this component to the specified output
8161 * stream.
8162 * @param out a print stream
8163 * @throws NullPointerException if {@code out} is {@code null}
8164 * @since JDK1.0
8165 */
8166 public void list(PrintStream out) {
8167 list(out, 0);
8168 }
8169
8170 /**
8171 * Prints out a list, starting at the specified indentation, to the
8172 * specified print stream.
8173 * @param out a print stream
8174 * @param indent number of spaces to indent
8175 * @see java.io.PrintStream#println(java.lang.Object)
8176 * @throws NullPointerException if {@code out} is {@code null}
8177 * @since JDK1.0
8178 */
8179 public void list(PrintStream out, int indent) {
8180 for (int i = 0 ; i < indent ; i++) {
8181 out.print(" ");
8182 }
8183 out.println(this);
8184 }
8185
8186 /**
8187 * Prints a listing to the specified print writer.
8188 * @param out the print writer to print to
8189 * @throws NullPointerException if {@code out} is {@code null}
8190 * @since JDK1.1
8191 */
8192 public void list(PrintWriter out) {
8193 list(out, 0);
8194 }
8195
8196 /**
8197 * Prints out a list, starting at the specified indentation, to
8198 * the specified print writer.
8199 * @param out the print writer to print to
8200 * @param indent the number of spaces to indent
8201 * @throws NullPointerException if {@code out} is {@code null}
8202 * @see java.io.PrintStream#println(java.lang.Object)
8203 * @since JDK1.1
8204 */
8205 public void list(PrintWriter out, int indent) {
8206 for (int i = 0 ; i < indent ; i++) {
8207 out.print(" ");
8208 }
8209 out.println(this);
8210 }
8211
8212 /*
8213 * Fetches the native container somewhere higher up in the component
8214 * tree that contains this component.
8215 */
8216 final Container getNativeContainer() {
8217 Container p = getContainer();
8218 while (p != null && p.peer instanceof LightweightPeer) {
8219 p = p.getContainer();
8220 }
8221 return p;
8222 }
8223
|
1001
1002 @SuppressWarnings({"rawtypes", "unchecked"})
1003 void initializeFocusTraversalKeys() {
1004 focusTraversalKeys = new Set[3];
1005 }
1006
1007 /**
1008 * Constructs a name for this component. Called by <code>getName</code>
1009 * when the name is <code>null</code>.
1010 */
1011 String constructComponentName() {
1012 return null; // For strict compliance with prior platform versions, a Component
1013 // that doesn't set its name should return null from
1014 // getName()
1015 }
1016
1017 /**
1018 * Gets the name of the component.
1019 * @return this component's name
1020 * @see #setName
1021 * @since 1.1
1022 */
1023 public String getName() {
1024 if (name == null && !nameExplicitlySet) {
1025 synchronized(getObjectLock()) {
1026 if (name == null && !nameExplicitlySet)
1027 name = constructComponentName();
1028 }
1029 }
1030 return name;
1031 }
1032
1033 /**
1034 * Sets the name of the component to the specified string.
1035 * @param name the string that is to be this
1036 * component's name
1037 * @see #getName
1038 * @since 1.1
1039 */
1040 public void setName(String name) {
1041 String oldName;
1042 synchronized(getObjectLock()) {
1043 oldName = this.name;
1044 this.name = name;
1045 nameExplicitlySet = true;
1046 }
1047 firePropertyChange("name", oldName, name);
1048 }
1049
1050 /**
1051 * Gets the parent of this component.
1052 * @return the parent container of this component
1053 * @since 1.0
1054 */
1055 public Container getParent() {
1056 return getParent_NoClientCode();
1057 }
1058
1059 // NOTE: This method may be called by privileged threads.
1060 // This functionality is implemented in a package-private method
1061 // to insure that it cannot be overridden by client subclasses.
1062 // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
1063 final Container getParent_NoClientCode() {
1064 return parent;
1065 }
1066
1067 // This method is overridden in the Window class to return null,
1068 // because the parent field of the Window object contains
1069 // the owner of the window, not its parent.
1070 Container getContainer() {
1071 return getParent_NoClientCode();
1072 }
1073
1204 * synchronization monitor) for AWT component-tree and layout
1205 * operations.
1206 * @return this component's locking object
1207 */
1208 public final Object getTreeLock() {
1209 return LOCK;
1210 }
1211
1212 final void checkTreeLock() {
1213 if (!Thread.holdsLock(getTreeLock())) {
1214 throw new IllegalStateException("This function should be called while holding treeLock");
1215 }
1216 }
1217
1218 /**
1219 * Gets the toolkit of this component. Note that
1220 * the frame that contains a component controls which
1221 * toolkit is used by that component. Therefore if the component
1222 * is moved from one frame to another, the toolkit it uses may change.
1223 * @return the toolkit of this component
1224 * @since 1.0
1225 */
1226 public Toolkit getToolkit() {
1227 return getToolkitImpl();
1228 }
1229
1230 /*
1231 * This is called by the native code, so client code can't
1232 * be called on the toolkit thread.
1233 */
1234 final Toolkit getToolkitImpl() {
1235 Container parent = this.parent;
1236 if (parent != null) {
1237 return parent.getToolkitImpl();
1238 }
1239 return Toolkit.getDefaultToolkit();
1240 }
1241
1242 /**
1243 * Determines whether this component is valid. A component is valid
1244 * when it is correctly sized and positioned within its parent
1245 * container and all its children are also valid.
1246 * In order to account for peers' size requirements, components are invalidated
1247 * before they are first shown on the screen. By the time the parent container
1248 * is fully realized, all its components will be valid.
1249 * @return <code>true</code> if the component is valid, <code>false</code>
1250 * otherwise
1251 * @see #validate
1252 * @see #invalidate
1253 * @since 1.0
1254 */
1255 public boolean isValid() {
1256 return (peer != null) && valid;
1257 }
1258
1259 /**
1260 * Determines whether this component is displayable. A component is
1261 * displayable when it is connected to a native screen resource.
1262 * <p>
1263 * A component is made displayable either when it is added to
1264 * a displayable containment hierarchy or when its containment
1265 * hierarchy is made displayable.
1266 * A containment hierarchy is made displayable when its ancestor
1267 * window is either packed or made visible.
1268 * <p>
1269 * A component is made undisplayable either when it is removed from
1270 * a displayable containment hierarchy or when its containment hierarchy
1271 * is made undisplayable. A containment hierarchy is made
1272 * undisplayable when its ancestor window is disposed.
1273 *
1275 * <code>false</code> otherwise
1276 * @see Container#add(Component)
1277 * @see Window#pack
1278 * @see Window#show
1279 * @see Container#remove(Component)
1280 * @see Window#dispose
1281 * @since 1.2
1282 */
1283 public boolean isDisplayable() {
1284 return getPeer() != null;
1285 }
1286
1287 /**
1288 * Determines whether this component should be visible when its
1289 * parent is visible. Components are
1290 * initially visible, with the exception of top level components such
1291 * as <code>Frame</code> objects.
1292 * @return <code>true</code> if the component is visible,
1293 * <code>false</code> otherwise
1294 * @see #setVisible
1295 * @since 1.0
1296 */
1297 @Transient
1298 public boolean isVisible() {
1299 return isVisible_NoClientCode();
1300 }
1301 final boolean isVisible_NoClientCode() {
1302 return visible;
1303 }
1304
1305 /**
1306 * Determines whether this component will be displayed on the screen.
1307 * @return <code>true</code> if the component and all of its ancestors
1308 * until a toplevel window or null parent are visible,
1309 * <code>false</code> otherwise
1310 */
1311 boolean isRecursivelyVisible() {
1312 return visible && (parent == null || parent.isRecursivelyVisible());
1313 }
1314
1315 /**
1402 boolean isSameOrAncestorOf(Component comp, boolean allowChildren) {
1403 return comp == this;
1404 }
1405
1406 /**
1407 * Determines whether this component is showing on screen. This means
1408 * that the component must be visible, and it must be in a container
1409 * that is visible and showing.
1410 * <p>
1411 * <strong>Note:</strong> sometimes there is no way to detect whether the
1412 * {@code Component} is actually visible to the user. This can happen when:
1413 * <ul>
1414 * <li>the component has been added to a visible {@code ScrollPane} but
1415 * the {@code Component} is not currently in the scroll pane's view port.
1416 * <li>the {@code Component} is obscured by another {@code Component} or
1417 * {@code Container}.
1418 * </ul>
1419 * @return <code>true</code> if the component is showing,
1420 * <code>false</code> otherwise
1421 * @see #setVisible
1422 * @since 1.0
1423 */
1424 public boolean isShowing() {
1425 if (visible && (peer != null)) {
1426 Container parent = this.parent;
1427 return (parent == null) || parent.isShowing();
1428 }
1429 return false;
1430 }
1431
1432 /**
1433 * Determines whether this component is enabled. An enabled component
1434 * can respond to user input and generate events. Components are
1435 * enabled initially by default. A component may be enabled or disabled by
1436 * calling its <code>setEnabled</code> method.
1437 * @return <code>true</code> if the component is enabled,
1438 * <code>false</code> otherwise
1439 * @see #setEnabled
1440 * @since 1.0
1441 */
1442 public boolean isEnabled() {
1443 return isEnabledImpl();
1444 }
1445
1446 /*
1447 * This is called by the native code, so client code can't
1448 * be called on the toolkit thread.
1449 */
1450 final boolean isEnabledImpl() {
1451 return enabled;
1452 }
1453
1454 /**
1455 * Enables or disables this component, depending on the value of the
1456 * parameter <code>b</code>. An enabled component can respond to user
1457 * input and generate events. Components are enabled initially by default.
1458 *
1459 * <p>Note: Disabling a lightweight component does not prevent it from
1460 * receiving MouseEvents.
1461 * <p>Note: Disabling a heavyweight container prevents all components
1462 * in this container from receiving any input events. But disabling a
1463 * lightweight container affects only this container.
1464 *
1465 * @param b If <code>true</code>, this component is
1466 * enabled; otherwise this component is disabled
1467 * @see #isEnabled
1468 * @see #isLightweight
1469 * @since 1.1
1470 */
1471 public void setEnabled(boolean b) {
1472 enable(b);
1473 }
1474
1475 /**
1476 * @deprecated As of JDK version 1.1,
1477 * replaced by <code>setEnabled(boolean)</code>.
1478 */
1479 @Deprecated
1480 public void enable() {
1481 if (!enabled) {
1482 synchronized (getTreeLock()) {
1483 enabled = true;
1484 ComponentPeer peer = this.peer;
1485 if (peer != null) {
1486 peer.setEnabled(true);
1487 if (visible) {
1488 updateCursorImmediately();
1489 }
1594 if (inputContext != null) {
1595 inputContext.endComposition();
1596 inputContext.removeNotify(this);
1597 }
1598 }
1599 eventMask &= ~AWTEvent.INPUT_METHODS_ENABLED_MASK;
1600 }
1601 }
1602
1603 /**
1604 * Shows or hides this component depending on the value of parameter
1605 * <code>b</code>.
1606 * <p>
1607 * This method changes layout-related information, and therefore,
1608 * invalidates the component hierarchy.
1609 *
1610 * @param b if <code>true</code>, shows this component;
1611 * otherwise, hides this component
1612 * @see #isVisible
1613 * @see #invalidate
1614 * @since 1.1
1615 */
1616 public void setVisible(boolean b) {
1617 show(b);
1618 }
1619
1620 /**
1621 * @deprecated As of JDK version 1.1,
1622 * replaced by <code>setVisible(boolean)</code>.
1623 */
1624 @Deprecated
1625 public void show() {
1626 if (!visible) {
1627 synchronized (getTreeLock()) {
1628 visible = true;
1629 mixOnShowing();
1630 ComponentPeer peer = this.peer;
1631 if (peer != null) {
1632 peer.setVisible(true);
1633 createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED,
1634 this, parent,
1722 (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0 ||
1723 Toolkit.enabledOnToolkit(AWTEvent.COMPONENT_EVENT_MASK)) {
1724 ComponentEvent e = new ComponentEvent(this,
1725 ComponentEvent.COMPONENT_HIDDEN);
1726 Toolkit.getEventQueue().postEvent(e);
1727 }
1728 }
1729 Container parent = this.parent;
1730 if (parent != null) {
1731 parent.invalidate();
1732 }
1733 }
1734 }
1735
1736 /**
1737 * Gets the foreground color of this component.
1738 * @return this component's foreground color; if this component does
1739 * not have a foreground color, the foreground color of its parent
1740 * is returned
1741 * @see #setForeground
1742 * @since 1.0
1743 * @beaninfo
1744 * bound: true
1745 */
1746 @Transient
1747 public Color getForeground() {
1748 Color foreground = this.foreground;
1749 if (foreground != null) {
1750 return foreground;
1751 }
1752 Container parent = this.parent;
1753 return (parent != null) ? parent.getForeground() : null;
1754 }
1755
1756 /**
1757 * Sets the foreground color of this component.
1758 * @param c the color to become this component's
1759 * foreground color; if this parameter is <code>null</code>
1760 * then this component will inherit
1761 * the foreground color of its parent
1762 * @see #getForeground
1763 * @since 1.0
1764 */
1765 public void setForeground(Color c) {
1766 Color oldColor = foreground;
1767 ComponentPeer peer = this.peer;
1768 foreground = c;
1769 if (peer != null) {
1770 c = getForeground();
1771 if (c != null) {
1772 peer.setForeground(c);
1773 }
1774 }
1775 // This is a bound property, so report the change to
1776 // any registered listeners. (Cheap if there are none.)
1777 firePropertyChange("foreground", oldColor, c);
1778 }
1779
1780 /**
1781 * Returns whether the foreground color has been explicitly set for this
1782 * Component. If this method returns <code>false</code>, this Component is
1783 * inheriting its foreground color from an ancestor.
1784 *
1785 * @return <code>true</code> if the foreground color has been explicitly
1786 * set for this Component; <code>false</code> otherwise.
1787 * @since 1.4
1788 */
1789 public boolean isForegroundSet() {
1790 return (foreground != null);
1791 }
1792
1793 /**
1794 * Gets the background color of this component.
1795 * @return this component's background color; if this component does
1796 * not have a background color,
1797 * the background color of its parent is returned
1798 * @see #setBackground
1799 * @since 1.0
1800 */
1801 @Transient
1802 public Color getBackground() {
1803 Color background = this.background;
1804 if (background != null) {
1805 return background;
1806 }
1807 Container parent = this.parent;
1808 return (parent != null) ? parent.getBackground() : null;
1809 }
1810
1811 /**
1812 * Sets the background color of this component.
1813 * <p>
1814 * The background color affects each component differently and the
1815 * parts of the component that are affected by the background color
1816 * may differ between operating systems.
1817 *
1818 * @param c the color to become this component's color;
1819 * if this parameter is <code>null</code>, then this
1820 * component will inherit the background color of its parent
1821 * @see #getBackground
1822 * @since 1.0
1823 * @beaninfo
1824 * bound: true
1825 */
1826 public void setBackground(Color c) {
1827 Color oldColor = background;
1828 ComponentPeer peer = this.peer;
1829 background = c;
1830 if (peer != null) {
1831 c = getBackground();
1832 if (c != null) {
1833 peer.setBackground(c);
1834 }
1835 }
1836 // This is a bound property, so report the change to
1837 // any registered listeners. (Cheap if there are none.)
1838 firePropertyChange("background", oldColor, c);
1839 }
1840
1841 /**
1842 * Returns whether the background color has been explicitly set for this
1843 * Component. If this method returns <code>false</code>, this Component is
1844 * inheriting its background color from an ancestor.
1845 *
1846 * @return <code>true</code> if the background color has been explicitly
1847 * set for this Component; <code>false</code> otherwise.
1848 * @since 1.4
1849 */
1850 public boolean isBackgroundSet() {
1851 return (background != null);
1852 }
1853
1854 /**
1855 * Gets the font of this component.
1856 * @return this component's font; if a font has not been set
1857 * for this component, the font of its parent is returned
1858 * @see #setFont
1859 * @since 1.0
1860 */
1861 @Transient
1862 public Font getFont() {
1863 return getFont_NoClientCode();
1864 }
1865
1866 // NOTE: This method may be called by privileged threads.
1867 // This functionality is implemented in a package-private method
1868 // to insure that it cannot be overridden by client subclasses.
1869 // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
1870 final Font getFont_NoClientCode() {
1871 Font font = this.font;
1872 if (font != null) {
1873 return font;
1874 }
1875 Container parent = this.parent;
1876 return (parent != null) ? parent.getFont_NoClientCode() : null;
1877 }
1878
1879 /**
1880 * Sets the font of this component.
1881 * <p>
1882 * This method changes layout-related information, and therefore,
1883 * invalidates the component hierarchy.
1884 *
1885 * @param f the font to become this component's font;
1886 * if this parameter is <code>null</code> then this
1887 * component will inherit the font of its parent
1888 * @see #getFont
1889 * @see #invalidate
1890 * @since 1.0
1891 * @beaninfo
1892 * bound: true
1893 */
1894 public void setFont(Font f) {
1895 Font oldFont, newFont;
1896 synchronized(getTreeLock()) {
1897 oldFont = font;
1898 newFont = font = f;
1899 ComponentPeer peer = this.peer;
1900 if (peer != null) {
1901 f = getFont();
1902 if (f != null) {
1903 peer.setFont(f);
1904 peerFont = f;
1905 }
1906 }
1907 }
1908 // This is a bound property, so report the change to
1909 // any registered listeners. (Cheap if there are none.)
1910 firePropertyChange("font", oldFont, newFont);
1923 * this method returns <code>false</code>, this Component is inheriting its
1924 * font from an ancestor.
1925 *
1926 * @return <code>true</code> if the font has been explicitly set for this
1927 * Component; <code>false</code> otherwise.
1928 * @since 1.4
1929 */
1930 public boolean isFontSet() {
1931 return (font != null);
1932 }
1933
1934 /**
1935 * Gets the locale of this component.
1936 * @return this component's locale; if this component does not
1937 * have a locale, the locale of its parent is returned
1938 * @see #setLocale
1939 * @exception IllegalComponentStateException if the <code>Component</code>
1940 * does not have its own locale and has not yet been added to
1941 * a containment hierarchy such that the locale can be determined
1942 * from the containing parent
1943 * @since 1.1
1944 */
1945 public Locale getLocale() {
1946 Locale locale = this.locale;
1947 if (locale != null) {
1948 return locale;
1949 }
1950 Container parent = this.parent;
1951
1952 if (parent == null) {
1953 throw new IllegalComponentStateException("This component must have a parent in order to determine its locale");
1954 } else {
1955 return parent.getLocale();
1956 }
1957 }
1958
1959 /**
1960 * Sets the locale of this component. This is a bound property.
1961 * <p>
1962 * This method changes layout-related information, and therefore,
1963 * invalidates the component hierarchy.
1964 *
1965 * @param l the locale to become this component's locale
1966 * @see #getLocale
1967 * @see #invalidate
1968 * @since 1.1
1969 */
1970 public void setLocale(Locale l) {
1971 Locale oldValue = locale;
1972 locale = l;
1973
1974 // This is a bound property, so report the change to
1975 // any registered listeners. (Cheap if there are none.)
1976 firePropertyChange("locale", oldValue, l);
1977
1978 // This could change the preferred size of the Component.
1979 invalidateIfValid();
1980 }
1981
1982 /**
1983 * Gets the instance of <code>ColorModel</code> used to display
1984 * the component on the output device.
1985 * @return the color model used by this component
1986 * @see java.awt.image.ColorModel
1987 * @see java.awt.peer.ComponentPeer#getColorModel()
1988 * @see Toolkit#getColorModel()
1989 * @since 1.0
1990 */
1991 public ColorModel getColorModel() {
1992 ComponentPeer peer = this.peer;
1993 if ((peer != null) && ! (peer instanceof LightweightPeer)) {
1994 return peer.getColorModel();
1995 } else if (GraphicsEnvironment.isHeadless()) {
1996 return ColorModel.getRGBdefault();
1997 } // else
1998 return getToolkit().getColorModel();
1999 }
2000
2001 /**
2002 * Gets the location of this component in the form of a
2003 * point specifying the component's top-left corner.
2004 * The location will be relative to the parent's coordinate space.
2005 * <p>
2006 * Due to the asynchronous nature of native event handling, this
2007 * method can return outdated values (for instance, after several calls
2008 * of <code>setLocation()</code> in rapid succession). For this
2009 * reason, the recommended method of obtaining a component's position is
2010 * within <code>java.awt.event.ComponentListener.componentMoved()</code>,
2011 * which is called after the operating system has finished moving the
2012 * component.
2013 * </p>
2014 * @return an instance of <code>Point</code> representing
2015 * the top-left corner of the component's bounds in
2016 * the coordinate space of the component's parent
2017 * @see #setLocation
2018 * @see #getLocationOnScreen
2019 * @since 1.1
2020 */
2021 public Point getLocation() {
2022 return location();
2023 }
2024
2025 /**
2026 * Gets the location of this component in the form of a point
2027 * specifying the component's top-left corner in the screen's
2028 * coordinate space.
2029 * @return an instance of <code>Point</code> representing
2030 * the top-left corner of the component's bounds in the
2031 * coordinate space of the screen
2032 * @throws IllegalComponentStateException if the
2033 * component is not showing on the screen
2034 * @see #setLocation
2035 * @see #getLocation
2036 */
2037 public Point getLocationOnScreen() {
2038 synchronized (getTreeLock()) {
2039 return getLocationOnScreen_NoTreeLock();
2078
2079 private Point location_NoClientCode() {
2080 return new Point(x, y);
2081 }
2082
2083 /**
2084 * Moves this component to a new location. The top-left corner of
2085 * the new location is specified by the <code>x</code> and <code>y</code>
2086 * parameters in the coordinate space of this component's parent.
2087 * <p>
2088 * This method changes layout-related information, and therefore,
2089 * invalidates the component hierarchy.
2090 *
2091 * @param x the <i>x</i>-coordinate of the new location's
2092 * top-left corner in the parent's coordinate space
2093 * @param y the <i>y</i>-coordinate of the new location's
2094 * top-left corner in the parent's coordinate space
2095 * @see #getLocation
2096 * @see #setBounds
2097 * @see #invalidate
2098 * @since 1.1
2099 */
2100 public void setLocation(int x, int y) {
2101 move(x, y);
2102 }
2103
2104 /**
2105 * @deprecated As of JDK version 1.1,
2106 * replaced by <code>setLocation(int, int)</code>.
2107 */
2108 @Deprecated
2109 public void move(int x, int y) {
2110 synchronized(getTreeLock()) {
2111 setBoundsOp(ComponentPeer.SET_LOCATION);
2112 setBounds(x, y, width, height);
2113 }
2114 }
2115
2116 /**
2117 * Moves this component to a new location. The top-left corner of
2118 * the new location is specified by point <code>p</code>. Point
2119 * <code>p</code> is given in the parent's coordinate space.
2120 * <p>
2121 * This method changes layout-related information, and therefore,
2122 * invalidates the component hierarchy.
2123 *
2124 * @param p the point defining the top-left corner
2125 * of the new location, given in the coordinate space of this
2126 * component's parent
2127 * @see #getLocation
2128 * @see #setBounds
2129 * @see #invalidate
2130 * @since 1.1
2131 */
2132 public void setLocation(Point p) {
2133 setLocation(p.x, p.y);
2134 }
2135
2136 /**
2137 * Returns the size of this component in the form of a
2138 * <code>Dimension</code> object. The <code>height</code>
2139 * field of the <code>Dimension</code> object contains
2140 * this component's height, and the <code>width</code>
2141 * field of the <code>Dimension</code> object contains
2142 * this component's width.
2143 * @return a <code>Dimension</code> object that indicates the
2144 * size of this component
2145 * @see #setSize
2146 * @since 1.1
2147 */
2148 public Dimension getSize() {
2149 return size();
2150 }
2151
2152 /**
2153 * @deprecated As of JDK version 1.1,
2154 * replaced by <code>getSize()</code>.
2155 */
2156 @Deprecated
2157 public Dimension size() {
2158 return new Dimension(width, height);
2159 }
2160
2161 /**
2162 * Resizes this component so that it has width <code>width</code>
2163 * and height <code>height</code>.
2164 * <p>
2165 * This method changes layout-related information, and therefore,
2166 * invalidates the component hierarchy.
2167 *
2168 * @param width the new width of this component in pixels
2169 * @param height the new height of this component in pixels
2170 * @see #getSize
2171 * @see #setBounds
2172 * @see #invalidate
2173 * @since 1.1
2174 */
2175 public void setSize(int width, int height) {
2176 resize(width, height);
2177 }
2178
2179 /**
2180 * @deprecated As of JDK version 1.1,
2181 * replaced by <code>setSize(int, int)</code>.
2182 */
2183 @Deprecated
2184 public void resize(int width, int height) {
2185 synchronized(getTreeLock()) {
2186 setBoundsOp(ComponentPeer.SET_SIZE);
2187 setBounds(x, y, width, height);
2188 }
2189 }
2190
2191 /**
2192 * Resizes this component so that it has width <code>d.width</code>
2193 * and height <code>d.height</code>.
2194 * <p>
2195 * This method changes layout-related information, and therefore,
2196 * invalidates the component hierarchy.
2197 *
2198 * @param d the dimension specifying the new size
2199 * of this component
2200 * @throws NullPointerException if {@code d} is {@code null}
2201 * @see #setSize
2202 * @see #setBounds
2203 * @see #invalidate
2204 * @since 1.1
2205 */
2206 public void setSize(Dimension d) {
2207 resize(d);
2208 }
2209
2210 /**
2211 * @deprecated As of JDK version 1.1,
2212 * replaced by <code>setSize(Dimension)</code>.
2213 */
2214 @Deprecated
2215 public void resize(Dimension d) {
2216 setSize(d.width, d.height);
2217 }
2218
2219 /**
2220 * Gets the bounds of this component in the form of a
2221 * <code>Rectangle</code> object. The bounds specify this
2222 * component's width, height, and location relative to
2223 * its parent.
2224 * @return a rectangle indicating this component's bounds
2241
2242 /**
2243 * Moves and resizes this component. The new location of the top-left
2244 * corner is specified by <code>x</code> and <code>y</code>, and the
2245 * new size is specified by <code>width</code> and <code>height</code>.
2246 * <p>
2247 * This method changes layout-related information, and therefore,
2248 * invalidates the component hierarchy.
2249 *
2250 * @param x the new <i>x</i>-coordinate of this component
2251 * @param y the new <i>y</i>-coordinate of this component
2252 * @param width the new <code>width</code> of this component
2253 * @param height the new <code>height</code> of this
2254 * component
2255 * @see #getBounds
2256 * @see #setLocation(int, int)
2257 * @see #setLocation(Point)
2258 * @see #setSize(int, int)
2259 * @see #setSize(Dimension)
2260 * @see #invalidate
2261 * @since 1.1
2262 */
2263 public void setBounds(int x, int y, int width, int height) {
2264 reshape(x, y, width, height);
2265 }
2266
2267 /**
2268 * @deprecated As of JDK version 1.1,
2269 * replaced by <code>setBounds(int, int, int, int)</code>.
2270 */
2271 @Deprecated
2272 public void reshape(int x, int y, int width, int height) {
2273 synchronized (getTreeLock()) {
2274 try {
2275 setBoundsOp(ComponentPeer.SET_BOUNDS);
2276 boolean resized = (this.width != width) || (this.height != height);
2277 boolean moved = (this.x != x) || (this.y != y);
2278 if (!resized && !moved) {
2279 return;
2280 }
2281 int oldX = this.x;
2385 }
2386
2387 /**
2388 * Moves and resizes this component to conform to the new
2389 * bounding rectangle <code>r</code>. This component's new
2390 * position is specified by <code>r.x</code> and <code>r.y</code>,
2391 * and its new size is specified by <code>r.width</code> and
2392 * <code>r.height</code>
2393 * <p>
2394 * This method changes layout-related information, and therefore,
2395 * invalidates the component hierarchy.
2396 *
2397 * @param r the new bounding rectangle for this component
2398 * @throws NullPointerException if {@code r} is {@code null}
2399 * @see #getBounds
2400 * @see #setLocation(int, int)
2401 * @see #setLocation(Point)
2402 * @see #setSize(int, int)
2403 * @see #setSize(Dimension)
2404 * @see #invalidate
2405 * @since 1.1
2406 */
2407 public void setBounds(Rectangle r) {
2408 setBounds(r.x, r.y, r.width, r.height);
2409 }
2410
2411
2412 /**
2413 * Returns the current x coordinate of the components origin.
2414 * This method is preferable to writing
2415 * <code>component.getBounds().x</code>,
2416 * or <code>component.getLocation().x</code> because it doesn't
2417 * cause any heap allocations.
2418 *
2419 * @return the current x coordinate of the components origin
2420 * @since 1.2
2421 */
2422 public int getX() {
2423 return x;
2424 }
2425
2866 }
2867
2868 /**
2869 * @deprecated As of JDK version 1.1,
2870 * replaced by <code>doLayout()</code>.
2871 */
2872 @Deprecated
2873 public void layout() {
2874 }
2875
2876 /**
2877 * Validates this component.
2878 * <p>
2879 * The meaning of the term <i>validating</i> is defined by the ancestors of
2880 * this class. See {@link Container#validate} for more details.
2881 *
2882 * @see #invalidate
2883 * @see #doLayout()
2884 * @see LayoutManager
2885 * @see Container#validate
2886 * @since 1.0
2887 */
2888 public void validate() {
2889 synchronized (getTreeLock()) {
2890 ComponentPeer peer = this.peer;
2891 boolean wasValid = isValid();
2892 if (!wasValid && peer != null) {
2893 Font newfont = getFont();
2894 Font oldfont = peerFont;
2895 if (newfont != oldfont && (oldfont == null
2896 || !oldfont.equals(newfont))) {
2897 peer.setFont(newfont);
2898 peerFont = newfont;
2899 }
2900 peer.layout();
2901 }
2902 valid = true;
2903 if (!wasValid) {
2904 mixOnValidating();
2905 }
2906 }
2909 /**
2910 * Invalidates this component and its ancestors.
2911 * <p>
2912 * By default, all the ancestors of the component up to the top-most
2913 * container of the hierarchy are marked invalid. If the {@code
2914 * java.awt.smartInvalidate} system property is set to {@code true},
2915 * invalidation stops on the nearest validate root of this component.
2916 * Marking a container <i>invalid</i> indicates that the container needs to
2917 * be laid out.
2918 * <p>
2919 * This method is called automatically when any layout-related information
2920 * changes (e.g. setting the bounds of the component, or adding the
2921 * component to a container).
2922 * <p>
2923 * This method might be called often, so it should work fast.
2924 *
2925 * @see #validate
2926 * @see #doLayout
2927 * @see LayoutManager
2928 * @see java.awt.Container#isValidateRoot
2929 * @since 1.0
2930 */
2931 public void invalidate() {
2932 synchronized (getTreeLock()) {
2933 /* Nullify cached layout and size information.
2934 * For efficiency, propagate invalidate() upwards only if
2935 * some other component hasn't already done so first.
2936 */
2937 valid = false;
2938 if (!isPreferredSizeSet()) {
2939 prefSize = null;
2940 }
2941 if (!isMinimumSizeSet()) {
2942 minSize = null;
2943 }
2944 if (!isMaximumSizeSet()) {
2945 maxSize = null;
2946 }
2947 invalidateParent();
2948 }
2949 }
3004 // If there's no validate roots, we'll validate the
3005 // topmost container
3006 break;
3007 }
3008
3009 root = root.getContainer();
3010 }
3011
3012 root.validate();
3013 }
3014 }
3015 }
3016
3017 /**
3018 * Creates a graphics context for this component. This method will
3019 * return <code>null</code> if this component is currently not
3020 * displayable.
3021 * @return a graphics context for this component, or <code>null</code>
3022 * if it has none
3023 * @see #paint
3024 * @since 1.0
3025 */
3026 public Graphics getGraphics() {
3027 if (peer instanceof LightweightPeer) {
3028 // This is for a lightweight component, need to
3029 // translate coordinate spaces and clip relative
3030 // to the parent.
3031 if (parent == null) return null;
3032 Graphics g = parent.getGraphics();
3033 if (g == null) return null;
3034 if (g instanceof ConstrainableGraphics) {
3035 ((ConstrainableGraphics) g).constrain(x, y, width, height);
3036 } else {
3037 g.translate(x,y);
3038 g.setClip(0, 0, width, height);
3039 }
3040 g.setFont(getFont());
3041 return g;
3042 } else {
3043 ComponentPeer peer = this.peer;
3044 return (peer != null) ? peer.getGraphics() : null;
3068 }
3069 }
3070
3071 /**
3072 * Gets the font metrics for the specified font.
3073 * Warning: Since Font metrics are affected by the
3074 * {@link java.awt.font.FontRenderContext FontRenderContext} and
3075 * this method does not provide one, it can return only metrics for
3076 * the default render context which may not match that used when
3077 * rendering on the Component if {@link Graphics2D} functionality is being
3078 * used. Instead metrics can be obtained at rendering time by calling
3079 * {@link Graphics#getFontMetrics()} or text measurement APIs on the
3080 * {@link Font Font} class.
3081 * @param font the font for which font metrics is to be
3082 * obtained
3083 * @return the font metrics for <code>font</code>
3084 * @see #getFont
3085 * @see #getPeer
3086 * @see java.awt.peer.ComponentPeer#getFontMetrics(Font)
3087 * @see Toolkit#getFontMetrics(Font)
3088 * @since 1.0
3089 */
3090 public FontMetrics getFontMetrics(Font font) {
3091 // This is an unsupported hack, but left in for a customer.
3092 // Do not remove.
3093 FontManager fm = FontManagerFactory.getInstance();
3094 if (fm instanceof SunFontManager
3095 && ((SunFontManager) fm).usePlatformFontMetrics()) {
3096
3097 if (peer != null &&
3098 !(peer instanceof LightweightPeer)) {
3099 return peer.getFontMetrics(font);
3100 }
3101 }
3102 return sun.font.FontDesignMetrics.getMetrics(font);
3103 }
3104
3105 /**
3106 * Sets the cursor image to the specified cursor. This cursor
3107 * image is displayed when the <code>contains</code> method for
3108 * this component returns true for the current cursor location, and
3109 * this Component is visible, displayable, and enabled. Setting the
3110 * cursor of a <code>Container</code> causes that cursor to be displayed
3111 * within all of the container's subcomponents, except for those
3112 * that have a non-<code>null</code> cursor.
3113 * <p>
3114 * The method may have no visual effect if the Java platform
3115 * implementation and/or the native system do not support
3116 * changing the mouse cursor shape.
3117 * @param cursor One of the constants defined
3118 * by the <code>Cursor</code> class;
3119 * if this parameter is <code>null</code>
3120 * then this component will inherit
3121 * the cursor of its parent
3122 * @see #isEnabled
3123 * @see #isShowing
3124 * @see #getCursor
3125 * @see #contains
3126 * @see Toolkit#createCustomCursor
3127 * @see Cursor
3128 * @since 1.1
3129 */
3130 public void setCursor(Cursor cursor) {
3131 this.cursor = cursor;
3132 updateCursorImmediately();
3133 }
3134
3135 /**
3136 * Updates the cursor. May not be invoked from the native
3137 * message pump.
3138 */
3139 final void updateCursorImmediately() {
3140 if (peer instanceof LightweightPeer) {
3141 Container nativeContainer = getNativeContainer();
3142
3143 if (nativeContainer == null) return;
3144
3145 ComponentPeer cPeer = nativeContainer.getPeer();
3146
3147 if (cPeer != null) {
3148 cPeer.updateCursorImmediately();
3149 }
3150 } else if (peer != null) {
3151 peer.updateCursorImmediately();
3152 }
3153 }
3154
3155 /**
3156 * Gets the cursor set in the component. If the component does
3157 * not have a cursor set, the cursor of its parent is returned.
3158 * If no cursor is set in the entire hierarchy,
3159 * <code>Cursor.DEFAULT_CURSOR</code> is returned.
3160 * @see #setCursor
3161 * @since 1.1
3162 */
3163 public Cursor getCursor() {
3164 return getCursor_NoClientCode();
3165 }
3166
3167 final Cursor getCursor_NoClientCode() {
3168 Cursor cursor = this.cursor;
3169 if (cursor != null) {
3170 return cursor;
3171 }
3172 Container parent = this.parent;
3173 if (parent != null) {
3174 return parent.getCursor_NoClientCode();
3175 } else {
3176 return Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
3177 }
3178 }
3179
3180 /**
3181 * Returns whether the cursor has been explicitly set for this Component.
3195 * <p>
3196 * This method is called when the contents of the component should
3197 * be painted; such as when the component is first being shown or
3198 * is damaged and in need of repair. The clip rectangle in the
3199 * <code>Graphics</code> parameter is set to the area
3200 * which needs to be painted.
3201 * Subclasses of <code>Component</code> that override this
3202 * method need not call <code>super.paint(g)</code>.
3203 * <p>
3204 * For performance reasons, <code>Component</code>s with zero width
3205 * or height aren't considered to need painting when they are first shown,
3206 * and also aren't considered to need repair.
3207 * <p>
3208 * <b>Note</b>: For more information on the paint mechanisms utilitized
3209 * by AWT and Swing, including information on how to write the most
3210 * efficient painting code, see
3211 * <a href="http://www.oracle.com/technetwork/java/painting-140037.html">Painting in AWT and Swing</a>.
3212 *
3213 * @param g the graphics context to use for painting
3214 * @see #update
3215 * @since 1.0
3216 */
3217 public void paint(Graphics g) {
3218 }
3219
3220 /**
3221 * Updates this component.
3222 * <p>
3223 * If this component is not a lightweight component, the
3224 * AWT calls the <code>update</code> method in response to
3225 * a call to <code>repaint</code>. You can assume that
3226 * the background is not cleared.
3227 * <p>
3228 * The <code>update</code> method of <code>Component</code>
3229 * calls this component's <code>paint</code> method to redraw
3230 * this component. This method is commonly overridden by subclasses
3231 * which need to do additional work in response to a call to
3232 * <code>repaint</code>.
3233 * Subclasses of Component that override this method should either
3234 * call <code>super.update(g)</code>, or call <code>paint(g)</code>
3235 * directly from their <code>update</code> method.
3236 * <p>
3237 * The origin of the graphics context, its
3238 * (<code>0</code>, <code>0</code>) coordinate point, is the
3239 * top-left corner of this component. The clipping region of the
3240 * graphics context is the bounding rectangle of this component.
3241 *
3242 * <p>
3243 * <b>Note</b>: For more information on the paint mechanisms utilitized
3244 * by AWT and Swing, including information on how to write the most
3245 * efficient painting code, see
3246 * <a href="http://www.oracle.com/technetwork/java/painting-140037.html">Painting in AWT and Swing</a>.
3247 *
3248 * @param g the specified context to use for updating
3249 * @see #paint
3250 * @see #repaint()
3251 * @since 1.0
3252 */
3253 public void update(Graphics g) {
3254 paint(g);
3255 }
3256
3257 /**
3258 * Paints this component and all of its subcomponents.
3259 * <p>
3260 * The origin of the graphics context, its
3261 * (<code>0</code>, <code>0</code>) coordinate point, is the
3262 * top-left corner of this component. The clipping region of the
3263 * graphics context is the bounding rectangle of this component.
3264 *
3265 * @param g the graphics context to use for painting
3266 * @see #paint
3267 * @since 1.0
3268 */
3269 public void paintAll(Graphics g) {
3270 if (isShowing()) {
3271 GraphicsCallback.PeerPaintCallback.getInstance().
3272 runOneComponent(this, new Rectangle(0, 0, width, height),
3273 g, g.getClip(),
3274 GraphicsCallback.LIGHTWEIGHTS |
3275 GraphicsCallback.HEAVYWEIGHTS);
3276 }
3277 }
3278
3279 /**
3280 * Simulates the peer callbacks into java.awt for painting of
3281 * lightweight Components.
3282 * @param g the graphics context to use for painting
3283 * @see #paintAll
3284 */
3285 void lightweightPaint(Graphics g) {
3286 paint(g);
3287 }
3291 */
3292 void paintHeavyweightComponents(Graphics g) {
3293 }
3294
3295 /**
3296 * Repaints this component.
3297 * <p>
3298 * If this component is a lightweight component, this method
3299 * causes a call to this component's <code>paint</code>
3300 * method as soon as possible. Otherwise, this method causes
3301 * a call to this component's <code>update</code> method as soon
3302 * as possible.
3303 * <p>
3304 * <b>Note</b>: For more information on the paint mechanisms utilitized
3305 * by AWT and Swing, including information on how to write the most
3306 * efficient painting code, see
3307 * <a href="http://www.oracle.com/technetwork/java/painting-140037.html">Painting in AWT and Swing</a>.
3308
3309 *
3310 * @see #update(Graphics)
3311 * @since 1.0
3312 */
3313 public void repaint() {
3314 repaint(0, 0, 0, width, height);
3315 }
3316
3317 /**
3318 * Repaints the component. If this component is a lightweight
3319 * component, this results in a call to <code>paint</code>
3320 * within <code>tm</code> milliseconds.
3321 * <p>
3322 * <b>Note</b>: For more information on the paint mechanisms utilitized
3323 * by AWT and Swing, including information on how to write the most
3324 * efficient painting code, see
3325 * <a href="http://www.oracle.com/technetwork/java/painting-140037.html">Painting in AWT and Swing</a>.
3326 *
3327 * @param tm maximum time in milliseconds before update
3328 * @see #paint
3329 * @see #update(Graphics)
3330 * @since 1.0
3331 */
3332 public void repaint(long tm) {
3333 repaint(tm, 0, 0, width, height);
3334 }
3335
3336 /**
3337 * Repaints the specified rectangle of this component.
3338 * <p>
3339 * If this component is a lightweight component, this method
3340 * causes a call to this component's <code>paint</code> method
3341 * as soon as possible. Otherwise, this method causes a call to
3342 * this component's <code>update</code> method as soon as possible.
3343 * <p>
3344 * <b>Note</b>: For more information on the paint mechanisms utilitized
3345 * by AWT and Swing, including information on how to write the most
3346 * efficient painting code, see
3347 * <a href="http://www.oracle.com/technetwork/java/painting-140037.html">Painting in AWT and Swing</a>.
3348 *
3349 * @param x the <i>x</i> coordinate
3350 * @param y the <i>y</i> coordinate
3351 * @param width the width
3352 * @param height the height
3353 * @see #update(Graphics)
3354 * @since 1.0
3355 */
3356 public void repaint(int x, int y, int width, int height) {
3357 repaint(0, x, y, width, height);
3358 }
3359
3360 /**
3361 * Repaints the specified rectangle of this component within
3362 * <code>tm</code> milliseconds.
3363 * <p>
3364 * If this component is a lightweight component, this method causes
3365 * a call to this component's <code>paint</code> method.
3366 * Otherwise, this method causes a call to this component's
3367 * <code>update</code> method.
3368 * <p>
3369 * <b>Note</b>: For more information on the paint mechanisms utilitized
3370 * by AWT and Swing, including information on how to write the most
3371 * efficient painting code, see
3372 * <a href="http://www.oracle.com/technetwork/java/painting-140037.html">Painting in AWT and Swing</a>.
3373 *
3374 * @param tm maximum time in milliseconds before update
3375 * @param x the <i>x</i> coordinate
3376 * @param y the <i>y</i> coordinate
3377 * @param width the width
3378 * @param height the height
3379 * @see #update(Graphics)
3380 * @since 1.0
3381 */
3382 public void repaint(long tm, int x, int y, int width, int height) {
3383 if (this.peer instanceof LightweightPeer) {
3384 // Needs to be translated to parent coordinates since
3385 // a parent native container provides the actual repaint
3386 // services. Additionally, the request is restricted to
3387 // the bounds of the component.
3388 if (parent != null) {
3389 if (x < 0) {
3390 width += x;
3391 x = 0;
3392 }
3393 if (y < 0) {
3394 height += y;
3395 y = 0;
3396 }
3397
3398 int pwidth = (width > this.width) ? this.width : width;
3399 int pheight = (height > this.height) ? this.height : height;
3400
3413 new Rectangle(x, y, width, height));
3414 Toolkit.getEventQueue().postEvent(e);
3415 }
3416 }
3417 }
3418
3419 /**
3420 * Prints this component. Applications should override this method
3421 * for components that must do special processing before being
3422 * printed or should be printed differently than they are painted.
3423 * <p>
3424 * The default implementation of this method calls the
3425 * <code>paint</code> method.
3426 * <p>
3427 * The origin of the graphics context, its
3428 * (<code>0</code>, <code>0</code>) coordinate point, is the
3429 * top-left corner of this component. The clipping region of the
3430 * graphics context is the bounding rectangle of this component.
3431 * @param g the graphics context to use for printing
3432 * @see #paint(Graphics)
3433 * @since 1.0
3434 */
3435 public void print(Graphics g) {
3436 paint(g);
3437 }
3438
3439 /**
3440 * Prints this component and all of its subcomponents.
3441 * <p>
3442 * The origin of the graphics context, its
3443 * (<code>0</code>, <code>0</code>) coordinate point, is the
3444 * top-left corner of this component. The clipping region of the
3445 * graphics context is the bounding rectangle of this component.
3446 * @param g the graphics context to use for printing
3447 * @see #print(Graphics)
3448 * @since 1.0
3449 */
3450 public void printAll(Graphics g) {
3451 if (isShowing()) {
3452 GraphicsCallback.PeerPrintCallback.getInstance().
3453 runOneComponent(this, new Rectangle(0, 0, width, height),
3454 g, g.getClip(),
3455 GraphicsCallback.LIGHTWEIGHTS |
3456 GraphicsCallback.HEAVYWEIGHTS);
3457 }
3458 }
3459
3460 /**
3461 * Simulates the peer callbacks into java.awt for printing of
3462 * lightweight Components.
3463 * @param g the graphics context to use for printing
3464 * @see #printAll
3465 */
3466 void lightweightPrint(Graphics g) {
3467 print(g);
3468 }
3508 * <p>
3509 * The interpretation of the <code>x</code>, <code>y</code>,
3510 * <code>width</code>, and <code>height</code> arguments depends on
3511 * the value of the <code>infoflags</code> argument.
3512 *
3513 * @param img the image being observed
3514 * @param infoflags see <code>imageUpdate</code> for more information
3515 * @param x the <i>x</i> coordinate
3516 * @param y the <i>y</i> coordinate
3517 * @param w the width
3518 * @param h the height
3519 * @return <code>false</code> if the infoflags indicate that the
3520 * image is completely loaded; <code>true</code> otherwise.
3521 *
3522 * @see java.awt.image.ImageObserver
3523 * @see Graphics#drawImage(Image, int, int, Color, java.awt.image.ImageObserver)
3524 * @see Graphics#drawImage(Image, int, int, java.awt.image.ImageObserver)
3525 * @see Graphics#drawImage(Image, int, int, int, int, Color, java.awt.image.ImageObserver)
3526 * @see Graphics#drawImage(Image, int, int, int, int, java.awt.image.ImageObserver)
3527 * @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)
3528 * @since 1.0
3529 */
3530 public boolean imageUpdate(Image img, int infoflags,
3531 int x, int y, int w, int h) {
3532 int rate = -1;
3533 if ((infoflags & (FRAMEBITS|ALLBITS)) != 0) {
3534 rate = 0;
3535 } else if ((infoflags & SOMEBITS) != 0) {
3536 if (isInc) {
3537 rate = incRate;
3538 if (rate < 0) {
3539 rate = 0;
3540 }
3541 }
3542 }
3543 if (rate >= 0) {
3544 repaint(rate, 0, 0, width, height);
3545 }
3546 return (infoflags & (ALLBITS|ABORT)) == 0;
3547 }
3548
3549 /**
3550 * Creates an image from the specified image producer.
3551 * @param producer the image producer
3552 * @return the image produced
3553 * @since 1.0
3554 */
3555 public Image createImage(ImageProducer producer) {
3556 ComponentPeer peer = this.peer;
3557 if ((peer != null) && ! (peer instanceof LightweightPeer)) {
3558 return peer.createImage(producer);
3559 }
3560 return getToolkit().createImage(producer);
3561 }
3562
3563 /**
3564 * Creates an off-screen drawable image
3565 * to be used for double buffering.
3566 * @param width the specified width
3567 * @param height the specified height
3568 * @return an off-screen drawable image, which can be used for double
3569 * buffering. The return value may be <code>null</code> if the
3570 * component is not displayable. This will always happen if
3571 * <code>GraphicsEnvironment.isHeadless()</code> returns
3572 * <code>true</code>.
3573 * @see #isDisplayable
3574 * @see GraphicsEnvironment#isHeadless
3575 * @since 1.0
3576 */
3577 public Image createImage(int width, int height) {
3578 ComponentPeer peer = this.peer;
3579 if (peer instanceof LightweightPeer) {
3580 if (parent != null) { return parent.createImage(width, height); }
3581 else { return null;}
3582 } else {
3583 return (peer != null) ? peer.createImage(width, height) : null;
3584 }
3585 }
3586
3587 /**
3588 * Creates a volatile off-screen drawable image
3589 * to be used for double buffering.
3590 * @param width the specified width.
3591 * @param height the specified height.
3592 * @return an off-screen drawable image, which can be used for double
3593 * buffering. The return value may be <code>null</code> if the
3594 * component is not displayable. This will always happen if
3595 * <code>GraphicsEnvironment.isHeadless()</code> returns
3626 * to manage surface contents loss and capabilities.
3627 * @see java.awt.image.VolatileImage
3628 * @since 1.4
3629 */
3630 public VolatileImage createVolatileImage(int width, int height,
3631 ImageCapabilities caps) throws AWTException {
3632 // REMIND : check caps
3633 return createVolatileImage(width, height);
3634 }
3635
3636 /**
3637 * Prepares an image for rendering on this component. The image
3638 * data is downloaded asynchronously in another thread and the
3639 * appropriate screen representation of the image is generated.
3640 * @param image the <code>Image</code> for which to
3641 * prepare a screen representation
3642 * @param observer the <code>ImageObserver</code> object
3643 * to be notified as the image is being prepared
3644 * @return <code>true</code> if the image has already been fully
3645 * prepared; <code>false</code> otherwise
3646 * @since 1.0
3647 */
3648 public boolean prepareImage(Image image, ImageObserver observer) {
3649 return prepareImage(image, -1, -1, observer);
3650 }
3651
3652 /**
3653 * Prepares an image for rendering on this component at the
3654 * specified width and height.
3655 * <p>
3656 * The image data is downloaded asynchronously in another thread,
3657 * and an appropriately scaled screen representation of the image is
3658 * generated.
3659 * @param image the instance of <code>Image</code>
3660 * for which to prepare a screen representation
3661 * @param width the width of the desired screen representation
3662 * @param height the height of the desired screen representation
3663 * @param observer the <code>ImageObserver</code> object
3664 * to be notified as the image is being prepared
3665 * @return <code>true</code> if the image has already been fully
3666 * prepared; <code>false</code> otherwise
3667 * @see java.awt.image.ImageObserver
3668 * @since 1.0
3669 */
3670 public boolean prepareImage(Image image, int width, int height,
3671 ImageObserver observer) {
3672 ComponentPeer peer = this.peer;
3673 if (peer instanceof LightweightPeer) {
3674 return (parent != null)
3675 ? parent.prepareImage(image, width, height, observer)
3676 : getToolkit().prepareImage(image, width, height, observer);
3677 } else {
3678 return (peer != null)
3679 ? peer.prepareImage(image, width, height, observer)
3680 : getToolkit().prepareImage(image, width, height, observer);
3681 }
3682 }
3683
3684 /**
3685 * Returns the status of the construction of a screen representation
3686 * of the specified image.
3687 * <p>
3688 * This method does not cause the image to begin loading. An
3689 * application must use the <code>prepareImage</code> method
3690 * to force the loading of an image.
3691 * <p>
3692 * Information on the flags returned by this method can be found
3693 * with the discussion of the <code>ImageObserver</code> interface.
3694 * @param image the <code>Image</code> object whose status
3695 * is being checked
3696 * @param observer the <code>ImageObserver</code>
3697 * object to be notified as the image is being prepared
3698 * @return the bitwise inclusive <b>OR</b> of
3699 * <code>ImageObserver</code> flags indicating what
3700 * information about the image is currently available
3701 * @see #prepareImage(Image, int, int, java.awt.image.ImageObserver)
3702 * @see Toolkit#checkImage(Image, int, int, java.awt.image.ImageObserver)
3703 * @see java.awt.image.ImageObserver
3704 * @since 1.0
3705 */
3706 public int checkImage(Image image, ImageObserver observer) {
3707 return checkImage(image, -1, -1, observer);
3708 }
3709
3710 /**
3711 * Returns the status of the construction of a screen representation
3712 * of the specified image.
3713 * <p>
3714 * This method does not cause the image to begin loading. An
3715 * application must use the <code>prepareImage</code> method
3716 * to force the loading of an image.
3717 * <p>
3718 * The <code>checkImage</code> method of <code>Component</code>
3719 * calls its peer's <code>checkImage</code> method to calculate
3720 * the flags. If this component does not yet have a peer, the
3721 * component's toolkit's <code>checkImage</code> method is called
3722 * instead.
3723 * <p>
3724 * Information on the flags returned by this method can be found
3725 * with the discussion of the <code>ImageObserver</code> interface.
3726 * @param image the <code>Image</code> object whose status
3727 * is being checked
3728 * @param width the width of the scaled version
3729 * whose status is to be checked
3730 * @param height the height of the scaled version
3731 * whose status is to be checked
3732 * @param observer the <code>ImageObserver</code> object
3733 * to be notified as the image is being prepared
3734 * @return the bitwise inclusive <b>OR</b> of
3735 * <code>ImageObserver</code> flags indicating what
3736 * information about the image is currently available
3737 * @see #prepareImage(Image, int, int, java.awt.image.ImageObserver)
3738 * @see Toolkit#checkImage(Image, int, int, java.awt.image.ImageObserver)
3739 * @see java.awt.image.ImageObserver
3740 * @since 1.0
3741 */
3742 public int checkImage(Image image, int width, int height,
3743 ImageObserver observer) {
3744 ComponentPeer peer = this.peer;
3745 if (peer instanceof LightweightPeer) {
3746 return (parent != null)
3747 ? parent.checkImage(image, width, height, observer)
3748 : getToolkit().checkImage(image, width, height, observer);
3749 } else {
3750 return (peer != null)
3751 ? peer.checkImage(image, width, height, observer)
3752 : getToolkit().checkImage(image, width, height, observer);
3753 }
3754 }
3755
3756 /**
3757 * Creates a new strategy for multi-buffering on this component.
3758 * Multi-buffering is useful for rendering performance. This method
3759 * attempts to create the best strategy available with the number of
3760 * buffers supplied. It will always create a <code>BufferStrategy</code>
4605 }
4606
4607 /**
4608 * @return whether or not paint messages received from the operating system
4609 * should be ignored.
4610 *
4611 * @since 1.4
4612 * @see #setIgnoreRepaint
4613 */
4614 public boolean getIgnoreRepaint() {
4615 return ignoreRepaint;
4616 }
4617
4618 /**
4619 * Checks whether this component "contains" the specified point,
4620 * where <code>x</code> and <code>y</code> are defined to be
4621 * relative to the coordinate system of this component.
4622 * @param x the <i>x</i> coordinate of the point
4623 * @param y the <i>y</i> coordinate of the point
4624 * @see #getComponentAt(int, int)
4625 * @since 1.1
4626 */
4627 public boolean contains(int x, int y) {
4628 return inside(x, y);
4629 }
4630
4631 /**
4632 * @deprecated As of JDK version 1.1,
4633 * replaced by contains(int, int).
4634 */
4635 @Deprecated
4636 public boolean inside(int x, int y) {
4637 return (x >= 0) && (x < width) && (y >= 0) && (y < height);
4638 }
4639
4640 /**
4641 * Checks whether this component "contains" the specified point,
4642 * where the point's <i>x</i> and <i>y</i> coordinates are defined
4643 * to be relative to the coordinate system of this component.
4644 * @param p the point
4645 * @throws NullPointerException if {@code p} is {@code null}
4646 * @see #getComponentAt(Point)
4647 * @since 1.1
4648 */
4649 public boolean contains(Point p) {
4650 return contains(p.x, p.y);
4651 }
4652
4653 /**
4654 * Determines if this component or one of its immediate
4655 * subcomponents contains the (<i>x</i>, <i>y</i>) location,
4656 * and if so, returns the containing component. This method only
4657 * looks one level deep. If the point (<i>x</i>, <i>y</i>) is
4658 * inside a subcomponent that itself has subcomponents, it does not
4659 * go looking down the subcomponent tree.
4660 * <p>
4661 * The <code>locate</code> method of <code>Component</code> simply
4662 * returns the component itself if the (<i>x</i>, <i>y</i>)
4663 * coordinate location is inside its bounding box, and <code>null</code>
4664 * otherwise.
4665 * @param x the <i>x</i> coordinate
4666 * @param y the <i>y</i> coordinate
4667 * @return the component or subcomponent that contains the
4668 * (<i>x</i>, <i>y</i>) location;
4669 * <code>null</code> if the location
4670 * is outside this component
4671 * @see #contains(int, int)
4672 * @since 1.0
4673 */
4674 public Component getComponentAt(int x, int y) {
4675 return locate(x, y);
4676 }
4677
4678 /**
4679 * @deprecated As of JDK version 1.1,
4680 * replaced by getComponentAt(int, int).
4681 */
4682 @Deprecated
4683 public Component locate(int x, int y) {
4684 return contains(x, y) ? this : null;
4685 }
4686
4687 /**
4688 * Returns the component or subcomponent that contains the
4689 * specified point.
4690 * @param p the point
4691 * @see java.awt.Component#contains
4692 * @since 1.1
4693 */
4694 public Component getComponentAt(Point p) {
4695 return getComponentAt(p.x, p.y);
4696 }
4697
4698 /**
4699 * @deprecated As of JDK version 1.1,
4700 * replaced by <code>dispatchEvent(AWTEvent e)</code>.
4701 */
4702 @Deprecated
4703 public void deliverEvent(Event e) {
4704 postEvent(e);
4705 }
4706
4707 /**
4708 * Dispatches an event to this component or one of its sub components.
4709 * Calls <code>processEvent</code> before returning for 1.1-style
4710 * events which have been enabled for the <code>Component</code>.
4711 * @param e the event
4712 */
5207 e.y = eventy;
5208 }
5209 return false;
5210 }
5211
5212 // Event source interfaces
5213
5214 /**
5215 * Adds the specified component listener to receive component events from
5216 * this component.
5217 * If listener <code>l</code> is <code>null</code>,
5218 * no exception is thrown and no action is performed.
5219 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
5220 * >AWT Threading Issues</a> for details on AWT's threading model.
5221 *
5222 * @param l the component listener
5223 * @see java.awt.event.ComponentEvent
5224 * @see java.awt.event.ComponentListener
5225 * @see #removeComponentListener
5226 * @see #getComponentListeners
5227 * @since 1.1
5228 */
5229 public synchronized void addComponentListener(ComponentListener l) {
5230 if (l == null) {
5231 return;
5232 }
5233 componentListener = AWTEventMulticaster.add(componentListener, l);
5234 newEventsOnly = true;
5235 }
5236
5237 /**
5238 * Removes the specified component listener so that it no longer
5239 * receives component events from this component. This method performs
5240 * no function, nor does it throw an exception, if the listener
5241 * specified by the argument was not previously added to this component.
5242 * If listener <code>l</code> is <code>null</code>,
5243 * no exception is thrown and no action is performed.
5244 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
5245 * >AWT Threading Issues</a> for details on AWT's threading model.
5246 * @param l the component listener
5247 * @see java.awt.event.ComponentEvent
5248 * @see java.awt.event.ComponentListener
5249 * @see #addComponentListener
5250 * @see #getComponentListeners
5251 * @since 1.1
5252 */
5253 public synchronized void removeComponentListener(ComponentListener l) {
5254 if (l == null) {
5255 return;
5256 }
5257 componentListener = AWTEventMulticaster.remove(componentListener, l);
5258 }
5259
5260 /**
5261 * Returns an array of all the component listeners
5262 * registered on this component.
5263 *
5264 * @return all <code>ComponentListener</code>s of this component
5265 * or an empty array if no component
5266 * listeners are currently registered
5267 *
5268 * @see #addComponentListener
5269 * @see #removeComponentListener
5270 * @since 1.4
5271 */
5272 public synchronized ComponentListener[] getComponentListeners() {
5273 return getListeners(ComponentListener.class);
5274 }
5275
5276 /**
5277 * Adds the specified focus listener to receive focus events from
5278 * this component when this component gains input focus.
5279 * If listener <code>l</code> is <code>null</code>,
5280 * no exception is thrown and no action is performed.
5281 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
5282 * >AWT Threading Issues</a> for details on AWT's threading model.
5283 *
5284 * @param l the focus listener
5285 * @see java.awt.event.FocusEvent
5286 * @see java.awt.event.FocusListener
5287 * @see #removeFocusListener
5288 * @see #getFocusListeners
5289 * @since 1.1
5290 */
5291 public synchronized void addFocusListener(FocusListener l) {
5292 if (l == null) {
5293 return;
5294 }
5295 focusListener = AWTEventMulticaster.add(focusListener, l);
5296 newEventsOnly = true;
5297
5298 // if this is a lightweight component, enable focus events
5299 // in the native container.
5300 if (peer instanceof LightweightPeer) {
5301 parent.proxyEnableEvents(AWTEvent.FOCUS_EVENT_MASK);
5302 }
5303 }
5304
5305 /**
5306 * Removes the specified focus listener so that it no longer
5307 * receives focus events from this component. This method performs
5308 * no function, nor does it throw an exception, if the listener
5309 * specified by the argument was not previously added to this component.
5310 * If listener <code>l</code> is <code>null</code>,
5311 * no exception is thrown and no action is performed.
5312 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
5313 * >AWT Threading Issues</a> for details on AWT's threading model.
5314 *
5315 * @param l the focus listener
5316 * @see java.awt.event.FocusEvent
5317 * @see java.awt.event.FocusListener
5318 * @see #addFocusListener
5319 * @see #getFocusListeners
5320 * @since 1.1
5321 */
5322 public synchronized void removeFocusListener(FocusListener l) {
5323 if (l == null) {
5324 return;
5325 }
5326 focusListener = AWTEventMulticaster.remove(focusListener, l);
5327 }
5328
5329 /**
5330 * Returns an array of all the focus listeners
5331 * registered on this component.
5332 *
5333 * @return all of this component's <code>FocusListener</code>s
5334 * or an empty array if no component
5335 * listeners are currently registered
5336 *
5337 * @see #addFocusListener
5338 * @see #removeFocusListener
5339 * @since 1.4
5340 */
5601 * because parent in Window is owner.
5602 */
5603 void adjustListeningChildrenOnParent(long mask, int num) {
5604 if (parent != null) {
5605 parent.adjustListeningChildren(mask, num);
5606 }
5607 }
5608
5609 /**
5610 * Adds the specified key listener to receive key events from
5611 * this component.
5612 * If l is null, no exception is thrown and no action is performed.
5613 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
5614 * >AWT Threading Issues</a> for details on AWT's threading model.
5615 *
5616 * @param l the key listener.
5617 * @see java.awt.event.KeyEvent
5618 * @see java.awt.event.KeyListener
5619 * @see #removeKeyListener
5620 * @see #getKeyListeners
5621 * @since 1.1
5622 */
5623 public synchronized void addKeyListener(KeyListener l) {
5624 if (l == null) {
5625 return;
5626 }
5627 keyListener = AWTEventMulticaster.add(keyListener, l);
5628 newEventsOnly = true;
5629
5630 // if this is a lightweight component, enable key events
5631 // in the native container.
5632 if (peer instanceof LightweightPeer) {
5633 parent.proxyEnableEvents(AWTEvent.KEY_EVENT_MASK);
5634 }
5635 }
5636
5637 /**
5638 * Removes the specified key listener so that it no longer
5639 * receives key events from this component. This method performs
5640 * no function, nor does it throw an exception, if the listener
5641 * specified by the argument was not previously added to this component.
5642 * If listener <code>l</code> is <code>null</code>,
5643 * no exception is thrown and no action is performed.
5644 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
5645 * >AWT Threading Issues</a> for details on AWT's threading model.
5646 *
5647 * @param l the key listener
5648 * @see java.awt.event.KeyEvent
5649 * @see java.awt.event.KeyListener
5650 * @see #addKeyListener
5651 * @see #getKeyListeners
5652 * @since 1.1
5653 */
5654 public synchronized void removeKeyListener(KeyListener l) {
5655 if (l == null) {
5656 return;
5657 }
5658 keyListener = AWTEventMulticaster.remove(keyListener, l);
5659 }
5660
5661 /**
5662 * Returns an array of all the key listeners
5663 * registered on this component.
5664 *
5665 * @return all of this component's <code>KeyListener</code>s
5666 * or an empty array if no key
5667 * listeners are currently registered
5668 *
5669 * @see #addKeyListener
5670 * @see #removeKeyListener
5671 * @since 1.4
5672 */
5673 public synchronized KeyListener[] getKeyListeners() {
5674 return getListeners(KeyListener.class);
5675 }
5676
5677 /**
5678 * Adds the specified mouse listener to receive mouse events from
5679 * this component.
5680 * If listener <code>l</code> is <code>null</code>,
5681 * no exception is thrown and no action is performed.
5682 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
5683 * >AWT Threading Issues</a> for details on AWT's threading model.
5684 *
5685 * @param l the mouse listener
5686 * @see java.awt.event.MouseEvent
5687 * @see java.awt.event.MouseListener
5688 * @see #removeMouseListener
5689 * @see #getMouseListeners
5690 * @since 1.1
5691 */
5692 public synchronized void addMouseListener(MouseListener l) {
5693 if (l == null) {
5694 return;
5695 }
5696 mouseListener = AWTEventMulticaster.add(mouseListener,l);
5697 newEventsOnly = true;
5698
5699 // if this is a lightweight component, enable mouse events
5700 // in the native container.
5701 if (peer instanceof LightweightPeer) {
5702 parent.proxyEnableEvents(AWTEvent.MOUSE_EVENT_MASK);
5703 }
5704 }
5705
5706 /**
5707 * Removes the specified mouse listener so that it no longer
5708 * receives mouse events from this component. This method performs
5709 * no function, nor does it throw an exception, if the listener
5710 * specified by the argument was not previously added to this component.
5711 * If listener <code>l</code> is <code>null</code>,
5712 * no exception is thrown and no action is performed.
5713 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
5714 * >AWT Threading Issues</a> for details on AWT's threading model.
5715 *
5716 * @param l the mouse listener
5717 * @see java.awt.event.MouseEvent
5718 * @see java.awt.event.MouseListener
5719 * @see #addMouseListener
5720 * @see #getMouseListeners
5721 * @since 1.1
5722 */
5723 public synchronized void removeMouseListener(MouseListener l) {
5724 if (l == null) {
5725 return;
5726 }
5727 mouseListener = AWTEventMulticaster.remove(mouseListener, l);
5728 }
5729
5730 /**
5731 * Returns an array of all the mouse listeners
5732 * registered on this component.
5733 *
5734 * @return all of this component's <code>MouseListener</code>s
5735 * or an empty array if no mouse
5736 * listeners are currently registered
5737 *
5738 * @see #addMouseListener
5739 * @see #removeMouseListener
5740 * @since 1.4
5741 */
5742 public synchronized MouseListener[] getMouseListeners() {
5743 return getListeners(MouseListener.class);
5744 }
5745
5746 /**
5747 * Adds the specified mouse motion listener to receive mouse motion
5748 * events from this component.
5749 * If listener <code>l</code> is <code>null</code>,
5750 * no exception is thrown and no action is performed.
5751 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
5752 * >AWT Threading Issues</a> for details on AWT's threading model.
5753 *
5754 * @param l the mouse motion listener
5755 * @see java.awt.event.MouseEvent
5756 * @see java.awt.event.MouseMotionListener
5757 * @see #removeMouseMotionListener
5758 * @see #getMouseMotionListeners
5759 * @since 1.1
5760 */
5761 public synchronized void addMouseMotionListener(MouseMotionListener l) {
5762 if (l == null) {
5763 return;
5764 }
5765 mouseMotionListener = AWTEventMulticaster.add(mouseMotionListener,l);
5766 newEventsOnly = true;
5767
5768 // if this is a lightweight component, enable mouse events
5769 // in the native container.
5770 if (peer instanceof LightweightPeer) {
5771 parent.proxyEnableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
5772 }
5773 }
5774
5775 /**
5776 * Removes the specified mouse motion listener so that it no longer
5777 * receives mouse motion events from this component. This method performs
5778 * no function, nor does it throw an exception, if the listener
5779 * specified by the argument was not previously added to this component.
5780 * If listener <code>l</code> is <code>null</code>,
5781 * no exception is thrown and no action is performed.
5782 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
5783 * >AWT Threading Issues</a> for details on AWT's threading model.
5784 *
5785 * @param l the mouse motion listener
5786 * @see java.awt.event.MouseEvent
5787 * @see java.awt.event.MouseMotionListener
5788 * @see #addMouseMotionListener
5789 * @see #getMouseMotionListeners
5790 * @since 1.1
5791 */
5792 public synchronized void removeMouseMotionListener(MouseMotionListener l) {
5793 if (l == null) {
5794 return;
5795 }
5796 mouseMotionListener = AWTEventMulticaster.remove(mouseMotionListener, l);
5797 }
5798
5799 /**
5800 * Returns an array of all the mouse motion listeners
5801 * registered on this component.
5802 *
5803 * @return all of this component's <code>MouseMotionListener</code>s
5804 * or an empty array if no mouse motion
5805 * listeners are currently registered
5806 *
5807 * @see #addMouseMotionListener
5808 * @see #removeMouseMotionListener
5809 * @since 1.4
5810 */
6055 } else {
6056 return parent.getInputContext();
6057 }
6058 }
6059
6060 /**
6061 * Enables the events defined by the specified event mask parameter
6062 * to be delivered to this component.
6063 * <p>
6064 * Event types are automatically enabled when a listener for
6065 * that event type is added to the component.
6066 * <p>
6067 * This method only needs to be invoked by subclasses of
6068 * <code>Component</code> which desire to have the specified event
6069 * types delivered to <code>processEvent</code> regardless of whether
6070 * or not a listener is registered.
6071 * @param eventsToEnable the event mask defining the event types
6072 * @see #processEvent
6073 * @see #disableEvents
6074 * @see AWTEvent
6075 * @since 1.1
6076 */
6077 protected final void enableEvents(long eventsToEnable) {
6078 long notifyAncestors = 0;
6079 synchronized (this) {
6080 if ((eventsToEnable & AWTEvent.HIERARCHY_EVENT_MASK) != 0 &&
6081 hierarchyListener == null &&
6082 (eventMask & AWTEvent.HIERARCHY_EVENT_MASK) == 0) {
6083 notifyAncestors |= AWTEvent.HIERARCHY_EVENT_MASK;
6084 }
6085 if ((eventsToEnable & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) != 0 &&
6086 hierarchyBoundsListener == null &&
6087 (eventMask & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) == 0) {
6088 notifyAncestors |= AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK;
6089 }
6090 eventMask |= eventsToEnable;
6091 newEventsOnly = true;
6092 }
6093
6094 // if this is a lightweight component, enable mouse events
6095 // in the native container.
6096 if (peer instanceof LightweightPeer) {
6097 parent.proxyEnableEvents(eventMask);
6098 }
6099 if (notifyAncestors != 0) {
6100 synchronized (getTreeLock()) {
6101 adjustListeningChildrenOnParent(notifyAncestors, 1);
6102 }
6103 }
6104 }
6105
6106 /**
6107 * Disables the events defined by the specified event mask parameter
6108 * from being delivered to this component.
6109 * @param eventsToDisable the event mask defining the event types
6110 * @see #enableEvents
6111 * @since 1.1
6112 */
6113 protected final void disableEvents(long eventsToDisable) {
6114 long notifyAncestors = 0;
6115 synchronized (this) {
6116 if ((eventsToDisable & AWTEvent.HIERARCHY_EVENT_MASK) != 0 &&
6117 hierarchyListener == null &&
6118 (eventMask & AWTEvent.HIERARCHY_EVENT_MASK) != 0) {
6119 notifyAncestors |= AWTEvent.HIERARCHY_EVENT_MASK;
6120 }
6121 if ((eventsToDisable & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK)!=0 &&
6122 hierarchyBoundsListener == null &&
6123 (eventMask & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) != 0) {
6124 notifyAncestors |= AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK;
6125 }
6126 eventMask &= ~eventsToDisable;
6127 }
6128 if (notifyAncestors != 0) {
6129 synchronized (getTreeLock()) {
6130 adjustListeningChildrenOnParent(notifyAncestors, -1);
6131 }
6268 }
6269
6270 /**
6271 * Processes events occurring on this component. By default this
6272 * method calls the appropriate
6273 * <code>process<event type>Event</code>
6274 * method for the given class of event.
6275 * <p>Note that if the event parameter is <code>null</code>
6276 * the behavior is unspecified and may result in an
6277 * exception.
6278 *
6279 * @param e the event
6280 * @see #processComponentEvent
6281 * @see #processFocusEvent
6282 * @see #processKeyEvent
6283 * @see #processMouseEvent
6284 * @see #processMouseMotionEvent
6285 * @see #processInputMethodEvent
6286 * @see #processHierarchyEvent
6287 * @see #processMouseWheelEvent
6288 * @since 1.1
6289 */
6290 protected void processEvent(AWTEvent e) {
6291 if (e instanceof FocusEvent) {
6292 processFocusEvent((FocusEvent)e);
6293
6294 } else if (e instanceof MouseEvent) {
6295 switch(e.getID()) {
6296 case MouseEvent.MOUSE_PRESSED:
6297 case MouseEvent.MOUSE_RELEASED:
6298 case MouseEvent.MOUSE_CLICKED:
6299 case MouseEvent.MOUSE_ENTERED:
6300 case MouseEvent.MOUSE_EXITED:
6301 processMouseEvent((MouseEvent)e);
6302 break;
6303 case MouseEvent.MOUSE_MOVED:
6304 case MouseEvent.MOUSE_DRAGGED:
6305 processMouseMotionEvent((MouseEvent)e);
6306 break;
6307 case MouseEvent.MOUSE_WHEEL:
6308 processMouseWheelEvent((MouseWheelEvent)e);
6334 * dispatching them to any registered
6335 * <code>ComponentListener</code> objects.
6336 * <p>
6337 * This method is not called unless component events are
6338 * enabled for this component. Component events are enabled
6339 * when one of the following occurs:
6340 * <ul>
6341 * <li>A <code>ComponentListener</code> object is registered
6342 * via <code>addComponentListener</code>.
6343 * <li>Component events are enabled via <code>enableEvents</code>.
6344 * </ul>
6345 * <p>Note that if the event parameter is <code>null</code>
6346 * the behavior is unspecified and may result in an
6347 * exception.
6348 *
6349 * @param e the component event
6350 * @see java.awt.event.ComponentEvent
6351 * @see java.awt.event.ComponentListener
6352 * @see #addComponentListener
6353 * @see #enableEvents
6354 * @since 1.1
6355 */
6356 protected void processComponentEvent(ComponentEvent e) {
6357 ComponentListener listener = componentListener;
6358 if (listener != null) {
6359 int id = e.getID();
6360 switch(id) {
6361 case ComponentEvent.COMPONENT_RESIZED:
6362 listener.componentResized(e);
6363 break;
6364 case ComponentEvent.COMPONENT_MOVED:
6365 listener.componentMoved(e);
6366 break;
6367 case ComponentEvent.COMPONENT_SHOWN:
6368 listener.componentShown(e);
6369 break;
6370 case ComponentEvent.COMPONENT_HIDDEN:
6371 listener.componentHidden(e);
6372 break;
6373 }
6374 }
6397 * method, which results in a call to the <code>Component</code>'s
6398 * <code>processFocusEvent</code> method.
6399 * <p>
6400 * If focus events are enabled for a <code>Component</code>, calling
6401 * the <code>Component</code>'s <code>dispatchEvent</code> method
6402 * with a <code>FocusEvent</code> as the argument will result in a
6403 * call to the <code>Component</code>'s <code>processFocusEvent</code>
6404 * method regardless of the current <code>KeyboardFocusManager</code>.
6405 *
6406 * <p>Note that if the event parameter is <code>null</code>
6407 * the behavior is unspecified and may result in an
6408 * exception.
6409 *
6410 * @param e the focus event
6411 * @see java.awt.event.FocusEvent
6412 * @see java.awt.event.FocusListener
6413 * @see java.awt.KeyboardFocusManager
6414 * @see #addFocusListener
6415 * @see #enableEvents
6416 * @see #dispatchEvent
6417 * @since 1.1
6418 */
6419 protected void processFocusEvent(FocusEvent e) {
6420 FocusListener listener = focusListener;
6421 if (listener != null) {
6422 int id = e.getID();
6423 switch(id) {
6424 case FocusEvent.FOCUS_GAINED:
6425 listener.focusGained(e);
6426 break;
6427 case FocusEvent.FOCUS_LOST:
6428 listener.focusLost(e);
6429 break;
6430 }
6431 }
6432 }
6433
6434 /**
6435 * Processes key events occurring on this component by
6436 * dispatching them to any registered
6437 * <code>KeyListener</code> objects.
6463 * method with a <code>KeyEvent</code> as the argument will
6464 * result in a call to the <code>Component</code>'s
6465 * <code>processKeyEvent</code> method regardless of the
6466 * current <code>KeyboardFocusManager</code> as long as the
6467 * component is showing, focused, and enabled, and key events
6468 * are enabled on it.
6469 * <p>If the event parameter is <code>null</code>
6470 * the behavior is unspecified and may result in an
6471 * exception.
6472 *
6473 * @param e the key event
6474 * @see java.awt.event.KeyEvent
6475 * @see java.awt.event.KeyListener
6476 * @see java.awt.KeyboardFocusManager
6477 * @see java.awt.DefaultKeyboardFocusManager
6478 * @see #processEvent
6479 * @see #dispatchEvent
6480 * @see #addKeyListener
6481 * @see #enableEvents
6482 * @see #isShowing
6483 * @since 1.1
6484 */
6485 protected void processKeyEvent(KeyEvent e) {
6486 KeyListener listener = keyListener;
6487 if (listener != null) {
6488 int id = e.getID();
6489 switch(id) {
6490 case KeyEvent.KEY_TYPED:
6491 listener.keyTyped(e);
6492 break;
6493 case KeyEvent.KEY_PRESSED:
6494 listener.keyPressed(e);
6495 break;
6496 case KeyEvent.KEY_RELEASED:
6497 listener.keyReleased(e);
6498 break;
6499 }
6500 }
6501 }
6502
6503 /**
6505 * dispatching them to any registered
6506 * <code>MouseListener</code> objects.
6507 * <p>
6508 * This method is not called unless mouse events are
6509 * enabled for this component. Mouse events are enabled
6510 * when one of the following occurs:
6511 * <ul>
6512 * <li>A <code>MouseListener</code> object is registered
6513 * via <code>addMouseListener</code>.
6514 * <li>Mouse events are enabled via <code>enableEvents</code>.
6515 * </ul>
6516 * <p>Note that if the event parameter is <code>null</code>
6517 * the behavior is unspecified and may result in an
6518 * exception.
6519 *
6520 * @param e the mouse event
6521 * @see java.awt.event.MouseEvent
6522 * @see java.awt.event.MouseListener
6523 * @see #addMouseListener
6524 * @see #enableEvents
6525 * @since 1.1
6526 */
6527 protected void processMouseEvent(MouseEvent e) {
6528 MouseListener listener = mouseListener;
6529 if (listener != null) {
6530 int id = e.getID();
6531 switch(id) {
6532 case MouseEvent.MOUSE_PRESSED:
6533 listener.mousePressed(e);
6534 break;
6535 case MouseEvent.MOUSE_RELEASED:
6536 listener.mouseReleased(e);
6537 break;
6538 case MouseEvent.MOUSE_CLICKED:
6539 listener.mouseClicked(e);
6540 break;
6541 case MouseEvent.MOUSE_EXITED:
6542 listener.mouseExited(e);
6543 break;
6544 case MouseEvent.MOUSE_ENTERED:
6545 listener.mouseEntered(e);
6553 * dispatching them to any registered
6554 * <code>MouseMotionListener</code> objects.
6555 * <p>
6556 * This method is not called unless mouse motion events are
6557 * enabled for this component. Mouse motion events are enabled
6558 * when one of the following occurs:
6559 * <ul>
6560 * <li>A <code>MouseMotionListener</code> object is registered
6561 * via <code>addMouseMotionListener</code>.
6562 * <li>Mouse motion events are enabled via <code>enableEvents</code>.
6563 * </ul>
6564 * <p>Note that if the event parameter is <code>null</code>
6565 * the behavior is unspecified and may result in an
6566 * exception.
6567 *
6568 * @param e the mouse motion event
6569 * @see java.awt.event.MouseEvent
6570 * @see java.awt.event.MouseMotionListener
6571 * @see #addMouseMotionListener
6572 * @see #enableEvents
6573 * @since 1.1
6574 */
6575 protected void processMouseMotionEvent(MouseEvent e) {
6576 MouseMotionListener listener = mouseMotionListener;
6577 if (listener != null) {
6578 int id = e.getID();
6579 switch(id) {
6580 case MouseEvent.MOUSE_MOVED:
6581 listener.mouseMoved(e);
6582 break;
6583 case MouseEvent.MOUSE_DRAGGED:
6584 listener.mouseDragged(e);
6585 break;
6586 }
6587 }
6588 }
6589
6590 /**
6591 * Processes mouse wheel events occurring on this component by
6592 * dispatching them to any registered
6593 * <code>MouseWheelListener</code> objects.
6865 * should register this component as ActionListener on component
6866 * which fires action events.
6867 */
6868 @Deprecated
6869 public boolean action(Event evt, Object what) {
6870 return false;
6871 }
6872
6873 /**
6874 * Makes this <code>Component</code> displayable by connecting it to a
6875 * native screen resource.
6876 * This method is called internally by the toolkit and should
6877 * not be called directly by programs.
6878 * <p>
6879 * This method changes layout-related information, and therefore,
6880 * invalidates the component hierarchy.
6881 *
6882 * @see #isDisplayable
6883 * @see #removeNotify
6884 * @see #invalidate
6885 * @since 1.0
6886 */
6887 public void addNotify() {
6888 synchronized (getTreeLock()) {
6889 ComponentPeer peer = this.peer;
6890 if (peer == null || peer instanceof LightweightPeer){
6891 if (peer == null) {
6892 // Update both the Component's peer variable and the local
6893 // variable we use for thread safety.
6894 this.peer = peer = getToolkit().createComponent(this);
6895 }
6896
6897 // This is a lightweight component which means it won't be
6898 // able to get window-related events by itself. If any
6899 // have been enabled, then the nearest native container must
6900 // be enabled.
6901 if (parent != null) {
6902 long mask = 0;
6903 if ((mouseListener != null) || ((eventMask & AWTEvent.MOUSE_EVENT_MASK) != 0)) {
6904 mask |= AWTEvent.MOUSE_EVENT_MASK;
6905 }
6968 HierarchyEvent.DISPLAYABILITY_CHANGED |
6969 ((isRecursivelyVisible())
6970 ? HierarchyEvent.SHOWING_CHANGED
6971 : 0));
6972 dispatchEvent(e);
6973 }
6974 }
6975 }
6976
6977 /**
6978 * Makes this <code>Component</code> undisplayable by destroying it native
6979 * screen resource.
6980 * <p>
6981 * This method is called by the toolkit internally and should
6982 * not be called directly by programs. Code overriding
6983 * this method should call <code>super.removeNotify</code> as
6984 * the first line of the overriding method.
6985 *
6986 * @see #isDisplayable
6987 * @see #addNotify
6988 * @since 1.0
6989 */
6990 public void removeNotify() {
6991 KeyboardFocusManager.clearMostRecentFocusOwner(this);
6992 if (KeyboardFocusManager.getCurrentKeyboardFocusManager().
6993 getPermanentFocusOwner() == this)
6994 {
6995 KeyboardFocusManager.getCurrentKeyboardFocusManager().
6996 setGlobalPermanentFocusOwner(null);
6997 }
6998
6999 synchronized (getTreeLock()) {
7000 clearLightweightDispatcherOnRemove(this);
7001
7002 if (isFocusOwner() && KeyboardFocusManager.isAutoFocusTransferEnabledFor(this)) {
7003 transferFocus(true);
7004 }
7005
7006 if (getContainer() != null && isAddNotifyComplete) {
7007 getContainer().decreaseComponentCount(this);
7008 }
7077 public boolean gotFocus(Event evt, Object what) {
7078 return false;
7079 }
7080
7081 /**
7082 * @deprecated As of JDK version 1.1,
7083 * replaced by processFocusEvent(FocusEvent).
7084 */
7085 @Deprecated
7086 public boolean lostFocus(Event evt, Object what) {
7087 return false;
7088 }
7089
7090 /**
7091 * Returns whether this <code>Component</code> can become the focus
7092 * owner.
7093 *
7094 * @return <code>true</code> if this <code>Component</code> is
7095 * focusable; <code>false</code> otherwise
7096 * @see #setFocusable
7097 * @since 1.1
7098 * @deprecated As of 1.4, replaced by <code>isFocusable()</code>.
7099 */
7100 @Deprecated
7101 public boolean isFocusTraversable() {
7102 if (isFocusTraversableOverridden == FOCUS_TRAVERSABLE_UNKNOWN) {
7103 isFocusTraversableOverridden = FOCUS_TRAVERSABLE_DEFAULT;
7104 }
7105 return focusable;
7106 }
7107
7108 /**
7109 * Returns whether this Component can be focused.
7110 *
7111 * @return <code>true</code> if this Component is focusable;
7112 * <code>false</code> otherwise.
7113 * @see #setFocusable
7114 * @since 1.4
7115 */
7116 public boolean isFocusable() {
7117 return isFocusTraversable();
7416 * Window is later focused by the user.
7417 * <p>
7418 * This method cannot be used to set the focus owner to no Component at
7419 * all. Use <code>KeyboardFocusManager.clearGlobalFocusOwner()</code>
7420 * instead.
7421 * <p>
7422 * Because the focus behavior of this method is platform-dependent,
7423 * developers are strongly encouraged to use
7424 * <code>requestFocusInWindow</code> when possible.
7425 *
7426 * <p>Note: Not all focus transfers result from invoking this method. As
7427 * such, a component may receive focus without this or any of the other
7428 * {@code requestFocus} methods of {@code Component} being invoked.
7429 *
7430 * @see #requestFocusInWindow
7431 * @see java.awt.event.FocusEvent
7432 * @see #addFocusListener
7433 * @see #isFocusable
7434 * @see #isDisplayable
7435 * @see KeyboardFocusManager#clearGlobalFocusOwner
7436 * @since 1.0
7437 */
7438 public void requestFocus() {
7439 requestFocusHelper(false, true);
7440 }
7441
7442 boolean requestFocus(CausedFocusEvent.Cause cause) {
7443 return requestFocusHelper(false, true, cause);
7444 }
7445
7446 /**
7447 * Requests that this <code>Component</code> get the input focus,
7448 * and that this <code>Component</code>'s top-level ancestor
7449 * become the focused <code>Window</code>. This component must be
7450 * displayable, focusable, visible and all of its ancestors (with
7451 * the exception of the top-level Window) must be visible for the
7452 * request to be granted. Every effort will be made to honor the
7453 * request; however, in some cases it may be impossible to do
7454 * so. Developers must never assume that this component is the
7455 * focus owner until this component receives a FOCUS_GAINED
7456 * event. If this request is denied because this component's
7845 *
7846 * @param container the Container to be tested
7847 * @return <code>true</code> if the specified Container is a focus-cycle-
7848 * root of this Component; <code>false</code> otherwise
7849 * @see Container#isFocusCycleRoot()
7850 * @since 1.4
7851 */
7852 public boolean isFocusCycleRoot(Container container) {
7853 Container rootAncestor = getFocusCycleRootAncestor();
7854 return (rootAncestor == container);
7855 }
7856
7857 Container getTraversalRoot() {
7858 return getFocusCycleRootAncestor();
7859 }
7860
7861 /**
7862 * Transfers the focus to the next component, as though this Component were
7863 * the focus owner.
7864 * @see #requestFocus()
7865 * @since 1.1
7866 */
7867 public void transferFocus() {
7868 nextFocus();
7869 }
7870
7871 /**
7872 * @deprecated As of JDK version 1.1,
7873 * replaced by transferFocus().
7874 */
7875 @Deprecated
7876 public void nextFocus() {
7877 transferFocus(false);
7878 }
7879
7880 boolean transferFocus(boolean clearOnFailure) {
7881 if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
7882 focusLog.finer("clearOnFailure = " + clearOnFailure);
7883 }
7884 Component toFocus = getNextFocusCandidate();
7885 boolean res = false;
8053
8054 /*
8055 * Used to disallow auto-focus-transfer on disposal of the focus owner
8056 * in the process of disposing its parent container.
8057 */
8058 private boolean autoFocusTransferOnDisposal = true;
8059
8060 void setAutoFocusTransferOnDisposal(boolean value) {
8061 autoFocusTransferOnDisposal = value;
8062 }
8063
8064 boolean isAutoFocusTransferOnDisposal() {
8065 return autoFocusTransferOnDisposal;
8066 }
8067
8068 /**
8069 * Adds the specified popup menu to the component.
8070 * @param popup the popup menu to be added to the component.
8071 * @see #remove(MenuComponent)
8072 * @exception NullPointerException if {@code popup} is {@code null}
8073 * @since 1.1
8074 */
8075 public void add(PopupMenu popup) {
8076 synchronized (getTreeLock()) {
8077 if (popup.parent != null) {
8078 popup.parent.remove(popup);
8079 }
8080 if (popups == null) {
8081 popups = new Vector<PopupMenu>();
8082 }
8083 popups.addElement(popup);
8084 popup.parent = this;
8085
8086 if (peer != null) {
8087 if (popup.peer == null) {
8088 popup.addNotify();
8089 }
8090 }
8091 }
8092 }
8093
8094 /**
8095 * Removes the specified popup menu from the component.
8096 * @param popup the popup menu to be removed
8097 * @see #add(PopupMenu)
8098 * @since 1.1
8099 */
8100 @SuppressWarnings("unchecked")
8101 public void remove(MenuComponent popup) {
8102 synchronized (getTreeLock()) {
8103 if (popups == null) {
8104 return;
8105 }
8106 int index = popups.indexOf(popup);
8107 if (index >= 0) {
8108 PopupMenu pmenu = (PopupMenu)popup;
8109 if (pmenu.peer != null) {
8110 pmenu.removeNotify();
8111 }
8112 pmenu.parent = null;
8113 popups.removeElementAt(index);
8114 if (popups.size() == 0) {
8115 popups = null;
8116 }
8117 }
8118 }
8119 }
8120
8121 /**
8122 * Returns a string representing the state of this component. This
8123 * method is intended to be used only for debugging purposes, and the
8124 * content and format of the returned string may vary between
8125 * implementations. The returned string may be empty but may not be
8126 * <code>null</code>.
8127 *
8128 * @return a string representation of this component's state
8129 * @since 1.0
8130 */
8131 protected String paramString() {
8132 final String thisName = Objects.toString(getName(), "");
8133 final String invalid = isValid() ? "" : ",invalid";
8134 final String hidden = visible ? "" : ",hidden";
8135 final String disabled = enabled ? "" : ",disabled";
8136 return thisName + ',' + x + ',' + y + ',' + width + 'x' + height
8137 + invalid + hidden + disabled;
8138 }
8139
8140 /**
8141 * Returns a string representation of this component and its values.
8142 * @return a string representation of this component
8143 * @since 1.0
8144 */
8145 public String toString() {
8146 return getClass().getName() + '[' + paramString() + ']';
8147 }
8148
8149 /**
8150 * Prints a listing of this component to the standard system output
8151 * stream <code>System.out</code>.
8152 * @see java.lang.System#out
8153 * @since 1.0
8154 */
8155 public void list() {
8156 list(System.out, 0);
8157 }
8158
8159 /**
8160 * Prints a listing of this component to the specified output
8161 * stream.
8162 * @param out a print stream
8163 * @throws NullPointerException if {@code out} is {@code null}
8164 * @since 1.0
8165 */
8166 public void list(PrintStream out) {
8167 list(out, 0);
8168 }
8169
8170 /**
8171 * Prints out a list, starting at the specified indentation, to the
8172 * specified print stream.
8173 * @param out a print stream
8174 * @param indent number of spaces to indent
8175 * @see java.io.PrintStream#println(java.lang.Object)
8176 * @throws NullPointerException if {@code out} is {@code null}
8177 * @since 1.0
8178 */
8179 public void list(PrintStream out, int indent) {
8180 for (int i = 0 ; i < indent ; i++) {
8181 out.print(" ");
8182 }
8183 out.println(this);
8184 }
8185
8186 /**
8187 * Prints a listing to the specified print writer.
8188 * @param out the print writer to print to
8189 * @throws NullPointerException if {@code out} is {@code null}
8190 * @since 1.1
8191 */
8192 public void list(PrintWriter out) {
8193 list(out, 0);
8194 }
8195
8196 /**
8197 * Prints out a list, starting at the specified indentation, to
8198 * the specified print writer.
8199 * @param out the print writer to print to
8200 * @param indent the number of spaces to indent
8201 * @throws NullPointerException if {@code out} is {@code null}
8202 * @see java.io.PrintStream#println(java.lang.Object)
8203 * @since 1.1
8204 */
8205 public void list(PrintWriter out, int indent) {
8206 for (int i = 0 ; i < indent ; i++) {
8207 out.print(" ");
8208 }
8209 out.println(this);
8210 }
8211
8212 /*
8213 * Fetches the native container somewhere higher up in the component
8214 * tree that contains this component.
8215 */
8216 final Container getNativeContainer() {
8217 Container p = getContainer();
8218 while (p != null && p.peer instanceof LightweightPeer) {
8219 p = p.getContainer();
8220 }
8221 return p;
8222 }
8223
|