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