KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > project > FlatProjectView


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.project;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.cayenne.access.DataNode;
26
27 /**
28  * FlatProjectView converts a project tree into a list of nodes,
29  * thus flattening the tree. Normally used as a singleton.
30  *
31  * @author Andrus Adamchik
32  */

33 public class FlatProjectView {
34
35     protected static FlatProjectView instance = new FlatProjectView();
36
37     /**
38      * Returns a FlatProjectView singleton.
39      */

40     public static FlatProjectView getInstance() {
41         return instance;
42     }
43
44     /**
45      * Returns flat tree view.
46      */

47     public List JavaDoc flattenProjectTree(Object JavaDoc rootNode) {
48         List JavaDoc nodes = new ArrayList JavaDoc();
49         TraversalHelper helper = new TraversalHelper(nodes);
50         new ProjectTraversal(helper).traverse(rootNode);
51         return nodes;
52     }
53
54     /**
55      * Helper class that serves as project traversal helper.
56      */

57     class TraversalHelper implements ProjectTraversalHandler {
58         protected List JavaDoc nodes;
59
60         public TraversalHelper(List JavaDoc nodes) {
61             this.nodes = nodes;
62         }
63
64         public void projectNode(ProjectPath path) {
65             nodes.add(path);
66         }
67
68         /**
69          * Returns true unless an object is a DataNode.
70          */

71         public boolean shouldReadChildren(
72             Object JavaDoc node,
73             ProjectPath parentPath) {
74             // don't read linked maps
75
return !(node instanceof DataNode);
76         }
77     }
78 }
79
Popular Tags