]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.akb.ui/src/main/java/org/argeo/slc/akb/ui/providers/TemplatesTreeContentProvider.java
Edit connector wizard
[gpl/argeo-slc.git] / plugins / org.argeo.slc.akb.ui / src / main / java / org / argeo / slc / akb / ui / providers / 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 active AKB environments */
33 public class TemplatesTreeContentProvider implements ITreeContentProvider {
34 private final static Log log = LogFactory
35 .getLog(TemplatesTreeContentProvider.class);
36
37 /**
38 * @param parent
39 */
40 public Object[] getElements(Object parent) {
41 if (parent instanceof Object[])
42 return (Object[]) parent;
43 else
44 return null;
45 }
46
47 public Object getParent(Object child) {
48 try {
49 Node node = (Node) child;
50
51 // Manual sanity check to avoid exception when trying to refresh an
52 // element that displays a node which has been removed
53 try {
54 String id = node.getIdentifier();
55 node.getSession().getNodeByIdentifier(id);
56 } catch (Exception e) {
57 log.warn("Trying to refresh an unexisting node");
58 return null;
59 }
60
61 if (node.getDepth() == 0)
62 return null;
63 else
64 return node.getParent();
65
66 } catch (RepositoryException e) {
67 throw new AkbException("Error while getting parent node", e);
68 }
69 }
70
71 public Object[] getChildren(Object parent) {
72 try {
73 NodeIterator ni = ((Node) parent).getNodes();
74 List<Node> nodes = new ArrayList<Node>();
75
76 while (ni.hasNext()) {
77 Node currNode = ni.nextNode();
78 if (!currNode.isNodeType(AkbTypes.AKB_CONNECTOR_FOLDER))
79 nodes.add(currNode);
80 }
81
82 return nodes.toArray();
83 } catch (RepositoryException e) {
84 throw new AkbException("Error while getting children nodes", e);
85 }
86 }
87
88 public boolean hasChildren(Object parent) {
89 try {
90 // refine this
91 return ((Node) parent).hasNodes();
92 } catch (RepositoryException e) {
93 throw new AkbException("Error while checking children nodes", e);
94 }
95 }
96
97 public void dispose() {
98 // FIXME implement if needed
99 }
100
101 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
102 }
103
104
105
106 }