KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > version > RevisionsTreeTools


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12

13 package org.jahia.services.version;
14
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17
18 import javax.swing.JTree JavaDoc;
19 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
20 import javax.swing.tree.DefaultTreeModel JavaDoc;
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     /**
38      *
39      * @param pageRevisionEntrySet
40      * @param user
41      * @param loadRequest
42      * @param operationMode
43      * @return
44      * @throws JahiaException
45      */

46     public static JTree JavaDoc getTreeOfPageRevisions(PageRevisionEntrySet pageRevisionEntrySet,
47             JahiaUser user, EntryLoadRequest loadRequest, String JavaDoc operationMode)
48     throws JahiaException {
49
50         JTree JavaDoc tree = null;
51         try {
52
53             // Root Node
54
DefaultMutableTreeNode JavaDoc top =
55                     new DefaultMutableTreeNode JavaDoc(
56                     JahiaObject.getInstance(pageRevisionEntrySet.getObjectKey()),true);
57             DefaultTreeModel JavaDoc treeModel = new DefaultTreeModel JavaDoc(top,true);
58             tree = new JTree JavaDoc(treeModel);
59
60             // Add revision to the tree
61
HashMap JavaDoc contentObjectsInTree = new HashMap JavaDoc();
62             contentObjectsInTree.put(pageRevisionEntrySet.getObjectKey(),top);
63
64             DefaultMutableTreeNode JavaDoc revNode = null;
65
66             Iterator JavaDoc iterator =
67                     pageRevisionEntrySet.getRevisions().iterator();
68             RevisionEntry revEntry = null;
69             while ( iterator.hasNext() ){
70
71                 // retrieve the content object and its parent
72
revEntry = (RevisionEntry)iterator.next();
73                 if ( revEntry.getObjectKey().getType().equals(ContentPageKey.PAGE_TYPE) ){
74                     revNode = new DefaultMutableTreeNode JavaDoc(revEntry);
75                     top.insert(revNode,0);
76                 } else {
77                     // check if the content object is already added int the tree
78
DefaultMutableTreeNode JavaDoc node = (DefaultMutableTreeNode JavaDoc)
79                             contentObjectsInTree.get(revEntry.getObjectKey());
80                     if ( node == null ){
81                         // add the content object in the tree
82
ContentObject contentObject = (ContentObject)
83                                 JahiaObject.getInstance(revEntry.getObjectKey());
84                         node = addContentObjectInTree(contentObject, tree,
85                                 contentObjectsInTree,user,loadRequest,operationMode);
86                         // automatically expand Field nodes
87
if ( contentObject instanceof ContentField ){
88                             GUITreeTools.expandAllPath(tree,node);
89                         }
90                     }
91
92                     // Add the revision node to the current content object node
93
if ( node != null ){
94                         revNode = new DefaultMutableTreeNode JavaDoc(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 JavaDoc 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     /**
117      *
118      * @param contentObject
119      * @param tree
120      * @param contentObjectsInTree
121      * @return
122      */

123     private static DefaultMutableTreeNode JavaDoc addContentObjectInTree(ContentObject contentObject,
124             JTree JavaDoc tree, HashMap JavaDoc contentObjectsInTree, JahiaUser user,
125             EntryLoadRequest loadRequest, String JavaDoc operationMode) throws JahiaException {
126
127         // check if the content object is already added int the tree
128
DefaultMutableTreeNode JavaDoc node = (DefaultMutableTreeNode JavaDoc)
129                 contentObjectsInTree.get(contentObject.getObjectKey());
130         if ( node == null ){
131             node = new DefaultMutableTreeNode JavaDoc(contentObject,true);
132             ContentObject parent =
133                     contentObject.getParent(user,loadRequest,operationMode);
134             DefaultMutableTreeNode JavaDoc parentNode =
135                     addContentObjectInTree(parent,tree,contentObjectsInTree,
136                     user,loadRequest,operationMode);
137             if ( parentNode != null ){
138                 parentNode.add(node);
139             } else {
140                 // parent node not found, append to root
141
// FIXME : unexpected situation
142
DefaultMutableTreeNode JavaDoc rootNode =
143                         (DefaultMutableTreeNode JavaDoc)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