1 /* 2 * Copyright (c) 2014, 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 package javafx.scene.control.test; 26 27 import javafx.beans.InvalidationListener; 28 import javafx.beans.Observable; 29 import javafx.beans.property.BooleanProperty; 30 import javafx.event.ActionEvent; 31 import javafx.event.EventHandler; 32 import javafx.scene.Scene; 33 import javafx.scene.control.Button; 34 import javafx.scene.control.ButtonBuilder; 35 import javafx.scene.control.CheckBox; 36 import javafx.scene.control.CheckBoxBuilder; 37 import javafx.scene.control.Label; 38 import javafx.scene.control.LabelBuilder; 39 import javafx.scene.control.RadioButtonBuilder; 40 import javafx.scene.control.ToggleButton; 41 import javafx.scene.control.ToggleButtonBuilder; 42 import javafx.scene.control.ToggleGroup; 43 import javafx.scene.input.MouseEvent; 44 import javafx.scene.layout.HBox; 45 import javafx.scene.layout.VBox; 46 import test.javaclient.shared.InteroperabilityApp; 47 import test.javaclient.shared.Utils; 48 49 /** 50 * @author Alexander Kirov 51 */ 52 public class ToggleButtonNewApp extends InteroperabilityApp { 53 54 public enum ControlType { 55 56 TOGGLE_BUTTON, RADIO_BUTTON 57 }; 58 public static String TOGGLE_BUTTON_1_ID = "toggle button 1"; 59 public static String TOGGLE_BUTTON_2_ID = "toggle button 2"; 60 public static String BINDING_SET_TOGGLE_BUTTON_ID = "binding set button"; 61 public static String GROUPING_TOGGLE_BUTTON_ID = "grouping button"; 62 public static String RESET_BUTTON_ID = "reset scene button"; 63 public static String SETTING_CONTROL_TOGGLE_BUTTON_ID = "ToggleButton which sets tested control"; 64 public static String CHECK_BOX_LISTENING_1_ID = "check box listening to toggle button 1"; 65 public static String CHECK_BOX_LISTENING_2_ID = "check box listening to toggle button 2"; 66 public static String CHECK_BOX_SETTING_1_BIDIR_ID = "check box setting toggle button 1 bidirectionaly"; 67 public static String CHECK_BOX_SETTING_2_UNIDIR_ID = "check box setting toggle button 2 unirectionaly"; 68 public static String MOUSE_EVENTS_LOGGER_LABEL_ID = "label logs mouse events"; 69 70 public static void main(String[] args) { 71 Utils.launch(ToggleButtonNewApp.class, args); 72 } 73 74 @Override 75 protected Scene getScene() { 76 return new ToggleButtonNewAppScene(); 77 } 78 79 class ToggleButtonNewAppScene extends Scene { 80 81 ToggleButton toggleButton1; 82 ToggleButton toggleButton2; 83 CheckBox checkBox1 = CheckBoxBuilder.create().wrapText(true).text("Unidirectionally binded to\ntoggle button 1 selection property").id(CHECK_BOX_LISTENING_1_ID).build(); 84 CheckBox checkBox2 = CheckBoxBuilder.create().wrapText(true).text("Unidirectionally binded to\ntoggle button 2 selection property").id(CHECK_BOX_LISTENING_2_ID).build(); 85 CheckBox checkBox3 = CheckBoxBuilder.create().wrapText(true).text("This check box is bidirectionally binded\nto Toggle Button 1 selection property").id(CHECK_BOX_SETTING_1_BIDIR_ID).build(); 86 CheckBox checkBox4 = CheckBoxBuilder.create().wrapText(true).text("This check box unidirectionally sets\ntoggle button 2 selection property").id(CHECK_BOX_SETTING_2_UNIDIR_ID).build(); 87 ToggleGroup toggleGroup = new ToggleGroup(); 88 ToggleButton bindingsSettingButton = ToggleButtonBuilder.create().text("State: Unbinded").id(BINDING_SET_TOGGLE_BUTTON_ID).build(); 89 ToggleButton groupingButton = ToggleButtonBuilder.create().text("State: Ungrouped").id(GROUPING_TOGGLE_BUTTON_ID).build(); 90 ToggleButton testedControlChangeButton = ToggleButtonBuilder.create().text("Control: radio button").id(SETTING_CONTROL_TOGGLE_BUTTON_ID).build(); 91 Button resetButton = ButtonBuilder.create().text("Reset state").id(RESET_BUTTON_ID).build(); 92 Label mouseEventsLogger = LabelBuilder.create().text("0").id(MOUSE_EVENTS_LOGGER_LABEL_ID).build(); 93 VBox vb1 = new VBox(); 94 95 public ToggleButtonNewAppScene() { 96 super(new HBox(), 800, 320); 97 98 setControl(ControlType.RADIO_BUTTON); 99 100 addButtonsListeners(); 101 bindListeningBindings(); 102 addToggleButtonActionEventListener(); 103 104 checkBox1.setDisable(true); 105 checkBox2.setDisable(true); 106 Label label1 = new Label("These CheckBoxes listens\n to \"selected\"" 107 + " property of according\n toggle button " 108 + "by binding"); 109 Label label2 = new Label("Next Checkboxes set its\n " 110 + "values to toggle buttons. \n" 111 + "Before using press button for binding"); 112 label1.setWrapText(true); 113 label2.setWrapText(true); 114 115 vb1.setSpacing(10); 116 117 VBox vb2 = new VBox(); 118 vb2.setSpacing(10); 119 vb2.getChildren().addAll(label1, checkBox1, checkBox2, label2, bindingsSettingButton, checkBox3, checkBox4); 120 121 VBox vb3 = new VBox(); 122 vb3.setSpacing(10); 123 vb3.getChildren().addAll(groupingButton, testedControlChangeButton, resetButton); 124 125 HBox hb = (HBox) getRoot(); 126 hb.setSpacing(10); 127 hb.setLayoutX(10); 128 hb.setLayoutY(10); 129 hb.getChildren().addAll(vb1, vb2, vb3, new Label("Action events handled : "), mouseEventsLogger); 130 } 131 132 private void setControl(ControlType type) { 133 if (type == ControlType.RADIO_BUTTON) { 134 toggleButton1 = RadioButtonBuilder.create().text("Radio button 1").id(TOGGLE_BUTTON_1_ID).build(); 135 toggleButton2 = RadioButtonBuilder.create().text("Radio button 2").id(TOGGLE_BUTTON_2_ID).build(); 136 } else { 137 toggleButton1 = ToggleButtonBuilder.create().text("Toggle button 1").id(TOGGLE_BUTTON_1_ID).build(); 138 toggleButton2 = ToggleButtonBuilder.create().text("Toggle button 2").id(TOGGLE_BUTTON_2_ID).build(); 139 } 140 141 vb1.getChildren().clear(); 142 vb1.getChildren().addAll(toggleButton1, toggleButton2); 143 } 144 145 private void addButtonsListeners() { 146 147 bindingsSettingButton.selectedProperty().addListener(new InvalidationListener() { 148 public void invalidated(Observable o) { 149 if (((BooleanProperty) o).getValue() == true) { 150 bindSettingBindings(); 151 bindingsSettingButton.setText("State: Binded"); 152 } else { 153 removeBindings(); 154 bindListeningBindings(); 155 bindingsSettingButton.setText("State: Unbinded"); 156 } 157 } 158 }); 159 160 groupingButton.selectedProperty().addListener(new InvalidationListener() { 161 public void invalidated(Observable o) { 162 if (((BooleanProperty) o).getValue() == true) { 163 toggleButton1.setToggleGroup(toggleGroup); 164 toggleButton2.setToggleGroup(toggleGroup); 165 groupingButton.setText("State: Grouped"); 166 } else { 167 toggleButton1.setToggleGroup(null); 168 toggleButton2.setToggleGroup(null); 169 groupingButton.setText("State: Ungrouped"); 170 } 171 } 172 }); 173 174 testedControlChangeButton.selectedProperty().addListener(new InvalidationListener() { 175 public void invalidated(Observable o) { 176 groupingButton.selectedProperty().setValue(false); 177 bindingsSettingButton.selectedProperty().setValue(false); 178 if (((BooleanProperty) o).getValue() == true) { 179 setControl(ControlType.TOGGLE_BUTTON); 180 testedControlChangeButton.setText("Control: toggle button"); 181 } else { 182 setControl(ControlType.RADIO_BUTTON); 183 testedControlChangeButton.setText("Control: radio button"); 184 } 185 186 addToggleButtonActionEventListener(); 187 bindListeningBindings(); 188 } 189 }); 190 191 resetButton.setOnMouseClicked(new EventHandler<MouseEvent>() { 192 public void handle(MouseEvent t) { 193 resetState(); 194 } 195 }); 196 } 197 198 private void addToggleButtonActionEventListener() { 199 EventHandler<ActionEvent> actionEventHandler = new EventHandler<ActionEvent>() { 200 public void handle(ActionEvent t) { 201 mouseEventsLogger.setText(String.valueOf(Integer.parseInt(mouseEventsLogger.getText()) + 1)); 202 } 203 }; 204 205 mouseEventsLogger.setText("0"); 206 207 toggleButton1.setOnAction(actionEventHandler); 208 toggleButton2.setOnAction(actionEventHandler); 209 } 210 211 private void bindListeningBindings() { 212 checkBox1.selectedProperty().bind(toggleButton1.selectedProperty()); 213 checkBox2.selectedProperty().bind(toggleButton2.selectedProperty()); 214 } 215 216 private void bindSettingBindings() { 217 checkBox3.selectedProperty().bindBidirectional(toggleButton1.selectedProperty()); 218 toggleButton2.selectedProperty().bind(checkBox4.selectedProperty()); 219 } 220 221 ; 222 223 private void removeBindings() { 224 checkBox1.selectedProperty().unbind(); 225 checkBox2.selectedProperty().unbind(); 226 checkBox3.selectedProperty().unbindBidirectional(toggleButton1.selectedProperty()); 227 228 toggleButton2.selectedProperty().unbind(); 229 230 setAllSelectionsFalse(); 231 } 232 233 private void setAllSelectionsFalse() { 234 toggleButton1.setSelected(false); 235 toggleButton2.setSelected(false); 236 237 checkBox3.setSelected(false); 238 checkBox4.setSelected(false); 239 } 240 241 private void resetEventLabel() { 242 mouseEventsLogger.setText("0"); 243 addToggleButtonActionEventListener(); 244 } 245 246 private void resetState() { 247 bindingsSettingButton.setSelected(false); 248 groupingButton.setSelected(false); 249 250 testedControlChangeButton.setSelected(false); 251 setControl(ControlType.RADIO_BUTTON); 252 testedControlChangeButton.setText("Control: radio button"); 253 254 removeBindings(); 255 bindListeningBindings(); 256 setAllSelectionsFalse(); 257 resetEventLabel(); 258 } 259 } 260 }