/
TreeTablePosition.java
142 lines (124 loc) · 6.49 KB
/
TreeTablePosition.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javafx.scene.control;
import java.lang.ref.WeakReference;
import javafx.beans.NamedArg;
/**
* This class is used to represent a single row/column/cell in a TreeTableView.
* This is used throughout the TreeTableView API to represent which rows/columns/cells
* are currently selected, focused, being edited, etc. Note that this class is
* immutable once it is created.
*
* <p>Because the TreeTableView 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 <S> The type of the {@link TreeItem} instances contained within the
* TreeTableView.
* @param <T> The type of the items contained within the TreeTableColumn.
* @see TreeTableView
* @see TreeTableColumn
* @since JavaFX 8.0
*/
public class TreeTablePosition<S,T> extends TablePositionBase<TreeTableColumn<S,T>> {
/***************************************************************************
* *
* Constructors *
* *
**************************************************************************/
/**
* Constructs a TreeTablePosition instance to represent the given row/column
* position in the given TreeTableView instance. Both the TreeTableView and
* TreeTableColumn are referenced weakly in this class, so it is possible that
* they will be null when their respective getters are called.
*
* @param treeTableView The TreeTableView that this position is related to.
* @param row The row that this TreeTablePosition is representing.
* @param tableColumn The TreeTableColumn instance that this TreeTablePosition represents.
*/
public TreeTablePosition(@NamedArg("treeTableView") TreeTableView<S> treeTableView, @NamedArg("row") int row, @NamedArg("tableColumn") TreeTableColumn<S,T> tableColumn) {
this(treeTableView, row, tableColumn, true);
}
// Not public API
TreeTablePosition(@NamedArg("treeTableView") TreeTableView<S> treeTableView, @NamedArg("row") int row, @NamedArg("tableColumn") TreeTableColumn<S,T> tableColumn, boolean doLookup) {
super(row, tableColumn);
this.controlRef = new WeakReference<>(treeTableView);
this.treeItemRef = new WeakReference<>(doLookup ? treeTableView.getTreeItem(row) : null);
nonFixedColumnIndex = treeTableView == null || tableColumn == null ? -1 : treeTableView.getVisibleLeafIndex(tableColumn);
}
// Not public API. A Copy-like constructor with a different row.
// It is used for updating the selection when the TreeItems are
// sorted using TreeTableView.sort() or reordered using setAll().
TreeTablePosition(@NamedArg("treeTableView") TreeTablePosition<S, T> pos, @NamedArg("row") int row) {
super(row, pos.getTableColumn());
this.controlRef = new WeakReference<>(pos.getTreeTableView());
this.treeItemRef = new WeakReference<>(pos.getTreeItem());
nonFixedColumnIndex = pos.getColumn();
}
/***************************************************************************
* *
* Instance Variables *
* *
**************************************************************************/
private final WeakReference<TreeTableView<S>> controlRef;
private final WeakReference<TreeItem<S>> treeItemRef;
int fixedColumnIndex = -1;
private final int nonFixedColumnIndex;
/***************************************************************************
* *
* Public API *
* *
**************************************************************************/
/**
* The column index that this TreeTablePosition represents in the TreeTableView. It
* is -1 if the TreeTableView or TreeTableColumn instances are null at the time the class
* is instantiated (i.e. it is computed at construction).
*/
@Override public int getColumn() {
if (fixedColumnIndex > -1) {
return fixedColumnIndex;
}
return nonFixedColumnIndex;
}
/**
* The TreeTableView that this TreeTablePosition is related to.
* @return the TreeTableView that this TreeTablePosition is related to
*/
public final TreeTableView<S> getTreeTableView() {
return controlRef.get();
}
@Override public final TreeTableColumn<S,T> getTableColumn() {
// Forcing the return type to be TreeTableColumn<S,T>, not TableColumnBase<S,T>
return super.getTableColumn();
}
/**
* Returns the {@link TreeItem} that backs the {@link #getRow()} row}.
* @return the {@link TreeItem} that backs the {@link #getRow()} row}
*/
public final TreeItem<S> getTreeItem() {
return treeItemRef.get();
}
}