KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > navigation > impl > GroupNode


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.navigation.impl;
17
18 import org.outerj.daisy.repository.RepositoryException;
19 import org.xml.sax.ContentHandler JavaDoc;
20 import org.xml.sax.SAXException JavaDoc;
21 import org.xml.sax.helpers.AttributesImpl JavaDoc;
22
23 import java.util.Map JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 public class GroupNode extends AbstractParentNode {
28     private final String JavaDoc id;
29     private final String JavaDoc label;
30     private final NodeVisibility nodeVisibility;
31
32     /**
33      *
34      * @param id an ID that should be unique within the parent node.
35      */

36     public GroupNode(String JavaDoc id, String JavaDoc label, NodeVisibility nodeVisibility) {
37         this.id = id;
38         this.label = label;
39         this.nodeVisibility = nodeVisibility;
40     }
41
42     public boolean checkId(String JavaDoc id, long branchId, long languageId) {
43         return this.id.equals(id);
44     }
45
46     public boolean isExpandable() {
47         return false;
48     }
49
50     public void populateNodeLookupMap(Map JavaDoc map, String JavaDoc path) throws RepositoryException {
51         path = path + "/" + id;
52         super.populateNodeLookupMap(map, path);
53     }
54
55     public void generateXml(ContentHandler JavaDoc contentHandler, int depth, String JavaDoc path, long userId, long[] roleIds) throws RepositoryException, SAXException JavaDoc {
56         if (!isVisible(userId, roleIds, null, -1))
57             return;
58
59         path = path + "/" + id;
60         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
61         attrs.addAttribute("", "label", "label", "CDATA", label);
62         attrs.addAttribute("", "path", "path", "CDATA", path);
63         attrs.addAttribute("", "id", "id", "CDATA", id);
64         contentHandler.startElement(NAVIGATION_NS, "group", "group", attrs);
65         if (depth != -1 && depth - 1 > 0)
66             super.generateXml(contentHandler, depth - 1, path, userId, roleIds);
67         else if (depth == -1)
68             super.generateXml(contentHandler, -1, path, userId, roleIds);
69         contentHandler.endElement(NAVIGATION_NS, "group", "group");
70     }
71
72     public void generateXml(ContentHandler JavaDoc contentHandler, Node[] activeNodePath, int pos, boolean includeOnlyActivePath,
73             String JavaDoc path, long userId, long[] roleIds) throws RepositoryException, SAXException JavaDoc {
74         if (!isVisible(userId, roleIds, activeNodePath, pos))
75             return;
76
77         path = path + "/" + id;
78         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
79         attrs.addAttribute("", "label", "label", "CDATA", label);
80         attrs.addAttribute("", "id", "id", "CDATA", id);
81         attrs.addAttribute("", "path", "path", "CDATA", path);
82         if (pos < activeNodePath.length && activeNodePath[pos] == this)
83             attrs.addAttribute("", "selected", "selected", "CDATA", "true");
84         if (pos == activeNodePath.length -1 && activeNodePath[pos] == this)
85             attrs.addAttribute("", "active", "active", "CDATA", "true");
86         contentHandler.startElement(NAVIGATION_NS, "group", "group", attrs);
87         if ((includeOnlyActivePath && pos < activeNodePath.length && activeNodePath[pos] == this) || (!includeOnlyActivePath))
88             super.generateXml(contentHandler, activeNodePath, pos + 1, includeOnlyActivePath, path, userId, roleIds);
89         contentHandler.endElement(NAVIGATION_NS, "group", "group");
90     }
91
92     public boolean isVisible(long userId, long[] roleIds, Node[] activeNodePath, int activeNodePathPos) throws RepositoryException {
93         if (nodeVisibility == NodeVisibility.HIDDEN) {
94             return false;
95         } else if (nodeVisibility == NodeVisibility.WHEN_ACTIVE
96                 && !(activeNodePath != null && activeNodePathPos < activeNodePath.length && activeNodePath[activeNodePathPos] == this)) {
97             return false;
98         }
99
100         List JavaDoc children = getExpandedChildList();
101         Iterator JavaDoc childrenIt = children.iterator();
102         activeNodePathPos++;
103         while (childrenIt.hasNext()) {
104             Node child = (Node)childrenIt.next();
105             if (child.isVisible(userId, roleIds, activeNodePath, activeNodePathPos))
106                 return true;
107         }
108         return false;
109     }
110
111     public boolean isIdentifiable() {
112         return true;
113     }
114
115     public String JavaDoc getId() {
116         return id;
117     }
118
119     public String JavaDoc findFirstDocumentNode(String JavaDoc path, long userId, long[] roleIds) throws RepositoryException {
120         List JavaDoc children = getExpandedChildList();
121         Iterator JavaDoc childrenIt = children.iterator();
122         while (childrenIt.hasNext()) {
123             Object JavaDoc child = childrenIt.next();
124             if (child instanceof GroupNode) {
125                 GroupNode groupChild = (GroupNode)child;
126                 String JavaDoc node = groupChild.findFirstDocumentNode(path + "/" + groupChild.getId(), userId, roleIds);
127                 if (node != null)
128                     return node;
129             } else if (child instanceof DocumentRepresentingNode && ((Node)child).isVisible(userId, roleIds, null, -1)) {
130                 return path + "/" + ((Node)child).getId();
131             }
132             // other types of nodes are skipped
133
}
134         return null;
135     }
136 }
137
Popular Tags