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.TableColumn;
  38 import javafx.scene.control.TableView;
  39 import javafx.scene.control.cell.PropertyValueFactory;
  40 
  41 /**
  42  *
  43  */
  44 class TableViewSampleData extends AbstractSampleData {
  45 
  46     private final List<SampleDataItem> sampleItems = new ArrayList<>();
  47 
  48     public TableViewSampleData() {
  49         for (int i = 0; i < 20; i++) {
  50             sampleItems.add(new SampleDataItem(i));
  51         }
  52     }
  53 
  54     public static boolean canApplyTo(TableView<?> tableView) {
  55         final boolean result;
  56 
  57         /*
  58          * We can insert sample data if:
  59          * 1) TableView.items() is empty
  60          * 2) TableView columns have no cell factory set
  61          */
  62 
  63         if (tableView.getItems().isEmpty() == false) {
  64             result = false;
  65         } else {
  66             final List<TableColumn<?, ?>> columns = new ArrayList<>();
  67             columns.addAll(tableView.getColumns());
  68             while (columns.isEmpty() == false) {
  69                 final TableColumn<?,?> tc = columns.get(0);
  70                 if (tc.getCellValueFactory() == null) {
  71                     columns.remove(0);
  72                     columns.addAll(tc.getColumns());
  73                 } else {
  74                     break;
  75                 }
  76             }
  77 
  78             result = columns.isEmpty();
  79         }
  80 
  81         return result;
  82     }
  83 
  84 
  85     /*
  86      * AbstractSampleData
  87      */
  88 
  89 
  90     @Override
  91     public void applyTo(Object sceneGraphObject) {
  92         assert sceneGraphObject instanceof TableView;
  93 
  94         @SuppressWarnings("unchecked")
  95         final TableView<SampleDataItem> tableView = (TableView<SampleDataItem>) sceneGraphObject;
  96 
  97         tableView.getItems().clear();
  98         tableView.getItems().addAll(sampleItems);
  99 
 100         final List<TableColumn<SampleDataItem, ?>> columns = new ArrayList<>(tableView.getColumns());
 101         while (columns.isEmpty() == false) {
 102             @SuppressWarnings("unchecked")
 103             final TableColumn<SampleDataItem,String> tc
 104                     = (TableColumn<SampleDataItem,String>)columns.get(0);
 105             tc.setCellValueFactory(SampleDataItem.FACTORY);
 106             columns.remove(0);
 107             columns.addAll(tc.getColumns());
 108         }
 109     }
 110 
 111     @Override
 112     public void removeFrom(Object sceneGraphObject) {
 113         assert sceneGraphObject instanceof TableView;
 114 
 115         @SuppressWarnings("unchecked")
 116         final TableView<SampleDataItem> tableView = TableView.class.cast(sceneGraphObject);
 117         tableView.getItems().clear();
 118 
 119         final List<TableColumn<SampleDataItem, ?>> columns = new ArrayList<>();
 120         columns.addAll(tableView.getColumns());
 121         while (columns.isEmpty() == false) {
 122             @SuppressWarnings("unchecked")
 123             final TableColumn<SampleDataItem,String> tc
 124                     = (TableColumn<SampleDataItem,String>)columns.get(0);
 125             tc.setCellValueFactory(null);
 126             columns.remove(0);
 127             columns.addAll(tc.getColumns());
 128         }
 129     }
 130 
 131 
 132     /*
 133      * Private
 134      */
 135 
 136 
 137     public static class SampleDataItem {
 138         int index;
 139 
 140         public final static PropertyValueFactory<SampleDataItem, String> FACTORY
 141                 = new PropertyValueFactory<>("prop"); //NOI18N
 142 
 143         public SampleDataItem(int index) {
 144             this.index = index;
 145         }
 146 
 147         public String getProp() {
 148             return TableViewSampleData.lorem(index);
 149         }
 150     }
 151 }