1 /*
   2  * Copyright (c) 2011, 2018, 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.TableCell;
  33 import javafx.scene.control.TableColumn;
  34 import javafx.scene.control.TableColumn.CellDataFeatures;
  35 import javafx.scene.control.TableView;
  36 import javafx.util.Callback;
  37 
  38 import com.sun.javafx.logging.PlatformLogger;
  39 import com.sun.javafx.logging.PlatformLogger.Level;
  40 import com.sun.javafx.property.PropertyReference;
  41 import com.sun.javafx.scene.control.Logging;
  42 
  43 
  44 /**
  45  * A convenience implementation of the Callback interface, designed specifically
  46  * for use within the {@link TableColumn}
  47  * {@link TableColumn#cellValueFactoryProperty() cell value factory}. An example
  48  * of how to use this class is:
  49  *
  50  * <pre><code>
  51  * TableColumn&lt;Person,String&gt; firstNameCol = new TableColumn&lt;Person,String&gt;("First Name");
  52  * firstNameCol.setCellValueFactory(new PropertyValueFactory&lt;Person,String&gt;("firstName"));
  53  * </code></pre>
  54  *
  55  * <p>
  56  * In this example, {@code Person} is the class type of the {@code TableView}
  57  * {@link TableView#itemsProperty() items} list.
  58  * The class {@code Person} must be declared public.
  59  * {@code PropertyValueFactory} uses the constructor argument,
  60  * {@code "firstName"}, to assume that {@code Person} has a public method
  61  * {@code firstNameProperty} with no formal parameters and a return type of
  62  * {@code ObservableValue<String>}.
  63  * </p>
  64  * <p>
  65  * If such a method exists, then it is invoked, and additionally assumed
  66  * to return an instance of {@code Property<String>}. The return value is used
  67  * to populate the {@link TableCell}. In addition, the {@code TableView} adds
  68  * an observer to the return value, such that any changes fired will be observed
  69  * by the {@code TableView}, resulting in the cell immediately updating.
  70  * </p>
  71  * <p>
  72  * If no such method exists, then {@code PropertyValueFactory}
  73  * assumes that {@code Person} has a public method {@code getFirstName} or
  74  * {@code isFirstName} with no formal parameters and a return type of
  75  * {@code String}. If such a method exists, then it is invoked, and its return
  76  * value is wrapped in a {@link ReadOnlyObjectWrapper}
  77  * and returned to the {@code TableCell}. In this situation,
  78  * the {@code TableCell} will not be able to observe changes to the property,
  79  * unlike in the first approach above.
  80  * </p>
  81  *
  82  * <p>For reference (and as noted in the TableColumn
  83  * {@link TableColumn#cellValueFactoryProperty()}  cell value factory} documentation), the
  84  * long form of the code above would be the following:
  85  * </p>
  86  *
  87  * <pre><code>
  88  * TableColumn&lt;Person,String&gt; firstNameCol = new TableColumn&lt;Person,String&gt;("First Name");
  89  * firstNameCol.setCellValueFactory(new Callback&lt;CellDataFeatures&lt;Person, String&gt;, ObservableValue&lt;String&gt;&gt;() {
  90  *     public ObservableValue&lt;String&gt; call(CellDataFeatures&lt;Person, String&gt; p) {
  91  *         // p.getValue() returns the Person instance for a particular TableView row
  92  *         return p.getValue().firstNameProperty();
  93  *     }
  94  *  });
  95  * }
  96  * </code></pre>
  97  *
  98  * <p><b>Deploying an Application as a Module</b></p>
  99  * <p>
 100  * If the referenced class is in a named module, then it must be reflectively
 101  * accessible to the {@code javafx.base} module.
 102  * A class is reflectively accessible if the module
 103  * {@link Module#isOpen(String,Module) opens} the containing package to at
 104  * least the {@code javafx.base} module.
 105  * Otherwise the {@link #call call(TableColumn.CellDataFeatures)} method
 106  * will log a warning and return {@code null}.
 107  * </p>
 108  * <p>
 109  * For example, if the {@code Person} class is in the {@code com.foo} package
 110  * in the {@code foo.app} module, the {@code module-info.java} might
 111  * look like this:
 112  * </p>
 113  *
 114 <pre>{@code module foo.app {
 115     opens com.foo to javafx.base;
 116 }}</pre>
 117  *
 118  * <p>
 119  * Alternatively, a class is reflectively accessible if the module
 120  * {@link Module#isExported(String) exports} the containing package
 121  * unconditionally.
 122  * </p>
 123  *
 124  * @see TableColumn
 125  * @see TableView
 126  * @see TableCell
 127  * @see TreeItemPropertyValueFactory
 128  * @see MapValueFactory
 129  * @param <S> The type of the class contained within the TableView.items list.
 130  * @param <T> The type of the class contained within the TableColumn cells.
 131  * @since JavaFX 2.0
 132  */
 133 public class PropertyValueFactory<S,T> implements Callback<CellDataFeatures<S,T>, ObservableValue<T>> {
 134 
 135     private final String property;
 136 
 137     private Class<?> columnClass;
 138     private String previousProperty;
 139     private PropertyReference<T> propertyRef;
 140 
 141     /**
 142      * Creates a default PropertyValueFactory to extract the value from a given
 143      * TableView row item reflectively, using the given property name.
 144      *
 145      * @param property The name of the property with which to attempt to
 146      *      reflectively extract a corresponding value for in a given object.
 147      */
 148     public PropertyValueFactory(@NamedArg("property") String property) {
 149         this.property = property;
 150     }
 151 
 152     /** {@inheritDoc} */
 153     @Override public ObservableValue<T> call(CellDataFeatures<S,T> param) {
 154         return getCellDataReflectively(param.getValue());
 155     }
 156 
 157     /**
 158      * Returns the property name provided in the constructor.
 159      * @return the property name provided in the constructor
 160      */
 161     public final String getProperty() { return property; }
 162 
 163     private ObservableValue<T> getCellDataReflectively(S rowData) {
 164         if (getProperty() == null || getProperty().isEmpty() || rowData == null) return null;
 165 
 166         try {
 167             // we attempt to cache the property reference here, as otherwise
 168             // performance suffers when working in large data models. For
 169             // a bit of reference, refer to RT-13937.
 170             if (columnClass == null || previousProperty == null ||
 171                     ! columnClass.equals(rowData.getClass()) ||
 172                     ! previousProperty.equals(getProperty())) {
 173 
 174                 // create a new PropertyReference
 175                 this.columnClass = rowData.getClass();
 176                 this.previousProperty = getProperty();
 177                 this.propertyRef = new PropertyReference<T>(rowData.getClass(), getProperty());
 178             }
 179 
 180             if (propertyRef != null) {
 181                 if (propertyRef.hasProperty()) {
 182                     return propertyRef.getProperty(rowData);
 183                 } else {
 184                     T value = propertyRef.get(rowData);
 185                     return new ReadOnlyObjectWrapper<T>(value);
 186                 }
 187             }
 188         } catch (RuntimeException e) {
 189             // log the warning and move on
 190             final PlatformLogger logger = Logging.getControlsLogger();
 191             if (logger.isLoggable(Level.WARNING)) {
 192                logger.warning("Can not retrieve property '" + getProperty() +
 193                         "' in PropertyValueFactory: " + this +
 194                         " with provided class type: " + rowData.getClass(), e);
 195             }
 196             propertyRef = null;
 197         }
 198 
 199         return null;
 200     }
 201 }