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