Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.scene.control;
027
028import javafx.css.PseudoClass;
029import javafx.beans.property.ReadOnlyIntegerProperty;
030import javafx.beans.property.ReadOnlyIntegerWrapper;
031
032/**
033 * An implementation of {@link Cell} which contains an index property which maps
034 * into the data model underlying the visualization. Despite this,
035 * {@code IndexedCell} should not be instantiated directly in a cell factory
036 * (refer to {@link Cell} for more details on what a cell factory is). 
037 * Instead of creating {@code IndexedCell} directly, you should
038 * instead make use of the control-specific cell implementations (for example,
039 * {@link ListCell}, {@link TreeCell}) {@link TableRow} and {@link TableCell}). 
040 * For more information about using and customizing cells, refer to the 
041 * {@link Cell} API documentation.
042 * 
043 * <p>Because each sequential index represents a single sequential element in the
044 * control, this allows for easy alternative row highlighting. By default the
045 * controls which use {@link Cell Cells} provide their own alternative row
046 * highlighting colors, but this can be overridden using two pseudo class states
047 * provided by {@code IndexedCell}: "even" and "odd".
048 * 
049 * @param <T> The type of the item contained within the Cell.
050 */
051public class IndexedCell<T> extends Cell<T> {
052    
053    /***************************************************************************
054     *                                                                         *
055     * Constructors                                                            *
056     *                                                                         *
057     **************************************************************************/
058    
059    /**
060     * Creates a default IndexedCell with the default style class of 'indexed-cell'.
061     */
062    public IndexedCell() {
063        getStyleClass().addAll(DEFAULT_STYLE_CLASS);
064    }
065    
066    
067    
068    /***************************************************************************
069     *                                                                         *
070     * Properties                                                              *
071     *                                                                         *
072     **************************************************************************/
073    
074    // --- Index
075    private ReadOnlyIntegerWrapper index = new ReadOnlyIntegerWrapper(this, "index", -1) {
076        @Override protected void invalidated() {
077            boolean active = ((get() % 2) == 0);
078            pseudoClassStateChanged(PSEUDO_CLASS_EVEN,  active);
079            pseudoClassStateChanged(PSEUDO_CLASS_ODD,  !active);
080        }
081    };
082
083    /**
084     * Returns the index that this cell represents in the underlying control
085     * data model.
086     */
087    public final int getIndex() { return index.get(); }
088
089    /**
090     * The location of this cell in the virtualized control (e.g: 
091     * {@link ListView}, {@link TreeView}, {@link TableView}, etc). This is the model
092     * index which corresponds exactly with the Cell {@link #itemProperty() item} 
093     * property. For example,
094     * in the case of a {@link ListView}, this means the following:
095     * <code>cell.item == listView.getItems().get(cell.getIndex())</code>
096     */
097    public final ReadOnlyIntegerProperty indexProperty() { return index.getReadOnlyProperty(); }
098
099    /***************************************************************************
100     *                                                                         *
101     * Expert API                                                              *
102     *                                                                         *
103     **************************************************************************/
104
105    /**
106     * Updates the index associated with this IndexedCell.
107     *
108     * @expert This function is intended to be used by experts, primarily
109     *         by those implementing new Skins. It is not common
110     *         for developers or designers to access this function directly.
111     */
112    public void updateIndex(int i) { 
113        index.set(i);
114        indexChanged();
115    }
116    
117    /** 
118     * This method is called whenever the index is changed, regardless of whether
119     * the new index is the same as the old index. 
120     */
121    void indexChanged() { 
122        // no-op
123    }
124    
125    /* *************************************************************************
126     *                                                                         *
127     * Stylesheet Handling                                                     *
128     *                                                                         *
129     **************************************************************************/
130
131    private static final String DEFAULT_STYLE_CLASS = "indexed-cell";
132
133    private static final PseudoClass PSEUDO_CLASS_ODD = PseudoClass.getPseudoClass("odd");
134    private static final PseudoClass PSEUDO_CLASS_EVEN = PseudoClass.getPseudoClass("even");
135
136}