1 /*
   2  * Copyright (c) 2012, 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 
  26 package javafx.scene.control.cell;
  27 
  28 import javafx.beans.NamedArg;
  29 import javafx.beans.property.Property;
  30 import javafx.beans.property.ReadOnlyObjectWrapper;
  31 import javafx.beans.value.ObservableValue;
  32 import javafx.scene.control.TreeItem;
  33 import javafx.scene.control.TreeTableColumn;
  34 import javafx.scene.control.TreeTableColumn.CellDataFeatures;
  35 import javafx.util.Callback;
  36 import com.sun.javafx.property.PropertyReference;
  37 import com.sun.javafx.scene.control.Logging;
  38 import sun.util.logging.PlatformLogger;
  39 import sun.util.logging.PlatformLogger.Level;
  40 
  41 
  42 /**
  43  * A convenience implementation of the Callback interface, designed specifically
  44  * for use within the {@link TreeTableColumn}
  45  * {@link TreeTableColumn#cellValueFactoryProperty() cell value factory}. An example
  46  * of how to use this class is:
  47  *
  48  * <pre><code>
  49  * TreeTableColumn&lt;Person,String&gt; firstNameCol = new TreeTableColumn&lt;Person,String&gt;("First Name");
  50  * firstNameCol.setCellValueFactory(new TreeItemPropertyValueFactory&lt;Person,String&gt;("firstName"));
  51  * </code></pre>
  52  *
  53  * In this example, the "firstName" string is used as a reference to an assumed
  54  * <code>firstNameProperty()</code> method in the <code>Person</code> class type
  55  * (which is the class type of the TreeTableView). Additionally, this method must
  56  * return a {@link Property} instance. If a method meeting these requirements
  57  * is found, then the {@link javafx.scene.control.TreeTableCell} is populated with this ObservableValue<T>.
  58  * In addition, the TreeTableView will automatically add an observer to the
  59  * returned value, such that any changes fired will be observed by the TreeTableView,
  60  * resulting in the cell immediately updating.
  61  *
  62  * <p>If no method matching this pattern exists, there is fall-through support
  63  * for attempting to call get&lt;property&gt;() or is&lt;property&gt;() (that is,
  64  * <code>getFirstName()</code> or <code>isFirstName()</code> in the example
  65  * above). If a  method matching this pattern exists, the value returned from this method
  66  * is wrapped in a {@link ReadOnlyObjectWrapper} and returned to the TreeTableCell.
  67  * However, in this situation, this means that the TreeTableCell will not be able
  68  * to observe the ObservableValue for changes (as is the case in the first
  69  * approach above).
  70  *
  71  * <p>For reference (and as noted in the TreeTableColumn
  72  * {@link TreeTableColumn#cellValueFactory cell value factory} documentation), the
  73  * long form of the code above would be the following:
  74  *
  75  * <pre><code>
  76  * TreeTableColumn&lt;Person,String&gt; firstNameCol = new TreeTableColumn&lt;Person,String&gt;("First Name");
  77  * firstNameCol.setCellValueFactory(new Callback&lt;CellDataFeatures&lt;Person, String&gt;, ObservableValue&lt;String&gt;&gt;() {
  78  *     public ObservableValue&lt;String&gt; call(CellDataFeatures&lt;Person, String&gt; p) {
  79  *         // p.getValue() returns the TreeItem<Person> instance for a particular
  80  *         // TreeTableView row, and the second getValue() call returns the
  81  *         // Person instance contained within the TreeItem.
  82  *         return p.getValue().getValue().firstNameProperty();
  83  *     }
  84  *  });
  85  * }
  86  * </code></pre>
  87  *
  88  * @see TreeTableColumn
  89  * @see javafx.scene.control.TreeTableView
  90  * @see javafx.scene.control.TreeTableCell
  91  * @see PropertyValueFactory
  92  * @see MapValueFactory
  93  * @since JavaFX 8.0
  94  */
  95 public class TreeItemPropertyValueFactory<S,T> implements Callback<TreeTableColumn.CellDataFeatures<S,T>, ObservableValue<T>> {
  96 
  97     private final String property;
  98 
  99     private Class<?> columnClass;
 100     private String previousProperty;
 101     private PropertyReference<T> propertyRef;
 102 
 103     /**
 104      * Creates a default PropertyValueFactory to extract the value from a given
 105      * TableView row item reflectively, using the given property name.
 106      *
 107      * @param property The name of the property with which to attempt to
 108      *      reflectively extract a corresponding value for in a given object.
 109      */
 110     public TreeItemPropertyValueFactory(@NamedArg("property") String property) {
 111         this.property = property;
 112     }
 113 
 114     /** {@inheritDoc} */
 115     @Override public ObservableValue<T> call(CellDataFeatures<S, T> param) {
 116         TreeItem<S> treeItem = param.getValue();
 117         return getCellDataReflectively(treeItem.getValue());
 118     }
 119 
 120     /**
 121      * Returns the property name provided in the constructor.
 122      */
 123     public final String getProperty() { return property; }
 124 
 125     private ObservableValue<T> getCellDataReflectively(S rowData) {
 126         if (getProperty() == null || getProperty().isEmpty() || rowData == null) return null;
 127 
 128         try {
 129             // we attempt to cache the property reference here, as otherwise
 130             // performance suffers when working in large data models. For
 131             // a bit of reference, refer to RT-13937.
 132             if (columnClass == null || previousProperty == null ||
 133                     ! columnClass.equals(rowData.getClass()) ||
 134                     ! previousProperty.equals(getProperty())) {
 135 
 136                 // create a new PropertyReference
 137                 this.columnClass = rowData.getClass();
 138                 this.previousProperty = getProperty();
 139                 this.propertyRef = new PropertyReference<T>(rowData.getClass(), getProperty());
 140             }
 141 
 142             return propertyRef.getProperty(rowData);
 143         } catch (IllegalStateException e) {
 144             try {
 145                 // attempt to just get the value
 146                 T value = propertyRef.get(rowData);
 147                 return new ReadOnlyObjectWrapper<T>(value);
 148             } catch (IllegalStateException e2) {
 149                 // fall through to logged exception below
 150             }
 151 
 152             // log the warning and move on
 153             final PlatformLogger logger = Logging.getControlsLogger();
 154             if (logger.isLoggable(Level.WARNING)) {
 155                logger.finest("Can not retrieve property '" + getProperty() +
 156                         "' in TreeItemPropertyValueFactory: " + this +
 157                         " with provided class type: " + rowData.getClass(), e);
 158             }
 159         }
 160 
 161         return null;
 162     }
 163 }