1 /*
   2  * Copyright (c) 2014, 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 package hello.dialog.wizard;
  26 
  27 import java.lang.reflect.Method;
  28 import java.util.Collections;
  29 import java.util.List;
  30 
  31 import javafx.collections.FXCollections;
  32 import javafx.collections.ObservableList;
  33 import javafx.scene.Group;
  34 import javafx.scene.Node;
  35 import javafx.scene.Parent;
  36 import javafx.scene.control.Control;
  37 import javafx.scene.control.Skin;
  38 import javafx.scene.control.SkinBase;
  39 import javafx.scene.layout.Pane;
  40 
  41 public class ImplUtils {
  42 
  43     private ImplUtils() {
  44         // no-op
  45     }
  46 
  47     public static List<Node> getChildren(Node n, boolean useReflection) {
  48         return n instanceof Parent ? getChildren((Parent)n, useReflection) : Collections.emptyList();
  49     }
  50 
  51     @SuppressWarnings("unchecked")
  52     public static ObservableList<Node> getChildren(Parent p, boolean useReflection) {
  53         ObservableList<Node> children = null;
  54 
  55         // previously we used reflection immediately, now we try to avoid reflection
  56         // by checking the type of the Parent. Still not great...
  57         if (p instanceof Pane) {
  58             // This should cover the majority of layout containers, including
  59             // AnchorPane, FlowPane, GridPane, HBox, Pane, StackPane, TilePane, VBox
  60             children = ((Pane)p).getChildren();
  61         } else if (p instanceof Group) {
  62             children = ((Group)p).getChildren();
  63         } else if (p instanceof Control) {
  64             Control c = (Control) p;
  65             Skin<?> s = c.getSkin();
  66             children = s instanceof SkinBase ? ((SkinBase<?>)s).getChildren() : null;
  67         } else if (useReflection) {
  68             // we really want to avoid using this!!!!
  69             try {
  70                 Method getChildrenMethod = Parent.class.getDeclaredMethod("getChildren"); //$NON-NLS-1$
  71 
  72                 if (getChildrenMethod != null) {
  73                     if (! getChildrenMethod.isAccessible()) {
  74                         getChildrenMethod.setAccessible(true);
  75                     }
  76                     children = (ObservableList<Node>) getChildrenMethod.invoke(p);
  77                 } else {
  78                     // uh oh, trouble
  79                 }
  80             } catch (ReflectiveOperationException | IllegalArgumentException e) {
  81                 throw new RuntimeException("Unable to get children for Parent of type " + p.getClass(), e);
  82             }
  83         }
  84 
  85         if (useReflection && children == null) {
  86             throw new RuntimeException("Unable to get children for Parent of type " + p.getClass() +
  87                                        ". useReflection is set to true");
  88         }
  89 
  90         return children == null ? FXCollections.emptyObservableList() : children;
  91     }
  92 }