1 /* 2 * Copyright (c) 2011, 2014, 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 com.sun.javafx.scene.control.behavior; 27 28 import javafx.beans.value.ChangeListener; 29 import javafx.beans.value.WeakChangeListener; 30 import javafx.collections.ObservableList; 31 import javafx.scene.control.TableColumn; 32 import javafx.scene.control.TableColumnBase; 33 import javafx.scene.control.TableFocusModel; 34 import javafx.scene.control.TablePosition; 35 import javafx.scene.control.TablePositionBase; 36 import javafx.scene.control.TableSelectionModel; 37 import javafx.scene.control.TableView; 38 import javafx.scene.control.TableView.TableViewSelectionModel; 39 import com.sun.javafx.scene.control.skin.Utils; 40 41 42 public class TableViewBehavior<T> extends TableViewBehaviorBase<TableView<T>, T, TableColumn<T, ?>> { 43 44 /************************************************************************** 45 * * 46 * Listeners * 47 * * 48 *************************************************************************/ 49 50 private final ChangeListener<TableViewSelectionModel<T>> selectionModelListener = 51 (observable, oldValue, newValue) -> { 52 if (oldValue != null) { 53 oldValue.getSelectedCells().removeListener(weakSelectedCellsListener); 54 } 55 if (newValue != null) { 56 newValue.getSelectedCells().addListener(weakSelectedCellsListener); 57 } 58 }; 59 60 private final WeakChangeListener<TableViewSelectionModel<T>> weakSelectionModelListener = 61 new WeakChangeListener<TableViewSelectionModel<T>>(selectionModelListener); 62 63 private TwoLevelFocusBehavior tlFocus; 64 65 66 67 /************************************************************************** 68 * * 69 * Constructors * 70 * * 71 *************************************************************************/ 72 73 public TableViewBehavior(TableView<T> control) { 74 super(control); 75 76 // Fix for RT-16565 77 control.selectionModelProperty().addListener(weakSelectionModelListener); 78 TableViewSelectionModel<T> sm = control.getSelectionModel(); 79 if (sm != null) { 80 sm.getSelectedCells().addListener(selectedCellsListener); 81 } 82 83 // Only add this if we're on an embedded platform that supports 5-button navigation 84 if (Utils.isTwoLevelFocus()) { 85 tlFocus = new TwoLevelFocusBehavior(control); // needs to be last. 86 } 87 } 88 89 @Override public void dispose() { 90 if (tlFocus != null) tlFocus.dispose(); 91 super.dispose(); 92 } 93 94 /************************************************************************** 95 * * 96 * Implement TableViewBehaviorBase abstract methods * 97 * * 98 *************************************************************************/ 99 100 /** {@inheritDoc} */ 101 @Override protected int getItemCount() { 102 return getControl().getItems() == null ? 0 : getControl().getItems().size(); 103 } 104 105 /** {@inheritDoc} */ 106 @Override protected TableFocusModel getFocusModel() { 107 return getControl().getFocusModel(); 108 } 109 110 /** {@inheritDoc} */ 111 @Override protected TableSelectionModel<T> getSelectionModel() { 112 return getControl().getSelectionModel(); 113 } 114 115 /** {@inheritDoc} */ 116 @Override protected ObservableList<TablePosition> getSelectedCells() { 117 return getControl().getSelectionModel().getSelectedCells(); 118 } 119 120 /** {@inheritDoc} */ 121 @Override protected TablePositionBase getFocusedCell() { 122 return getControl().getFocusModel().getFocusedCell(); 123 } 124 125 /** {@inheritDoc} */ 126 @Override protected int getVisibleLeafIndex(TableColumnBase tc) { 127 return getControl().getVisibleLeafIndex((TableColumn)tc); 128 } 129 130 /** {@inheritDoc} */ 131 @Override protected TableColumn<T,?> getVisibleLeafColumn(int index) { 132 return getControl().getVisibleLeafColumn(index); 133 } 134 135 /** {@inheritDoc} */ 136 @Override protected void editCell(int row, TableColumnBase tc) { 137 getControl().edit(row, (TableColumn)tc); 138 } 139 140 /** {@inheritDoc} */ 141 @Override protected ObservableList<TableColumn<T,?>> getVisibleLeafColumns() { 142 return getControl().getVisibleLeafColumns(); 143 } 144 145 /** {@inheritDoc} */ 146 @Override protected TablePositionBase<TableColumn<T, ?>> 147 getTablePosition(int row, TableColumnBase<T, ?> tc) { 148 return new TablePosition(getControl(), row, (TableColumn)tc); 149 } 150 151 152 153 /************************************************************************** 154 * * 155 * Modify TableViewBehaviorBase behavior * 156 * * 157 *************************************************************************/ 158 159 /** {@inheritDoc} */ 160 @Override protected void selectAllToFocus(boolean setAnchorToFocusIndex) { 161 // Fix for RT-31241 162 if (getControl().getEditingCell() != null) return; 163 164 super.selectAllToFocus(setAnchorToFocusIndex); 165 } 166 }