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      */
  88     public final int getIndex() { return index.get(); }
  89 
  90     /**
  91      * The location of this cell in the virtualized control (e.g:
  92      * {@link ListView}, {@link TreeView}, {@link TableView}, etc). This is the model
  93      * index which corresponds exactly with the Cell {@link #itemProperty() item}
  94      * property. For example,
  95      * in the case of a {@link ListView}, this means the following:
  96      * <code>cell.item == listView.getItems().get(cell.getIndex())</code>
  97      */
  98     public final ReadOnlyIntegerProperty indexProperty() { return index.getReadOnlyProperty(); }
  99 
 100     /***************************************************************************
 101      *                                                                         *
 102      * Expert API                                                              *
 103      *                                                                         *
 104      **************************************************************************/
 105 
 106     /**
 107      * Updates the index associated with this IndexedCell.
 108      *
 109      * @expert This function is intended to be used by experts, primarily
 110      *         by those implementing new Skins. It is not common
 111      *         for developers or designers to access this function directly.
 112      */
 113     public void updateIndex(int i) {
 114         final int oldIndex = index.get();
 115         index.set(i);
 116         indexChanged(oldIndex, i);
 117     }
 118 
 119     /**
 120      * This method is called whenever the index is changed, regardless of whether
 121      * the new index is the same as the old index.
 122      * @param oldIndex
 123      * @param newIndex
 124      */
 125     void indexChanged(int oldIndex, int newIndex) {
 126         // no-op
 127     }
 128 
 129     /* *************************************************************************
 130      *                                                                         *
 131      * Stylesheet Handling                                                     *
 132      *                                                                         *
 133      **************************************************************************/
 134 
 135     private static final String DEFAULT_STYLE_CLASS = "indexed-cell";
 136 
 137     private static final PseudoClass PSEUDO_CLASS_ODD = PseudoClass.getPseudoClass("odd");
 138     private static final PseudoClass PSEUDO_CLASS_EVEN = PseudoClass.getPseudoClass("even");
 139 
 140 }