Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/awt/SecurityWarning.java
38831 views
/*1* Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.awt;2627import java.awt.*;28import java.awt.geom.*;2930import sun.awt.AWTAccessor;313233/**34* Security Warning control interface.35*36* This class provides a couple of methods that help a developer relocate37* the AWT security warning to an appropriate position relative to the current38* window size. A "top-level window" is an instance of the {@code Window}39* class (or its descendant, such as {@code JFrame}). The security warning40* is applied to all windows created by an untrusted code. All such windows41* have a non-null "warning string" (see {@link Window#getWarningString()}).42* <p>43* <b>WARNING</b>: This class is an implementation detail and only meant44* for limited use outside of the core platform. This API may change45* drastically between update release, and it may even be46* removed or be moved to some other packages or classes.47*/48public final class SecurityWarning {4950/**51* The SecurityWarning class should not be instantiated52*/53private SecurityWarning() {54}5556/**57* Gets the size of the security warning.58*59* The returned value is not valid until the peer has been created. Before60* invoking this method a developer must call the {@link Window#pack()},61* {@link Window#setVisible()}, or some other method that creates the peer.62*63* @param window the window to get the security warning size for64*65* @throws NullPointerException if the window argument is null66* @throws IllegalArgumentException if the window is trusted (i.e.67* the {@code getWarningString()} returns null)68*/69public static Dimension getSize(Window window) {70if (window == null) {71throw new NullPointerException(72"The window argument should not be null.");73}74if (window.getWarningString() == null) {75throw new IllegalArgumentException(76"The window must have a non-null warning string.");77}78// We don't check for a non-null peer since it may be destroyed79// after assigning a valid value to the security warning size.8081return AWTAccessor.getWindowAccessor().getSecurityWarningSize(window);82}8384/**85* Sets the position of the security warning.86* <p>87* The {@code alignmentX} and {@code alignmentY} arguments specify the88* origin of the coordinate system used to calculate the position of the89* security warning. The values must be in the range [0.0f...1.0f]. The90* {@code 0.0f} value represents the left (top) edge of the rectangular91* bounds of the window. The {@code 1.0f} value represents the right92* (bottom) edge of the bounds. Whenever the size of the window changes,93* the origin of the coordinate system gets relocated accordingly. For94* convenience a developer may use the {@code Component.*_ALIGNMENT}95* constants to pass predefined values for these arguments.96* <p>97* The {@code point} argument specifies the location of the security98* warning in the coordinate system described above. If both {@code x} and99* {@code y} coordinates of the point are equal to zero, the warning will100* be located right in the origin of the coordinate system. On the other101* hand, if both {@code alignmentX} and {@code alignmentY} are equal to102* zero (i.e. the origin of the coordinate system is placed at the top-left103* corner of the window), then the {@code point} argument represents the104* absolute location of the security warning relative to the location of105* the window. The "absolute" in this case means that the position of the106* security warning is not effected by resizing of the window.107* <p>108* Note that the security warning managment code guarantees that:109* <ul>110* <li>The security warning cannot be located farther than two pixels from111* the rectangular bounds of the window (see {@link Window#getBounds}), and112* <li>The security warning is always visible on the screen.113* </ul>114* If either of the conditions is violated, the calculated position of the115* security warning is adjusted by the system to meet both these116* conditions.117* <p>118* The default position of the security warning is in the upper-right119* corner of the window, two pixels to the right from the right edge. This120* corresponds to the following arguments passed to this method:121* <ul>122* <li>{@code alignmentX = Component.RIGHT_ALIGNMENT}123* <li>{@code alignmentY = Component.TOP_ALIGNMENT}124* <li>{@code point = (2, 0)}125* </ul>126*127* @param window the window to set the position of the security warning for128* @param alignmentX the horizontal origin of the coordinate system129* @param alignmentY the vertical origin of the coordinate system130* @param point the position of the security warning in the specified131* coordinate system132*133* @throws NullPointerException if the window argument is null134* @throws NullPointerException if the point argument is null135* @throws IllegalArgumentException if the window is trusted (i.e.136* the {@code getWarningString()} returns null137* @throws IllegalArgumentException if the alignmentX or alignmentY138* arguments are not within the range [0.0f ... 1.0f]139*/140public static void setPosition(Window window, Point2D point,141float alignmentX, float alignmentY)142{143if (window == null) {144throw new NullPointerException(145"The window argument should not be null.");146}147if (window.getWarningString() == null) {148throw new IllegalArgumentException(149"The window must have a non-null warning string.");150}151if (point == null) {152throw new NullPointerException(153"The point argument must not be null");154}155if (alignmentX < 0.0f || alignmentX > 1.0f) {156throw new IllegalArgumentException(157"alignmentX must be in the range [0.0f ... 1.0f].");158}159if (alignmentY < 0.0f || alignmentY > 1.0f) {160throw new IllegalArgumentException(161"alignmentY must be in the range [0.0f ... 1.0f].");162}163164AWTAccessor.getWindowAccessor().setSecurityWarningPosition(window,165point, alignmentX, alignmentY);166}167}168169170171