1 /* 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.javafx.scene.control.skin; 27 28 import javafx.scene.control.RadioButton; 29 import javafx.scene.layout.StackPane; 30 31 import com.sun.javafx.scene.control.behavior.ButtonBehavior; 32 33 public class RadioButtonSkin extends LabeledSkinBase<RadioButton, ButtonBehavior<RadioButton>> { 34 35 /** The radio contains the "dot", which is usually a circle */ 36 private StackPane radio; 37 38 /** 39 * Used for laying out the label + radio together as a group 40 * 41 * NOTE: This extra node should be eliminated in the future. 42 * Instead, position inner nodes directly with the utility 43 * functions in Pane (computeXOffset()/computeYOffset()). 44 */ 45 public RadioButtonSkin(RadioButton radioButton) { 46 super(radioButton, new ButtonBehavior<RadioButton>(radioButton)); 47 48 radio = createRadio(); 49 updateChildren(); 50 } 51 52 @Override protected void updateChildren() { 53 super.updateChildren(); 54 if (radio != null) { 55 getChildren().add(radio); 56 } 57 } 58 59 private static StackPane createRadio() { 60 StackPane radio = new StackPane(); 61 radio.getStyleClass().setAll("radio"); 62 radio.setSnapToPixel(false); 63 StackPane region = new StackPane(); 64 region.getStyleClass().setAll("dot"); 65 radio.getChildren().clear(); 66 radio.getChildren().addAll(region); 67 return radio; 68 } 69 70 71 /*************************************************************************** 72 * * 73 * Layout * 74 * * 75 **************************************************************************/ 76 77 @Override protected double computeMinWidth(double height, double topInset, double rightInset, double bottomInset, double leftInset) { 78 return super.computeMinWidth(height, topInset, rightInset, bottomInset, leftInset) + snapSize(radio.minWidth(-1)); 79 } 80 81 @Override protected double computeMinHeight(double width, double topInset, double rightInset, double bottomInset, double leftInset) { 82 return Math.max(snapSize(super.computeMinHeight(width - radio.minWidth(-1), topInset, rightInset, bottomInset, leftInset)), 83 topInset + radio.minHeight(-1) + bottomInset); 84 } 85 86 @Override protected double computePrefWidth(double height, double topInset, double rightInset, double bottomInset, double leftInset) { 87 return super.computePrefWidth(height, topInset, rightInset, bottomInset, leftInset) + snapSize(radio.prefWidth(-1)); 88 } 89 90 @Override protected double computePrefHeight(double width, double topInset, double rightInset, double bottomInset, double leftInset) { 91 return Math.max(snapSize(super.computePrefHeight(width - radio.prefWidth(-1), topInset, rightInset, bottomInset, leftInset)), 92 topInset + radio.prefHeight(-1) + bottomInset); 93 } 94 95 @Override protected void layoutChildren(final double x, final double y, 96 final double w, final double h) { 97 final RadioButton radioButton = getSkinnable(); 98 final double radioWidth = radio.prefWidth(-1); 99 final double radioHeight = radio.prefHeight(-1); 100 final double computeWidth = Math.max(radioButton.prefWidth(-1),radioButton.minWidth(-1)); 101 final double labelWidth = Math.min(computeWidth - radioWidth, w - snapSize(radioWidth)); 102 final double labelHeight = Math.min(radioButton.prefHeight(labelWidth), h); 103 final double maxHeight = Math.max(radioHeight, labelHeight); 104 final double xOffset = Utils.computeXOffset(w, labelWidth + radioWidth, radioButton.getAlignment().getHpos()) + x; 105 final double yOffset = Utils.computeYOffset(h, maxHeight, radioButton.getAlignment().getVpos()) + y; 106 107 layoutLabelInArea(xOffset + radioWidth, yOffset, labelWidth, maxHeight, radioButton.getAlignment()); 108 radio.resize(snapSize(radioWidth), snapSize(radioHeight)); 109 positionInArea(radio, xOffset, yOffset, radioWidth, maxHeight, 0, radioButton.getAlignment().getHpos(), radioButton.getAlignment().getVpos()); 110 } 111 }