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.List;
20  
21  import javax.jcr.Node;
22  import javax.jcr.RepositoryException;
23  import javax.jcr.nodetype.NodeType;
24  import javax.jcr.version.Version;
25  import javax.jcr.version.VersionHistory;
26  import javax.jcr.version.VersionIterator;
27  import javax.jcr.version.VersionManager;
28  
29  import org.argeo.eclipse.ui.EclipseUiException;
30  import org.eclipse.jface.viewers.ITreeContentProvider;
31  import org.eclipse.jface.viewers.Viewer;
32  
33  /**
34   * Display some version information of a JCR full versionable node in a tree
35   * like structure
36   */
37  public class FullVersioningTreeContentProvider implements ITreeContentProvider {
38  	private static final long serialVersionUID = 8691772509491211112L;
39  
40  	/**
41  	 * Sends back the first level of the Tree. input element must be a single
42  	 * node object
43  	 */
44  	public Object[] getElements(Object inputElement) {
45  		try {
46  			Node rootNode = (Node) inputElement;
47  			String curPath = rootNode.getPath();
48  			VersionManager vm = rootNode.getSession().getWorkspace()
49  					.getVersionManager();
50  
51  			VersionHistory vh = vm.getVersionHistory(curPath);
52  			List<Version> result = new ArrayList<Version>();
53  			VersionIterator vi = vh.getAllLinearVersions();
54  
55  			while (vi.hasNext()) {
56  				result.add(vi.nextVersion());
57  			}
58  			return result.toArray();
59  		} catch (RepositoryException re) {
60  			throw new EclipseUiException(
61  					"Unexpected error while getting version elements", re);
62  		}
63  	}
64  
65  	public Object[] getChildren(Object parentElement) {
66  		try {
67  			if (parentElement instanceof Version) {
68  				List<Node> tmp = new ArrayList<Node>();
69  				tmp.add(((Version) parentElement).getFrozenNode());
70  				return tmp.toArray();
71  			}
72  		} catch (RepositoryException re) {
73  			throw new EclipseUiException("Unexpected error while getting child "
74  					+ "node for version element", re);
75  		}
76  		return null;
77  	}
78  
79  	public Object getParent(Object element) {
80  		try {
81  			// this will not work in a simpleVersionning environment, parent is
82  			// not a node.
83  			if (element instanceof Node
84  					&& ((Node) element).isNodeType(NodeType.NT_FROZEN_NODE)) {
85  				Node node = (Node) element;
86  				return node.getParent();
87  			} else
88  				return null;
89  		} catch (RepositoryException e) {
90  			return null;
91  		}
92  	}
93  
94  	public boolean hasChildren(Object element) {
95  		try {
96  			if (element instanceof Version)
97  				return true;
98  			else if (element instanceof Node)
99  				return ((Node) element).hasNodes();
100 			else
101 				return false;
102 		} catch (RepositoryException e) {
103 			throw new EclipseUiException("Cannot check children of " + element, e);
104 		}
105 	}
106 
107 	public void dispose() {
108 	}
109 
110 	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
111 	}
112 
113 }