1 /* 2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. 3 * All rights reserved. Use is subject to license terms. 4 * 5 * This file is available and licensed under the following license: 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * - Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * - Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the distribution. 16 * - Neither the name of Oracle Corporation nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 package com.oracle.javafx.scenebuilder.kit.fxom.sampledata; 34 35 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMCollection; 36 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMInstance; 37 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMObject; 38 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMProperty; 39 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMPropertyC; 40 import java.util.HashMap; 41 import java.util.Map; 42 import javafx.scene.chart.PieChart; 43 import javafx.scene.chart.XYChart; 44 import javafx.scene.control.ChoiceBox; 45 import javafx.scene.control.ComboBox; 46 import javafx.scene.control.ListView; 47 import javafx.scene.control.Spinner; 48 import javafx.scene.control.TableView; 49 import javafx.scene.control.TreeTableView; 50 import javafx.scene.control.TreeView; 51 52 /** 53 * 54 */ 55 public class SampleDataGenerator { 56 57 private final Map<FXOMObject, AbstractSampleData> sampleDataMap = new HashMap<>(); 58 59 public void assignSampleData(FXOMObject startObject) { 60 assert startObject != null; 61 62 final Object sceneGraphObject = startObject.getSceneGraphObject(); 63 final AbstractSampleData currentData = sampleDataMap.get(startObject); 64 final AbstractSampleData newData; 65 66 if (sceneGraphObject == null) { 67 // startObject is unresolved 68 newData = null; 69 } else { 70 final Class<?> sceneGraphClass = sceneGraphObject.getClass(); 71 if (sceneGraphClass == ChoiceBox.class) { 72 final ChoiceBox<?> choiceBox = (ChoiceBox<?>) sceneGraphObject; 73 if (choiceBox.getItems().isEmpty()) { 74 if (currentData instanceof ChoiceBoxSampleData) { 75 newData = currentData; 76 } else { 77 newData = new ChoiceBoxSampleData(); 78 } 79 } else { 80 newData = null; 81 } 82 } else if (sceneGraphClass == ComboBox.class) { 83 final ComboBox<?> comboBox = (ComboBox<?>) sceneGraphObject; 84 if (comboBox.getItems().isEmpty()) { 85 if (currentData instanceof ComboBoxSampleData) { 86 newData = currentData; 87 } else { 88 newData = new ComboBoxSampleData(); 89 } 90 } else { 91 newData = null; 92 } 93 } else if (sceneGraphClass == ListView.class) { 94 final ListView<?> listView = (ListView<?>) sceneGraphObject; 95 if (listView.getItems().isEmpty()) { 96 if (currentData instanceof ListViewSampleData) { 97 newData = currentData; 98 } else { 99 newData = new ListViewSampleData(); 100 } 101 } else { 102 newData = null; 103 } 104 } else if (sceneGraphClass == TreeView.class) { 105 final TreeView<?> treeView = (TreeView<?>) sceneGraphObject; 106 if (treeView.getRoot() == null) { 107 if (currentData instanceof TreeViewSampleData) { 108 newData = currentData; 109 } else { 110 newData = new TreeViewSampleData(); 111 } 112 } else { 113 newData = null; 114 } 115 } else if (sceneGraphClass == TableView.class) { 116 final TableView<?> treeView = (TableView<?>) sceneGraphObject; 117 if (TableViewSampleData.canApplyTo(treeView)) { 118 if (currentData instanceof TableViewSampleData) { 119 newData = currentData; 120 } else { 121 newData = new TableViewSampleData(); 122 } 123 } else { 124 newData = null; 125 } 126 } else if (sceneGraphClass == TreeTableView.class) { 127 final TreeTableView<?> treeTableView = (TreeTableView<?>) sceneGraphObject; 128 if (treeTableView.getRoot() == null) { 129 if (currentData instanceof TreeTableViewSampleData) { 130 newData = currentData; 131 } else { 132 newData = new TreeTableViewSampleData(); 133 } 134 } else { 135 newData = null; 136 } 137 } else if (sceneGraphClass == PieChart.class) { 138 final PieChart pieChart = (PieChart) sceneGraphObject; 139 if (pieChart.getData().isEmpty()) { 140 if (currentData instanceof PieChartSampleData) { 141 newData = currentData; 142 } else { 143 newData = new PieChartSampleData(); 144 } 145 } else { 146 newData = null; 147 } 148 } else if (sceneGraphClass == Spinner.class) { 149 final Spinner<?> spinner = (Spinner<?>) sceneGraphObject; 150 if (spinner.getValue() == null) { 151 if (currentData instanceof SpinnerSampleData) { 152 newData = currentData; 153 } else { 154 newData = new SpinnerSampleData(); 155 } 156 } else { 157 newData = null; 158 } 159 } else if (XYChartSampleData.isKnownXYChart(sceneGraphObject)) { 160 final XYChart<?,?> xyChart = (XYChart<?,?>) sceneGraphObject; 161 if (xyChart.getData().isEmpty()) { 162 if (currentData instanceof XYChartSampleData) { 163 newData = currentData; 164 } else { 165 newData = new XYChartSampleData(); 166 } 167 } else { 168 newData = null; 169 } 170 } else { 171 newData = null; 172 } 173 } 174 175 if (newData == null) { 176 if (currentData != null) { 177 sampleDataMap.remove(startObject); 178 } 179 } else { 180 newData.applyTo(sceneGraphObject); 181 sampleDataMap.put(startObject, newData); 182 } 183 184 if (startObject instanceof FXOMInstance) { 185 final FXOMInstance fxomInstance = (FXOMInstance) startObject; 186 for (FXOMProperty p : fxomInstance.getProperties().values()) { 187 if (p instanceof FXOMPropertyC) { 188 final FXOMPropertyC pc = (FXOMPropertyC) p; 189 for (FXOMObject v : pc.getValues()) { 190 assignSampleData(v); 191 } 192 } 193 } 194 } else if (startObject instanceof FXOMCollection) { 195 final FXOMCollection fxomCollection = (FXOMCollection) startObject; 196 for (FXOMObject i : fxomCollection.getItems()) { 197 assignSampleData(i); 198 } 199 } 200 } 201 202 203 public void removeSampleData(FXOMObject startObject) { 204 final AbstractSampleData currentData = sampleDataMap.get(startObject); 205 if (currentData != null) { 206 currentData.removeFrom(startObject.getSceneGraphObject()); 207 } 208 209 if (startObject instanceof FXOMInstance) { 210 final FXOMInstance fxomInstance = (FXOMInstance) startObject; 211 for (FXOMProperty p : fxomInstance.getProperties().values()) { 212 if (p instanceof FXOMPropertyC) { 213 final FXOMPropertyC pc = (FXOMPropertyC) p; 214 for (FXOMObject v : pc.getValues()) { 215 removeSampleData(v); 216 } 217 } 218 } 219 } else if (startObject instanceof FXOMCollection) { 220 final FXOMCollection fxomCollection = (FXOMCollection) startObject; 221 for (FXOMObject i : fxomCollection.getItems()) { 222 removeSampleData(i); 223 } 224 } 225 } 226 227 /* 228 * Private 229 */ 230 // 231 // private AbstractSampleData<?> makeSampleData(FXOMObject fxomObject) { 232 // final Object obj = fxomObject.getSceneGraphObject(); 233 // assert obj == null; 234 // 235 // if (obj instanceof ListView) { 236 // @SuppressWarnings("unchecked") 237 // final ListView<Object> xyChart = (ListView)obj; 238 // return visitList(xyChart); 239 // } else if (obj instanceof TreeView) { 240 // @SuppressWarnings("unchecked") 241 // final TreeView<Object> xyChart = (TreeView)obj; 242 // return visitTree(xyChart); 243 // } else if (obj instanceof TableView) { 244 // @SuppressWarnings("unchecked") 245 // final TableView<Object> tableView = (TableView)obj; 246 // return visitTable(tableView); 247 // } else if (obj instanceof TableColumn) { 248 // @SuppressWarnings("unchecked") 249 // final TableColumn<Object,Object> tableColumn = 250 // (TableColumn<Object,Object>)obj; 251 // return visitTableColumn(tableColumn); 252 // } else if (obj instanceof XYChart && XYChartSeries.isKnownXYChart(obj)) { 253 // @SuppressWarnings("unchecked") 254 // final XYChart<Object,Object> chart = (XYChart<Object,Object>)obj; 255 // return visitXYChart(chart); 256 // } else if (obj instanceof PieChart) { 257 // final PieChart chart = (PieChart)obj; 258 // return visitPieChart(chart); 259 // } else { 260 // return Visit.DESCEND; 261 // } 262 // } 263 }