001 /* 002 * Created on Dec 22, 2007 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 005 * the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 010 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 011 * specific language governing permissions and limitations under the License. 012 * 013 * Copyright @2007-2010 the original author or authors. 014 */ 015 package org.fest.swing.hierarchy; 016 017 import static javax.swing.SwingUtilities.isDescendingFrom; 018 019 import java.awt.*; 020 import java.util.*; 021 import java.util.List; 022 023 /** 024 * Understands a component hierarchy created with a specified <code>{@link Component}</code> as root. 025 * 026 * @author Alex Ruiz 027 */ 028 public final class SingleComponentHierarchy implements ComponentHierarchy { 029 030 private final Container root; 031 private final List<Container> list = new ArrayList<Container>(); 032 private final ComponentHierarchy hierarchy; 033 034 /** 035 * Creates a new </code>{@link SingleComponentHierarchy}</code>. 036 * @param root the root component for this hierarchy 037 * @param hierarchy the base component hierarchy. 038 */ 039 public SingleComponentHierarchy(Container root, ComponentHierarchy hierarchy) { 040 this.root = root; 041 this.hierarchy = hierarchy; 042 list.add(root); 043 } 044 045 /** 046 * Returns the root component in this hierarchy. 047 * @return the root component in this hierarchy. 048 */ 049 public Container root() { return root; } 050 051 /** 052 * Returns the parent component for the given <code>{@link Component}</code>. 053 * @param c the given <code>Component</code>. 054 * @return the parent component for the given <code>{@link Component}</code>. 055 */ 056 public Container parentOf(Component c) { 057 return hierarchy.parentOf(c); 058 } 059 060 /** 061 * Returns a collection containing only the root <code>{@link Component}</code> in this hierarchy. 062 * @return a collection containing only the root <code>{@link Component}</code> in this hierarchy. 063 */ 064 public Collection<? extends Container> roots() { 065 return list; 066 } 067 068 /** 069 * Returns all sub-components of the given <code>{@link Component}</code>. 070 * @return all sub-components of the given <code>{@link Component}</code>. 071 */ 072 public Collection<Component> childrenOf(Component c) { 073 return hierarchy.childrenOf(c); 074 } 075 076 /** 077 * Returns whether the hierarchy contains the given <code>{@link Component}</code>. 078 * @return whether the hierarchy contains the given <code>{@link Component}</code>. 079 */ 080 public boolean contains(Component c) { 081 return hierarchy.contains(c) && isDescendingFrom(c, root); 082 } 083 084 /** 085 * Provides proper disposal of the given <code>{@link Window}</code>, appropriate to this hierarchy. After disposal, 086 * the <code>{@link Window}</code> and its descendants will no longer be reachable from this hierarchy. 087 * @param w the window to dispose. 088 */ 089 public void dispose(Window w) { 090 hierarchy.dispose(w); 091 } 092 }