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.Arrays;
  28 import java.util.Comparator;
  29 import java.util.Iterator;
  30 import java.util.Random;
  31 import javafx.beans.property.DoubleProperty;
  32 import javafx.beans.property.SimpleDoubleProperty;
  33 import javafx.beans.value.ChangeListener;
  34 import javafx.beans.value.ObservableValue;
  35 import javafx.collections.FXCollections;
  36 import javafx.collections.ObservableList;
  37 import javafx.event.ActionEvent;
  38 import javafx.event.Event;
  39 import javafx.event.EventHandler;
  40 import javafx.scene.Scene;
  41 import javafx.scene.chart.NumberAxis;
  42 import javafx.scene.chart.StackedAreaChart;
  43 import javafx.scene.chart.XYChart;
  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 NewStackedAreaChartApp extends InteroperabilityApp implements ChartIDsInterface {
  58 
  59     public static void main(String[] args) {
  60         Utils.launch(NewStackedAreaChartApp.class, args);
  61     }
  62 
  63     @Override
  64     protected Scene getScene() {
  65         Utils.setTitleToStage(stage, "StackedAreaChartTestApp");
  66         return new NewStackedAreaChartApp.AreaChartScene();
  67     }
  68 
  69     class AreaChartScene extends CommonPropertiesScene {
  70 
  71         //AreaChart to be tested.
  72         StackedAreaChart testedStackedAreaChart;
  73         NumberAxis axis1;
  74         NumberAxis axis2;
  75         TabPaneWithControl pane;
  76 
  77         public AreaChartScene() {
  78             super("StackedAreaChartApp", 1300, 800);
  79         }
  80 
  81         @Override
  82         final protected void prepareScene() {
  83             Utils.addBrowser(this);
  84 
  85             axis1 = new NumberAxis(0, 100, 10);
  86             axis2 = new NumberAxis(0, 100, 10);
  87             testedStackedAreaChart = getNewChart();
  88             testedStackedAreaChart.setId(TESTED_CHART_ID);
  89 
  90             Button hardResetButton = new Button("Hard reset");
  91             hardResetButton.setId(HARD_RESET_BUTTON_ID);
  92             hardResetButton.setOnAction(new EventHandler<ActionEvent>() {
  93                 public void handle(ActionEvent t) {
  94                     HBox hb = (HBox) getRoot();
  95                     hb.getChildren().clear();
  96                     prepareMainSceneStructure();
  97                     prepareScene();
  98                 }
  99             });
 100 
 101             Button softResetButton = new Button("Soft reset");
 102             softResetButton.setId(SOFT_RESET_BUTTON_ID);
 103             softResetButton.setOnAction(new EventHandler<ActionEvent>() {
 104                 public void handle(ActionEvent t) {
 105                     //throw new UnsupportedOperationException("Not supported yet.");
 106                 }
 107             });
 108 
 109             HBox resetButtonsHBox = new HBox();
 110             resetButtonsHBox.getChildren().addAll(hardResetButton, softResetButton);
 111 
 112             VBox vb = new VBox(5);
 113             vb.getChildren().addAll(resetButtonsHBox, getAddItemHBox(), getRemoveDataDialog());
 114             
 115             pane = getPaneFor(testedStackedAreaChart, CHART_TAB_NAME, axis1, AXIS_1_TAB_NAME, axis2, AXIS_2_TAB_NAME);
 116 
 117             setTestedControlContainerSize(500, 500);
 118             setTestedControl(testedStackedAreaChart);
 119             setPropertiesContent(pane);
 120             setControllersContent(vb);
 121         }
 122 
 123         public HBox getRemoveDataDialog() {
 124             HBox hb = new HBox();
 125             Label lb = new Label("From position");
 126             final TextField tf = new TextField("0");
 127             tf.setPrefWidth(50);
 128             tf.setId(REMOVE_AREA_INDEX_TEXTFIELD_ID);
 129             Button bt = new Button("Remove!");
 130             bt.setId(REMOVE_AREA_BUTTON_ID);
 131             bt.setOnAction(new EventHandler() {
 132                 public void handle(Event t) {
 133                     int index = Integer.parseInt(tf.getText());
 134                     testedStackedAreaChart.getData().remove(index);
 135                 }
 136             });
 137             hb.getChildren().addAll(lb, tf, bt);
 138             return hb;
 139         }
 140 
 141         public HBox getAddItemHBox() {
 142             HBox hb = new HBox();
 143             Label lb = new Label("Add series named ");
 144             final TextField tf = new TextField();
 145             tf.setPrefWidth(50);
 146             tf.setId(ADDED_SERIES_NAME_TEXTFIELD_ID);
 147 
 148             Label minLabel = new Label(" min ");
 149             final TextField minText = new TextField();
 150             minText.setPrefWidth(50);
 151             minText.setId(ADDED_SERIES_MIN_VALUE_TEXTFIELD_ID);
 152 
 153             Label maxLabel = new Label(" max ");
 154             final TextField maxText = new TextField();
 155             maxText.setPrefWidth(50);
 156             maxText.setId(ADDED_SERIES_MAX_VALUE_TEXTFIELD_ID);
 157 
 158             Label amountLabel = new Label(" amount ");
 159             final TextField amountText = new TextField();
 160             amountText.setPrefWidth(50);
 161             amountText.setId(ADDED_SERIES_DOTS_COUNT_TEXTFIELD_ID);
 162 
 163             Button bt = new Button("Add!");
 164             bt.setId(ADD_SERIES_COMMAND_BUTTON_ID);
 165             bt.setOnAction(new EventHandler() {
 166                 public void handle(Event t) {
 167                     String serieName = tf.getText();
 168                     double min = Double.parseDouble(minText.getText());
 169                     double max = Double.parseDouble(maxText.getText());
 170                     int amount = Integer.parseInt(amountText.getText());
 171 
 172                     ObservableList list = FXCollections.observableArrayList();
 173 
 174                     for (int i = 0; i < amount; i++) {
 175                         XYChart.Data newData = new XYChart.Data();
 176                         newData.setXValue(new Random().nextDouble() * (max - min) + min);
 177                         newData.setYValue(new Random().nextDouble() * (max - min) / 4 + min);
 178                         list.add(newData);
 179                     }
 180                     Object[] array = list.toArray();
 181                     Arrays.sort(array, new Comparator() {
 182                         public int compare(Object t, Object t1) {
 183                             return (int) Math.round(((Double) ((XYChart.Data) t).getXValue()) - ((Double) ((XYChart.Data) t1).getXValue()));
 184                         }
 185                     });
 186 
 187                     XYChart.Series serie = new XYChart.Series(serieName, FXCollections.observableArrayList(array));
 188 
 189                     testedStackedAreaChart.getData().add(serie);
 190                     pane.addPropertiesTable(serieName, getTableForProperty(serie, min, max).getVisualRepresentation());
 191                 }
 192             });
 193             hb.getChildren().addAll(lb, tf, minLabel, minText, maxLabel, maxText, amountLabel, amountText, bt);
 194             return hb;
 195         }
 196 
 197         protected PropertiesTable getTableForProperty(XYChart.Series serie, double min, double max) {
 198             PropertiesTable table = new PropertiesTable(serie);
 199 
 200             table.addSimpleListener(serie.chartProperty(), serie);
 201             table.addSimpleListener(serie.nameProperty(), serie);
 202             table.addSimpleListener(serie.dataProperty(), serie);
 203 
 204             for (Iterator it = serie.getData().iterator(); it.hasNext();) {
 205                 final XYChart.Data data = (XYChart.Data) it.next();
 206 
 207                 final DoubleProperty intermediateX = new SimpleDoubleProperty(null, "XValue");
 208                 final DoubleProperty intermediateY = new SimpleDoubleProperty(null, "YValue");
 209 
 210                 data.XValueProperty().addListener(new ChangeListener() {
 211                     public void changed(ObservableValue ov, Object t, Object t1) {
 212                         intermediateX.setValue((Double) data.XValueProperty().getValue());
 213                     }
 214                 });
 215 
 216                 data.YValueProperty().addListener(new ChangeListener() {
 217                     public void changed(ObservableValue ov, Object t, Object t1) {
 218                         intermediateY.setValue((Double) data.YValueProperty().getValue());
 219                     }
 220                 });
 221 
 222                 intermediateX.addListener(new ChangeListener() {
 223                     public void changed(ObservableValue ov, Object t, Object t1) {
 224                         data.setXValue(t1);
 225                     }
 226                 });
 227 
 228                 intermediateY.addListener(new ChangeListener() {
 229                     public void changed(ObservableValue ov, Object t, Object t1) {
 230                         data.setYValue(t1);
 231                     }
 232                 });
 233 
 234                 table.addDoublePropertyLine(intermediateX, min, max, (Double) data.getXValue(), data);
 235                 table.addDoublePropertyLine(intermediateY, min, max, (Double) data.getYValue(), data);
 236             }
 237 
 238             return table;
 239         }
 240 
 241         public StackedAreaChart getNewChart() {
 242             StackedAreaChart chart = new StackedAreaChart(axis1, axis2);
 243             chart.setTitle("StackedAreaChart");
 244             chart.setStyle("-fx-border-color: darkgray;");
 245             return chart;
 246         }
 247     }
 248 }