KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > plugins > opentree > Controller


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23  
24 package org.infoglue.cms.plugins.opentree;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 import javax.swing.JTree JavaDoc;
32 import javax.swing.tree.DefaultTreeModel JavaDoc;
33 import javax.swing.tree.TreePath JavaDoc;
34
35 import org.infoglue.cms.net.Node;
36
37 public class Controller
38 {
39     public static final int NODE_CHANGED = 1;
40     public static final int NODE_CHILDREN_CHANGED = 2;
41     public static final int NODE_MOVED = 3;
42     public static final int NODE_DELETED = 4;
43     
44     private JTree JavaDoc nodeTree;
45     private Communicator communicator;
46     private Hashtable JavaDoc nodes = new Hashtable JavaDoc();
47     
48     private String JavaDoc hideLeafs;
49     
50     public Controller(String JavaDoc serverAddress, String JavaDoc entityName, Integer JavaDoc repositoryId, String JavaDoc hideLeafs)
51     {
52         this.communicator = new Communicator(serverAddress, entityName, repositoryId);
53         this.hideLeafs = hideLeafs;
54     }
55
56     public void setNodeTree(JTree JavaDoc nodeTree)
57     {
58         this.nodeTree = nodeTree;
59     }
60     
61     /**
62      * This method refreshes a node and emties it's children so they can be reinitialized.
63      */

64     
65     public void refreshNode(Integer JavaDoc nodeId, Integer JavaDoc changeTypeId, Integer JavaDoc addedNodeId)
66     {
67         DefaultTreeModel JavaDoc model = (DefaultTreeModel JavaDoc)nodeTree.getModel();
68         CMSNode node = (CMSNode)nodes.get(nodeId);
69         //System.out.println("Node found:" + node);
70

71         if(changeTypeId.intValue() == NODE_CHANGED)
72         {
73             CMSNode newNode = getNode(nodeId);
74             //System.out.println("Updated Node found:" + newNode);
75

76             node.setName(newNode.getName());
77             model.nodeChanged(node);
78         }
79         else if(changeTypeId.intValue() == NODE_CHILDREN_CHANGED)
80         {
81             node.setAreChildrenDefined(false);
82             
83             //model.nodeChanged(node);
84
model.nodeStructureChanged(node);
85             //model.reload(node);
86

87             CMSNode addedNode = (CMSNode)nodes.get(addedNodeId);
88             TreePath JavaDoc currentPath = nodeTree.getSelectionPath();
89             if(currentPath == null)
90                  currentPath = new TreePath JavaDoc(node);
91                  
92             //System.out.println("Current path:" + currentPath);
93
//System.out.println("Added Node:" + addedNode);
94
nodeTree.setSelectionPath(currentPath.pathByAddingChild(addedNode));
95         }
96         else if(changeTypeId.intValue() == NODE_MOVED)
97         {
98             CMSNode currentParent = (CMSNode)((CMSNode)nodeTree.getLastSelectedPathComponent()).getParent();
99             //System.out.println("node:" + node.getName());
100
//System.out.println("currentParent:" + currentParent.getName());
101
currentParent.setAreChildrenDefined(false);
102             model.nodeStructureChanged(currentParent);
103             
104             node.setAreChildrenDefined(false);
105             model.nodeStructureChanged(node);
106
107             CMSNode addedNode = (CMSNode)nodes.get(addedNodeId);
108         }
109         else if(changeTypeId.intValue() == NODE_DELETED)
110         {
111             nodeTree.setSelectionPath(nodeTree.getSelectionPath().getParentPath());
112             node.setAreChildrenDefined(false);
113             model.nodeStructureChanged(node);
114             //System.out.println("Hoping this is the parent:" + node.getName());
115
}
116
117     }
118     
119     
120     public CMSNode getRootNode() throws Exception JavaDoc
121     {
122         Node rootNode = communicator.getRootNode();
123         CMSNode rootCMSNode = generateVisualTreeModel(rootNode);
124         
125         updateHash(rootCMSNode);
126         
127         //This should be there later when I know how to fire of events
128
//addContentChildrenToParent(rootCMSContent);
129

130         return rootCMSNode;
131     }
132     
133     public CMSNode getNode(Integer JavaDoc nodeId)
134     {
135         Node node = communicator.getNode(nodeId);
136         CMSNode cmsNode = generateVisualTreeModel(node);
137         
138         updateHash(cmsNode);
139         
140         return cmsNode;
141     }
142
143     public void updateHash(CMSNode node)
144     {
145         if(!nodes.containsKey(node.getId()))
146             nodes.put(node.getId(), node);
147     }
148
149     public List JavaDoc getChildNodes(Integer JavaDoc parentId)
150     {
151         List JavaDoc childNodeVOList = communicator.getChildNodeList(parentId);
152         List JavaDoc childNodes = generateVisualTreeModel(childNodeVOList);
153         
154         return childNodes;
155     }
156
157
158     private CMSNode generateVisualTreeModel(Node node)
159     {
160         
161         CMSNode rootNode = new CMSNode(this, node.getIsBranch().booleanValue());
162         rootNode.setId(node.getId());
163         rootNode.setName(node.getName());
164                
165         return rootNode;
166     }
167
168     private List JavaDoc generateVisualTreeModel(List JavaDoc nodeList)
169     {
170         ArrayList JavaDoc children = new ArrayList JavaDoc();
171         if(nodeList != null)
172         {
173             Iterator JavaDoc iterator = nodeList.iterator();
174             while(iterator.hasNext())
175             {
176                 Node node = (Node)iterator.next();
177                 if(this.hideLeafs == null || this.hideLeafs.equals("false") || node.getIsBranch().booleanValue() == true)
178                 {
179                     CMSNode cmsNode = new CMSNode(this, node.getIsBranch().booleanValue());
180                     cmsNode.setId(node.getId());
181                     cmsNode.setName(node.getName());
182                     children.add(cmsNode);
183     
184                     updateHash(cmsNode);
185                 }
186             }
187         }
188                 
189         return children;
190     }
191
192    
193 }
Popular Tags