]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.akb.ui/src/main/java/org/argeo/slc/akb/ui/providers/ActiveEnvsTreeContentProvider.java
Edit connector wizard
[gpl/argeo-slc.git] / plugins / org.argeo.slc.akb.ui / src / main / java / org / argeo / slc / akb / ui / providers / ActiveEnvsTreeContentProvider.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 import javax.jcr.Session;
25
26 import org.argeo.slc.akb.AkbException;
27 import org.argeo.slc.akb.AkbNames;
28 import org.argeo.slc.akb.AkbTypes;
29 import org.eclipse.jface.viewers.ITreeContentProvider;
30 import org.eclipse.jface.viewers.Viewer;
31
32 /**
33 * Content provider for a tree of active AKB environments. Displays
34 * <CODE>ActiveTreeItem</CODE> to be able to display subtrees of item without
35 * duplicating the information from the corresponding template.
36 */
37 public class ActiveEnvsTreeContentProvider implements ITreeContentProvider {
38 // private final static Log log = LogFactory
39 // .getLog(ActiveEnvsTreeContentProvider.class);
40
41 /**
42 * @param parent
43 * Pass base parent node as parameter
44 *
45 */
46 public Object[] getElements(Object parent) {
47 if (parent instanceof Node)
48 return initializeTree((Node) parent);
49 else
50 return null;
51 }
52
53 private ActiveTreeItem[] initializeTree(Node activeEnvsParentNode) {
54 try {
55 NodeIterator ni = activeEnvsParentNode.getNodes();
56 List<ActiveTreeItem> envs = new ArrayList<ActiveTreeItem>();
57 while (ni.hasNext()) {
58 Node currNode = ni.nextNode();
59 if (currNode.isNodeType(AkbTypes.AKB_ENV)) {
60 envs.add(new ActiveTreeItem(null, currNode, currNode));
61 }
62 }
63 ActiveTreeItem[] envArr = envs.toArray(new ActiveTreeItem[envs
64 .size()]);
65 return envArr;
66 } catch (RepositoryException re) {
67 throw new AkbException("Error while initializing the "
68 + "tree of active environments.", re);
69 }
70 }
71
72 public Object getParent(Object child) {
73 return ((ActiveTreeItem) child).getParent();
74 }
75
76 public Object[] getChildren(Object parent) {
77 try {
78 ActiveTreeItem currItem = (ActiveTreeItem) parent;
79 Node parNode = currItem.getNode();
80 Node envNode = currItem.getEnvironment();
81
82 if (parNode.isNodeType(AkbTypes.AKB_ENV)) {
83 Session session = parNode.getSession();
84 if (parNode.hasProperty(AkbNames.AKB_ENV_TEMPLATE_PATH)
85 && session.nodeExists(parNode.getProperty(
86 AkbNames.AKB_ENV_TEMPLATE_PATH).getString()))
87 parNode = session.getNode(parNode.getProperty(
88 AkbNames.AKB_ENV_TEMPLATE_PATH).getString());
89 else
90 return null;
91 }
92
93 NodeIterator ni = parNode.getNodes();
94 List<ActiveTreeItem> children = new ArrayList<ActiveTreeItem>();
95 while (ni.hasNext()) {
96 Node currNode = ni.nextNode();
97 if (!currNode.isNodeType(AkbTypes.AKB_CONNECTOR_FOLDER)) {
98 ActiveTreeItem currChild = new ActiveTreeItem(currItem,
99 currNode, envNode);
100 children.add(currChild);
101 }
102 }
103 return children.toArray();
104 } catch (RepositoryException e) {
105 throw new AkbException("Error while getting children nodes", e);
106 }
107 }
108
109 public boolean hasChildren(Object parent) {
110 try {
111 ActiveTreeItem currItem = (ActiveTreeItem) parent;
112 Node parNode = currItem.getNode();
113
114 if (parNode.isNodeType(AkbTypes.AKB_ENV)) {
115 Session session = parNode.getSession();
116 if (parNode.hasProperty(AkbNames.AKB_ENV_TEMPLATE_PATH)
117 && session.nodeExists(parNode.getProperty(
118 AkbNames.AKB_ENV_TEMPLATE_PATH).getString()))
119 parNode = session.getNode(parNode.getProperty(
120 AkbNames.AKB_ENV_TEMPLATE_PATH).getString());
121 else
122 return false;
123 }
124
125 return parNode.hasNodes();
126 } catch (RepositoryException e) {
127 throw new AkbException("Error while checking children nodes", e);
128 }
129 }
130
131 public void dispose() {
132 // FIXME implement if needed
133 }
134
135 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
136 }
137 }