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 java.util.ArrayList;
  36 import java.util.List;
  37 import javafx.scene.control.TreeItem;
  38 import javafx.scene.control.TreeTableColumn;
  39 import javafx.scene.control.TreeTableView;
  40 import javafx.scene.control.cell.TreeItemPropertyValueFactory;
  41 import javafx.scene.shape.Circle;
  42 import javafx.scene.shape.Rectangle;
  43 
  44 /**
  45  *
  46  */
  47 class TreeTableViewSampleData extends AbstractSampleData {
  48 
  49     private final TreeItem<SampleDataItem> sampleRoot;
  50 
  51     public TreeTableViewSampleData() {
  52         int i = 0;
  53         sampleRoot = new TreeItem<>(new SampleDataItem(i++));
  54         sampleRoot.setExpanded(true);
  55         for (int j = 0; j<10; j++) {
  56             final Rectangle r = new Rectangle(10, 10);
  57             r.setFill(TreeTableViewSampleData.color(i));
  58             TreeItem<SampleDataItem> child = new TreeItem<>(new SampleDataItem(i++));
  59             child.setExpanded(true);
  60             child.setGraphic(r);
  61             for (int k=0; k<3; k++) {
  62                 final TreeItem<SampleDataItem> child2 = new TreeItem<>(new SampleDataItem(i++));
  63                 child2.setExpanded(true);
  64                 final Circle c = new Circle(5);
  65                 c.setFill(TreeTableViewSampleData.color(i));
  66                 child2.setGraphic(c);
  67                 child.getChildren().add(child2);
  68             }
  69             sampleRoot.getChildren().add(child);
  70         }
  71     }
  72 
  73     public static boolean canApplyTo(TreeTableView<?> treeTableView) {
  74         final boolean result;
  75 
  76         /*
  77          * We can insert sample data if:
  78          * 1) TreeTableView.items() is null
  79          * 2) TreeTableView columns have no cell factory set
  80          */
  81 
  82         if (treeTableView.getRoot() != null) {
  83             result = false;
  84         } else {
  85             final List<TreeTableColumn<?, ?>> columns = new ArrayList<>();
  86             columns.addAll(treeTableView.getColumns());
  87             while (columns.isEmpty() == false) {
  88                 final TreeTableColumn<?,?> tc = columns.get(0);
  89                 if (tc.getCellValueFactory() == null) {
  90                     columns.remove(0);
  91                     columns.addAll(tc.getColumns());
  92                 } else {
  93                     break;
  94                 }
  95             }
  96 
  97             result = columns.isEmpty();
  98         }
  99 
 100         return result;
 101     }
 102 
 103 
 104     /*
 105      * AbstractSampleData
 106      */
 107 
 108 
 109     @Override
 110     public void applyTo(Object sceneGraphObject) {
 111         assert sceneGraphObject instanceof TreeTableView;
 112 
 113         @SuppressWarnings("unchecked")
 114         final TreeTableView<SampleDataItem> tableView = (TreeTableView<SampleDataItem>) sceneGraphObject;
 115 
 116         tableView.setRoot(sampleRoot);
 117 
 118         final List<TreeTableColumn<SampleDataItem, ?>> columns = new ArrayList<>(tableView.getColumns());
 119         while (columns.isEmpty() == false) {
 120             @SuppressWarnings("unchecked")
 121             final TreeTableColumn<SampleDataItem,String> ttc
 122                     = (TreeTableColumn<SampleDataItem,String>)columns.get(0);
 123             ttc.setCellValueFactory(SampleDataItem.FACTORY);
 124             columns.remove(0);
 125             columns.addAll(ttc.getColumns());
 126         }
 127     }
 128 
 129     @Override
 130     public void removeFrom(Object sceneGraphObject) {
 131         assert sceneGraphObject instanceof TreeTableView;
 132 
 133         @SuppressWarnings("unchecked")
 134         final TreeTableView<SampleDataItem> tableView = TreeTableView.class.cast(sceneGraphObject);
 135         tableView.setRoot(null);
 136 
 137         final List<TreeTableColumn<SampleDataItem, ?>> columns = new ArrayList<>();
 138         columns.addAll(tableView.getColumns());
 139         while (columns.isEmpty() == false) {
 140             @SuppressWarnings("unchecked")
 141             final TreeTableColumn<SampleDataItem,String> tc
 142                     = (TreeTableColumn<SampleDataItem,String>)columns.get(0);
 143             tc.setCellValueFactory(null);
 144             columns.remove(0);
 145             columns.addAll(tc.getColumns());
 146         }
 147     }
 148 
 149 
 150     /*
 151      * Private
 152      */
 153 
 154 
 155     public static class SampleDataItem {
 156         int index;
 157 
 158         public final static TreeItemPropertyValueFactory<SampleDataItem, String> FACTORY
 159                 = new TreeItemPropertyValueFactory<>("prop"); //NOI18N
 160 
 161         public SampleDataItem(int index) {
 162             this.index = index;
 163         }
 164 
 165         public String getProp() {
 166             return TreeTableViewSampleData.lorem(index);
 167         }
 168     }
 169 }