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.Insets;
029import javafx.scene.paint.Color;
030import javafx.scene.paint.Paint;
031
032/**
033 * The fill and associated properties that direct how to fill the background of a
034 * {@link Region}. Because BackgroundFill is an immutable object, it can safely be
035 * used in any cache, and can safely be reused among multiple Regions or multiple
036 * times in the same Region.
037 * <p/>
038 * All BackgroundFills are drawn in order.
039 * <p/>
040 * When applied to a Region with a defined shape, the corner radii are ignored.
041 */
042public final class BackgroundFill {
043    /**
044     * The Paint to use for filling the background of the {@link Region}.
045     * This value will never be null.
046     */
047    final Paint fill;
048    public final Paint getFill() { return fill; }
049
050    /**
051     * The Radii to use for representing the four radii of the
052     * BackgroundFill. Each corner can therefore be independently
053     * specified. This will never be null. The radii values will
054     * never be negative.
055     */
056    final CornerRadii radii;
057    public final CornerRadii getRadii() { return radii; }
058
059    /**
060     * The Insets to use for this fill. Each inset indicates at what
061     * distance from the Region's bounds the drawing should begin.
062     * The insets will never be null, but the values may be negative
063     * in order to position the border beyond the natural bounds
064     * (that is, (0, 0, width, height)) of the Region.
065     */
066    final Insets insets;
067    public final Insets getInsets() { return insets; }
068
069    /**
070     * A cached hash for improved performance on subsequent hash or
071     * equality look ups. It is expected that BackgroundFill will in
072     * many instances be loaded from a CSS cache, such that storing
073     * this value is beneficial.
074     */
075    private final int hash;
076
077    /**
078     * Creates a new BackgroundFill with the specified fill, radii, and
079     * insets. Null values are acceptable, but default values will be
080     * used in place of any null value.
081     *
082     * @param fill   Any Paint. If null, the value Color.TRANSPARENT is used.
083     * @param radii  The corner Radii. If null, the value Radii.EMPTY is used.
084     * @param insets The insets. If null, the value Insets.EMPTY is used.
085     */
086    public BackgroundFill(Paint fill, CornerRadii radii, Insets insets) {
087        // As per the CSS Spec (section 3.2): initial value of background-color is TRANSPARENT
088        this.fill = fill == null ? Color.TRANSPARENT : fill;
089        this.radii = radii == null ? CornerRadii.EMPTY : radii;
090        this.insets = insets == null ? Insets.EMPTY : insets;
091
092        // Pre-compute the hash code. NOTE: all variables are prefixed with "this" so that we
093        // do not accidentally compute the hash based on the constructor arguments rather than
094        // based on the fields themselves!
095        int result = this.fill.hashCode();
096        result = 31 * result + this.radii.hashCode();
097        result = 31 * result + this.insets.hashCode();
098        hash = result;
099    }
100
101    /**
102     * @inheritDoc
103     */
104    @Override public boolean equals(Object o) {
105        if (this == o) return true;
106        if (o == null || getClass() != o.getClass()) return false;
107        BackgroundFill that = (BackgroundFill) o;
108        // Because the hash is cached, this can be a very fast check
109        if (hash != that.hash) return false;
110        if (!fill.equals(that.fill)) return false;
111        if (!insets.equals(that.insets)) return false;
112        if (!radii.equals(that.radii)) return false;
113
114        return true;
115    }
116
117    /**
118     * @inheritDoc
119     */
120    @Override public int hashCode() {
121        return hash;
122    }
123}