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.BubbleChart;
  40 import javafx.scene.chart.NumberAxis;
  41 import javafx.scene.chart.XYChart;
  42 import javafx.scene.chart.XYChart.Series;
  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 NewBubbleChartApp 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(NewBubbleChartApp.class, args);
  66     }
  67 
  68     @Override
  69     protected Scene getScene() {
  70         Utils.setTitleToStage(stage, "BubbleChartTestApp");
  71         return new NewBubbleChartApp.BubbleChartScene();
  72     }
  73 
  74     class BubbleChartScene extends CommonPropertiesScene {
  75 
  76         //BubbleChart to be tested.
  77         BubbleChart testedBubbleChart;
  78         NumberAxis axis1;
  79         NumberAxis axis2;
  80         TabPaneWithControl pane;
  81 
  82         public BubbleChartScene() {
  83             super("BubbleChart", 1300, 800);
  84         }
  85 
  86         @Override
  87         final protected void prepareScene() {
  88             Utils.addBrowser(this);
  89 
  90             axis1 = new NumberAxis(0, 100, 10);
  91             axis2 = new NumberAxis(0, 100, 10);
  92             testedBubbleChart = getNewChart();
  93             testedBubbleChart.setId(TESTED_CHART_ID);
  94 
  95             Button hardResetButton = ButtonBuilder.create().id(HARD_RESET_BUTTON_ID).text("Hard reset").build();

  96             hardResetButton.setOnAction(new EventHandler<ActionEvent>() {
  97                 public void handle(ActionEvent t) {
  98                     HBox hb = (HBox) getRoot();
  99                     hb.getChildren().clear();
 100                     prepareMainSceneStructure();
 101                     prepareScene();
 102                 }
 103             });
 104 
 105             Button softResetButton = ButtonBuilder.create().id(SOFT_RESET_BUTTON_ID).text("Soft reset").build();

 106             softResetButton.setOnAction(new EventHandler<ActionEvent>() {
 107                 public void handle(ActionEvent t) {
 108                     //throw new UnsupportedOperationException("Not supported yet.");
 109                 }
 110             });
 111 
 112             HBox resetButtonsHBox = new HBox();
 113             resetButtonsHBox.getChildren().addAll(hardResetButton, softResetButton);
 114 
 115             VBox vb = new VBox(5);
 116             vb.getChildren().addAll(resetButtonsHBox, getAddItemHBox(), getAddBubbleToSerieDialog(), getRemoveDataDialog(), getRemoveDataFromSerieDialog());
 117 
 118             pane = getPaneFor(testedBubbleChart, CHART_TAB_NAME, axis1, AXIS_1_TAB_NAME, axis2, AXIS_2_TAB_NAME);
 119 
 120             setTestedControlContainerSize(500, 500);
 121             setTestedControl(testedBubbleChart);
 122             setPropertiesContent(pane);
 123             setControllersContent(vb);
 124         }
 125 
 126         public HBox getRemoveDataDialog() {
 127             HBox hb = new HBox();
 128             Label lb = new Label("From position");
 129             final TextField tf = TextFieldBuilder.create().text("0").prefWidth(50).id(REMOVE_ITEM_POS_TEXT_FIELD_ID).build();
 130             Button bt = ButtonBuilder.create().text("Remove serie!").id(REMOVE_BUTTON_ID).build();



 131             bt.setOnAction(new EventHandler() {
 132                 public void handle(Event t) {
 133                     int index = Integer.parseInt(tf.getText());
 134                     testedBubbleChart.getData().remove(index);
 135                 }
 136             });
 137             hb.getChildren().addAll(lb, tf, bt);
 138             return hb;
 139         }
 140 
 141         public HBox getRemoveDataFromSerieDialog() {
 142             HBox hb = new HBox();
 143             Label lb1 = new Label("From serie");
 144             final TextField tf1 = TextFieldBuilder.create().text("0").prefWidth(50).build();

 145 
 146             Label lb2 = new Label("from index");
 147             final TextField tf2 = TextFieldBuilder.create().text("0").prefWidth(50).build();

 148 
 149             Button bt = ButtonBuilder.create().text("remove bubble!").id(REMOVE_BUTTON_ID).build();

 150             bt.setOnAction(new EventHandler() {
 151                 public void handle(Event t) {
 152                     int serie = Integer.parseInt(tf1.getText());
 153                     int index = Integer.parseInt(tf2.getText());
 154                     ((Series) testedBubbleChart.getData().get(serie)).getData().remove(index);
 155                 }
 156             });
 157             hb.getChildren().addAll(lb1, tf1, lb2, tf2, bt);
 158             return hb;
 159         }
 160 
 161         public HBox getAddBubbleToSerieDialog() {
 162             HBox hb = new HBox();
 163             Label lb1 = new Label("To serie");
 164             final TextField tf1 = TextFieldBuilder.create().text("0").prefWidth(50).build();

 165 
 166             Label lb2 = new Label("to index");
 167             final TextField tf2 = TextFieldBuilder.create().text("0").prefWidth(50).build();

 168 
 169             Label lb3 = new Label("X");
 170             final TextField tf3 = TextFieldBuilder.create().text("0").prefWidth(50).build();

 171 
 172             Label lb4 = new Label("Y");
 173             final TextField tf4 = TextFieldBuilder.create().text("0").prefWidth(50).build();

 174 
 175             Label lb5 = new Label("R");
 176             final TextField tf5 = TextFieldBuilder.create().text("0").prefWidth(50).build();

 177 
 178             Button bt = ButtonBuilder.create().text("add bubble!").id(REMOVE_BUTTON_ID).build();

 179             bt.setOnAction(new EventHandler() {
 180                 public void handle(Event t) {
 181                     int serie = Integer.parseInt(tf1.getText());
 182                     int index = Integer.parseInt(tf2.getText());
 183                     double X = Double.parseDouble(tf3.getText());
 184                     double Y = Double.parseDouble(tf4.getText());
 185                     double R = Double.parseDouble(tf5.getText());
 186 
 187                     XYChart.Data newData = new XYChart.Data();
 188                     newData.setXValue(X);
 189                     newData.setYValue(Y);
 190                     newData.setExtraValue(R);
 191 
 192                     ((Series) testedBubbleChart.getData().get(serie)).getData().add(index, newData);
 193                 }
 194             });
 195             hb.getChildren().addAll(lb1, tf1, lb2, tf2, lb3, tf3, lb4, tf4, lb5, tf5, bt);
 196             return hb;
 197         }
 198 
 199         public HBox getAddItemHBox() {
 200             HBox hb = new HBox();
 201             Label lb = new Label("Add series named ");
 202             final TextField tf = TextFieldBuilder.create().prefWidth(50).id(ADDED_SERIES_NAME_TEXTFIELD_ID).build();


 203 
 204             Label minLabel = new Label(" min X,Y ");
 205             final TextField minText = TextFieldBuilder.create().prefWidth(50).id(ADDED_SERIES_MIN_VALUE_TEXTFIELD_ID).build();


 206 
 207             Label maxLabel = new Label(" max X,Y ");
 208             final TextField maxText = TextFieldBuilder.create().prefWidth(50).id(ADDED_SERIES_MAX_VALUE_TEXTFIELD_ID).build();


 209 
 210             Label amountLabel = new Label(" add ");
 211             final TextField amountText = TextFieldBuilder.create().prefWidth(50).id(ADDED_SERIES_DOTS_COUNT_TEXTFIELD_ID).build();


 212 
 213             Button bt = ButtonBuilder.create().text(" bubbles!").id(ADD_SERIES_COMMAND_BUTTON_ID).build();

 214             bt.setOnAction(new EventHandler() {
 215                 public void handle(Event t) {
 216                     String serieName = tf.getText();
 217                     double min = Double.parseDouble(minText.getText());
 218                     double max = Double.parseDouble(maxText.getText());
 219                     int amount = Integer.parseInt(amountText.getText());
 220 
 221                     ObservableList list = FXCollections.observableArrayList();
 222 
 223                     XYChart.Series serie = new XYChart.Series(serieName, list);
 224 
 225                     for (int i = 0; i < amount; i++) {
 226                         XYChart.Data newData = new XYChart.Data();
 227                         newData.setXValue(new Random().nextDouble() * (max - min) + min);
 228                         newData.setYValue(new Random().nextDouble() * (max - min) + min);
 229                         newData.setExtraValue(new Random().nextDouble() * (max - min) / 8 + min);
 230                         list.add(newData);
 231                     }
 232 
 233                     testedBubbleChart.getData().add(serie);
 234                     pane.addPropertiesTable(serieName, getTableForProperty(serie, min, max).getVisualRepresentation());
 235                 }
 236             });
 237             hb.getChildren().addAll(lb, tf, minLabel, minText, maxLabel, maxText, amountLabel, amountText, bt);
 238             return hb;
 239         }
 240 
 241         protected PropertiesTable getTableForProperty(XYChart.Series serie, double min, double max) {
 242             PropertiesTable table = new PropertiesTable(serie);
 243 
 244             table.addSimpleListener(serie.chartProperty(), serie);
 245             table.addSimpleListener(serie.nameProperty(), serie);
 246             table.addSimpleListener(serie.dataProperty(), serie);
 247 
 248             for (Iterator it = serie.getData().iterator(); it.hasNext();) {
 249                 final XYChart.Data data = (XYChart.Data) it.next();
 250 
 251                 final DoubleProperty intermediateX = new SimpleDoubleProperty(null, "XValue");
 252                 final DoubleProperty intermediateY = new SimpleDoubleProperty(null, "YValue");
 253                 final DoubleProperty intermediateExtra = new SimpleDoubleProperty(null, "ExtraValue");
 254 
 255                 data.XValueProperty().addListener(new ChangeListener() {
 256                     public void changed(ObservableValue ov, Object t, Object t1) {
 257                         if (!intermediateX.isBound()) {
 258                             intermediateX.setValue((Double) data.XValueProperty().getValue());
 259                         }
 260                     }
 261                 });
 262 
 263                 data.YValueProperty().addListener(new ChangeListener() {
 264                     public void changed(ObservableValue ov, Object t, Object t1) {
 265                         if (!intermediateY.isBound()) {
 266                             intermediateY.setValue((Double) data.YValueProperty().getValue());
 267                         }
 268                     }
 269                 });
 270 
 271                 data.extraValueProperty().addListener(new ChangeListener() {
 272                     public void changed(ObservableValue ov, Object t, Object t1) {
 273                         if (!intermediateExtra.isBound()) {
 274                             intermediateExtra.setValue((Double) data.extraValueProperty().getValue());
 275                         }
 276                     }
 277                 });
 278 
 279                 intermediateX.addListener(new ChangeListener() {
 280                     public void changed(ObservableValue ov, Object t, Object t1) {
 281                         data.setXValue(t1);
 282                     }
 283                 });
 284 
 285                 intermediateY.addListener(new ChangeListener() {
 286                     public void changed(ObservableValue ov, Object t, Object t1) {
 287                         data.setYValue(t1);
 288                     }
 289                 });
 290 
 291                 intermediateExtra.addListener(new ChangeListener() {
 292                     public void changed(ObservableValue ov, Object t, Object t1) {
 293                         data.setExtraValue(t1);
 294                     }
 295                 });
 296 
 297                 table.addDoublePropertyLine(intermediateX, min, max, (Double) data.getXValue(), data);
 298                 table.addDoublePropertyLine(intermediateY, min, max, (Double) data.getYValue(), data);
 299                 table.addDoublePropertyLine(intermediateExtra, min, max, (Double) data.getYValue(), data);
 300             }
 301 
 302             return table;
 303         }
 304 
 305         public BubbleChart getNewChart() {
 306             BubbleChart chart = new BubbleChart(axis1, axis2);
 307             chart.setTitle("BubbleChart");
 308             chart.setStyle("-fx-border-color: darkgray;");
 309             return chart;
 310         }
 311     }
 312 }
--- EOF ---