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