PivotUtils.java
01 /*
02  * SPDX-License-Identifier: Apache-2.0
03  *
04  * Copyright 2008-2017 the original author or authors.
05  *
06  * Licensed under the Apache License, Version 2.0 (the "License");
07  * you may not use this file except in compliance with the License.
08  * You may obtain a copy of the License at
09  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package griffon.pivot.support;
19 
20 import griffon.exceptions.InstanceMethodInvocationException;
21 import org.apache.pivot.wtk.Component;
22 import org.apache.pivot.wtk.Container;
23 
24 import javax.annotation.Nonnull;
25 import javax.annotation.Nullable;
26 
27 import static griffon.util.GriffonClassUtils.invokeExactInstanceMethod;
28 import static griffon.util.GriffonNameUtils.requireNonBlank;
29 import static java.util.Objects.requireNonNull;
30 
31 /**
32  @author Andres Almiray
33  @since 2.0.0
34  */
35 public class PivotUtils {
36     private PivotUtils() {
37         // prevent instantiation
38     }
39 
40     /**
41      * Searches a component by name in a particular component hierarchy.<p>
42      * A component must have a value for its <tt>name</tt> property if it's
43      * to be found with this method.<br/>
44      * This method performs a depth-first search.
45      *
46      @param name the value of the component's <tt>name</tt> property
47      @param root the root of the component hierarchy from where searching
48      *             searching should start
49      *
50      @return the component reference if found, null otherwise
51      */
52     @Nullable
53     public static Component findComponentByName(@Nonnull String name, @Nonnull Container root) {
54         requireNonNull(root, "Argument 'root' must not be null");
55         requireNonBlank(name, "Argument 'name' must not be blank");
56         if (name.equals(root.getName())) {
57             return root;
58         }
59 
60         for (Component comp : root) {
61             if (name.equals(comp.getName())) {
62                 return comp;
63             }
64             if (comp instanceof Container) {
65                 Component found = findComponentByName(name, (Containercomp);
66                 if (found != null) {
67                     return found;
68                 }
69             }
70         }
71 
72         // finally attempt calling getContent()
73         try {
74             Component component = (ComponentinvokeExactInstanceMethod(root, "getContent");
75             if (component != null) {
76                 if (name.equals(component.getName())) {
77                     return component;
78                 else if (component instanceof Container) {
79                     return findComponentByName(name, (Containercomponent);
80                 }
81             }
82         catch (InstanceMethodInvocationException imie) {
83             // ignore
84         }
85 
86         return null;
87     }
88 }