]> git.argeo.org Git - gpl/argeo-slc.git/blob - TemplatesTreeContentProvider.java
14a8a63d008b89cec59b22d0c63ffa84af9daca4
[gpl/argeo-slc.git] / TemplatesTreeContentProvider.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
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.argeo.slc.akb.ui.providers;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import javax.jcr.Node;
22 import javax.jcr.NodeIterator;
23 import javax.jcr.RepositoryException;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.argeo.slc.akb.AkbException;
28 import org.argeo.slc.akb.AkbTypes;
29 import org.eclipse.jface.viewers.ITreeContentProvider;
30 import org.eclipse.jface.viewers.Viewer;
31
32 /** Basic content provider for a tree of AKB environment templates */
33 public class TemplatesTreeContentProvider implements ITreeContentProvider {
34 private final static Log log = LogFactory
35 .getLog(TemplatesTreeContentProvider.class);
36
37 /**
38 * @param parent
39 * Pass current user home as parameter
40 *
41 */
42 public Object[] getElements(Object parent) {
43 if (parent instanceof Object[])
44 return (Object[]) parent;
45 else
46 return null;
47 }
48
49 public Object getParent(Object child) {
50 try {
51 Node node = (Node) child;
52
53 // Manual sanity check to avoid exception when tryin to refresh an
54 // element that displays a node which has been removed
55 try {
56 String id = node.getIdentifier();
57 node.getSession().getNodeByIdentifier(id);
58 } catch (Exception e) {
59 log.warn("Trying to refresh an unexisting node");
60 return null;
61 }
62
63 if (node.getDepth() == 0)
64 return null;
65 else
66 return node.getParent();
67
68 } catch (RepositoryException e) {
69 throw new AkbException("Error while getting parent node", e);
70 }
71 }
72
73 public Object[] getChildren(Object parent) {
74 try {
75 NodeIterator ni = ((Node) parent).getNodes();
76 List<Node> nodes = new ArrayList<Node>();
77
78 while (ni.hasNext()) {
79 Node currNode = ni.nextNode();
80 if (!currNode.isNodeType(AkbTypes.AKB_CONNECTOR_FOLDER))
81 nodes.add(currNode);
82 }
83
84 return nodes.toArray();
85 } catch (RepositoryException e) {
86 throw new AkbException("Error while getting children nodes", e);
87 }
88 }
89
90 public boolean hasChildren(Object parent) {
91 try {
92 // refine this
93 return ((Node) parent).hasNodes();
94 } catch (RepositoryException e) {
95 throw new AkbException("Error while checking children nodes", e);
96 }
97 }
98
99 public void dispose() {
100 // FIXME implement if needed
101 }
102
103 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
104 }
105 }