001    /* 
002     * This file is part of the Echo Point Project.  This project is a collection
003     * of Components that have extended the Echo Web Application Framework.
004     *
005     * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006     *
007     * The contents of this file are subject to the Mozilla Public License Version
008     * 1.1 (the "License"); you may not use this file except in compliance with
009     * the License. You may obtain a copy of the License at
010     * http://www.mozilla.org/MPL/
011     *
012     * Software distributed under the License is distributed on an "AS IS" basis,
013     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014     * for the specific language governing rights and limitations under the
015     * License.
016     *
017     * Alternatively, the contents of this file may be used under the terms of
018     * either the GNU General Public License Version 2 or later (the "GPL"), or
019     * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020     * in which case the provisions of the GPL or the LGPL are applicable instead
021     * of those above. If you wish to allow use of your version of this file only
022     * under the terms of either the GPL or the LGPL, and not to allow others to
023     * use your version of this file under the terms of the MPL, indicate your
024     * decision by deleting the provisions above and replace them with the notice
025     * and other provisions required by the GPL or the LGPL. If you do not delete
026     * the provisions above, a recipient may use your version of this file under
027     * the terms of any one of the MPL, the GPL or the LGPL.
028     */
029    package echopoint.util;
030    
031    import java.io.Serializable;
032    import java.util.HashMap;
033    import java.util.Iterator;
034    import java.util.Map;
035    
036    import nextapp.echo.app.Component;
037    
038    /** 
039     * <code>ComponentTracker</code> keeps track of components that have been
040     * added and removed from it and its associated component.  This class allows
041     * you to add/remove child components while keeping them in a known 
042     * bucket (the tracker). 
043     */
044    
045    public class ComponentTracker implements Serializable {
046            Map componentMap;
047            Component trackee;
048            
049            /**
050             * Constructs a <code>ComponentTracker</code> that will keep track 
051             * of components on behalf of <code>trackeeComponent</code>
052             *
053             * @param trackeeComponent - the component for which child components
054             *      will be tracked.
055             */
056            public ComponentTracker(Component trackeeComponent) {
057                    this.trackee = trackeeComponent;
058            }
059            
060            /**
061             * @see Component#add(Component)
062             */
063            public void add(Component child) {
064                    add(child,-1);
065            }
066            
067            /**
068             * @see Component#add(Component, int)
069             */
070            public void add(Component child, int index) {
071                    if (componentMap == null)
072                            componentMap = new HashMap();
073                    componentMap.put(child,child);
074                    trackee.add(child,index);
075            }
076            
077            /**
078             * @see Component#remove(Component)
079             */
080            public void remove(Component child) {
081                    if (componentMap != null) {
082                            componentMap.remove(child);
083                    }
084                    trackee.remove(child);
085            }
086    
087            /**
088             * @see Component#remove(int)
089             */
090            public void remove(int index) {
091                    Component child = trackee.getComponent(index);
092                    if (componentMap != null) {
093                            componentMap.remove(child);
094                    }
095                    trackee.remove(index);
096            }
097            
098            /**
099             * @see Component#removeAll()
100             */
101            public void removeAll() {
102                    if (componentMap != null) {
103                            for (Iterator iter = componentMap.keySet().iterator(); iter.hasNext();) {
104                                    Component child = (Component) iter.next();
105                                    trackee.remove(child);
106                                    iter.remove();
107                            }
108                    }
109            }
110            
111            /**
112             * @see Component#getComponents()
113             */
114            public Component[] getComponents() {
115                    if (componentMap == null) {
116                            return new Component[0];
117                    }
118                    Component children[] = new Component[componentMap.size()];
119                    int i = 0;
120                    for (Iterator iter = componentMap.keySet().iterator(); iter.hasNext(); i++) {
121                            Component child = (Component) iter.next();
122                            children[i] = child;
123                    }
124                    return children;
125            }
126            
127            /**
128             * @see Component#getComponentCount()
129             */
130            public int getComponentCount() {
131                    if (componentMap == null) {
132                            return 0;
133                    }
134                    return componentMap.size();
135            }
136            
137            
138    }