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 = new Button("Hard reset");
  97             hardResetButton.setId(HARD_RESET_BUTTON_ID);
  98             hardResetButton.setOnAction(new EventHandler<ActionEvent>() {
  99                 public void handle(ActionEvent t) {
 100                     HBox hb = (HBox) getRoot();
 101                     hb.getChildren().clear();
 102                     prepareMainSceneStructure();
 103                     prepareScene();
 104                 }
 105             });
 106 
 107             Button softResetButton = new Button("Soft reset");
 108             softResetButton.setId(SOFT_RESET_BUTTON_ID);
 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(), getAddPointToSerieDialog(), getRemoveDataFromSerieDialog(), getRemoveDataDialog());
 120 
 121             pane = getPaneFor(testedScatterChart, CHART_TAB_NAME, axis1, AXIS_1_TAB_NAME, axis2, AXIS_2_TAB_NAME);
 122 
 123             setTestedControlContainerSize(500, 500);
 124             setTestedControl(testedScatterChart);
 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 = new TextField("0");
 133             tf.setPrefWidth(50);
 134             tf.setId(REMOVE_ITEM_POS_TEXT_FIELD_ID);
 135             Button bt = new Button("Remove!");
 136             bt.setId(REMOVE_BUTTON_ID);
 137             bt.setOnAction(new EventHandler() {
 138                 public void handle(Event t) {
 139                     int index = Integer.parseInt(tf.getText());
 140                     testedScatterChart.getData().remove(index);
 141                 }
 142             });
 143             hb.getChildren().addAll(lb, tf, bt);
 144             return hb;
 145         }
 146 
 147         public HBox getRemoveDataFromSerieDialog() {
 148             HBox hb = new HBox();
 149             Label lb1 = new Label("From serie");
 150             final TextField tf1 = new TextField("0");
 151             tf1.setPrefWidth(50);
 152 
 153             Label lb2 = new Label("from index");
 154             final TextField tf2 = new TextField("0");
 155             tf2.setPrefWidth(50);
 156 
 157             Button bt = new Button("remove point!");
 158             bt.setId(REMOVE_BUTTON_ID);
 159             bt.setOnAction(new EventHandler() {
 160                 public void handle(Event t) {
 161                     int serie = Integer.parseInt(tf1.getText());
 162                     int index = Integer.parseInt(tf2.getText());
 163                     ((Series) testedScatterChart.getData().get(serie)).getData().remove(index);
 164                 }
 165             });
 166             hb.getChildren().addAll(lb1, tf1, lb2, tf2, bt);
 167             return hb;
 168         }
 169 
 170         public HBox getAddPointToSerieDialog() {
 171             HBox hb = new HBox();
 172             Label lb1 = new Label("To serie");
 173             final TextField tf1 = new TextField("0");
 174             tf1.setPrefWidth(50);
 175 
 176             Label lb2 = new Label("to index");
 177             final TextField tf2 = new TextField("0");
 178             tf2.setPrefWidth(50);
 179 
 180             Label lb3 = new Label("X");
 181             final TextField tf3 = new TextField("0");
 182             tf3.setPrefWidth(50);
 183 
 184             Label lb4 = new Label("Y");
 185             final TextField tf4 = new TextField("0");
 186             tf4.setPrefWidth(50);
 187 
 188             Button bt = new Button("add point!");
 189             bt.setId(REMOVE_BUTTON_ID);
 190             bt.setOnAction(new EventHandler() {
 191                 public void handle(Event t) {
 192                     int serie = Integer.parseInt(tf1.getText());
 193                     int index = Integer.parseInt(tf2.getText());
 194                     double X = Double.parseDouble(tf3.getText());
 195                     double Y = Double.parseDouble(tf4.getText());
 196 
 197                     XYChart.Data newData = new XYChart.Data();
 198                     newData.setXValue(X);
 199                     newData.setYValue(Y);
 200 
 201                     ((Series) testedScatterChart.getData().get(serie)).getData().add(index, newData);
 202                 }
 203             });
 204             hb.getChildren().addAll(lb1, tf1, lb2, tf2, lb3, tf3, lb4, tf4, bt);
 205             return hb;
 206         }
 207 
 208         public HBox getAddItemHBox() {
 209             HBox hb = new HBox();
 210             Label lb = new Label("Add series named ");
 211             final TextField tf = new TextField();
 212             tf.setPrefWidth(50);
 213             tf.setId(ADDED_SERIES_NAME_TEXTFIELD_ID);
 214 
 215             Label minLabel = new Label(" min ");
 216             final TextField minText = new TextField();
 217             minText.setPrefWidth(50);
 218             minText.setId(ADDED_SERIES_MIN_VALUE_TEXTFIELD_ID);
 219 
 220             Label maxLabel = new Label(" max ");
 221             final TextField maxText = new TextField();
 222             maxText.setPrefWidth(50);
 223             maxText.setId(ADDED_SERIES_MAX_VALUE_TEXTFIELD_ID);
 224 
 225             Label amountLabel = new Label(" amount ");
 226             final TextField amountText = new TextField();
 227             amountText.setPrefWidth(50);
 228             amountText.setId(ADDED_SERIES_DOTS_COUNT_TEXTFIELD_ID);
 229 
 230             Button bt = new Button("Add!");
 231             bt.setId(ADD_SERIES_COMMAND_BUTTON_ID);
 232             bt.setOnAction(new EventHandler() {
 233                 public void handle(Event t) {
 234                     String serieName = tf.getText();
 235                     double min = Double.parseDouble(minText.getText());
 236                     double max = Double.parseDouble(maxText.getText());
 237                     int amount = Integer.parseInt(amountText.getText());
 238 
 239                     ObservableList list = FXCollections.observableArrayList();
 240 
 241                     Series serie = new XYChart.Series(serieName, list);
 242 
 243                     for (int i = 0; i < amount; i++) {
 244                         Data newData = new Data();
 245                         newData.setXValue(new Random().nextDouble() * (max - min) + min);
 246                         newData.setYValue(new Random().nextDouble() * (max - min) + min);
 247                         list.add(newData);
 248                     }
 249 
 250                     testedScatterChart.getData().add(serie);
 251                     pane.addPropertiesTable(serieName, getTableForProperty(serie, min, max).getVisualRepresentation());
 252                 }
 253             });
 254             hb.getChildren().addAll(lb, tf, minLabel, minText, maxLabel, maxText, amountLabel, amountText, bt);
 255             return hb;
 256         }
 257 
 258         protected PropertiesTable getTableForProperty(Series serie, double min, double max) {
 259             PropertiesTable table = new PropertiesTable(serie);
 260 
 261             table.addSimpleListener(serie.chartProperty(), serie);
 262             table.addSimpleListener(serie.nameProperty(), serie);
 263             table.addSimpleListener(serie.dataProperty(), serie);
 264 
 265             for (Iterator it = serie.getData().iterator(); it.hasNext();) {
 266                 final Data data = (Data) it.next();
 267 
 268                 final DoubleProperty intermediateX = new SimpleDoubleProperty(null, "XValue");
 269                 final DoubleProperty intermediateY = new SimpleDoubleProperty(null, "YValue");
 270 
 271                 data.XValueProperty().addListener(new ChangeListener() {
 272                     public void changed(ObservableValue ov, Object t, Object t1) {
 273                         intermediateX.setValue((Double) data.XValueProperty().getValue());
 274                     }
 275                 });
 276 
 277                 data.YValueProperty().addListener(new ChangeListener() {
 278                     public void changed(ObservableValue ov, Object t, Object t1) {
 279                         intermediateY.setValue((Double) data.YValueProperty().getValue());
 280                     }
 281                 });
 282 
 283                 intermediateX.addListener(new ChangeListener() {
 284                     public void changed(ObservableValue ov, Object t, Object t1) {
 285                         data.setXValue(t1);
 286                     }
 287                 });
 288 
 289                 intermediateY.addListener(new ChangeListener() {
 290                     public void changed(ObservableValue ov, Object t, Object t1) {
 291                         data.setYValue(t1);
 292                     }
 293                 });
 294 
 295                 table.addDoublePropertyLine(intermediateX, min, max, (Double) data.getXValue(), data);
 296                 table.addDoublePropertyLine(intermediateY, min, max, (Double) data.getYValue(), data);
 297             }
 298 
 299             return table;
 300         }
 301 
 302         public ScatterChart getNewChart() {
 303             ScatterChart chart = new ScatterChart(axis1, axis2);
 304             chart.setTitle("ScatterChart");
 305             chart.setStyle("-fx-border-color: darkgray;");
 306             return chart;
 307         }
 308     }
 309 }