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.manual;
  26 
  27 import javafx.scene.control.test.utils.ptables.WeakPropertyValueController;
  28 import java.util.Arrays;
  29 import javafx.collections.FXCollections;
  30 import javafx.collections.ObservableList;
  31 import javafx.event.ActionEvent;
  32 import javafx.event.EventHandler;
  33 import javafx.scene.chart.PieChart;
  34 import javafx.scene.Scene;
  35 import javafx.scene.control.*;
  36 import javafx.scene.control.test.utils.CommonPropertiesScene;
  37 import javafx.geometry.Side;
  38 import javafx.scene.layout.HBox;
  39 import test.javaclient.shared.InteroperabilityApp;
  40 import test.javaclient.shared.Utils;
  41 import javafx.scene.layout.VBox;
  42 import javafx.geometry.Orientation;
  43 
  44 /**
  45  * @author Alexander Kirov
  46  */
  47 public class PieChartManualApp extends InteroperabilityApp {
  48 
  49     public static void main(String[] args) {
  50         Utils.launch(PieChartManualApp.class, args);
  51     }
  52 
  53     @Override
  54     protected Scene getScene() {
  55         Utils.setTitleToStage(stage, "PieChartTestApp");
  56         return new PieChartScene();
  57     }
  58 
  59     class PieChartScene extends CommonPropertiesScene {
  60 
  61         //PieChart to be tested.
  62         PieChart testedPieChart;
  63         int controlContainerWidth = 400;
  64         int controlContainerHeight = 400;
  65         ObservableList<PieChart.Data> data;
  66         VBox properties;
  67 
  68         public PieChartScene() {
  69             super("PieChart", 900, 630);
  70 
  71             prepareScene();
  72         }
  73 
  74         @Override
  75         final protected void prepareScene() {
  76             testedPieChart = getNewChart();
  77             testedPieChart.setPrefSize(controlContainerHeight, controlContainerWidth);
  78             properties = new VBox(10);
  79 
  80             Button addDataButton = new Button("Add data at index 0");
  81             addDataButton.setOnAction(new EventHandler<ActionEvent>() {
  82                 public void handle(ActionEvent t) {
  83                     testedPieChart.getData().add(0, new PieChart.Data("Added data", Math.random() * 100));
  84                 }
  85             });
  86 
  87             Button removeDataButton = new Button("Remove data from index 0");
  88             removeDataButton.setOnAction(new EventHandler<ActionEvent>() {
  89                 public void handle(ActionEvent t) {
  90                     if (testedPieChart.getData().size() > 0) {
  91                         testedPieChart.getData().remove(0);
  92                     }
  93                 }
  94             });
  95 
  96             Button changeDataButton = new Button("Change data at index 0");
  97             changeDataButton.setOnAction(new EventHandler<ActionEvent>() {
  98                 public void handle(ActionEvent t) {
  99                     if (testedPieChart.getData().size() > 0) {
 100                         PieChart.Data data = testedPieChart.getData().get(0);
 101                         data.setPieValue(Math.max(0, data.getPieValue() + (Math.random() - 0.5) * 200));
 102                     }
 103                 }
 104             });
 105 
 106             Button hardResetButton = new Button("Reset");
 107             hardResetButton.setOnAction(new EventHandler<ActionEvent>() {
 108                 public void handle(ActionEvent t) {
 109                     HBox hb = (HBox) getRoot();
 110                     hb.getChildren().clear();
 111                     prepareMainSceneStructure();
 112                     prepareScene();
 113                 }
 114             });
 115 
 116             VBox vb = new VBox(5);
 117             vb.getChildren().addAll(hardResetButton,
 118                     new Separator(Orientation.HORIZONTAL), new Label("Add new data : "), addDataButton,
 119                     new Separator(Orientation.HORIZONTAL), new Label("Remove existing data : "), removeDataButton,
 120                     new Separator(Orientation.HORIZONTAL), new Label("Change existing data : "), changeDataButton);
 121 
 122             addController(new WeakPropertyValueController(testedPieChart.animatedProperty(), testedPieChart));
 123             addController(new WeakPropertyValueController(testedPieChart.clockwiseProperty(), testedPieChart));
 124             addController(new WeakPropertyValueController(testedPieChart.legendVisibleProperty(), testedPieChart));
 125             addController(new WeakPropertyValueController(testedPieChart.startAngleProperty(), testedPieChart, -300.0, 0.0, 300.0));
 126             addController(new WeakPropertyValueController(testedPieChart.labelLineLengthProperty(), testedPieChart, -20.0, 10.0, 30.0));
 127             addController(new WeakPropertyValueController(testedPieChart.labelsVisibleProperty(), testedPieChart));
 128             addController(new WeakPropertyValueController(testedPieChart.titleProperty(), testedPieChart, "New title"));
 129             addController(new WeakPropertyValueController(testedPieChart.titleSideProperty(), testedPieChart, Arrays.asList(Side.values())));
 130             addController(new WeakPropertyValueController(testedPieChart.legendSideProperty(), testedPieChart, Arrays.asList(Side.values())));
 131             addController(new WeakPropertyValueController(testedPieChart.prefWidthProperty(), testedPieChart, 100.0, 400.0, 400.0));
 132             addController(new WeakPropertyValueController(testedPieChart.prefHeightProperty(), testedPieChart, 100.0, 400.0, 400.0));
 133 
 134             setTestedControlContainerSize(400, 400);
 135             setTestedControl(testedPieChart);
 136             setControllersContent(vb);
 137             setPropertiesContent(properties);
 138         }
 139 
 140         private void addController(WeakPropertyValueController controller) {
 141             properties.getChildren().addAll(new Separator(Orientation.HORIZONTAL), controller);
 142         }
 143 
 144         public PieChart getNewChart() {
 145             data = FXCollections.<PieChart.Data>observableArrayList();
 146             for (int i = 0; i < 4; i++) {
 147                 data.add(new PieChart.Data("Data item " + i, 50));
 148             }
 149             PieChart chart = new PieChart(data);
 150             chart.setTitle("PieChart");
 151             chart.setStyle("-fx-border-color: darkgray;");
 152             return chart;
 153         }
 154     }
 155 }