1 /* 2 * Copyright (c) 2010, 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; 27 28 import javafx.css.PseudoClass; 29 import javafx.beans.property.ReadOnlyIntegerProperty; 30 import javafx.beans.property.ReadOnlyIntegerWrapper; 31 32 /** 33 * An implementation of {@link Cell} which contains an index property which maps 34 * into the data model underlying the visualization. Despite this, 35 * {@code IndexedCell} should not be instantiated directly in a cell factory 36 * (refer to {@link Cell} for more details on what a cell factory is). 37 * Instead of creating {@code IndexedCell} directly, you should 38 * instead make use of the control-specific cell implementations (for example, 39 * {@link ListCell}, {@link TreeCell}) {@link TableRow} and {@link TableCell}). 40 * For more information about using and customizing cells, refer to the 41 * {@link Cell} API documentation. 42 * 43 * <p>Because each sequential index represents a single sequential element in the 44 * control, this allows for easy alternative row highlighting. By default the 45 * controls which use {@link Cell Cells} provide their own alternative row 46 * highlighting colors, but this can be overridden using two pseudo class states 47 * provided by {@code IndexedCell}: "even" and "odd". 48 * 49 * @param <T> The type of the item contained within the Cell. 50 * @since JavaFX 2.0 51 */ 52 public class IndexedCell<T> extends Cell<T> { 53 54 /*************************************************************************** 55 * * 56 * Constructors * 57 * * 58 **************************************************************************/ 59 60 /** 61 * Creates a default IndexedCell with the default style class of 'indexed-cell'. 62 */ 63 public IndexedCell() { 64 getStyleClass().addAll(DEFAULT_STYLE_CLASS); 65 } 66 67 68 69 /*************************************************************************** 70 * * 71 * Properties * 72 * * 73 **************************************************************************/ 74 75 // --- Index 76 private ReadOnlyIntegerWrapper index = new ReadOnlyIntegerWrapper(this, "index", -1) { 77 @Override protected void invalidated() { 78 boolean active = ((get() % 2) == 0); 79 pseudoClassStateChanged(PSEUDO_CLASS_EVEN, active); 80 pseudoClassStateChanged(PSEUDO_CLASS_ODD, !active); 81 } 82 }; 83 84 /** 85 * Returns the index that this cell represents in the underlying control 86 * data model. 87 * @return the index that this cell represents in the underlying control 88 * data model 89 */ 90 public final int getIndex() { return index.get(); } 91 92 /** 93 * The location of this cell in the virtualized control (e.g: 94 * {@link ListView}, {@link TreeView}, {@link TableView}, etc). This is the model 95 * index which corresponds exactly with the Cell {@link #itemProperty() item} 96 * property. For example, 97 * in the case of a {@link ListView}, this means the following: 98 * <code>cell.item == listView.getItems().get(cell.getIndex())</code> 99 * @return the location of this cell in the virtualized control 100 */ 101 public final ReadOnlyIntegerProperty indexProperty() { return index.getReadOnlyProperty(); } 102 103 /*************************************************************************** 104 * * 105 * Expert API * 106 * * 107 **************************************************************************/ 108 109 /** 110 * Updates the index associated with this IndexedCell. 111 * 112 * Note: This function is intended to be used by experts, primarily 113 * by those implementing new Skins. It is not common 114 * for developers or designers to access this function directly. 115 * @param i the index associated with this indexed cell 116 */ 117 public void updateIndex(int i) { 118 final int oldIndex = index.get(); 119 index.set(i); 120 indexChanged(oldIndex, i); 121 } 122 123 /** 124 * This method is called whenever the index is changed, regardless of whether 125 * the new index is the same as the old index. 126 * @param oldIndex 127 * @param newIndex 128 */ 129 void indexChanged(int oldIndex, int newIndex) { 130 // no-op 131 } 132 133 /* ************************************************************************* 134 * * 135 * Stylesheet Handling * 136 * * 137 **************************************************************************/ 138 139 private static final String DEFAULT_STYLE_CLASS = "indexed-cell"; 140 141 private static final PseudoClass PSEUDO_CLASS_ODD = PseudoClass.getPseudoClass("odd"); 142 private static final PseudoClass PSEUDO_CLASS_EVEN = PseudoClass.getPseudoClass("even"); 143 144 }