1 /*
   2  * Copyright (c) 2012, 2015, 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 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         Tab selectedTab = tabPane.getSelectionModel().getSelectedItem();
 102         if (selectedTab != null) {
 103             result = selectedTab.getContent();
 104         } else {
 105             result = null;
 106         }
 107 
 108         return result;
 109     }
 110 
 111 
 112     /**
 113      * Returns the node representing the pulldown menu in the TabPane skin.
 114      */
 115     public Node getControlMenuNode(TabPane tabPane) {
 116         assert tabPane != null;
 117 
 118         // Looks for the sub node which matches the '.control-buttons-tab' selector
 119         return tabPane.lookup(".control-buttons-tab"); //NOI18N
 120     }
 121 
 122 
 123     /**
 124      * Returns the tab below (sceneX, sceneY) if any.
 125      * If (sceneX, sceneY) is over a tab header, returns the matching Tab.
 126      * If (sceneX, sceneY) is over the tab content area, returns the Tab
 127      * currently selected.
 128      *
 129      * @param tabPane a tab pane
 130      * @param sceneX x in scene coordinate space
 131      * @param sceneY y in scene coordinate space
 132      * @return null or the tab below (sceneX, sceneY).
 133      */
 134     public Tab lookupTab(TabPane tabPane, double sceneX, double sceneY) {
 135         Tab result = null;
 136 
 137         // The control menu may cover a tab header.
 138         // So we check first if (sceneX, sceneY) is in the control menu.
 139         // If yes, we return null because the control menu is considered
 140         // as a piece of the tab pane.
 141         final Node controlMenuNode = getControlMenuNode(tabPane);
 142         final boolean insideControlMenu;
 143         if (controlMenuNode == null) {
 144             insideControlMenu = false;
 145         } else {
 146             Point2D p = controlMenuNode.sceneToLocal(sceneX, sceneY, true /* rootScene */);
 147             insideControlMenu = controlMenuNode.contains(p);
 148         }
 149 
 150         // If not inside the control menu, then checks:
 151         //  1) (sceneX, sceneY) is over a tab header => returns the matching tab
 152         //  2) (sceneX, sceneY) is over the content area => returns the selected tab
 153         if (insideControlMenu == false) {
 154 
 155             // Checks the headers.
 156             final Iterator<Tab> it = tabPane.getTabs().iterator();
 157             while ((result == null) && it.hasNext()) {
 158                 Tab tab = it.next();
 159                 Node tabNode = getTabNode(tabPane, tab);
 160                 assert tabNode != null;
 161                 Point2D p = tabNode.sceneToLocal(sceneX, sceneY, true /* rootScene */);
 162                 if (tabNode.contains(p)) {
 163                     result = tab;
 164                 }
 165             }
 166 
 167             // Checks the content area
 168             if (result == null) {
 169                 final Node contentNode = getContentNode(tabPane);
 170                 if (contentNode != null) {
 171                     final Point2D p = contentNode.sceneToLocal(sceneX, sceneY, true /* rootScene */);
 172                     if (contentNode.contains(p)) {
 173                         result = tabPane.getSelectionModel().getSelectedItem();
 174                     }
 175                 }
 176             }
 177         }
 178 
 179 
 180         return result;
 181     }
 182 
 183 
 184     public Bounds computeTabBounds(TabPane tabPane, Tab tab) {
 185         final Node tabNode = getTabNode(tabPane, tab);
 186         final Bounds b = tabNode.getLayoutBounds();
 187 
 188         // Convert b from tabNode local space to tabPane local space
 189         final Point2D min = Deprecation.localToLocal(tabNode, b.getMinX(), b.getMinY(), tabPane);
 190         final Point2D max = Deprecation.localToLocal(tabNode, b.getMaxX(), b.getMaxY(), tabPane);
 191         return makeBoundingBox(min, max);
 192     }
 193 
 194 
 195     public Bounds computeContentAreaBounds(TabPane tabPane) {
 196         final Node contentNode = getContentNode(tabPane);
 197         assert contentNode != null;
 198         final Bounds b = contentNode.getLayoutBounds();
 199 
 200         // Convert b from contentNode local space to tabPane local space
 201         final Point2D min = Deprecation.localToLocal(contentNode, b.getMinX(), b.getMinY(), tabPane);
 202         final Point2D max = Deprecation.localToLocal(contentNode, b.getMaxX(), b.getMaxY(), tabPane);
 203         return makeBoundingBox(min, max);
 204     }
 205 
 206     private static BoundingBox makeBoundingBox(Point2D p1, Point2D p2) {
 207         return new BoundingBox(
 208                 Math.min(p1.getX(), p2.getX()),
 209                 Math.min(p1.getY(), p2.getY()),
 210                 Math.abs(p2.getX() - p1.getX()),
 211                 Math.abs(p2.getY() - p1.getY()));
 212     }
 213 }