Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.scene.control;
027
028import java.lang.ref.WeakReference;
029
030/**
031 * This class is used to represent a single row/column/cell in a table. Concrete
032 * subclasses of this abstract class are used in the {@link TableView} and 
033 * {@link TreeTableView} APIs to represent which rows/columns/cells
034 * are currently selected, focused, being edited, etc. Note that this class is
035 * immutable once it is created.
036 *
037 * <p>Because the TableView and TreeTableView controls can have different
038 * {@link SelectionMode selection modes}, the row and column properties in
039 * TablePositionBase can be 'disabled' to represent an entire row or column. This is
040 * done by setting the unrequired property to -1 or null.
041 *
042 * @see TablePosition
043 * @see TreeTablePosition
044 */
045public abstract class TablePositionBase<TC extends TableColumnBase> {
046    
047    /***************************************************************************
048     *                                                                         *
049     * Constructors                                                            *
050     *                                                                         *
051     **************************************************************************/  
052
053    /**
054     * Constructs a TablePositionBase instance to represent the given row/column
055     * position in the underlying table instance (which is not part of the 
056     * abstract TablePositionBase class, but is part of concrete subclasses such
057     * as {@link TablePosition} and {@link TreeTablePosition}). In all cases, 
058     * all fields inside TablePositionBase instances are referenced weakly so as
059     * to prevent memory leaks. This means that it is possible (but unlikely) 
060     * that the get methods will return null.
061     * 
062     * @param row The row that this TablePosition is representing.
063     * @param tableColumn The TableColumn instance that this TablePosition represents.
064     */
065    protected TablePositionBase(int row, TC tableColumn) {
066        this.row = row;
067        this.tableColumnRef = new WeakReference<TC>(tableColumn);
068    }
069    
070    
071    
072    /***************************************************************************
073     *                                                                         *
074     * Instance Variables                                                      *
075     *                                                                         *
076     **************************************************************************/
077
078    private final int row;
079    private final WeakReference<TC> tableColumnRef;
080
081    
082
083    /***************************************************************************
084     *                                                                         *
085     * Public API                                                              *
086     *                                                                         *
087     **************************************************************************/
088    
089    /**
090     * The row that this TablePosition represents in the TableView.
091     */
092    public int getRow() {
093        return row;
094    }
095    
096    /**
097     * The column index that this TablePosition represents in the TableView. It
098     * is -1 if the TableView or TableColumn instances are null.
099     */
100    public abstract int getColumn();
101    
102    /**
103     * The TableColumn that this TablePosition represents in the TableView.
104     */
105    public TC getTableColumn() {
106        return tableColumnRef.get();
107    }
108
109    /**
110     * Indicates whether some other object is "equal to" this one.
111     * @param obj the reference object with which to compare.
112     * @return {@code true} if this object is equal to the {@code obj} argument; {@code false} otherwise.
113     */
114    @Override public boolean equals(Object obj) {
115        if (obj == null) {
116            return false;
117        }
118        if (getClass() != obj.getClass()) {
119            return false;
120        }
121        @SuppressWarnings("unchecked")
122        final TablePositionBase other = (TablePositionBase) obj;
123        if (this.row != other.row) {
124            return false;
125        }
126        TC tableColumn = getTableColumn();
127        TableColumnBase otherTableColumn = other.getTableColumn();
128        if (tableColumn != otherTableColumn && (tableColumn == null || !tableColumn.equals(otherTableColumn))) {
129            return false;
130        }
131        return true;
132    }
133
134    /**
135     * Returns a hash code for this {@code TablePosition} object.
136     * @return a hash code for this {@code TablePosition} object.
137     */ 
138    @Override public int hashCode() {
139        int hash = 5;
140        hash = 79 * hash + this.row;
141        TableColumnBase tableColumn = getTableColumn();
142        hash = 79 * hash + (tableColumn != null ? tableColumn.hashCode() : 0); 
143        return hash;
144    }
145}