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      * @return the row that this TablePosition represents in the TableView
  93      */
  94     public int getRow() {
  95         return row;
  96     }
  97 
  98     /**
  99      * The column index that this TablePosition represents in the TableView. It
 100      * is -1 if the TableView or TableColumn instances are null.
 101      * @return the column index that this TablePosition represents in the
 102      * TableView
 103      */
 104     public abstract int getColumn();
 105 
 106     /**
 107      * The TableColumn that this TablePosition represents in the TableView.
 108      * @return the TableColumn that this TablePosition represents in the TableView
 109      */
 110     public TC getTableColumn() {
 111         return tableColumnRef.get();
 112     }
 113 
 114     /**
 115      * Indicates whether some other object is "equal to" this one.
 116      * @param obj the reference object with which to compare.
 117      * @return {@code true} if this object is equal to the {@code obj} argument; {@code false} otherwise.
 118      */
 119     @Override public boolean equals(Object obj) {
 120         if (obj == null) {
 121             return false;
 122         }
 123         if (getClass() != obj.getClass()) {
 124             return false;
 125         }
 126         @SuppressWarnings("unchecked")
 127         final TablePositionBase other = (TablePositionBase) obj;
 128         if (this.row != other.row) {
 129             return false;
 130         }
 131         TC tableColumn = getTableColumn();
 132         TableColumnBase otherTableColumn = other.getTableColumn();
 133         if (tableColumn != otherTableColumn && (tableColumn == null || !tableColumn.equals(otherTableColumn))) {
 134             return false;
 135         }
 136         return true;
 137     }
 138 
 139     /**
 140      * Returns a hash code for this {@code TablePosition} object.
 141      * @return a hash code for this {@code TablePosition} object.
 142      */
 143     @Override public int hashCode() {
 144         int hash = 5;
 145         hash = 79 * hash + this.row;
 146         TableColumnBase tableColumn = getTableColumn();
 147         hash = 79 * hash + (tableColumn != null ? tableColumn.hashCode() : 0);
 148         return hash;
 149     }
 150 }