View Javadoc
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.cms.ui.jcr;
17  
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.Comparator;
21  import java.util.List;
22  
23  import javax.jcr.Node;
24  import javax.jcr.RepositoryException;
25  import javax.jcr.RepositoryFactory;
26  import javax.jcr.Session;
27  import javax.jcr.nodetype.NodeType;
28  
29  import org.argeo.api.NodeConstants;
30  import org.argeo.api.NodeUtils;
31  import org.argeo.api.security.Keyring;
32  import org.argeo.cms.ui.jcr.model.RepositoriesElem;
33  import org.argeo.cms.ui.jcr.model.SingleJcrNodeElem;
34  import org.argeo.eclipse.ui.TreeParent;
35  import org.eclipse.jface.viewers.ITreeContentProvider;
36  import org.eclipse.jface.viewers.Viewer;
37  
38  /**
39   * Implementation of the {@code ITreeContentProvider} to display multiple
40   * repository environment in a tree like structure
41   */
42  public class NodeContentProvider implements ITreeContentProvider {
43  	private static final long serialVersionUID = -4083809398848374403L;
44  	final private RepositoryRegister repositoryRegister;
45  	final private RepositoryFactory repositoryFactory;
46  
47  	// Current user session on the default workspace of the argeo Node
48  	final private Session userSession;
49  	final private Keyring keyring;
50  	private boolean sortChildren;
51  
52  	// Reference for cleaning
53  	private SingleJcrNodeElem homeNode = null;
54  	private RepositoriesElem repositoriesNode = null;
55  
56  	// Utils
57  	private TreeBrowserComparator itemComparator = new TreeBrowserComparator();
58  
59  	public NodeContentProvider(Session userSession, Keyring keyring,
60  			RepositoryRegister repositoryRegister,
61  			RepositoryFactory repositoryFactory, Boolean sortChildren) {
62  		this.userSession = userSession;
63  		this.keyring = keyring;
64  		this.repositoryRegister = repositoryRegister;
65  		this.repositoryFactory = repositoryFactory;
66  		this.sortChildren = sortChildren;
67  	}
68  
69  	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
70  		if (newInput == null)// dispose
71  			return;
72  
73  		if (userSession != null) {
74  			Node userHome = NodeUtils.getUserHome(userSession);
75  			if (userHome != null) {
76  				// TODO : find a way to dynamically get alias for the node
77  				if (homeNode != null)
78  					homeNode.dispose();
79  				homeNode = new SingleJcrNodeElem(null, userHome,
80  						userSession.getUserID(), NodeConstants.EGO_REPOSITORY);
81  			}
82  		}
83  		if (repositoryRegister != null) {
84  			if (repositoriesNode != null)
85  				repositoriesNode.dispose();
86  			repositoriesNode = new RepositoriesElem("Repositories",
87  					repositoryRegister, repositoryFactory, null, userSession,
88  					keyring);
89  		}
90  	}
91  
92  	/**
93  	 * Sends back the first level of the Tree. Independent from inputElement
94  	 * that can be null
95  	 */
96  	public Object[] getElements(Object inputElement) {
97  		List<Object> objs = new ArrayList<Object>();
98  		if (homeNode != null)
99  			objs.add(homeNode);
100 		if (repositoriesNode != null)
101 			objs.add(repositoriesNode);
102 		return objs.toArray();
103 	}
104 
105 	public Object[] getChildren(Object parentElement) {
106 		if (parentElement instanceof TreeParent) {
107 			if (sortChildren) {
108 				Object[] tmpArr = ((TreeParent) parentElement).getChildren();
109 				if (tmpArr == null)
110 					return new Object[0];
111 				TreeParent[] arr = new TreeParent[tmpArr.length];
112 				for (int i = 0; i < tmpArr.length; i++)
113 					arr[i] = (TreeParent) tmpArr[i];
114 				Arrays.sort(arr, itemComparator);
115 				return arr;
116 			} else
117 				return ((TreeParent) parentElement).getChildren();
118 		} else
119 			return new Object[0];
120 	}
121 
122 	/**
123 	 * Sets whether the content provider should order the children nodes or not.
124 	 * It is user duty to call a full refresh of the tree after changing this
125 	 * parameter.
126 	 */
127 	public void setSortChildren(boolean sortChildren) {
128 		this.sortChildren = sortChildren;
129 	}
130 
131 	public Object getParent(Object element) {
132 		if (element instanceof TreeParent) {
133 			return ((TreeParent) element).getParent();
134 		} else
135 			return null;
136 	}
137 
138 	public boolean hasChildren(Object element) {
139 		if (element instanceof RepositoriesElem) {
140 			RepositoryRegister rr = ((RepositoriesElem) element)
141 					.getRepositoryRegister();
142 			return rr.getRepositories().size() > 0;
143 		} else if (element instanceof TreeParent) {
144 			TreeParent tp = (TreeParent) element;
145 			return tp.hasChildren();
146 		}
147 		return false;
148 	}
149 
150 	public void dispose() {
151 		if (homeNode != null)
152 			homeNode.dispose();
153 		if (repositoriesNode != null) {
154 			// logs out open sessions
155 			// see https://bugzilla.argeo.org/show_bug.cgi?id=23
156 			repositoriesNode.dispose();
157 		}
158 	}
159 
160 	/**
161 	 * Specific comparator for this view. See specification here:
162 	 * https://www.argeo.org/bugzilla/show_bug.cgi?id=139
163 	 */
164 	private class TreeBrowserComparator implements Comparator<TreeParent> {
165 
166 		public int category(TreeParent element) {
167 			if (element instanceof SingleJcrNodeElem) {
168 				Node node = ((SingleJcrNodeElem) element).getNode();
169 				try {
170 					if (node.isNodeType(NodeType.NT_FOLDER))
171 						return 5;
172 				} catch (RepositoryException e) {
173 					// TODO Auto-generated catch block
174 					e.printStackTrace();
175 				}
176 			}
177 			return 10;
178 		}
179 
180 		public int compare(TreeParent o1, TreeParent o2) {
181 			int cat1 = category(o1);
182 			int cat2 = category(o2);
183 
184 			if (cat1 != cat2) {
185 				return cat1 - cat2;
186 			}
187 			return o1.getName().compareTo(o2.getName());
188 		}
189 	}
190 }