1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package test.com.sun.javafx.test;
  27 
  28 import java.util.List;
  29 import javafx.geometry.NodeOrientation;
  30 import javafx.scene.Group;
  31 import javafx.scene.Node;
  32 import javafx.scene.Parent;
  33 import javafx.scene.ParentShim;
  34 import javafx.scene.Scene;
  35 import javafx.scene.SubScene;
  36 
  37 public abstract class NodeOrientationTestBase {
  38     protected NodeOrientationTestBase() {
  39     }
  40 
  41     public interface StateEncoder {
  42         char map(Scene scene);
  43         char map(Node node);
  44     }
  45 
  46     protected static Scene ltrScene(final Parent rootNode) {
  47         final Scene scene = new Scene(rootNode);
  48         scene.setNodeOrientation(NodeOrientation.LEFT_TO_RIGHT);
  49         return scene;
  50     }
  51 
  52     protected static Group ltrAutGroup(final Node... childNodes) {
  53         return autGroup(NodeOrientation.LEFT_TO_RIGHT, childNodes);
  54     }
  55 
  56     protected static Group rtlAutGroup(final Node... childNodes) {
  57         return autGroup(NodeOrientation.RIGHT_TO_LEFT, childNodes);
  58     }
  59 
  60     protected static Group inhAutGroup(final Node... childNodes) {
  61         return autGroup(NodeOrientation.INHERIT, childNodes);
  62     }
  63 
  64     protected static SubScene inhSubScene(final Parent rootNode) {
  65         final SubScene subScene = new SubScene(rootNode, 400, 300);
  66         subScene.setNodeOrientation(NodeOrientation.INHERIT);
  67         return subScene;
  68     }
  69 
  70     protected static Group ltrManGroup(final Node... childNodes) {
  71         return manGroup(NodeOrientation.LEFT_TO_RIGHT, childNodes);
  72     }
  73 
  74     protected static Group rtlManGroup(final Node... childNodes) {
  75         return manGroup(NodeOrientation.RIGHT_TO_LEFT, childNodes);
  76     }
  77 
  78     protected static Group inhManGroup(final Node... childNodes) {
  79         return manGroup(NodeOrientation.INHERIT, childNodes);
  80     }
  81 
  82     protected static void updateOrientation(final Scene scene,
  83                                             final String updateString) {
  84         final NodeOrientation update =
  85                 decode(updateString.charAt(0));
  86         if (update != null) {
  87             scene.setNodeOrientation(update);
  88         }
  89 
  90         final Node rootNode = scene.getRoot();
  91         if (rootNode != null) {
  92             updateOrientation(rootNode, updateString, 1);
  93         }
  94     }
  95 
  96     protected static String collectState(final Scene scene,
  97                                          final StateEncoder encoder) {
  98         final StringBuilder dest = new StringBuilder();
  99         collectState(dest, scene, encoder);
 100         return dest.toString();
 101     }
 102 
 103     protected static String collectState(final Node node,
 104                                          final StateEncoder encoder) {
 105         final StringBuilder dest = new StringBuilder();
 106         collectState(dest, node, encoder);
 107         return dest.toString();
 108     }
 109 
 110     private static Group autGroup(final NodeOrientation nodeOrientation,
 111                                   final Node... childNodes) {
 112         final Group group = new Group();
 113         group.setNodeOrientation(nodeOrientation);
 114         ParentShim.getChildren(group).setAll(childNodes);
 115 
 116         return group;
 117     }
 118 
 119     private static Group manGroup(final NodeOrientation nodeOrientation,
 120                                   final Node... childNodes) {
 121         final Group group = new Group() {
 122                                     @Override
 123                                     public boolean usesMirroring() {
 124                                         return false;
 125                                     }
 126                                 };
 127         group.setNodeOrientation(nodeOrientation);
 128         ParentShim.getChildren(group).setAll(childNodes);
 129 
 130         return group;
 131     }
 132 
 133     private static int updateOrientation(final Node node,
 134                                          final String updateString,
 135                                          final int index) {
 136         final NodeOrientation update =
 137                 decode(updateString.charAt(index));
 138         if (update != null) {
 139             node.setNodeOrientation(update);
 140         }
 141 
 142         int nextIndex = index + 1;
 143         if (node instanceof Parent) {
 144             final List<Node> childNodes =
 145                     ((Parent) node).getChildrenUnmodifiable();
 146             for (final Node childNode: childNodes) {
 147                 nextIndex = updateOrientation(childNode, updateString,
 148                                               nextIndex);
 149             }
 150         } else if (node instanceof SubScene) {
 151             final Node nextRoot = ((SubScene) node).getRoot();
 152             nextIndex = updateOrientation(nextRoot, updateString,
 153                                           nextIndex);
 154         }
 155 
 156         return nextIndex;
 157     }
 158 
 159     private static NodeOrientation decode(final char updateChar) {
 160         switch (updateChar) {
 161             case '.':
 162                 return null;
 163             case 'L':
 164                 return NodeOrientation.LEFT_TO_RIGHT;
 165             case 'R':
 166                 return NodeOrientation.RIGHT_TO_LEFT;
 167             case 'I':
 168                 return NodeOrientation.INHERIT;
 169             default:
 170                 throw new IllegalArgumentException("Invalid update character");
 171         }
 172     }
 173 
 174     private static void collectState(final StringBuilder dest,
 175                                      final Scene scene,
 176                                      final StateEncoder encoder) {
 177         dest.append(encoder.map(scene));
 178         final Node rootNode = scene.getRoot();
 179         if (rootNode != null) {
 180             collectState(dest, rootNode, encoder);
 181         }
 182     }
 183 
 184     private static void collectState(final StringBuilder dest,
 185                                      final Node node,
 186                                      final StateEncoder encoder) {
 187         dest.append(encoder.map(node));
 188         if (node instanceof Parent) {
 189             final List<Node> childNodes =
 190                     ((Parent) node).getChildrenUnmodifiable();
 191             for (final Node childNode: childNodes) {
 192                 collectState(dest, childNode, encoder);
 193             }
 194         } else if (node instanceof SubScene) {
 195             final Node nextRoot = ((SubScene) node).getRoot();
 196             collectState(dest, nextRoot, encoder);
 197         }
 198     }
 199 }