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.editor.drag.target.AbstractDropTarget;
  35 import com.oracle.javafx.scenebuilder.kit.editor.drag.target.ContainerZDropTarget;
  36 import com.oracle.javafx.scenebuilder.kit.editor.panel.content.ContentPanelController;
  37 import com.oracle.javafx.scenebuilder.kit.editor.panel.content.driver.tring.AbstractTring;
  38 import com.oracle.javafx.scenebuilder.kit.editor.panel.content.driver.tring.VBoxTring;
  39 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMInstance;
  40 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMObject;
  41 import com.oracle.javafx.scenebuilder.kit.metadata.util.DesignHierarchyMask;
  42 import javafx.geometry.Bounds;
  43 import javafx.scene.Node;
  44 import javafx.scene.layout.VBox;
  45 
  46 /**
  47  *
  48  */
  49 public class VBoxDriver extends AbstractNodeDriver {
  50 
  51     public VBoxDriver(ContentPanelController contentPanelController) {
  52         super(contentPanelController);
  53     }
  54 
  55     /*
  56      * AbstractDriver
  57      */
  58     @Override
  59     public AbstractDropTarget makeDropTarget(FXOMObject fxomObject, double sceneX, double sceneY) {
  60 
  61         assert fxomObject instanceof FXOMInstance;
  62         assert fxomObject.getSceneGraphObject() instanceof VBox;
  63 
  64         final VBox hbox = (VBox) fxomObject.getSceneGraphObject();
  65         assert hbox.getScene() != null;
  66 
  67         final double localY = hbox.sceneToLocal(sceneX, sceneY, true /* rootScene */).getY();
  68         final int childCount = hbox.getChildrenUnmodifiable().size();
  69 
  70         final int targetIndex;
  71         if (childCount == 0) {
  72             // No children : we append
  73             targetIndex = -1;
  74 
  75         } else {
  76             assert childCount >= 1;
  77 
  78             int childIndex = 0;
  79             double midY;
  80             do {
  81                 Node child = hbox.getChildrenUnmodifiable().get(childIndex++);
  82                 Bounds childBounds = child.getBoundsInParent();
  83                 midY = (childBounds.getMinY() + childBounds.getMaxY()) / 2.0;
  84             } while ((localY > midY) && (childIndex < childCount));
  85 
  86             if (localY <= midY) {
  87                 assert childIndex-1 < childCount;
  88                 targetIndex = childIndex-1;
  89             } else {
  90                 targetIndex = -1;
  91             }
  92         }
  93 
  94         final FXOMObject beforeChild;
  95         if (targetIndex == -1) {
  96             beforeChild = null;
  97         } else {
  98             final DesignHierarchyMask m = new DesignHierarchyMask(fxomObject);
  99             if (targetIndex < m.getSubComponentCount()) {
 100                 beforeChild = m.getSubComponentAtIndex(targetIndex);
 101             } else {
 102                 beforeChild = null;
 103             }
 104         }
 105 
 106         return new ContainerZDropTarget((FXOMInstance)fxomObject, beforeChild);
 107     }
 108 
 109 
 110     @Override
 111     public AbstractTring<?> makeTring(AbstractDropTarget dropTarget) {
 112         assert dropTarget instanceof ContainerZDropTarget;
 113         assert dropTarget.getTargetObject() instanceof FXOMInstance;
 114         assert dropTarget.getTargetObject().getSceneGraphObject() instanceof VBox;
 115 
 116         final ContainerZDropTarget zDropTarget = (ContainerZDropTarget) dropTarget;
 117         final int targetIndex;
 118         if (zDropTarget.getBeforeChild() == null) {
 119             targetIndex = -1;
 120         } else {
 121             targetIndex = zDropTarget.getBeforeChild().getIndexInParentProperty();
 122         }
 123         return new VBoxTring(contentPanelController,
 124                 (FXOMInstance) dropTarget.getTargetObject(),
 125                 targetIndex);
 126     }
 127 
 128 }