1 /*
   2  * Copyright (c) 2011, 2013, 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 
  30 /**
  31  * This class is used to represent a single row/column/cell in a table. Concrete
  32  * subclasses of this abstract class are used in the {@link TableView} and
  33  * {@link TreeTableView} APIs 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 TableView and TreeTableView controls can have different
  38  * {@link SelectionMode selection modes}, the row and column properties in
  39  * TablePositionBase 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  * @see TablePosition
  43  * @see TreeTablePosition
  44  * @since JavaFX 8.0
  45  */
  46 public abstract class TablePositionBase<TC extends TableColumnBase> {
  47 
  48     /***************************************************************************
  49      *                                                                         *
  50      * Constructors                                                            *
  51      *                                                                         *
  52      **************************************************************************/
  53 
  54     /**
  55      * Constructs a TablePositionBase instance to represent the given row/column
  56      * position in the underlying table instance (which is not part of the
  57      * abstract TablePositionBase class, but is part of concrete subclasses such
  58      * as {@link TablePosition} and {@link TreeTablePosition}). In all cases,
  59      * all fields inside TablePositionBase instances are referenced weakly so as
  60      * to prevent memory leaks. This means that it is possible (but unlikely)
  61      * that the get methods will return null.
  62      *
  63      * @param row The row that this TablePosition is representing.
  64      * @param tableColumn The TableColumn instance that this TablePosition represents.
  65      */
  66     protected TablePositionBase(int row, TC tableColumn) {
  67         this.row = row;
  68         this.tableColumnRef = new WeakReference<TC>(tableColumn);
  69     }
  70 
  71 
  72 
  73     /***************************************************************************
  74      *                                                                         *
  75      * Instance Variables                                                      *
  76      *                                                                         *
  77      **************************************************************************/
  78 
  79     private final int row;
  80     private final WeakReference<TC> tableColumnRef;
  81 
  82 
  83 
  84     /***************************************************************************
  85      *                                                                         *
  86      * Public API                                                              *
  87      *                                                                         *
  88      **************************************************************************/
  89 
  90     /**
  91      * The row that this TablePosition represents in the TableView.
  92      */
  93     public int getRow() {
  94         return row;
  95     }
  96 
  97     /**
  98      * The column index that this TablePosition represents in the TableView. It
  99      * is -1 if the TableView or TableColumn instances are null.
 100      */
 101     public abstract int getColumn();
 102 
 103     /**
 104      * The TableColumn that this TablePosition represents in the TableView.
 105      */
 106     public TC getTableColumn() {
 107         return tableColumnRef.get();
 108     }
 109 
 110     /**
 111      * Indicates whether some other object is "equal to" this one.
 112      * @param obj the reference object with which to compare.
 113      * @return {@code true} if this object is equal to the {@code obj} argument; {@code false} otherwise.
 114      */
 115     @Override public boolean equals(Object obj) {
 116         if (obj == null) {
 117             return false;
 118         }
 119         if (getClass() != obj.getClass()) {
 120             return false;
 121         }
 122         @SuppressWarnings("unchecked")
 123         final TablePositionBase other = (TablePositionBase) obj;
 124         if (this.row != other.row) {
 125             return false;
 126         }
 127         TC tableColumn = getTableColumn();
 128         TableColumnBase otherTableColumn = other.getTableColumn();
 129         if (tableColumn != otherTableColumn && (tableColumn == null || !tableColumn.equals(otherTableColumn))) {
 130             return false;
 131         }
 132         return true;
 133     }
 134 
 135     /**
 136      * Returns a hash code for this {@code TablePosition} object.
 137      * @return a hash code for this {@code TablePosition} object.
 138      */
 139     @Override public int hashCode() {
 140         int hash = 5;
 141         hash = 79 * hash + this.row;
 142         TableColumnBase tableColumn = getTableColumn();
 143         hash = 79 * hash + (tableColumn != null ? tableColumn.hashCode() : 0);
 144         return hash;
 145     }
 146 }