KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.Color JavaDoc;
27 import java.awt.FlowLayout JavaDoc;
28 import java.net.URL JavaDoc;
29
30 import javax.swing.Icon JavaDoc;
31 import javax.swing.ImageIcon JavaDoc;
32 import javax.swing.JApplet JavaDoc;
33 import javax.swing.JScrollPane JavaDoc;
34 import javax.swing.JTextPane JavaDoc;
35 import javax.swing.JTree JavaDoc;
36 import javax.swing.SwingUtilities JavaDoc;
37 import javax.swing.UIManager JavaDoc;
38 import javax.swing.tree.DefaultTreeCellRenderer JavaDoc;
39 import javax.swing.tree.TreeNode JavaDoc;
40
41 import netscape.javascript.JSObject;
42
43 /**
44  * This is the applet-class. It is very simple and apart from initializing the tree
45  * it consists mainly of methods for the applet to influence and be influenced by its surroundings.
46  */

47  
48 public class OpenTree extends JApplet JavaDoc
49 {
50     
51     private Icon JavaDoc customOpenIcon = null;
52     private Icon JavaDoc customClosedIcon = null;
53     private Icon JavaDoc customLeafIcon = null;
54
55     private TreeNode JavaDoc rootNode;
56     protected JTree JavaDoc nodeTree;
57     private JScrollPane JavaDoc sp;
58     private Controller controller;
59     private Integer JavaDoc repositoryId;
60     private String JavaDoc entityName;
61     private String JavaDoc hideLeafs;
62     
63     private Integer JavaDoc currentNodeId;
64
65     public void init()
66     {
67         System.out.println("Initializing tree...");
68             
69         String JavaDoc serverAddress = null;
70         Color JavaDoc bgColor = null;
71         
72         try
73         {
74             int rColor = Integer.parseInt(this.getParameter("bgColorR"));
75             int gColor = Integer.parseInt(this.getParameter("bgColorG"));
76             int bColor = Integer.parseInt(this.getParameter("bgColorB"));
77             bgColor = new Color JavaDoc(rColor,gColor,bColor);
78             
79             this.entityName = this.getParameter("entityName");
80             this.repositoryId = new Integer JavaDoc(this.getParameter("repositoryId"));
81             this.hideLeafs = this.getParameter("hideLeafs");
82             
83             URL JavaDoc codeBase = this.getCodeBase();
84             String JavaDoc applicationPath = this.getDocumentBase().getPath().substring(0, this.getDocumentBase().getPath().lastIndexOf("/"));
85             System.out.println("applicationPath:" + applicationPath);
86             serverAddress = codeBase.getProtocol() + "://" + codeBase.getHost() + ":" + codeBase.getPort() + "" + applicationPath + "/" + entityName + "TreeService";
87             String JavaDoc imageBaseURL = codeBase.getProtocol() + "://" + codeBase.getHost() + ":" + codeBase.getPort() + "" + applicationPath + "/";
88             System.out.println("serverAddress:" + serverAddress);
89             
90             String JavaDoc folderOpenImage = imageBaseURL + "images/" + entityName + "folderOpen.gif";
91             String JavaDoc folderClosedImage = imageBaseURL + "images/" + entityName + "folderClosed.gif";
92             String JavaDoc documentImage = imageBaseURL + "images/" + entityName + "document.gif";
93             
94             System.out.println(folderOpenImage);
95             System.out.println(folderClosedImage);
96             System.out.println(documentImage);
97             
98             customOpenIcon = new ImageIcon JavaDoc(new URL JavaDoc(folderOpenImage));
99             customClosedIcon = new ImageIcon JavaDoc(new URL JavaDoc(folderClosedImage));
100             customLeafIcon = new ImageIcon JavaDoc(new URL JavaDoc(documentImage));
101         }
102         catch(Exception JavaDoc e)
103         {
104             e.printStackTrace();
105         }
106         
107         try
108         {
109             controller = new Controller(serverAddress, entityName, repositoryId, hideLeafs);
110             rootNode = controller.getRootNode();
111             nodeTree = new JTree JavaDoc(rootNode);
112             controller.setNodeTree(this.nodeTree);
113             
114             try{ UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); SwingUtilities.updateComponentTreeUI(nodeTree); } catch (Exception JavaDoc e){ e.printStackTrace(); }
115             
116             DefaultTreeCellRenderer JavaDoc renderer = new DefaultTreeCellRenderer JavaDoc();
117             renderer.setOpenIcon(customOpenIcon);
118             renderer.setClosedIcon(customClosedIcon);
119             renderer.setLeafIcon(customLeafIcon);
120             renderer.setBackgroundNonSelectionColor(bgColor);
121             nodeTree.setCellRenderer(renderer);
122         
123             FlowLayout JavaDoc flowManager = new FlowLayout JavaDoc(FlowLayout.LEFT);
124             flowManager.setHgap(0);
125             flowManager.setVgap(0);
126             
127             sp = new JScrollPane JavaDoc(nodeTree);
128             sp.setBorder(null);
129             getContentPane().setBackground(bgColor);
130             getContentPane().add(sp, "Center");
131             
132             nodeTree.setBackground(bgColor);
133             nodeTree.addTreeSelectionListener(new NodeTreeSelectionListener(this));
134             CMSTreeListener cmsTreeListener = new CMSTreeListener(nodeTree, controller);
135             nodeTree.addMouseListener(cmsTreeListener);
136             nodeTree.addTreeWillExpandListener(cmsTreeListener);
137             
138         }
139         catch(Exception JavaDoc e)
140         {
141             FlowLayout JavaDoc flowManager = new FlowLayout JavaDoc(FlowLayout.LEFT);
142             flowManager.setHgap(0);
143             flowManager.setVgap(0);
144             getContentPane().setBounds(5, 5, 150, 300);
145             getContentPane().setLayout(flowManager);
146             getContentPane().setBackground(bgColor);
147             JTextPane JavaDoc errorMessage = new JTextPane JavaDoc();
148             errorMessage.setBackground(bgColor);
149             errorMessage.setText("Error:\r\nAn server-error made it\r\nimpossible to show the tree. \r\nPlease make sure that there are\r\nat least one repository available. \r\nIf the problem persists report it to \r\nyour technical straff.");
150             
151             getContentPane().add(errorMessage);
152             e.printStackTrace();
153         }
154         
155         notifyLoaded();
156     }
157
158     /**
159      * This method is used to call the external javascript that updates the
160      * mainarea with a new screen. It's used to show the node you just clicked on
161      * in the tree.
162      */

163     
164     public void openMainArea(CMSNode node)
165     {
166         System.out.println("Trying to activate the main area through javascript...");
167         try
168         {
169             JSObject win = JSObject.getWindow(this);
170             Object JavaDoc[] args = {node.getId().toString(), this.repositoryId.toString(), getPath(node, new StringBuffer JavaDoc())};
171             String JavaDoc functionName = "loadMainArea";
172             //System.out.println("Calling function " + functionName + " with arguments " + args[0] + ", " + args[1]);
173
win.call(functionName, args);
174             //System.out.println("Setting the content for easy access:" + node.getId());
175
this.currentNodeId = node.getId();
176         }
177         catch(Exception JavaDoc e)
178         {
179             System.out.println("An error occurred while we tried to call a javascript: " + e);
180         }
181     }
182
183
184     /**
185      * This method is used to call the external javascript that updates the
186      * mainarea with a new screen. It's used to show the node you just clicked on
187      * in the tree.
188      */

189     
190     public void notifyLoaded()
191     {
192         System.out.println("Going to tell the world that I'm fully loaded...");
193         try
194         {
195             JSObject win = JSObject.getWindow(this);
196             Object JavaDoc[] args = {};
197             String JavaDoc functionName = "notifyIsLoaded";
198             //System.out.println("Calling function " + functionName);
199
win.call(functionName, args);
200         }
201         catch(Exception JavaDoc e)
202         {
203             System.out.println("An error occurred while we tried to call a javascript: " + e);
204             e.printStackTrace();
205         }
206         System.out.println("notifyLoaded done...");
207     }
208
209
210     public void refreshTreeNode(Integer JavaDoc nodeId, Integer JavaDoc changeTypeId, Integer JavaDoc addedNodeId)
211     {
212         //System.out.println("RefreshNode was called with id:" + nodeId + " and changeTypeId:" + changeTypeId);
213
controller.refreshNode(nodeId, changeTypeId, addedNodeId);
214     }
215     
216     public void refreshTreeNode(int nodeId, int changeTypeId, int addedNodeId)
217     {
218         //System.out.println("RefreshNode was called with id:" + nodeId + " and changeTypeId:" + changeTypeId);
219
controller.refreshNode(new Integer JavaDoc(nodeId), new Integer JavaDoc(changeTypeId), new Integer JavaDoc(addedNodeId));
220     }
221     
222     private StringBuffer JavaDoc getPath(CMSNode node, StringBuffer JavaDoc path)
223     {
224         
225         path.insert(0, "/" + node.getName());
226         //System.out.println("path:" + path);
227
if(node.getParent() != null)
228         {
229             getPath((CMSNode)node.getParent(), path);
230         }
231             
232         return path;
233     }
234
235     /**
236      * This method is called to resize the applet when the frame it is in is changed.
237      */

238     
239     public void setSize(int width, int height)
240     {
241         //System.out.println("Resizing the entire applet to: " + width + ":" + height);
242
super.setSize(width,height);
243         //this.setSize(width,height);
244
//this.setBounds(0,0,width,height);
245
validate();
246         //this.getContentPane().setSize(width-2,height-2);
247
//this.getContentPane().setBounds(1,1,width-2,height-2);
248
//getContentPane().validate();
249
//getContentPane().repaint();
250
//sp.setSize(width,height);
251
//sp.validate();
252
//sp.repaint();
253
//validate();
254
}
255
256
257 }
258
Popular Tags