1 18 package org.visualcontent.ui.nodetree; 19 20 import java.util.LinkedList ; 21 import java.util.List ; 22 23 import javax.jcr.AccessDeniedException; 24 import javax.jcr.ItemNotFoundException; 25 import javax.jcr.Node; 26 import javax.jcr.NodeIterator; 27 import javax.jcr.RepositoryException; 28 import javax.jcr.query.QueryResult; 29 import javax.naming.directory.SearchResult ; 30 31 import org.eclipse.jface.viewers.ITreeContentProvider; 32 import org.eclipse.jface.viewers.Viewer; 33 import org.visualcontent.ui.UiPlugin; 34 35 class NodeTreeContentProvider implements ITreeContentProvider { 36 37 38 public void inputChanged(Viewer v, Object oldInput, Object newInput) { 39 } 40 41 public Object [] getElements(Object parent) { 42 return getChildren(parent); 43 } 44 45 public Object getParent(Object child) { 46 if (child instanceof Node) { 47 Node currentNode = ((Node) child); 48 try { 49 Node rootNode = currentNode.getSession().getRootNode(); 50 if (!currentNode.equals(rootNode)){ 51 return currentNode.getParent(); 52 } 53 } catch (ItemNotFoundException e) { 54 e.printStackTrace(); 56 } catch (AccessDeniedException e) { 57 e.printStackTrace(); 59 } catch (RepositoryException e) { 60 e.printStackTrace(); 62 } 63 } 64 return null; 65 } 66 67 public Object [] getChildren(Object parent) { 68 if (parent instanceof Node || parent instanceof QueryResult) { 69 NodeIterator nodeIterator = null; 70 if (parent instanceof Node ) { 71 try { 72 nodeIterator = ((Node) parent).getNodes(); 73 } catch (RepositoryException e) { 74 e.printStackTrace(); 76 } 77 }else if (parent instanceof QueryResult ) { 78 try { 79 nodeIterator = ((QueryResult) parent).getNodes(); 80 } catch (RepositoryException e) { 81 UiPlugin.getDefault().showError("Could not get the node of the query result.",e); 82 } 83 } 84 List nodeList = new LinkedList (); 85 while (nodeIterator.hasNext()) { 86 Node element = (Node) nodeIterator.nextNode(); 87 nodeList.add(element); 88 } 89 Node[] nodeArray = (Node[]) nodeList.toArray(new Node[nodeList 90 .size()]); 91 return nodeArray; 92 } 93 return new Object [0]; 94 } 95 96 public boolean hasChildren(Object parent) { 97 if (parent instanceof Node) 98 try { 99 return ((Node) parent).hasNodes(); 100 } catch (RepositoryException e) { 101 e.printStackTrace(); 102 } 103 return false; 104 } 105 106 public void dispose() { 107 } 108 } | Popular Tags |