1 /* 2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. 3 * All rights reserved. Use is subject to license terms. 4 * 5 * This file is available and licensed under the following license: 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * - Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * - Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the distribution. 16 * - Neither the name of Oracle Corporation nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package com.oracle.javafx.scenebuilder.kit.editor.panel.content.driver; 33 34 import com.oracle.javafx.scenebuilder.kit.util.Deprecation; 35 import com.sun.javafx.scene.control.skin.TableColumnHeader; 36 import java.util.ArrayList; 37 import java.util.Collections; 38 import java.util.Iterator; 39 import java.util.List; 40 import java.util.Set; 41 import javafx.geometry.BoundingBox; 42 import javafx.geometry.Bounds; 43 import javafx.geometry.Point2D; 44 import javafx.scene.Node; 45 import javafx.scene.control.TableColumn; 46 import javafx.scene.control.TableColumnBase; 47 import javafx.scene.control.TableView; 48 49 /** 50 * A temporary class that should extend TableViewDesignInfo and adds 51 * some additional verbs for managing TableView at design time. 52 * This could potentially move to TabDesignInfo some day. 53 * 54 */ 55 public class TableViewDesignInfoX /* extends TableViewDesignInfo */ { 56 57 58 public Bounds getColumnBounds(TableColumn<?,?> tableColumn) { 59 final TableView<?> tv = tableColumn.getTableView(); 60 final Bounds tb = tv.getLayoutBounds(); 61 final Bounds hb = getColumnHeaderBounds(tableColumn); 62 63 // 64 // x0 x1 65 // 66 // +--------------------------------------+ 67 // y0 | +--------------+ | 68 // | | header | | 69 // | +--------------+ | 70 // +--------------------------------------+ 71 // | | 72 // | table view content | 73 // | | 74 // y1 +--------------------------------------+ 75 // 76 77 final double x0 = hb.getMinX(); 78 final double x1 = hb.getMaxX(); 79 final double y0 = hb.getMinY(); 80 final double y1 = tb.getMaxY(); 81 82 return new BoundingBox(x0, y0, x1 - x0, y1 - y0); 83 } 84 85 86 public Bounds getColumnHeaderBounds(TableColumn<?,?> tableColumn) { 87 final TableView<?> tv = tableColumn.getTableView(); 88 final Node hn = getColumnNode(tableColumn); 89 return Deprecation.localToLocal(hn, hn.getLayoutBounds(), tv); 90 } 91 92 93 public Node getColumnNode(TableColumn<?,?> tableColumn) { 94 assert tableColumn != null; 95 assert tableColumn.getTableView() != null; 96 97 98 // Looks for the sub nodes which match the .column-header CSS selector 99 final TableView<?> tableView = tableColumn.getTableView(); 100 final Set<Node> set = tableView.lookupAll(".column-header"); //NOI18N 101 102 // Searches the result for the node associated to 'tableColumn'. 103 // This item has (TableColumn.class, tableColumn) in its property list. 104 Node result = null; 105 final Iterator<Node> it = set.iterator(); 106 while ((result == null) && it.hasNext()) { 107 Node n = it.next(); 108 assert n instanceof TableColumnHeader; 109 final TableColumnBase<?,?> tc = ((TableColumnHeader)n).getTableColumn(); 110 if (tc == tableColumn) { 111 result = n; 112 } 113 } 114 115 return result; 116 } 117 118 119 public <T> TableColumn<T,?> lookupColumn(TableView<T> tableView, double sceneX, double sceneY) { 120 TableColumn<T,?> result = null; 121 122 // 123 // x 124 // +--------------------------------------+ 125 // | +----------------------------+ | 126 // #1 y | | header | | 127 // | +----------------------------+ | 128 // | +--------------+-------------+ | 129 // #2 y | | header | header | | 130 // | +--------------+-------------+ | 131 // +--------------------------------------+ 132 // | | 133 // #3 y | table view content | 134 // | | 135 // +--------------------------------------+ 136 // 137 138 // Walk through the column to see if one contains 'x' vertical 139 List<TableColumn<T,?>> tableColumns = tableView.getColumns(); 140 List<TableColumn<T,?>> columnPath = new ArrayList<>(); 141 while (tableColumns.isEmpty() == false) { 142 final TableColumn<T,?> tc = lookupColumn(tableColumns, sceneX); 143 if (tc != null) { 144 columnPath.add(0, tc); 145 tableColumns = tc.getColumns(); 146 } else { 147 tableColumns = Collections.emptyList(); // To stop the loop 148 } 149 } 150 151 if (columnPath.isEmpty()) { 152 // No column contains sceneX 153 result = null; 154 } else { 155 // Check if one column in columnPath contains (sceneX, sceneY) 156 // => case #1 or #2 157 for (TableColumn<T,?> tc : columnPath) { 158 final Node headerNode = getColumnNode(tc); 159 final Bounds headerBounds = headerNode.getLayoutBounds(); 160 final Point2D p = headerNode.sceneToLocal(sceneX, sceneY, true /* rootScene */); 161 if (headerBounds.contains(p)) { 162 result = tc; 163 break; 164 } 165 } 166 } 167 168 return result; 169 } 170 171 172 private <T> TableColumn<T,?> lookupColumn( 173 List<TableColumn<T,?>> tableColumns, double sceneX) { 174 TableColumn<T,?> result = null; 175 176 // Walk through the columns to see if one contains 'x' vertical 177 for (TableColumn<T,?> tc : tableColumns) { 178 final Node headerNode = getColumnNode(tc); 179 if (headerNode != null) { 180 final Bounds headerBounds = headerNode.getLayoutBounds(); 181 final Point2D p = headerNode.sceneToLocal(sceneX, 0, true /* rootScene */); 182 if ((headerBounds.getMinX() <= p.getX()) 183 && (p.getX() < headerBounds.getMaxX())) { 184 result = tc; 185 break; 186 } 187 } 188 } 189 190 return result; 191 } 192 }