001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/portal/context/LayerGroup.java $
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005     - Department of Geography, University of Bonn -
006     and
007     - lat/lon GmbH -
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035     ----------------------------------------------------------------------------*/
036    package org.deegree.portal.context;
037    
038    import java.util.ArrayList;
039    
040    import java.util.Collections;
041    import java.util.List;
042    
043    /**
044     * TODO add class documentation here
045     * 
046     * @author <a href="mailto:name@deegree.org">Andreas Poth</a>
047     * @author last edited by: $Author: apoth $
048     * 
049     * @version $Revision: 20771 $, $Date: 2009-11-12 18:37:45 +0100 (Do, 12. Nov 2009) $
050     */
051    public class LayerGroup extends MapModelEntry {
052    
053        private boolean expanded;
054    
055        private List<MMLayer> layers;
056    
057        private List<LayerGroup> layerGroups;
058    
059        private List<MapModelEntry> entries;
060    
061        /**
062         * 
063         * @param identifier
064         * @param title
065         * @param hidden
066         * @param expanded
067         * @param parent
068         * @param owner
069         */
070        public LayerGroup( String identifier, String title, boolean hidden, boolean expanded, LayerGroup parent,
071                           MapModel owner ) {
072            super( identifier, title, hidden, parent, owner );
073            layers = new ArrayList<MMLayer>();
074            layerGroups = new ArrayList<LayerGroup>();
075            entries = new ArrayList<MapModelEntry>();
076            this.expanded = expanded;
077        }
078    
079        /**
080         * 
081         * @param layer
082         * @param antecessor
083         * @param first
084         */
085        public void insert( MMLayer layer, MapModelEntry antecessor, boolean first ) {
086            if ( layer.getParent() == null || !layer.getParent().equals( parent ) ) {
087                layers.remove( layer );
088                layers.add( layer );
089                layer.setParent( this );
090                int i = 0;
091                while ( i < entries.size() && !entries.get( i ).equals( antecessor ) ) {
092                    i++;
093                }
094                if ( i >= entries.size() - 1 ) {
095                    if ( first && antecessor == null ) {
096                        entries.add( 0, layer );
097                    } else {
098                        entries.add( layer );
099                    }
100                } else {
101                    if ( first && antecessor == null ) {
102                        entries.add( 0, layer );
103                    } else {
104                        entries.add( i + 1, layer );
105                    }
106                }
107            }
108        }
109    
110        /**
111         * 
112         * @param layerGroup
113         * @param antecessor
114         * @param first
115         */
116        public void insert( LayerGroup layerGroup, MapModelEntry antecessor, boolean first ) {
117            if ( layerGroup.getParent() == null || !layerGroup.getParent().equals( parent ) ) {
118                // register as child
119                layerGroups.add( layerGroup );
120                // that this a parent
121                layerGroup.setParent( this );
122    
123                int i = 0;
124                if ( entries.size() > 0 ) {
125                    while ( i < entries.size() && !entries.get( i ).equals( antecessor ) ) {
126                        i++;
127                    }
128                }
129                if ( i >= entries.size() - 1 ) {
130                    if ( first && antecessor == null ) {
131                        entries.add( 0, layerGroup );
132                    } else {
133                        entries.add( layerGroup );
134                    }
135                } else {
136                    if ( first && antecessor == null ) {
137                        entries.add( 0, layerGroup );
138                    } else {
139                        entries.add( i + 1, layerGroup );
140                    }
141                }
142            }
143        }
144    
145        /**
146         * 
147         * @param layer
148         */
149        public void addLayer( MMLayer layer ) {
150            if ( !layers.contains( layer ) ) {
151                layers.add( layer );
152                entries.add( layer );
153            }
154        }
155    
156        /**
157         * 
158         * @param layerGroup
159         */
160        public void addLayerGroup( LayerGroup layerGroup ) {
161            layerGroups.add( layerGroup );
162            entries.add( layerGroup );
163        }
164    
165        /**
166         * @return the layerGroups
167         */
168        public List<LayerGroup> getLayerGroups() {
169            return Collections.unmodifiableList( layerGroups );
170        }
171    
172        /**
173         * @return the layers
174         */
175        public List<MMLayer> getLayers() {
176            return Collections.unmodifiableList( layers );
177        }
178    
179        /**
180         * @return the entries
181         */
182        public List<MapModelEntry> getMapModelEntries() {
183            return Collections.unmodifiableList( entries );
184        }
185    
186        /**
187         * @return the expanded
188         */
189        public boolean isExpanded() {
190            return expanded;
191        }
192    
193        /**
194         * @param expanded
195         *            the expanded to set
196         */
197        public void setExpanded( boolean expanded ) {
198            this.expanded = expanded;
199        }
200    
201    }