Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2010, 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;
027
028import java.util.Collection;
029import javafx.beans.DefaultProperty;
030import javafx.beans.property.BooleanProperty;
031import javafx.beans.property.BooleanPropertyBase;
032import javafx.collections.ObservableList;
033
034
035
036/**
037 * A {@code Group} node contains an ObservableList of children that
038 * are rendered in order whenever this node is rendered.
039 * <p>
040 * A {@code Group} will take on the collective bounds of its children and is
041 * not directly resizable.
042 * <p>
043 * Any transform, effect, or state applied to a {@code Group} will be applied
044 * to all children of that group.  Such transforms and effects will NOT be included
045 * in this Group's layout bounds, however if transforms and effects are set
046 * directly on children of this Group, those will be included in this Group's layout bounds.
047 * <p>
048 * By default, a {@code Group} will "auto-size" its managed resizable
049 * children to their preferred sizes during the layout pass to ensure that Regions
050 * and Controls are sized properly as their state changes.  If an application
051 * needs to disable this auto-sizing behavior, then it should set {@link #autoSizeChildren}
052 * to {@code false} and understand that if the preferred size of the children
053 * change, they will not automatically resize (so buyer beware!).
054 *
055 * <p>Group Example:</p>
056 *
057<PRE>
058import javafx.scene.*;
059import javafx.scene.paint.*;
060import javafx.scene.shape.*;
061import java.lang.Math;
062
063Group g = new Group();
064for (int i = 0; i < 5; i++) {
065    Rectangle r = new Rectangle();
066    r.setY(i * 20);
067    r.setWidth(100);
068    r.setHeight(10);
069    r.setFill(Color.RED);
070    g.getChildren().add(r);
071}
072</PRE>
073 */
074@DefaultProperty("children")
075public  class Group extends Parent {
076
077    /**
078     * Constructs a group.
079     */
080    public Group() { }
081
082    /**
083     * Constructs a group consisting of children.
084     *
085     * @param children children.
086     */
087    public Group(Node... children) {
088        getChildren().addAll(children);
089    }
090
091    /**
092     * Constructs a group consisting of the given children.
093     *
094     * @param children children of the group
095     * @throws NullPointerException if the specified collection is null
096     */
097    public Group(Collection<Node> children) {
098        getChildren().addAll(children);
099    }
100
101    /**
102     * Controls whether or not this {@code Group} will automatically resize any
103     * managed resizable children to their preferred sizes
104     * during the layout pass. If set to {@code false}, then the application is
105     * responsible for setting the size of this Group's resizable children, otherwise
106     * such nodes may end up with a zero width/height and will not be visible.
107     * This variable has no effect on content nodes which are not resizable (Shape, Text, etc).
108     *
109     * @since JavaFX 1.3
110     * @defaultValue true
111     */
112    private BooleanProperty autoSizeChildren;
113
114
115    public final void setAutoSizeChildren(boolean value){
116        autoSizeChildrenProperty().set(value);
117    }
118
119    public final boolean isAutoSizeChildren() {
120        return autoSizeChildren == null ? true : autoSizeChildren.get();
121    }
122
123    public final BooleanProperty autoSizeChildrenProperty() {
124        if (autoSizeChildren == null) {
125            autoSizeChildren = new BooleanPropertyBase(true) {
126
127                @Override
128                protected void invalidated() {
129                    requestLayout();
130                }
131
132                @Override
133                public Object getBean() {
134                    return Group.this;
135                }
136
137                @Override
138                public String getName() {
139                    return "autoSizeChildren";
140                }
141            };
142        }
143        return autoSizeChildren;
144    }
145
146    /**
147     * Gets the list of children of this {@code Group}.
148     * @return the list of children of this {@code Group}.
149     */
150    @Override public ObservableList<Node> getChildren() {
151        return super.getChildren();
152    }
153
154    // don't need to cache values, because layoutBounds already handles that
155    // Note that these values return the "current" layout bounds; if this group
156    // contains any resizable descendents, layout may in fact result in the
157    // group's layout bounds changing, necessitating another layout pass to
158    // adjust.  Scene handles this in doLayoutPass().  If this becomes a
159    // performance issue, we can revisit doing the proper computation/predicting here.
160
161    /**
162     * Group defines the preferred width as simply being the width of its layout bounds, which
163     * in turn is simply the sum of the positions & widths of all of its children. That is,
164     * the preferred width is the one that it is at, because a Group cannot be resized.
165     *
166     * @param height This parameter is ignored by Group
167     * @return The layout bounds width
168     */
169    @Override public double prefWidth(double height) {
170        return getLayoutBounds().getWidth();
171    }
172
173    /**
174     * Group defines the preferred height as simply being the height of its layout bounds, which
175     * in turn is simply the sum of the positions & heights of all of its children. That is,
176     * the preferred height is the one that it is at, because a Group cannot be resized.
177     *
178     * @param width This parameter is ignored by Group
179     * @return The layout bounds height
180     */
181    @Override public double prefHeight(double width) {
182        return getLayoutBounds().getHeight();
183    }
184
185    @Override
186    public double minHeight(double width) {
187        return prefHeight(width);
188    }
189
190    @Override
191    public double minWidth(double height) {
192        return prefWidth(height);
193    }
194
195    /**
196     * Group implements layoutChildren such that each child is resized to its preferred
197     * size, if the child is resizable. Non-resizable children are simply left alone.
198     * If {@link #autoSizeChildren} is false, then Group does nothing in this method.
199     */
200    @Override protected void layoutChildren() {
201        if (isAutoSizeChildren()) {
202            super.layoutChildren();
203        }
204    }
205}