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 javafx.beans.NamedArg;
  30 
  31 /**
  32  * This class is used to represent a single row/column/cell in a TreeTableView.
  33  * This is used throughout the TreeTableView API to represent which rows/columns/cells
  34  * are currently selected, focused, being edited, etc. Note that this class is
  35  * immutable once it is created.
  36  *
  37  * <p>Because the TreeTableView can have different
  38  * {@link SelectionMode selection modes}, the row and column properties in
  39  * TablePosition can be 'disabled' to represent an entire row or column. This is
  40  * done by setting the unrequired property to -1 or null.
  41  *
  42  * @param <S> The type of the {@link TreeItem} instances contained within the
  43  *      TreeTableView.
  44  * @param <T> The type of the items contained within the TreeTableColumn.
  45  * @see TreeTableView
  46  * @see TreeTableColumn
  47  * @since JavaFX 8.0
  48  */
  49 public class TreeTablePosition<S,T> extends TablePositionBase<TreeTableColumn<S,T>> {
  50 
  51     /***************************************************************************
  52      *                                                                         *
  53      * Constructors                                                            *
  54      *                                                                         *
  55      **************************************************************************/
  56 
  57     /**
  58      * Constructs a TreeTablePosition instance to represent the given row/column
  59      * position in the given TreeTableView instance. Both the TreeTableView and
  60      * TreeTableColumn are referenced weakly in this class, so it is possible that
  61      * they will be null when their respective getters are called.
  62      *
  63      * @param treeTableView The TreeTableView that this position is related to.
  64      * @param row The row that this TreeTablePosition is representing.
  65      * @param tableColumn The TreeTableColumn instance that this TreeTablePosition represents.
  66      */
  67     public TreeTablePosition(@NamedArg("treeTableView") TreeTableView<S> treeTableView, @NamedArg("row") int row, @NamedArg("tableColumn") TreeTableColumn<S,T> tableColumn) {
  68         this(treeTableView, row, tableColumn, true);
  69     }
  70 
  71     // Not public API
  72     TreeTablePosition(@NamedArg("treeTableView") TreeTableView<S> treeTableView, @NamedArg("row") int row, @NamedArg("tableColumn") TreeTableColumn<S,T> tableColumn, boolean doLookup) {
  73         super(row, tableColumn);
  74         this.controlRef = new WeakReference<>(treeTableView);
  75         this.treeItemRef = new WeakReference<>(doLookup ? treeTableView.getTreeItem(row) : null);
  76 
  77         nonFixedColumnIndex = treeTableView == null || tableColumn == null ? -1 : treeTableView.getVisibleLeafIndex(tableColumn);
  78     }
  79 
  80 
  81 
  82     /***************************************************************************
  83      *                                                                         *
  84      * Instance Variables                                                      *
  85      *                                                                         *
  86      **************************************************************************/
  87 
  88     private final WeakReference<TreeTableView<S>> controlRef;
  89     private final WeakReference<TreeItem<S>> treeItemRef;
  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 TreeTablePosition represents in the TreeTableView. It
 101      * is -1 if the TreeTableView or TreeTableColumn 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 TreeTableView that this TreeTablePosition is related to.
 114      * @return the TreeTableView that this TreeTablePosition is related to
 115      */
 116     public final TreeTableView<S> getTreeTableView() {
 117         return controlRef.get();
 118     }
 119 
 120     @Override public final TreeTableColumn<S,T> getTableColumn() {
 121         // Forcing the return type to be TreeTableColumn<S,T>, not TableColumnBase<S,T>
 122         return super.getTableColumn();
 123     }
 124 
 125     /**
 126      * Returns the {@link TreeItem} that backs the {@link #getRow()} row}.
 127      * @return the {@link TreeItem} that backs the {@link #getRow()} row}
 128      */
 129     public final TreeItem<S> getTreeItem() {
 130         return treeItemRef.get();
 131     }
 132 }