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.job.wrap;
  33 
  34 import com.oracle.javafx.scenebuilder.kit.editor.EditorController;
  35 import com.oracle.javafx.scenebuilder.kit.editor.job.JobUtils;
  36 import com.oracle.javafx.scenebuilder.kit.editor.job.wrap.FXOMObjectCourseComparator.BidimensionalComparator;
  37 import com.oracle.javafx.scenebuilder.kit.editor.job.wrap.FXOMObjectCourseComparator.GridCourse;
  38 import com.oracle.javafx.scenebuilder.kit.editor.job.wrap.FXOMObjectCourseComparator.UnidimensionalComparator;
  39 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMObject;
  40 import java.util.ArrayList;
  41 import java.util.Collection;
  42 import java.util.Collections;
  43 import java.util.List;
  44 import javafx.geometry.Bounds;
  45 import javafx.geometry.Orientation;
  46 import javafx.scene.Node;
  47 import javafx.scene.control.SplitPane;
  48 
  49 /**
  50  * Job used to wrap selection in a SplitPane.
  51  */
  52 public class WrapInSplitPaneJob extends AbstractWrapInSubComponentJob {
  53 
  54     public WrapInSplitPaneJob(EditorController editorController) {
  55         super(editorController);
  56         newContainerClass = SplitPane.class;
  57     }
  58 
  59     @Override
  60     protected void modifyNewContainer(final List<FXOMObject> children) {
  61         super.modifyNewContainer(children);
  62 
  63         // Update the SplitPane orientation depending on its children positionning
  64         final Orientation orientation = getOrientation(children);
  65         JobUtils.setOrientation(newContainer, SplitPane.class, orientation.name());
  66     }
  67 
  68     @Override
  69     protected Collection<FXOMObject> sortChildren(List<FXOMObject> children) {
  70         final List<FXOMObject> sorted = new ArrayList<>(children);
  71         final Orientation orientation = getOrientation(children);
  72         Collections.sort(sorted, UnidimensionalComparator.of(orientation));
  73         return sorted;
  74     }
  75 
  76     private Orientation getOrientation(final List<FXOMObject> fxomObjects) {
  77         int cols = computeSizeByCourse(fxomObjects, GridCourse.COL_BY_COL);
  78         if (cols == fxomObjects.size()) {
  79             return Orientation.HORIZONTAL;
  80         }
  81         int rows = computeSizeByCourse(fxomObjects, GridCourse.ROW_BY_ROW);
  82         if (rows == fxomObjects.size()) {
  83             return Orientation.VERTICAL;
  84         }
  85         final Orientation orientation = cols >= rows
  86                 ? Orientation.HORIZONTAL : Orientation.VERTICAL;
  87         return orientation;
  88     }
  89 
  90     private int computeSizeByCourse(
  91             final List<FXOMObject> fxomObjects,
  92             final GridCourse course) {
  93 
  94         final BidimensionalComparator comparator = new BidimensionalComparator(course);
  95         FXOMObject lastObject = null;
  96         int rc = 0;
  97         int max = -1;
  98         for (FXOMObject currentObject : fxomObjects) {
  99             if (lastObject != null) {
 100                 if (comparator.compare(lastObject, currentObject) != 0) {
 101                     final Node lastNode = (Node) lastObject.getSceneGraphObject();
 102                     final Node currentNode = (Node) currentObject.getSceneGraphObject();
 103                     final Bounds lastBounds = lastNode.getBoundsInParent();
 104                     final Bounds currentBounds = currentNode.getBoundsInParent();
 105                     if (course.getMinY(currentBounds) >= course.getMaxY(lastBounds)) {
 106                         rc++;
 107                     }
 108                 }
 109             }
 110             max = Math.max(max, rc);
 111             lastObject = currentObject;
 112         }
 113         return max;
 114     }
 115 }