001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/portal/context/Node.java $
002    /*----------------    FILE HEADER  -----------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2008 by:
006     EXSE, Department of Geography, University of Bonn
007     http://www.giub.uni-bonn.de/deegree/
008     lat/lon GmbH
009     http://www.lat-lon.de
010    
011     This library is free software; you can redistribute it and/or
012     modify it under the terms of the GNU Lesser General Public
013     License as published by the Free Software Foundation; either
014     version 2.1 of the License, or (at your option) any later version.
015    
016     This library is distributed in the hope that it will be useful,
017     but WITHOUT ANY WARRANTY; without even the implied warranty of
018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019     Lesser General Public License for more details.
020    
021     You should have received a copy of the GNU Lesser General Public
022     License along with this library; if not, write to the Free Software
023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024    
025     Contact:
026    
027     Andreas Poth
028     lat/lon GmbH
029     Aennchenstr. 19
030     53115 Bonn
031     Germany
032     E-Mail: poth@lat-lon.de
033    
034     Prof. Dr. Klaus Greve
035     Department of Geography
036     University of Bonn
037     Meckenheimer Allee 166
038     53115 Bonn
039     Germany
040     E-Mail: greve@giub.uni-bonn.de
041    
042     
043     ---------------------------------------------------------------------------*/
044    package org.deegree.portal.context;
045    
046    import java.util.ArrayList;
047    import java.util.Collections;
048    import java.util.List;
049    
050    /**
051     * encapsulates about a node described/contained by a Web Map Context
052     * 
053     * @version $Revision: 9346 $
054     * @author <a href="mailto:vesll@idgis.nl">Linda Vels</a>
055     * @author last edited by: $Author: apoth $
056     * 
057     * @version 1.0. $Revision: 9346 $, $Date: 2007-12-27 17:39:07 +0100 (Do, 27 Dez 2007) $
058     * 
059     * @since 2.0
060     */
061    public class Node {
062    
063        private int id;
064    
065        private String title = null;
066    
067        private boolean selectable = false;
068    
069        private boolean collapsed = false;
070    
071        private Node[] nodes = new Node[0];
072    
073        private Node parent = null;
074    
075        List<Node[]> tree = new ArrayList<Node[]>( 50 );
076    
077        /**
078         * Creates a new ContextNode object.
079         * 
080         * @param id
081         *            id of the selected node
082         * @param parent
083         * @param title
084         *            title of the selected node
085         * @param selectable
086         * @param collapsed
087         *            defines if the node is collapsed in the legend viewer
088         * @throws ContextException
089         */
090        public Node( int id, Node parent, String title, boolean selectable, boolean collapsed ) throws ContextException {
091            setId( id );
092            setParent( parent );
093            setTitle( title );
094            setCollapsed( collapsed );
095            setSelectable( selectable );
096        }
097    
098        /**
099         * The childnodes of the selected node in the tree
100         * 
101         * @return all nodes
102         */
103        public Node[] getNodes() {
104            return nodes;
105        }
106    
107        /**
108         * Returns a childnodes of the selected node by id
109         * 
110         * @return node by id
111         */
112        public Node getNode( int nodeId ) {
113            Node node = null;
114            for ( int i = 0; i < nodes.length; i++ ) {
115                node = nodes[i].getNode( nodeId );
116                if ( node != null ) {
117                    return node;
118                }
119                if ( nodes[i].getId() == nodeId ) {
120                    return nodes[i];
121                }
122            }
123            return node;
124        }
125    
126        /**
127         * 
128         * @param nodeId
129         * @param nodes
130         * @return node by id from a list
131         */
132        public Node getNode( int nodeId, Node[] nodes ) {
133            Node node = null;
134            for ( int i = 0; i < nodes.length; i++ ) {
135                node = nodes[i].getNode( nodeId, nodes[i].getNodes() );
136                if ( node != null ) {
137                    return node;
138                } else {
139                    if ( nodes[i].getId() == nodeId ) {
140                        return nodes[i];
141                    }
142                }
143    
144            }
145            return node;
146        }
147    
148        /**
149         * return the maximum id of all nodes
150         * 
151         * @return maximum id of all nodes
152         */
153        public int getMaxNodeId() {
154            int maxNodeId = id;
155            for ( int i = 0; i < nodes.length; i++ ) {
156                Node[] brancheNodes = nodes[i].getNodes();
157                if ( nodes[i].getId() > maxNodeId ) {
158                    maxNodeId = nodes[i].getId();
159    
160                }
161                maxNodeId = getMaxId( brancheNodes, maxNodeId );
162            }
163            return maxNodeId;
164        }
165    
166        /**
167         * 
168         * @param nodes
169         * @param maxNodeId
170         * @return maximum id 
171         */
172        private int getMaxId( Node[] nodes, int maxNodeId ) {
173            for ( int i = 0; i < nodes.length; i++ ) {
174                Node[] brancheNodes = nodes[i].getNodes();
175                if ( nodes[i].getId() > maxNodeId ) {
176                    maxNodeId = nodes[i].getId();
177    
178                }
179                maxNodeId = getMaxId( brancheNodes, maxNodeId );
180            }
181            return maxNodeId;
182        }
183    
184        /**
185         * 
186         * @param parent
187         */
188        public void setParent( Node parent ) {
189            this.parent = parent;
190        }
191    
192        /**
193         * 
194         * @return parent node
195         */
196        public Node getParent() {
197            return parent;
198        }
199    
200        /**
201         * The id of the selected node.
202         * 
203         * @return id
204         */
205        public int getId() {
206            return id;
207        }
208    
209        /**
210         * The title of the selected node.
211         * 
212         * @return title
213         */
214        public String getTitle() {
215            return title;
216        }
217    
218        /**
219         * The status of the node (collapsed or not (expanded)).
220         * 
221         * @return true if node is collapsed
222         */
223        public boolean isCollapsed() {
224            return collapsed;
225        }
226    
227        /**
228         * The selectable status of the node.
229         * 
230         * @return true if node is selectable
231         */
232        public boolean isSelectable() {
233            return selectable;
234        }
235    
236        /**
237         * @param nodes
238         */
239        public void setNodes( Node[] nodes ) {
240            if ( nodes == null ) {
241                nodes = new Node[0];
242            }
243            this.nodes = nodes;
244        }
245    
246        /**
247         * @param selectable
248         */
249        public void setSelectable( boolean selectable ) {
250            this.selectable = selectable;
251        }
252    
253        /**
254         * @param collapsed
255         */
256        public void setCollapsed( boolean collapsed ) {
257            this.collapsed = collapsed;
258        }
259    
260        /**
261         * @param id
262         */
263        public void setId( int id ) {
264            this.id = id;
265        }
266    
267        /**
268         * 
269         * @param title
270         * 
271         * @throws ContextException
272         */
273        public void setTitle( String title )
274                                throws ContextException {
275            if ( title == null ) {
276                throw new ContextException( "title isn't allowed to be null" );
277            }
278            this.title = title;
279        }
280    
281        /**
282         * 
283         * @return flat tree as node matrix
284         */
285        public Node[][] getFlatTree() {
286            tree = new ArrayList<Node[]>();
287            tree = getBranches( nodes );
288            Node[][] flatTree = new Node[tree.size()][];
289            return tree.toArray( flatTree );
290        }
291    
292        /**
293         * 
294         * @param nodes
295         * @return tree branches
296         */
297        private List<Node[]> getBranches( Node[] nodes ) {
298            for ( int i = 0; i < nodes.length; i++ ) {
299                Node[] branchNodes = nodes[i].getNodes();
300                if ( branchNodes.length == 0 ) {
301                    List<Node> treeRowList = new ArrayList<Node>( 50 );
302                    Node tmpNode = nodes[i];
303                    while ( tmpNode != null ) {
304                        treeRowList.add( tmpNode );
305                        tmpNode = tmpNode.getParent();
306                    }
307                    Collections.reverse( treeRowList );
308                    Node[] treeRow = new Node[treeRowList.size()];
309                    tree.add( treeRowList.toArray( treeRow ) );
310                } else {
311                    getBranches( branchNodes );
312                }
313            }
314            return tree;
315    
316        }
317    
318        /**
319         * moves a node within the tree up or down
320         * 
321         * @param nodeId
322         * @param up
323         */
324        public void moveNode( int nodeId, Boolean up ) {
325    
326            for ( int i = 0; i < nodes.length; i++ ) {
327                if ( nodes[i].getId() == nodeId ) {
328                    Node source = null;
329                    Node target = null;
330                    if ( up ) {
331                        source = nodes[i];
332                        target = nodes[i - 1];
333                        nodes[i] = target;
334                        nodes[i - 1] = source;
335                        return;
336                    }
337                    source = nodes[i];
338                    target = nodes[i + 1];
339                    nodes[i] = target;
340                    nodes[i + 1] = source;
341                    return;
342    
343                }
344                nodes[i].moveNode( nodeId, up );
345            }
346    
347        }
348    
349    }