1 /*
   2  * Copyright (c) 2014, 2016, 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.util.Collections;
  28 import java.util.List;
  29 
  30 import javafx.collections.FXCollections;
  31 import javafx.collections.ObservableList;
  32 import javafx.scene.Group;
  33 import javafx.scene.Node;
  34 import javafx.scene.Parent;
  35 import javafx.scene.control.Control;
  36 import javafx.scene.control.Skin;
  37 import javafx.scene.control.SkinBase;
  38 import javafx.scene.layout.Pane;
  39 
  40 public class ImplUtils {
  41 
  42     private ImplUtils() {
  43         // no-op
  44     }
  45 
  46     public static List<Node> getChildren(Node n) {
  47         return n instanceof Parent ? getChildren((Parent)n) : Collections.emptyList();
  48     }
  49 
  50     @SuppressWarnings("unchecked")
  51     public static ObservableList<Node> getChildren(Parent p) {
  52         ObservableList<Node> children = null;
  53 
  54         // previously we used reflection immediately, now we try to avoid reflection
  55         // by checking the type of the Parent. Still not great...
  56         if (p instanceof Pane) {
  57             // This should cover the majority of layout containers, including
  58             // AnchorPane, FlowPane, GridPane, HBox, Pane, StackPane, TilePane, VBox
  59             children = ((Pane)p).getChildren();
  60         } else if (p instanceof Group) {
  61             children = ((Group)p).getChildren();
  62         } else if (p instanceof Control) {
  63             Control c = (Control) p;
  64             Skin<?> s = c.getSkin();
  65             children = s instanceof SkinBase ? ((SkinBase<?>)s).getChildren() : null;
  66         }
  67         return children == null ? FXCollections.emptyObservableList() : children;
  68     }
  69 }