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