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