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 static javafx.scene.layout.Region.USE_COMPUTED_SIZE;
029import static javafx.scene.layout.Region.USE_PREF_SIZE;
030import javafx.beans.property.BooleanProperty;
031import javafx.beans.property.BooleanPropertyBase;
032import javafx.beans.property.DoubleProperty;
033import javafx.beans.property.DoublePropertyBase;
034import javafx.beans.property.ObjectProperty;
035import javafx.beans.property.ObjectPropertyBase;
036import javafx.geometry.HPos;
037
038
039/**
040 * Defines optional layout constraints for a column in a {@link GridPane}.
041 * If a ColumnConstraints object is added for a column in a gridpane, the gridpane
042 * will use those constraint values when computing the column's width and layout.
043 * <p>
044 * For example, to create a GridPane with 5 columns 100 pixels wide:
045 * <pre><code>
046 *     GridPane gridpane = new GridPane();
047 *     for (int i = 0; i < 5; i++) {
048 *         ColumnConstraints column = new ColumnConstraints(100);
049 *         gridpane.getColumnConstraints().add(column);
050 *     }
051 * </code></pre>
052 * Or, to create a GridPane where columns take 25%, 50%, 25% of its width:
053 * <pre><code>
054 *     GridPane gridpane = new GridPane();
055 *     ColumnConstraints col1 = new ColumnConstraints();
056 *     col1.setPercentWidth(25);
057 *     ColumnConstraints col2 = new ColumnConstraints();
058 *     col2.setPercentWidth(50);
059 *     ColumnConstraints col3 = new ColumnConstraints();
060 *     col3.setPercentWidth(25);
061 *     gridpane.getColumnConstraints().addAll(col1,col2,col3);
062 * </code></pre>
063 *
064 * Note that adding an empty ColumnConstraints object has the effect of not setting
065 * any constraints, leaving the GridPane to compute the column's layout based
066 * solely on its content's size preferences and constraints.
067 *
068 */
069public class ColumnConstraints extends ConstraintsBase {
070
071    /**
072     * Create a column constraint object with no properties set.
073     */
074    public ColumnConstraints() {
075        super();
076    }
077
078    /**
079     * Creates a column constraint object with a fixed width.
080     * This is a convenience for setting the preferred width constraint to the
081     * fixed value and the minWidth and maxWidth constraints to the USE_PREF_SIZE
082     * flag to ensure the column is always that width.
083     *
084     * @param width the width of the column
085     */
086    public ColumnConstraints(double width) {
087        this();
088        setMinWidth(USE_PREF_SIZE);
089        setPrefWidth(width);
090        setMaxWidth(USE_PREF_SIZE);
091    }
092
093    /**
094     * Creates a column constraint object with a fixed size range.
095     * This is a convenience for setting the minimum, preferred, and maximum
096     * width constraints.
097     *
098     */
099    public ColumnConstraints(double minWidth, double prefWidth, double maxWidth) {
100        this();
101        setMinWidth(minWidth);
102        setPrefWidth(prefWidth);
103        setMaxWidth(maxWidth);
104    }
105
106    /**
107     * Creates a column constraint object with a fixed size range, horizontal
108     * grow priority, horizonal alignment, and horizontal fill behavior.
109     *
110     */
111    public ColumnConstraints(double minWidth, double prefWidth, double maxWidth, Priority hgrow, HPos halignment, boolean fillWidth) {
112        this(minWidth, prefWidth, maxWidth);
113        setHgrow(hgrow);
114        setHalignment(halignment);
115        setFillWidth(fillWidth);
116    }
117
118    /**
119     * The minimum width for the column.
120     * This property is ignored if percentWidth is set.
121     * <p>
122     * The default value is USE_COMPUTED_SIZE, which means the minimum width
123     * will be computed to be the largest minimum width of the column's content.
124     */
125    private DoubleProperty minWidth;
126
127    public final void setMinWidth(double value) {
128        minWidthProperty().set(value);
129    }
130
131    public final double getMinWidth() {
132        return minWidth == null ? USE_COMPUTED_SIZE : minWidth.get();
133    }
134
135    public final DoubleProperty minWidthProperty() {
136        if (minWidth == null) {
137            minWidth = new DoublePropertyBase(USE_COMPUTED_SIZE) {
138
139                @Override
140                protected void invalidated() {
141                    requestLayout();
142                }
143
144                @Override
145                public Object getBean() {
146                    return ColumnConstraints.this;
147                }
148
149                @Override
150                public String getName() {
151                    return "minWidth";
152                }
153            };
154        }
155        return minWidth;
156    }
157
158    /**
159     * The preferred width for the column.
160     * This property is ignored if percentWidth is set.
161     * <p>
162     * The default value is USE_COMPUTED_SIZE, which means the preferred width
163     * will be computed to be the largest preferred width of the column's content.
164     */
165    private DoubleProperty prefWidth;
166
167    public final void setPrefWidth(double value) {
168        prefWidthProperty().set(value);
169    }
170
171    public final double getPrefWidth() {
172        return prefWidth == null ? USE_COMPUTED_SIZE : prefWidth.get();
173    }
174
175    public final DoubleProperty prefWidthProperty() {
176        if (prefWidth == null) {
177            prefWidth = new DoublePropertyBase(USE_COMPUTED_SIZE) {
178
179                @Override
180                protected void invalidated() {
181                    requestLayout();
182                }
183
184                @Override
185                public Object getBean() {
186                    return ColumnConstraints.this;
187                }
188
189                @Override
190                public String getName() {
191                    return "prefWidth";
192                }
193            };
194        }
195        return prefWidth;
196    }
197
198    /**
199     * The maximum width for the column.
200     * This property is ignored if percentWidth is set.
201     * <p>
202     * The default value is USE_COMPUTED_SIZE, which means the maximum width
203     * will be computed to be the smallest maximum width of the column's content.
204     */
205    private DoubleProperty maxWidth;
206
207    public final void setMaxWidth(double value) {
208        maxWidthProperty().set(value);
209    }
210
211    public final double getMaxWidth() {
212        return maxWidth == null ? USE_COMPUTED_SIZE : maxWidth.get();
213    }
214
215    public final DoubleProperty maxWidthProperty() {
216        if (maxWidth == null) {
217            maxWidth = new DoublePropertyBase(USE_COMPUTED_SIZE) {
218
219                @Override
220                protected void invalidated() {
221                    requestLayout();
222                }
223
224                @Override
225                public Object getBean() {
226                    return ColumnConstraints.this;
227                }
228
229                @Override
230                public String getName() {
231                    return "maxWidth";
232                }
233            };
234        }
235        return maxWidth;
236    }
237
238    /**
239     * The width percentage of the column.  If set to a value greater than 0,
240     * the column will be resized to this percentage of the gridpane's available
241     * width and the other size constraints (minWidth, prefWidth, maxWidth, hgrow)
242     * will be ignored.
243     *
244     * The default value is -1, which means the percentage will be ignored.
245     */
246    private DoubleProperty percentWidth;
247
248    public final void setPercentWidth(double value) {
249        percentWidthProperty().set(value);
250    }
251
252    public final double getPercentWidth() {
253        return percentWidth == null ? -1 : percentWidth.get();
254    }
255
256    public final DoubleProperty percentWidthProperty() {
257        if (percentWidth == null) {
258            percentWidth = new DoublePropertyBase(-1) {
259
260                @Override
261                protected void invalidated() {
262                    requestLayout();
263                }
264
265                @Override
266                public Object getBean() {
267                    return ColumnConstraints.this;
268                }
269
270                @Override
271                public String getName() {
272                    return "percentWidth";
273                }
274            };
275        }
276        return percentWidth;
277    }
278
279
280    /**
281     * The horizontal grow priority for the column.  If set, the gridpane will
282     * use this priority to determine whether the column should be given any
283     * additional width if the gridpane is resized larger than its preferred width.
284     * This property is ignored if percentWidth is set.
285     * <p>
286     * This default value is null, which means that the column's grow priority
287     * will be derived from largest grow priority set on a content node.
288     */
289    private ObjectProperty<Priority> hgrow;
290
291    public final void setHgrow(Priority value) {
292        hgrowProperty().set(value);
293    }
294
295    public final Priority getHgrow() {
296        return hgrow == null ? null : hgrow.get();
297    }
298
299    public final ObjectProperty<Priority> hgrowProperty() {
300        if (hgrow == null) {
301            hgrow = new ObjectPropertyBase<Priority>() {
302
303                @Override
304                protected void invalidated() {
305                    requestLayout();
306                }
307
308                @Override
309                public Object getBean() {
310                    return ColumnConstraints.this;
311                }
312
313                @Override
314                public String getName() {
315                    return "hgrow";
316                }
317            };
318        }
319        return hgrow;
320    }
321
322    /**
323     * The horizontal alignment for the column. If set, will be the default
324     * horizontal alignment for nodes contained within the column.
325     * <p>
326     * The default value is null, which means the column alignment will fall
327     * back to the default halignment set on the gridpane.
328     */
329    private ObjectProperty<HPos> halignment;
330
331    public final void setHalignment(HPos value) {
332        halignmentProperty().set(value);
333    }
334
335    public final HPos getHalignment() {
336        return halignment == null ? null : halignment.get();
337    }
338
339    public final ObjectProperty<HPos> halignmentProperty() {
340        if (halignment == null) {
341            halignment = new ObjectPropertyBase<HPos>() {
342
343                @Override
344                protected void invalidated() {
345                    requestLayout();
346                }
347
348                @Override
349                public Object getBean() {
350                    return ColumnConstraints.this;
351                }
352
353                @Override
354                public String getName() {
355                    return "halignment";
356                }
357            };
358        }
359        return halignment;
360    }
361
362    /**
363     * The horizontal fill policy for the column.  The gridpane will
364     * use this property to determine whether nodes contained within the column
365     * should be expanded to fill the column or kept to their preferred widths.
366     * <p>
367     * The default value is true.
368     *
369     */
370    private BooleanProperty fillWidth;
371
372    public final void setFillWidth(boolean value) {
373        fillWidthProperty().set(value);
374    }
375
376    public final boolean isFillWidth() {
377        return fillWidth == null ? true : fillWidth.get();
378    }
379
380    public final BooleanProperty fillWidthProperty() {
381        if (fillWidth == null) {
382            fillWidth = new BooleanPropertyBase(true) {
383
384                @Override
385                protected void invalidated() {
386                    requestLayout();
387                }
388
389                @Override
390                public Object getBean() {
391                    return ColumnConstraints.this;
392                }
393
394                @Override
395                public String getName() {
396                    return "fillWidth";
397                }
398            };
399        }
400        return fillWidth;
401    }
402
403    /**
404     * Returns a string representation of this {@code ColumnConstraints} object.
405     * @return a string representation of this {@code ColumnConstraints} object.
406     */ 
407    @Override public String toString() {
408        return "ColumnConstraints percentWidth="+getPercentWidth()+
409                " minWidth="+getMinWidth()+
410                " prefWidth="+getPrefWidth()+
411                " maxWidth="+getMaxWidth()+
412                " hgrow="+getHgrow()+
413                " fillWidth="+isFillWidth()+
414                " halignment="+getHalignment();
415    }
416}