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.TabPaneSkin;
  36 import java.util.Iterator;
  37 import java.util.Set;
  38 import javafx.geometry.BoundingBox;
  39 import javafx.geometry.Bounds;
  40 import javafx.geometry.Point2D;
  41 import javafx.scene.Node;
  42 import javafx.scene.control.Tab;
  43 import javafx.scene.control.TabPane;
  44 
  45 /**
  46  * @treatAsPrivate
  47  * A temporary class that should extend TabDesignInfo and adds
  48  * some additional verbs for managing TabPane at design time.
  49  * This could potentially move to TabDesignInfo some day.
  50  *
  51  */
  52 public class TabPaneDesignInfoX /* extends TabDesignInfo */ {
  53 
  54 
  55     /**
  56      * Returns the node representing the tab header in the TabPane skin.
  57      * @param tabPane
  58      * @param tab
  59      * @return 
  60      */
  61     public Node getTabNode(TabPane tabPane, Tab tab) {
  62         assert tabPane != null;
  63         assert tabPane.getTabs().contains(tab);
  64         
  65         // Looks for the sub nodes which match the .tab CSS selector
  66         final Set<Node> set = tabPane.lookupAll(".tab"); //NOI18N
  67         
  68         // Searches the result for the node associated to 'tab'.
  69         // This item has (Tab.class, tab) in its property list.
  70         Node result = null;
  71         final Iterator<Node> it = set.iterator();
  72         while ((result == null) && it.hasNext()) {
  73             Node n = it.next();
  74             if (n.getProperties().get(Tab.class) == tab) {
  75                 result = n;
  76             }
  77         }
  78 
  79         return result;
  80     }
  81     
  82     /**
  83      * Returns the node representing the content area in the TabPane skin.
  84      * @param tabPane
  85      * @param tab
  86      * @return 
  87      */
  88     public Node getContentNode(TabPane tabPane) {
  89         assert tabPane != null;
  90         
  91         final Node result;
  92        
  93         if (tabPane.getSkin() != null) {
  94             assert tabPane.getSkin() instanceof TabPaneSkin;
  95             final TabPaneSkin tabPaneSkin = (TabPaneSkin) tabPane.getSkin();
  96             result = tabPaneSkin.getSelectedTabContentRegion();
  97         } else {
  98             result = null;
  99         }
 100 
 101         return result;
 102     }
 103     
 104     
 105     /**
 106      * Returns the node representing the pulldown menu in the TabPane skin.
 107      */
 108     public Node getControlMenuNode(TabPane tabPane) {
 109         assert tabPane != null;
 110         
 111         // Looks for the sub node which matches the '.control-buttons-tab' selector
 112         return tabPane.lookup(".control-buttons-tab"); //NOI18N
 113     }
 114     
 115     
 116     /**
 117      * Returns the tab below (sceneX, sceneY) if any.
 118      * If (sceneX, sceneY) is over a tab header, returns the matching Tab.
 119      * If (sceneX, sceneY) is over the tab content area, returns the Tab
 120      * currently selected.
 121      * 
 122      * @param tabPane a tab pane
 123      * @param sceneX x in scene coordinate space
 124      * @param sceneY y in scene coordinate space
 125      * @return null or the tab below (sceneX, sceneY).
 126      */
 127     public Tab lookupTab(TabPane tabPane, double sceneX, double sceneY) {
 128         Tab result = null;
 129         
 130         // The control menu may cover a tab header.
 131         // So we check first if (sceneX, sceneY) is in the control menu.
 132         // If yes, we return null because the control menu is considered
 133         // as a piece of the tab pane.
 134         final Node controlMenuNode = getControlMenuNode(tabPane);
 135         final boolean insideControlMenu;
 136         if (controlMenuNode == null) {
 137             insideControlMenu = false;
 138         } else {
 139             Point2D p = controlMenuNode.sceneToLocal(sceneX, sceneY, true /* rootScene */);
 140             insideControlMenu = controlMenuNode.contains(p);
 141         }
 142         
 143         // If not inside the control menu, then checks:
 144         //  1) (sceneX, sceneY) is over a tab header => returns the matching tab
 145         //  2) (sceneX, sceneY) is over the content area => returns the selected tab
 146         if (insideControlMenu == false) {
 147             
 148             // Checks the headers.
 149             final Iterator<Tab> it = tabPane.getTabs().iterator();
 150             while ((result == null) && it.hasNext()) {
 151                 Tab tab = it.next();
 152                 Node tabNode = getTabNode(tabPane, tab);
 153                 assert tabNode != null;
 154                 Point2D p = tabNode.sceneToLocal(sceneX, sceneY, true /* rootScene */);
 155                 if (tabNode.contains(p)) {
 156                     result = tab;
 157                 }
 158             }
 159 
 160             // Checks the content area
 161             if (result == null) {
 162                 final Node contentNode = getContentNode(tabPane);
 163                 if (contentNode != null) {
 164                     final Point2D p = contentNode.sceneToLocal(sceneX, sceneY, true /* rootScene */);
 165                     if (contentNode.contains(p)) {
 166                         result = tabPane.getSelectionModel().getSelectedItem();
 167                     }
 168                 }
 169             }
 170         }
 171         
 172         
 173         return result;
 174     }
 175 
 176     
 177     public Bounds computeTabBounds(TabPane tabPane, Tab tab) {
 178         final Node tabNode = getTabNode(tabPane, tab);
 179         final Bounds b = tabNode.getLayoutBounds();
 180 
 181         // Convert b from tabNode local space to tabPane local space
 182         final Point2D min = Deprecation.localToLocal(tabNode, b.getMinX(), b.getMinY(), tabPane);
 183         final Point2D max = Deprecation.localToLocal(tabNode, b.getMaxX(), b.getMaxY(), tabPane);
 184         return makeBoundingBox(min, max);
 185     }
 186     
 187     
 188     public Bounds computeContentAreaBounds(TabPane tabPane) {
 189         final Node contentNode = getContentNode(tabPane);
 190         assert contentNode != null;
 191         final Bounds b = contentNode.getLayoutBounds();
 192         
 193         // Convert b from contentNode local space to tabPane local space
 194         final Point2D min = Deprecation.localToLocal(contentNode, b.getMinX(), b.getMinY(), tabPane);
 195         final Point2D max = Deprecation.localToLocal(contentNode, b.getMaxX(), b.getMaxY(), tabPane);
 196         return makeBoundingBox(min, max);
 197     }
 198     
 199     private static BoundingBox makeBoundingBox(Point2D p1, Point2D p2) {
 200         return new BoundingBox(
 201                 Math.min(p1.getX(), p2.getX()),
 202                 Math.min(p1.getY(), p2.getY()),
 203                 Math.abs(p2.getX() - p1.getX()),
 204                 Math.abs(p2.getY() - p1.getY()));
 205     }
 206 }