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 javafx.collections.FXCollections;
  28 import javafx.collections.ObservableList;
  29 import javafx.event.ActionEvent;
  30 import javafx.event.Event;
  31 import javafx.event.EventHandler;
  32 import javafx.geometry.Insets;
  33 import javafx.scene.Scene;
  34 import javafx.scene.chart.PieChart;
  35 import javafx.scene.chart.PieChart.Data;
  36 import javafx.scene.control.*;
  37 import javafx.scene.control.test.utils.CommonPropertiesScene;
  38 import javafx.scene.control.test.utils.ptables.PropertiesTable;
  39 import javafx.scene.control.test.utils.ptables.PropertyTablesFactory;
  40 import javafx.scene.control.test.utils.ptables.SpecialTablePropertiesProvider;
  41 import javafx.scene.layout.HBox;
  42 import javafx.scene.layout.Pane;
  43 import javafx.scene.layout.VBox;
  44 import test.javaclient.shared.InteroperabilityApp;
  45 import test.javaclient.shared.Utils;
  46 
  47 /**
  48  * @author Alexander Kirov
  49  */
  50 public class NewPieChartApp extends InteroperabilityApp implements ChartIDsInterface {
  51 
  52     public final static String REMOVE_ITEM_POS_TEXT_FIELD_ID = "REMOVE_ITEM_POS_TEXT_FIELD_ID";
  53     public final static String REMOVE_BUTTON_ID = "REMOVE_BUTTON_ID";
  54     public final static String ADD_ITEM_VALUE_TEXT_FIELD_ID = "ADD_ITEM_VALUE_TEXT_FIELD_ID";
  55     public final static String ADD_ITEM_TEXT_FIELD_ID = "ADD_ITEM_TEXT_FIELD_ID";
  56     public final static String ADD_ITEM_POSITION_TEXT_FIELD_ID = "ADD_ITEM_POSITION_TEXT_FIELD_ID";
  57     public final static String ADD_ITEM_BUTTON_ID = "ADD_ITEM_BUTTON_ID";
  58 
  59     public static void main(String[] args) {
  60         Utils.launch(NewPieChartApp.class, args);
  61     }
  62 
  63     @Override
  64     protected Scene getScene() {
  65         Utils.setTitleToStage(stage, "PieChartTestApp");
  66         return new PieChartScene();
  67     }
  68 
  69     class PieChartScene extends CommonPropertiesScene {
  70 
  71         //VBox which contain tested PieChart.
  72         Pane pane;
  73         PropertiesTable tb;
  74         //PieChart to be tested.
  75         PieChart testedPieChart;
  76         double controlContainerWidth = 600;
  77         double controlContainerHeight = 600;
  78         ObservableList<Data> data;
  79 
  80         public PieChartScene() {
  81             super("PieChart", 1300, 800);
  82         }
  83 
  84         @Override
  85         final protected void prepareScene() {
  86             Utils.addBrowser(this);
  87             pane = new Pane();
  88             testedPieChart = getNewChart();
  89             testedPieChart.setId(TESTED_CHART_ID);
  90 
  91             tb = new PropertiesTable(testedPieChart);
  92             PropertyTablesFactory.explorePropertiesList(testedPieChart, tb);
  93             SpecialTablePropertiesProvider.provideForControl(testedPieChart, tb);
  94 
  95             pane.setMinSize(controlContainerWidth, controlContainerHeight);
  96             pane.setPrefSize(controlContainerWidth, controlContainerHeight);
  97             pane.setStyle("-fx-border-color : red;");
  98             pane.getChildren().add(testedPieChart);
  99 
 100             VBox vb = new VBox();
 101             vb.setSpacing(5);
 102 
 103             HBox hb = (HBox) getRoot();
 104             hb.setPadding(new Insets(5, 5, 5, 5));
 105             hb.setStyle("-fx-border-color : green;");
 106 
 107             Button hardResetButton = ButtonBuilder.create().id(HARD_RESET_BUTTON_ID).text("Hard reset").build();
 108             hardResetButton.setOnAction(new EventHandler<ActionEvent>() {
 109                 public void handle(ActionEvent t) {
 110                     HBox hb = (HBox) getRoot();
 111                     hb.getChildren().clear();
 112                     prepareMainSceneStructure();
 113                     prepareScene();
 114                 }
 115             });
 116 
 117             Button softResetButton = ButtonBuilder.create().id(SOFT_RESET_BUTTON_ID).text("Soft reset").build();
 118             softResetButton.setOnAction(new EventHandler<ActionEvent>() {
 119                 public void handle(ActionEvent t) {
 120                     throw new UnsupportedOperationException("Not supported yet.");
 121                 }
 122             });
 123 
 124             Button b = new Button("Add");
 125             b.setOnAction(new EventHandler() {
 126                 public void handle(Event t) {
 127                     for (Data data : testedPieChart.getData()) {
 128                         Label label = new Label("Label");
 129                         label.setLabelFor(data.getNode());
 130                     }
 131                 }
 132             });
 133 
 134             HBox resetButtonsHBox = new HBox();
 135             resetButtonsHBox.getChildren().addAll(hardResetButton, softResetButton, b);
 136             vb.getChildren().addAll(resetButtonsHBox, getAddItemHBox(), getRemoveDataDialog());
 137 
 138             ScrollPane sp = new ScrollPane();
 139             sp.setContent(tb);
 140             sp.setPannable(true);
 141             sp.setMinWidth(1000);
 142             sp.setMinHeight(800);
 143 
 144             setTestedControl(testedPieChart);
 145             setPropertiesContent(sp);
 146             setTestedControlContainerSize(500, 500);
 147             setControllersContent(vb);
 148         }
 149 
 150         public HBox getRemoveDataDialog() {
 151             HBox hb = new HBox();
 152             Label lb = new Label("From position");
 153             final TextField tf = TextFieldBuilder.create().text("0").prefWidth(50).id(REMOVE_ITEM_POS_TEXT_FIELD_ID).build();
 154             Button bt = ButtonBuilder.create().text("Remove!").id(REMOVE_BUTTON_ID).build();
 155             bt.setOnAction(new EventHandler() {
 156                 public void handle(Event t) {
 157                     int index = Integer.parseInt(tf.getText());
 158                     ((PieChart) testedPieChart).getData().remove(index);
 159                 }
 160             });
 161             hb.getChildren().addAll(lb, tf, bt);
 162             return hb;
 163         }
 164 
 165         public HBox getAddItemHBox() {
 166             HBox hb = new HBox();
 167             Label lb = new Label("Add item");
 168             final TextField tf = TextFieldBuilder.create().prefWidth(50).id(ADD_ITEM_VALUE_TEXT_FIELD_ID).build();
 169             Label namedLabel = new Label(" named ");
 170             final TextField name = TextFieldBuilder.create().prefWidth(50).id(ADD_ITEM_TEXT_FIELD_ID).build();
 171             Label atLb = new Label("at pos");
 172             final TextField tfPos = TextFieldBuilder.create().prefWidth(50).id(ADD_ITEM_POSITION_TEXT_FIELD_ID).build();
 173             Button bt = ButtonBuilder.create().text("Add!").id(ADD_ITEM_BUTTON_ID).build();
 174             bt.setOnAction(new EventHandler() {
 175                 public void handle(Event t) {
 176                     int index = Integer.parseInt(tfPos.getText());
 177 
 178                     Data newData = new Data("".equals(name.getText()) ? String.valueOf(index) : name.getText(), Double.parseDouble(tf.getText()));
 179 
 180                     ((PieChart) testedPieChart).getData().add(index, newData);
 181 
 182                     tb.addDoublePropertyLine(newData.pieValueProperty(), -10, 10000, 100, newData);
 183                 }
 184             });
 185             hb.getChildren().addAll(lb, tf, namedLabel, name, atLb, tfPos, bt);
 186             return hb;
 187         }
 188 
 189         public PieChart getNewChart() {
 190             data = FXCollections.<Data>observableArrayList();
 191             //Don't change numbers here.
 192             for (int i = 0; i < 4; i++) {
 193                 data.add(new Data("Data item " + i, 100));
 194             }
 195             PieChart chart = new PieChart(data);
 196             chart.setTitle("PieChart");
 197             chart.setStyle("-fx-border-color: darkgray;");
 198             return chart;
 199         }
 200     }
 201 }