1 /* 2 * Copyright (c) 2011, 2016, 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 java.lang.ref.WeakReference; 29 import java.util.List; 30 31 import javafx.beans.NamedArg; 32 33 /** 34 * This class is used to represent a single row/column/cell in a TableView. 35 * This is used throughout the TableView API to represent which rows/columns/cells 36 * are currently selected, focused, being edited, etc. Note that this class is 37 * immutable once it is created. 38 * 39 * <p>Because the TableView can have different 40 * {@link SelectionMode selection modes}, the row and column properties in 41 * TablePosition can be 'disabled' to represent an entire row or column. This is 42 * done by setting the unrequired property to -1 or null. 43 * 44 * @param <S> The type of the items contained within the TableView (i.e. the same 45 * generic type as the S in TableView<S>). 46 * @param <T> The type of the items contained within the TableColumn. 47 * @see TableView 48 * @see TableColumn 49 * @since JavaFX 2.0 50 */ 51 public class TablePosition<S,T> extends TablePositionBase<TableColumn<S,T>> { 52 53 /*************************************************************************** 54 * * 55 * Constructors * 56 * * 57 **************************************************************************/ 58 59 /** 60 * Constructs a TablePosition instance to represent the given row/column 61 * position in the given TableView instance. Both the TableView and 62 * TableColumn are referenced weakly in this class, so it is possible that 63 * they will be null when their respective getters are called. 64 * 65 * @param tableView The TableView that this position is related to. 66 * @param row The row that this TablePosition is representing. 67 * @param tableColumn The TableColumn instance that this TablePosition represents. 68 */ 69 public TablePosition(@NamedArg("tableView") TableView<S> tableView, @NamedArg("row") int row, @NamedArg("tableColumn") TableColumn<S,T> tableColumn) { 70 super(row, tableColumn); 71 this.controlRef = new WeakReference<>(tableView); 72 73 List<S> items = tableView.getItems(); 74 this.itemRef = new WeakReference<>( 75 items != null && row >= 0 && row < items.size() ? items.get(row) : null); 76 77 nonFixedColumnIndex = tableView == null || tableColumn == null ? -1 : tableView.getVisibleLeafIndex(tableColumn); 78 } 79 80 81 82 /*************************************************************************** 83 * * 84 * Instance Variables * 85 * * 86 **************************************************************************/ 87 88 private final WeakReference<TableView<S>> controlRef; 89 private final WeakReference<S> itemRef; 90 int fixedColumnIndex = -1; 91 private final int nonFixedColumnIndex; 92 93 /*************************************************************************** 94 * * 95 * Public API * 96 * * 97 **************************************************************************/ 98 99 /** 100 * The column index that this TablePosition represents in the TableView. It 101 * is -1 if the TableView or TableColumn instances are null at the time the class 102 * is instantiated (i.e. it is computed at construction). 103 */ 104 @Override public int getColumn() { 105 if (fixedColumnIndex > -1) { 106 return fixedColumnIndex; 107 } 108 109 return nonFixedColumnIndex; 110 } 111 112 /** 113 * The TableView that this TablePosition is related to. 114 * @return the TableView 115 */ 116 public final TableView<S> getTableView() { 117 return controlRef.get(); 118 } 119 120 /** {@inheritDoc} */ 121 @Override public final TableColumn<S,T> getTableColumn() { 122 // Forcing the return type to be TableColumn<S,T>, not TableColumnBase<S,T> 123 return super.getTableColumn(); 124 } 125 126 /** 127 * Returns the item that backs the {@link #getRow()} row}, at the point 128 * in time when this TablePosition was created. 129 */ 130 final S getItem() { 131 return itemRef == null ? null : itemRef.get(); 132 } 133 134 /** 135 * Returns a string representation of this {@code TablePosition} object. 136 * @return a string representation of this {@code TablePosition} object. 137 */ 138 @Override public String toString() { 139 return "TablePosition [ row: " + getRow() + ", column: " + getTableColumn() + ", " 140 + "tableView: " + getTableView() + " ]"; 141 } 142 }