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.chart.apps; 26 27 import java.util.Iterator; 28 import java.util.Random; 29 import javafx.beans.property.DoubleProperty; 30 import javafx.beans.property.SimpleDoubleProperty; 31 import javafx.beans.value.ChangeListener; 32 import javafx.beans.value.ObservableValue; 33 import javafx.collections.FXCollections; 34 import javafx.collections.ObservableList; 35 import javafx.event.ActionEvent; 36 import javafx.event.Event; 37 import javafx.event.EventHandler; 38 import javafx.scene.Scene; 39 import javafx.scene.chart.NumberAxis; 40 import javafx.scene.chart.ScatterChart; 41 import javafx.scene.chart.XYChart; 42 import javafx.scene.chart.XYChart.Data; 43 import javafx.scene.chart.XYChart.Series; 44 import javafx.scene.control.*; 45 import static javafx.scene.control.test.chart.apps.CommonFunctions.*; 46 import javafx.scene.control.test.utils.CommonPropertiesScene; 47 import javafx.scene.control.test.utils.ptables.PropertiesTable; 48 import javafx.scene.control.test.utils.ptables.TabPaneWithControl; 49 import javafx.scene.layout.HBox; 50 import javafx.scene.layout.VBox; 51 import test.javaclient.shared.InteroperabilityApp; 52 import test.javaclient.shared.Utils; 53 54 /** 55 * @author Alexander Kirov 56 */ 57 public class NewNumberScatterChartApp extends InteroperabilityApp implements ChartIDsInterface { 58 59 public final static String REMOVE_ITEM_POS_TEXT_FIELD_ID = "REMOVE_ITEM_POS_TEXT_FIELD_ID"; 60 public final static String REMOVE_BUTTON_ID = "REMOVE_BUTTON_ID"; 61 public final static String ADD_ITEM_TEXT_FIELD_ID = "ADD_ITEM_TEXT_FIELD_ID"; 62 public final static String ADD_ITEM_POSITION_TEXT_FIELD_ID = "ADD_ITEM_POSITION_TEXT_FIELD_ID"; 63 public final static String ADD_ITEM_BUTTON_ID = "ADD_ITEM_BUTTON_ID"; 64 65 public static void main(String[] args) { 66 Utils.launch(NewNumberScatterChartApp.class, args); 67 } 68 69 @Override 70 protected Scene getScene() { 71 Utils.setTitleToStage(stage, "ScatterChartTestApp"); 72 return new NewNumberScatterChartApp.ScatterChartScene(); 73 } 74 75 class ScatterChartScene extends CommonPropertiesScene { 76 77 //ScatterChart to be tested. 78 ScatterChart testedScatterChart; 79 NumberAxis axis1; 80 NumberAxis axis2; 81 TabPaneWithControl pane; 82 83 public ScatterChartScene() { 84 super("NumberScatterChart", 1300, 800); 85 } 86 87 @Override 88 final protected void prepareScene() { 89 Utils.addBrowser(this); 90 91 axis1 = new NumberAxis(0, 100, 10); 92 axis2 = new NumberAxis(0, 100, 10); 93 testedScatterChart = getNewChart(); 94 testedScatterChart.setId(TESTED_CHART_ID); 95 96 Button hardResetButton = ButtonBuilder.create().id(HARD_RESET_BUTTON_ID).text("Hard reset").build(); 97 hardResetButton.setOnAction(new EventHandler<ActionEvent>() { 98 public void handle(ActionEvent t) { 99 HBox hb = (HBox) getRoot(); 100 hb.getChildren().clear(); 101 prepareMainSceneStructure(); 102 prepareScene(); 103 } 104 }); 105 106 Button softResetButton = ButtonBuilder.create().id(SOFT_RESET_BUTTON_ID).text("Soft reset").build(); 107 softResetButton.setOnAction(new EventHandler<ActionEvent>() { 108 public void handle(ActionEvent t) { 109 //throw new UnsupportedOperationException("Not supported yet."); 110 } 111 }); 112 113 HBox resetButtonsHBox = new HBox(); 114 resetButtonsHBox.getChildren().addAll(hardResetButton, softResetButton); 115 116 VBox vb = new VBox(5); 117 vb.getChildren().addAll(resetButtonsHBox, getAddItemHBox(), getAddPointToSerieDialog(), getRemoveDataFromSerieDialog(), getRemoveDataDialog()); 118 119 pane = getPaneFor(testedScatterChart, CHART_TAB_NAME, axis1, AXIS_1_TAB_NAME, axis2, AXIS_2_TAB_NAME); 120 121 setTestedControlContainerSize(500, 500); 122 setTestedControl(testedScatterChart); 123 setPropertiesContent(pane); 124 setControllersContent(vb); 125 } 126 127 public HBox getRemoveDataDialog() { 128 HBox hb = new HBox(); 129 Label lb = new Label("From position"); 130 final TextField tf = TextFieldBuilder.create().text("0").prefWidth(50).id(REMOVE_ITEM_POS_TEXT_FIELD_ID).build(); 131 Button bt = ButtonBuilder.create().text("Remove!").id(REMOVE_BUTTON_ID).build(); 132 bt.setOnAction(new EventHandler() { 133 public void handle(Event t) { 134 int index = Integer.parseInt(tf.getText()); 135 testedScatterChart.getData().remove(index); 136 } 137 }); 138 hb.getChildren().addAll(lb, tf, bt); 139 return hb; 140 } 141 142 public HBox getRemoveDataFromSerieDialog() { 143 HBox hb = new HBox(); 144 Label lb1 = new Label("From serie"); 145 final TextField tf1 = TextFieldBuilder.create().text("0").prefWidth(50).build(); 146 147 Label lb2 = new Label("from index"); 148 final TextField tf2 = TextFieldBuilder.create().text("0").prefWidth(50).build(); 149 150 Button bt = ButtonBuilder.create().text("remove point!").id(REMOVE_BUTTON_ID).build(); 151 bt.setOnAction(new EventHandler() { 152 public void handle(Event t) { 153 int serie = Integer.parseInt(tf1.getText()); 154 int index = Integer.parseInt(tf2.getText()); 155 ((Series) testedScatterChart.getData().get(serie)).getData().remove(index); 156 } 157 }); 158 hb.getChildren().addAll(lb1, tf1, lb2, tf2, bt); 159 return hb; 160 } 161 162 public HBox getAddPointToSerieDialog() { 163 HBox hb = new HBox(); 164 Label lb1 = new Label("To serie"); 165 final TextField tf1 = TextFieldBuilder.create().text("0").prefWidth(50).build(); 166 167 Label lb2 = new Label("to index"); 168 final TextField tf2 = TextFieldBuilder.create().text("0").prefWidth(50).build(); 169 170 Label lb3 = new Label("X"); 171 final TextField tf3 = TextFieldBuilder.create().text("0").prefWidth(50).build(); 172 173 Label lb4 = new Label("Y"); 174 final TextField tf4 = TextFieldBuilder.create().text("0").prefWidth(50).build(); 175 176 Button bt = ButtonBuilder.create().text("add point!").id(REMOVE_BUTTON_ID).build(); 177 bt.setOnAction(new EventHandler() { 178 public void handle(Event t) { 179 int serie = Integer.parseInt(tf1.getText()); 180 int index = Integer.parseInt(tf2.getText()); 181 double X = Double.parseDouble(tf3.getText()); 182 double Y = Double.parseDouble(tf4.getText()); 183 184 XYChart.Data newData = new XYChart.Data(); 185 newData.setXValue(X); 186 newData.setYValue(Y); 187 188 ((Series) testedScatterChart.getData().get(serie)).getData().add(index, newData); 189 } 190 }); 191 hb.getChildren().addAll(lb1, tf1, lb2, tf2, lb3, tf3, lb4, tf4, bt); 192 return hb; 193 } 194 195 public HBox getAddItemHBox() { 196 HBox hb = new HBox(); 197 Label lb = new Label("Add series named "); 198 final TextField tf = TextFieldBuilder.create().prefWidth(50).id(ADDED_SERIES_NAME_TEXTFIELD_ID).build(); 199 200 Label minLabel = new Label(" min "); 201 final TextField minText = TextFieldBuilder.create().prefWidth(50).id(ADDED_SERIES_MIN_VALUE_TEXTFIELD_ID).build(); 202 203 Label maxLabel = new Label(" max "); 204 final TextField maxText = TextFieldBuilder.create().prefWidth(50).id(ADDED_SERIES_MAX_VALUE_TEXTFIELD_ID).build(); 205 206 Label amountLabel = new Label(" amount "); 207 final TextField amountText = TextFieldBuilder.create().prefWidth(50).id(ADDED_SERIES_DOTS_COUNT_TEXTFIELD_ID).build(); 208 209 Button bt = ButtonBuilder.create().text("Add!").id(ADD_SERIES_COMMAND_BUTTON_ID).build(); 210 bt.setOnAction(new EventHandler() { 211 public void handle(Event t) { 212 String serieName = tf.getText(); 213 double min = Double.parseDouble(minText.getText()); 214 double max = Double.parseDouble(maxText.getText()); 215 int amount = Integer.parseInt(amountText.getText()); 216 217 ObservableList list = FXCollections.observableArrayList(); 218 219 Series serie = new XYChart.Series(serieName, list); 220 221 for (int i = 0; i < amount; i++) { 222 Data newData = new Data(); 223 newData.setXValue(new Random().nextDouble() * (max - min) + min); 224 newData.setYValue(new Random().nextDouble() * (max - min) + min); 225 list.add(newData); 226 } 227 228 testedScatterChart.getData().add(serie); 229 pane.addPropertiesTable(serieName, getTableForProperty(serie, min, max).getVisualRepresentation()); 230 } 231 }); 232 hb.getChildren().addAll(lb, tf, minLabel, minText, maxLabel, maxText, amountLabel, amountText, bt); 233 return hb; 234 } 235 236 protected PropertiesTable getTableForProperty(Series serie, double min, double max) { 237 PropertiesTable table = new PropertiesTable(serie); 238 239 table.addSimpleListener(serie.chartProperty(), serie); 240 table.addSimpleListener(serie.nameProperty(), serie); 241 table.addSimpleListener(serie.dataProperty(), serie); 242 243 for (Iterator it = serie.getData().iterator(); it.hasNext();) { 244 final Data data = (Data) it.next(); 245 246 final DoubleProperty intermediateX = new SimpleDoubleProperty(null, "XValue"); 247 final DoubleProperty intermediateY = new SimpleDoubleProperty(null, "YValue"); 248 249 data.XValueProperty().addListener(new ChangeListener() { 250 public void changed(ObservableValue ov, Object t, Object t1) { 251 intermediateX.setValue((Double) data.XValueProperty().getValue()); 252 } 253 }); 254 255 data.YValueProperty().addListener(new ChangeListener() { 256 public void changed(ObservableValue ov, Object t, Object t1) { 257 intermediateY.setValue((Double) data.YValueProperty().getValue()); 258 } 259 }); 260 261 intermediateX.addListener(new ChangeListener() { 262 public void changed(ObservableValue ov, Object t, Object t1) { 263 data.setXValue(t1); 264 } 265 }); 266 267 intermediateY.addListener(new ChangeListener() { 268 public void changed(ObservableValue ov, Object t, Object t1) { 269 data.setYValue(t1); 270 } 271 }); 272 273 table.addDoublePropertyLine(intermediateX, min, max, (Double) data.getXValue(), data); 274 table.addDoublePropertyLine(intermediateY, min, max, (Double) data.getYValue(), data); 275 } 276 277 return table; 278 } 279 280 public ScatterChart getNewChart() { 281 ScatterChart chart = new ScatterChart(axis1, axis2); 282 chart.setTitle("ScatterChart"); 283 chart.setStyle("-fx-border-color: darkgray;"); 284 return chart; 285 } 286 } 287 }