KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > visualcontent > ui > nodetree > NodeTreeContentProvider


1 /**
2  * VC Browser - Visualizes the content of a JSR 170 compatible repository
3  * Copyright (C) 2006 Sandro Böhme
4  *
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * 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, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.visualcontent.ui.nodetree;
19
20 import java.util.LinkedList JavaDoc;
21 import java.util.List JavaDoc;
22
23 import javax.jcr.AccessDeniedException;
24 import javax.jcr.ItemNotFoundException;
25 import javax.jcr.Node;
26 import javax.jcr.NodeIterator;
27 import javax.jcr.RepositoryException;
28 import javax.jcr.query.QueryResult;
29 import javax.naming.directory.SearchResult JavaDoc;
30
31 import org.eclipse.jface.viewers.ITreeContentProvider;
32 import org.eclipse.jface.viewers.Viewer;
33 import org.visualcontent.ui.UiPlugin;
34
35 class NodeTreeContentProvider implements ITreeContentProvider {
36
37
38     public void inputChanged(Viewer v, Object JavaDoc oldInput, Object JavaDoc newInput) {
39     }
40
41     public Object JavaDoc[] getElements(Object JavaDoc parent) {
42         return getChildren(parent);
43     }
44
45     public Object JavaDoc getParent(Object JavaDoc child) {
46         if (child instanceof Node) {
47             Node currentNode = ((Node) child);
48             try {
49                 Node rootNode = currentNode.getSession().getRootNode();
50                 if (!currentNode.equals(rootNode)){
51                     return currentNode.getParent();
52                 }
53             } catch (ItemNotFoundException e) {
54                 // TODO Auto-generated catch block
55
e.printStackTrace();
56             } catch (AccessDeniedException e) {
57                 // TODO Auto-generated catch block
58
e.printStackTrace();
59             } catch (RepositoryException e) {
60                 // TODO Auto-generated catch block
61
e.printStackTrace();
62             }
63         }
64         return null;
65     }
66
67     public Object JavaDoc[] getChildren(Object JavaDoc parent) {
68         if (parent instanceof Node || parent instanceof QueryResult) {
69             NodeIterator nodeIterator = null;
70             if (parent instanceof Node ) {
71                 try {
72                     nodeIterator = ((Node) parent).getNodes();
73                 } catch (RepositoryException e) {
74                     // TODO Auto-generated catch block
75
e.printStackTrace();
76                 }
77             }else if (parent instanceof QueryResult ) {
78                 try {
79                     nodeIterator = ((QueryResult) parent).getNodes();
80                 } catch (RepositoryException e) {
81                     UiPlugin.getDefault().showError("Could not get the node of the query result.",e);
82                 }
83             }
84             List JavaDoc nodeList = new LinkedList JavaDoc();
85             while (nodeIterator.hasNext()) {
86                 Node element = (Node) nodeIterator.nextNode();
87                 nodeList.add(element);
88             }
89             Node[] nodeArray = (Node[]) nodeList.toArray(new Node[nodeList
90                     .size()]);
91             return nodeArray;
92         }
93         return new Object JavaDoc[0];
94     }
95
96     public boolean hasChildren(Object JavaDoc parent) {
97         if (parent instanceof Node)
98             try {
99                 return ((Node) parent).hasNodes();
100             } catch (RepositoryException e) {
101                 e.printStackTrace();
102             }
103         return false;
104     }
105
106     public void dispose() {
107     }
108 }
Popular Tags