Spec-Zone .ru
спецификации, руководства, описания, API
|
001/* 002 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. Oracle designates this 008 * particular file as subject to the "Classpath" exception as provided 009 * by Oracle in the LICENSE file that accompanied this code. 010 * 011 * This code is distributed in the hope that it will be useful, but WITHOUT 012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 014 * version 2 for more details (a copy is included in the LICENSE file that 015 * accompanied this code). 016 * 017 * You should have received a copy of the GNU General Public License version 018 * 2 along with this work; if not, write to the Free Software Foundation, 019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 020 * 021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 022 * or visit www.oracle.com if you need additional information or have any 023 * questions. 024 */ 025 026package javafx.scene.layout; 027 028import javafx.geometry.Side; 029 030/** 031 * Represents the position of a {@link BackgroundImage} within the 032 * {@link Region}'s drawing area. 033 * <p/> 034 * The BackgroundImage can be positioned either from the left or right side 035 * along the horizontal axis, and from either the top or bottom side along 036 * the vertical axis. The {@link #getHorizontalSide() horizontalSide} and 037 * {@link #getVerticalSide() verticalSide} properties define to which side the 038 * remaining properties pertain. The {@link #getHorizontalPosition() horizontalPosition} 039 * specifies the distance of the BackgroundImage from the corresponding side of the Region, 040 * and {@link #isHorizontalAsPercentage() horizontalAsPercentage} indicates whether 041 * this is as a literal value or a percentage. Similar properties exist for 042 * specifying the size relative to the vertical axis. 043 * <p/> 044 * For example, suppose I had a BackgroundPosition with a {@code horizontalSide} 045 * of {@code Side.RIGHT}, a {@code horizontalPosition} of .05, and a 046 * {@code horizontalAsPercentage} of {@code true}. In this case, the right 047 * side of the BackgroundImage will be 5% of the width of the Region from 048 * the Region's right edge. 049 */ 050public class BackgroundPosition { 051 /** 052 * The default BackgroundPosition for any BackgroundImage. The default 053 * is to have no insets and to be defined as 0% and 0%. That is, the 054 * position is in the top-left corner. 055 */ 056 public static final BackgroundPosition DEFAULT = new BackgroundPosition( 057 Side.LEFT, 0, true, Side.TOP, 0, true); 058 // As per the CSS 3 Spec (3.6), 0% 0% is the default 059 060 /** 061 * The side along the horizontal axis to which the BackgroundImage is 062 * anchored. This will only be LEFT or RIGHT and never null. 063 */ 064 final Side horizontalSide; 065 public final Side getHorizontalSide() { return horizontalSide; } 066 067 /** 068 * The side along the vertical axis to which the BackgroundImage is 069 * anchored. This will only be TOP or BOTTOM and never null. 070 */ 071 final Side verticalSide; 072 public final Side getVerticalSide() { return verticalSide; } 073 074 /** 075 * The value indicating the position of the BackgroundImage relative 076 * to the Region along the side indicated by the 077 * {@link #getHorizontalSide() horizontalSide} property. This value 078 * is either a literal or a percentage, depending on the 079 * {@link #isHorizontalAsPercentage() horizontalAsPercentage} property. 080 * Negative values are acceptable. 081 */ 082 final double horizontalPosition; 083 public final double getHorizontalPosition() { return horizontalPosition; } 084 085 /** 086 * The value indicating the position of the BackgroundImage relative 087 * to the Region along the side indicated by the {@link #getVerticalSide() verticalSide} 088 * property. This value is either a literal or a percentage, depending on the 089 * {@link #isVerticalAsPercentage() verticalAsPercentage} property. Negative 090 * values are acceptable. 091 */ 092 final double verticalPosition; 093 public final double getVerticalPosition() { return verticalPosition; } 094 095 /** 096 * Specifies whether the {@link #getHorizontalPosition() horizontalPosition} should 097 * be interpreted as a literal number or as a percentage. 098 */ 099 final boolean horizontalAsPercentage; 100 public final boolean isHorizontalAsPercentage() { return horizontalAsPercentage; } 101 102 /** 103 * Specifies whether the {@link #getVerticalPosition() verticalPosition} should 104 * be interpreted as a literal number or as a percentage. 105 */ 106 final boolean verticalAsPercentage; 107 public final boolean isVerticalAsPercentage() { return verticalAsPercentage; } 108 109 /** 110 * A cached has code value. 111 */ 112 private final int hash; 113 114 /** 115 * Creates a new BackgroundPosition. 116 * 117 * @param horizontalSide The horizontal side, must be either null, LEFT, or RIGHT. If null, LEFT 118 * will be used. If TOP or BOTTOM is specified, an IllegalArgumentException 119 * is thrown. 120 * @param horizontalPosition The horizontal position value. 121 * @param horizontalAsPercentage Whether to interpret the horizontal position as a decimal or percentage 122 * @param verticalSide The vertical side, must be either null, TOP, or BOTTOM. If null, TOP 123 * will be used. If LEFT or RIGHT is specified, an IllegalArgumentException 124 * is thrown. 125 * @param verticalPosition The vertical position value. 126 * @param verticalAsPercentage Whether to interpret the vertical position as a decimal or percentage 127 */ 128 public BackgroundPosition(Side horizontalSide, double horizontalPosition, boolean horizontalAsPercentage, 129 Side verticalSide, double verticalPosition, boolean verticalAsPercentage) { 130 131 if (horizontalSide == Side.TOP || horizontalSide == Side.BOTTOM) { 132 throw new IllegalArgumentException("The horizontalSide must be LEFT or RIGHT"); 133 } 134 135 if (verticalSide == Side.LEFT || verticalSide == Side.RIGHT) { 136 throw new IllegalArgumentException("The verticalSide must be TOP or BOTTOM"); 137 } 138 139 this.horizontalSide = horizontalSide == null ? Side.LEFT : horizontalSide; 140 this.verticalSide = verticalSide == null ? Side.TOP : verticalSide; 141 this.horizontalPosition = horizontalPosition; 142 this.verticalPosition = verticalPosition; 143 this.horizontalAsPercentage = horizontalAsPercentage; 144 this.verticalAsPercentage = verticalAsPercentage; 145 146 // Pre-compute the hash code. NOTE: all variables are prefixed with "this" so that we 147 // do not accidentally compute the hash based on the constructor arguments rather than 148 // based on the fields themselves! 149 int result; 150 long temp; 151 result = this.horizontalSide.hashCode(); 152 result = 31 * result + this.verticalSide.hashCode(); 153 temp = this.horizontalPosition != +0.0d ? Double.doubleToLongBits(this.horizontalPosition) : 0L; 154 result = 31 * result + (int) (temp ^ (temp >>> 32)); 155 temp = this.verticalPosition != +0.0d ? Double.doubleToLongBits(this.verticalPosition) : 0L; 156 result = 31 * result + (int) (temp ^ (temp >>> 32)); 157 result = 31 * result + (this.horizontalAsPercentage ? 1 : 0); 158 result = 31 * result + (this.verticalAsPercentage ? 1 : 0); 159 hash = result; 160 } 161 162 /** 163 * @inheritDoc 164 */ 165 @Override public boolean equals(Object o) { 166 if (this == o) return true; 167 if (o == null || getClass() != o.getClass()) return false; 168 BackgroundPosition that = (BackgroundPosition) o; 169 if (hash != that.hash) return false; 170 if (horizontalAsPercentage != that.horizontalAsPercentage) return false; 171 if (Double.compare(that.horizontalPosition, horizontalPosition) != 0) return false; 172 if (verticalAsPercentage != that.verticalAsPercentage) return false; 173 if (Double.compare(that.verticalPosition, verticalPosition) != 0) return false; 174 if (horizontalSide != that.horizontalSide) return false; 175 if (verticalSide != that.verticalSide) return false; 176 177 return true; 178 } 179 180 /** 181 * @inheritDoc 182 */ 183 @Override public int hashCode() { 184 return hash; 185 } 186}