/* * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * * * * * * * * * * * * * * * * * * * */ package javafx.scene.control; import java.lang.ref.WeakReference; import java.util.List; import javafx.beans.NamedArg; /** * This class is used to represent a single row/column/cell in a TableView. * This is used throughout the TableView API to represent which rows/columns/cells * are currently selected, focused, being edited, etc. Note that this class is * immutable once it is created. * *
Because the TableView can have different
* {@link SelectionMode selection modes}, the row and column properties in
* TablePosition can be 'disabled' to represent an entire row or column. This is
* done by setting the unrequired property to -1 or null.
*
* @param The type of the items contained within the TableView (i.e. the same
* generic type as the S in TableView<S>).
* @param extends TablePositionBase tableView, @NamedArg("row") int row, @NamedArg("tableColumn") TableColumn tableColumn) {
super(row, tableColumn);
this.controlRef = new WeakReference<>(tableView);
List items = tableView.getItems();
this.itemRef = new WeakReference<>(
items != null && row >= 0 && row < items.size() ? items.get(row) : null);
}
/***************************************************************************
* *
* Instance Variables *
* *
**************************************************************************/
private final WeakReference itemRef;
int fixedColumnIndex = -1;
/***************************************************************************
* *
* Public API *
* *
**************************************************************************/
/**
* The column index that this TablePosition represents in the TableView. It
* is -1 if the TableView or TableColumn instances are null.
*/
@Override public int getColumn() {
if (fixedColumnIndex > -1) {
return fixedColumnIndex;
}
TableView tableView = getTableView();
TableColumn tableColumn = getTableColumn();
return tableView == null || tableColumn == null ? -1 :
tableView.getVisibleLeafIndex(tableColumn);
}
/**
* The TableView that this TablePosition is related to.
*/
public final TableView getTableView() {
return controlRef.get();
}
/** {@inheritDoc} */
@Override public final TableColumn getTableColumn() {
// Forcing the return type to be TableColumn, not TableColumnBase
return super.getTableColumn();
}
/**
* Returns the item that backs the {@link #getRow()} row}, at the point
* in time when this TablePosition was created.
*/
final S getItem() {
return itemRef == null ? null : itemRef.get();
}
/**
* Returns a string representation of this {@code TablePosition} object.
* @return a string representation of this {@code TablePosition} object.
*/
@Override public String toString() {
return "TablePosition [ row: " + getRow() + ", column: " + getTableColumn() + ", "
+ "tableView: " + getTableView() + " ]";
}
}