1 13 package org.jahia.services.version; 14 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 18 import javax.swing.JTree ; 19 import javax.swing.tree.DefaultMutableTreeNode ; 20 import javax.swing.tree.DefaultTreeModel ; 21 22 import org.jahia.content.ContentContainerKey; 23 import org.jahia.content.ContentContainerListKey; 24 import org.jahia.content.ContentObject; 25 import org.jahia.content.ContentPageKey; 26 import org.jahia.exceptions.JahiaException; 27 import org.jahia.services.fields.ContentField; 28 import org.jahia.services.usermanager.JahiaUser; 29 import org.jahia.utils.GUITreeTools; 30 import org.jahia.content.JahiaObject; 31 32 public class RevisionsTreeTools { 33 34 private static org.apache.log4j.Logger logger = 35 org.apache.log4j.Logger.getLogger(RevisionsTreeTools.class); 36 37 46 public static JTree getTreeOfPageRevisions(PageRevisionEntrySet pageRevisionEntrySet, 47 JahiaUser user, EntryLoadRequest loadRequest, String operationMode) 48 throws JahiaException { 49 50 JTree tree = null; 51 try { 52 53 DefaultMutableTreeNode top = 55 new DefaultMutableTreeNode ( 56 JahiaObject.getInstance(pageRevisionEntrySet.getObjectKey()),true); 57 DefaultTreeModel treeModel = new DefaultTreeModel (top,true); 58 tree = new JTree (treeModel); 59 60 HashMap contentObjectsInTree = new HashMap (); 62 contentObjectsInTree.put(pageRevisionEntrySet.getObjectKey(),top); 63 64 DefaultMutableTreeNode revNode = null; 65 66 Iterator iterator = 67 pageRevisionEntrySet.getRevisions().iterator(); 68 RevisionEntry revEntry = null; 69 while ( iterator.hasNext() ){ 70 71 revEntry = (RevisionEntry)iterator.next(); 73 if ( revEntry.getObjectKey().getType().equals(ContentPageKey.PAGE_TYPE) ){ 74 revNode = new DefaultMutableTreeNode (revEntry); 75 top.insert(revNode,0); 76 } else { 77 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) 79 contentObjectsInTree.get(revEntry.getObjectKey()); 80 if ( node == null ){ 81 ContentObject contentObject = (ContentObject) 83 JahiaObject.getInstance(revEntry.getObjectKey()); 84 node = addContentObjectInTree(contentObject, tree, 85 contentObjectsInTree,user,loadRequest,operationMode); 86 if ( contentObject instanceof ContentField ){ 88 GUITreeTools.expandAllPath(tree,node); 89 } 90 } 91 92 if ( node != null ){ 94 revNode = new DefaultMutableTreeNode (revEntry); 95 if ( revEntry.getObjectKey().getType() 96 .equals(ContentContainerKey.CONTAINER_TYPE) ){ 97 node.insert(revNode,0); 98 } else if ( revEntry.getObjectKey().getType() 99 .equals(ContentContainerListKey.CONTAINERLIST_TYPE) ){ 100 node.insert(revNode,0); 101 } else { 102 node.add(revNode); 103 } 104 } 105 } 106 } 107 } catch ( java.lang.ClassNotFoundException cnf ){ 108 throw new JahiaException("Error creating Page Revisions Tree", 109 "Error creating Page Revisions Tree", 110 JahiaException.APPLICATION_ERROR, 111 JahiaException.APPLICATION_ERROR,cnf); 112 } 113 return tree; 114 } 115 116 123 private static DefaultMutableTreeNode addContentObjectInTree(ContentObject contentObject, 124 JTree tree, HashMap contentObjectsInTree, JahiaUser user, 125 EntryLoadRequest loadRequest, String operationMode) throws JahiaException { 126 127 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) 129 contentObjectsInTree.get(contentObject.getObjectKey()); 130 if ( node == null ){ 131 node = new DefaultMutableTreeNode (contentObject,true); 132 ContentObject parent = 133 contentObject.getParent(user,loadRequest,operationMode); 134 DefaultMutableTreeNode parentNode = 135 addContentObjectInTree(parent,tree,contentObjectsInTree, 136 user,loadRequest,operationMode); 137 if ( parentNode != null ){ 138 parentNode.add(node); 139 } else { 140 DefaultMutableTreeNode rootNode = 143 (DefaultMutableTreeNode )tree.getModel().getRoot(); 144 rootNode.add(node); 145 } 146 } 147 if ( node != null ){ 148 contentObjectsInTree.put(contentObject.getObjectKey(),node); 149 } 150 return node; 151 } 152 } 153 | Popular Tags |