KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > util > BasicTreeview


1 /*
2  * Created on 22 mars 2004
3  *
4  * Copyright Improve SA 2004.
5  * All rights reserved.
6  */

7 package fr.improve.struts.taglib.layout.util;
8
9 import javax.servlet.ServletContext JavaDoc;
10 import javax.servlet.http.HttpServletRequest JavaDoc;
11 import javax.servlet.jsp.JspException JavaDoc;
12
13 import org.apache.struts.Globals;
14
15 import fr.improve.struts.taglib.layout.menu.MenuAction;
16 import fr.improve.struts.taglib.layout.menu.MenuComponent;
17 import fr.improve.struts.taglib.layout.skin.Skin;
18 import fr.improve.struts.taglib.layout.treeview.TreeItemInfo;
19 import fr.improve.struts.taglib.layout.treeview.TreeViewTag;
20
21 /**
22  * TODO Modify the javascript to allow \n.
23  *
24  * @author jnribette
25  */

26 public class BasicTreeview implements TreeviewInterface {
27     protected static String JavaDoc VERTICAL_TREE = "treeLine.gif";
28     protected static String JavaDoc CLOSE_NODE = "treeNodeClose.gif";
29     protected static String JavaDoc LAST_CLOSE_NODE = "treeNodeCloseLast.gif";
30     protected static String JavaDoc OPEN_NODE = "treeNodeOpen.gif";
31     protected static String JavaDoc LAST_OPEN_NODE = "treeNodeOpenLast.gif";
32     protected static String JavaDoc LAST_ITEM = "treeItemLast.gif";
33     protected static String JavaDoc ITEM = "treeItem.gif";
34     
35     /**
36      * Starts a link.
37      */

38     protected void startLink(StringBuffer JavaDoc buffer, HttpServletRequest JavaDoc in_request, ServletContext JavaDoc in_servletcontext, String JavaDoc in_styleClass, String JavaDoc in_link, String JavaDoc in_target, String JavaDoc in_bundle, String JavaDoc in_tooltip) throws JspException JavaDoc {
39         buffer.append("<a HREF=\"");
40         String JavaDoc lc_link = in_link;
41         if (in_target==null && !LayoutUtils.getSkin(in_request.getSession()).getFollowLinkIfFormChanged()) {
42             // PENDING
43
// LayoutUtils.computeURL should be called, but this would required a lot of refactorization.
44
StringBuffer JavaDoc lc_newLink = new StringBuffer JavaDoc("javascript:checkFormChange('");
45             lc_newLink.append(in_link);
46             lc_newLink.append("','");
47             lc_newLink.append(LayoutUtils.getLabel(in_request, in_servletcontext, Globals.MESSAGES_KEY, "layout.dataLost", null, false));
48             lc_newLink.append("')");
49             lc_newLink.append("\" onMouseOver=\"status='");
50             lc_newLink.append(in_link);
51             lc_newLink.append("';return true;\" onMouseOut=\"status='';return true;");
52             lc_link = lc_newLink.toString();
53         }
54         buffer.append(lc_link);
55         buffer.append("\"");
56         if (in_styleClass!=null && in_styleClass.length()!=0) {
57             buffer.append(" class=\"");
58             buffer.append(in_styleClass);
59             buffer.append("\"");
60         }
61         if (in_target!=null && in_target.length()!=0) {
62             buffer.append(" target=\"");
63             buffer.append(in_target);
64             buffer.append("\"");
65         }
66         if (in_tooltip!=null && in_tooltip.length()!=0) {
67             String JavaDoc lc_tooltip = LayoutUtils.getLabel(in_request, in_servletcontext,in_bundle, in_tooltip, null, false);
68             buffer.append(" title=\"");
69             buffer.append(lc_tooltip);
70             buffer.append("\" alt=\"");
71             buffer.append(lc_tooltip);
72             buffer.append("\"");
73         }
74         buffer.append(">");
75     }
76
77     /**
78      * Ends a link.
79      */

80     protected void endLink(StringBuffer JavaDoc buffer) {
81         buffer.append("</a>");
82     }
83
84     /**
85      * Renders a label (easy !)
86      */

87     protected void renderLabel(StringBuffer JavaDoc buffer, HttpServletRequest JavaDoc in_request, ServletContext JavaDoc in_servletcontext, String JavaDoc in_bundle, String JavaDoc key) throws JspException JavaDoc {
88         buffer.append(LayoutUtils.getLabel(in_request, in_servletcontext,in_bundle, key, null, false));
89     }
90     
91     /**
92      * Renders a link with a label.
93      */

94     public void renderLabel(StringBuffer JavaDoc buffer, HttpServletRequest JavaDoc in_request, ServletContext JavaDoc in_servletcontext, TreeItemInfo in_info, MenuComponent in_item) throws JspException JavaDoc {
95         String JavaDoc lc_link = in_item.getLocation();
96         if (lc_link!=null && lc_link.length()!=0) {
97             startLink(buffer, in_request, in_servletcontext, in_info.styleClass, lc_link, in_item.getTarget(), in_info.bundle, in_item.getToolTip());
98         }
99         renderLabel(buffer, in_request, in_servletcontext, in_info.bundle, in_item.getTitle());
100         if (lc_link!=null && lc_link.length()!=0) {
101             endLink(buffer);
102         }
103     }
104
105     // Render the item image.
106
public void renderImage(StringBuffer JavaDoc buffer, HttpServletRequest JavaDoc in_request, TreeItemInfo in_info, MenuComponent in_item) {
107         if (in_item.getImage()!=null) {
108             if (in_info.hasSubMenus) {
109                 // javascript to fully collapse or expand a node
110
buffer.append("<a HREF=\"");
111                 if (!in_info.useIndirection) {
112                     buffer.append("javascript://\"");
113                 } else {
114                     buffer.append(in_request.getContextPath());
115                     buffer.append("/");
116                     buffer.append(LayoutUtils.getSkin(in_request.getSession()).getProperty(Skin.TREEVIEW_ACTION, TreeViewTag.DEFAULT_TREEVIEW_ACTION));
117                     buffer.append("?open=");
118                     buffer.append(in_info.path);
119                     buffer.append("&bundle=");
120                     buffer.append(in_info.bundle);
121                     buffer.append("&name=");
122                     buffer.append(in_info.name);
123                     buffer.append("&class=");
124                     buffer.append(in_info.styleClass);
125                     buffer.append("\" id=\"treeViewNode");
126                     buffer.append(in_info.path);
127                     buffer.append("\"");
128                 }
129                 
130                 buffer.append(" onclick=\"return changeTreeAndSubtrees('");
131                 
132                 
133     // buffer.append("<a HREF=\"");
134
// buffer.append("javascript://\" onclick=\"return changeTreeAndSubtrees('");
135
buffer.append(in_info.path);
136                 buffer.append("');");
137                 buffer.append("\" target=\"treeFrame\">");
138             }
139             
140             // Image
141
buffer.append("<img SRC=\"");
142             buffer.append(in_info.imageDirectory);
143             buffer.append("/");
144             buffer.append(in_item.getImage());
145             buffer.append("\" border=\"0\">");
146             
147             if (in_info.hasSubMenus) {
148                 // javascript to fully collapse or expand a node
149
buffer.append("</a>&nbsp;");
150             }
151         }
152     }
153
154     public void endLabel(StringBuffer JavaDoc buffer) {
155         buffer.append("</td>");
156     }
157
158     public void startLabel(StringBuffer JavaDoc buffer, String JavaDoc in_styleClass, String JavaDoc onclick, String JavaDoc style) {
159         // display the label
160
buffer.append("<td");
161         if (in_styleClass!=null && in_styleClass.length()!=0) {
162             buffer.append(" class=\"");
163             buffer.append(in_styleClass);
164             buffer.append("\"");
165         }
166         if (style!=null) {
167             buffer.append(" style=\"");
168             buffer.append(style);
169             buffer.append("\"");
170         }
171         if (onclick!=null) {
172             buffer.append(" onclick=\"");
173             buffer.append(onclick);
174             buffer.append("\"");
175         }
176         buffer.append(">");
177     }
178
179     public void endRow(StringBuffer JavaDoc buffer) {
180         // end a row.
181
buffer.append("</tr>");
182     }
183
184     public int endSubMenu(StringBuffer JavaDoc buffer, HttpServletRequest JavaDoc in_request, ServletContext JavaDoc in_servletcontext, int lc_numberOfMenusPrinted, MenuComponent currentMenu, TreeItemInfo in_info) throws JspException JavaDoc {
185         buffer.append("</tr>");
186         buffer.append("<tr><td valign=\"top\" colspan=\"2\" id=\"treeView" + in_info.path + "\"");
187         if (in_info.isClosed) {
188             buffer.append(" style=\"display:none;\"");
189         }
190         buffer.append(">");
191         if (!in_info.useIndirection) {
192             buffer.append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">");
193             lc_numberOfMenusPrinted += TreeViewTag.doPrintMenu(buffer, currentMenu.getMenuComponents(), in_request, in_servletcontext, in_info.bundle, in_info.path, in_info.name, in_info.styleClass);
194             buffer.append("</table>");
195         }
196         buffer.append("</td></tr>");
197         buffer.append("</table></td>");
198         return lc_numberOfMenusPrinted;
199     }
200
201     public void startSubMenu(StringBuffer JavaDoc buffer) {
202         buffer.append("<td valign=\"top\" colspan=\"2\"><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\"><tr>");
203     }
204
205     /**
206      * Render an icon.
207      * Do nothing in this implementation.
208      */

209     public void renderIcon(StringBuffer JavaDoc buffer, TreeItemInfo in_info, MenuComponent in_item) {
210         buffer.append("<td width=\"1\">");
211         /* zone coup?e et d?plac?e ? c?t? du label/lien, qqs lignes plus loin
212             if (image!=null) {
213                 buffer.append("<img SRC=\"");
214                 buffer.append(imageDirectory);
215                 buffer.append("/");
216                 buffer.append(image);
217                 buffer.append("\" border=\"0\">");
218             }
219         */

220         buffer.append("</td>");
221     }
222
223     /**
224      * Display the tree images in a TD.
225      */

226     public void renderTree(StringBuffer JavaDoc buffer, HttpServletRequest JavaDoc in_request, TreeItemInfo in_info) {
227         
228         buffer.append("<td valign=\"top\" height=\"100%\"");
229
230         // if it's not the last item, special code to display a vertical line in background.
231
if (!in_info.isLast) {
232             buffer.append(" style=\"background-image: url(");
233             buffer.append(in_info.imageDirectory);
234             buffer.append("/");
235             buffer.append(VERTICAL_TREE);
236             buffer.append("); background-repeat: repeat-y; \"");
237         }
238         
239         buffer.append(" width=\"");
240         buffer.append(16);
241         buffer.append("\">");
242         
243         // choose the right image to display.
244
if (in_info.hasSubMenus) {
245             String JavaDoc lc_closeImage;
246             String JavaDoc lc_openImage;
247             if (in_info.isLast) {
248                 lc_closeImage = LAST_CLOSE_NODE;
249                 lc_openImage = LAST_OPEN_NODE;
250             } else {
251                 lc_closeImage = CLOSE_NODE;
252                 lc_openImage = OPEN_NODE;
253             }
254             startTreeUrl(buffer, in_request, in_info, lc_closeImage, lc_openImage);
255             renderTreeImage(buffer, in_info, lc_closeImage, lc_openImage, 16, 22);
256             endTreeUrl(buffer);
257         } else {
258             buffer.append("<img SRC=\"");
259             buffer.append(in_info.imageDirectory);
260             buffer.append("/");
261             if (in_info.isLast) {
262                 // last item (not a folder)
263
buffer.append(LAST_ITEM);
264             } else {
265                 // not last item (not a folder)
266
buffer.append(ITEM);
267             }
268             buffer.append("\" border=\"0\" width=\"16\" height=\"22\">");
269         }
270         
271         buffer.append("</td>");
272         // ------------------ end display the tree -------------------
273
}
274
275     protected void renderTreeImage(StringBuffer JavaDoc buffer, TreeItemInfo in_info, String JavaDoc lc_closeImage, String JavaDoc lc_openImage, int in_width, int in_height) {
276         buffer.append("<img SRC=\"");
277         buffer.append(in_info.imageDirectory);
278         buffer.append("/");
279         buffer.append(in_info.isClosed ? lc_closeImage : lc_openImage);
280         buffer.append("\" id=\"treeViewImage");
281         buffer.append(in_info.path);
282         buffer.append("\" border=\"0\" width=\"");
283         buffer.append(in_width);
284         buffer.append("\" height=\"");
285         buffer.append(in_height);
286         buffer.append("\" align=\"middle\">");
287     }
288
289     protected void endTreeUrl(StringBuffer JavaDoc buffer) {
290         buffer.append("</a>");
291     }
292
293     protected void startTreeUrl(StringBuffer JavaDoc buffer, HttpServletRequest JavaDoc in_request, TreeItemInfo in_info, String JavaDoc lc_closeImage, String JavaDoc lc_openImage) {
294         buffer.append("<a HREF=\"");
295         if (!in_info.useIndirection) {
296             buffer.append("javascript://\" onclick=\"return changeTree('");
297         } else {
298             buffer.append(in_request.getContextPath());
299             buffer.append("/");
300             buffer.append(LayoutUtils.getSkin(in_request.getSession()).getProperty(Skin.TREEVIEW_ACTION, TreeViewTag.DEFAULT_TREEVIEW_ACTION));
301             buffer.append("?open=");
302             buffer.append(in_info.path);
303             buffer.append("&bundle=");
304             buffer.append(in_info.bundle);
305             buffer.append("&name=");
306             buffer.append(in_info.name);
307             buffer.append("&class=");
308             buffer.append(in_info.styleClass);
309             buffer.append("\" id=\"treeViewNode");
310             buffer.append(in_info.path);
311             buffer.append("\" onclick=\"return changeTree('");
312         }
313         buffer.append(in_info.path);
314         buffer.append("','");
315         buffer.append(in_info.imageDirectory);
316         buffer.append("/");
317         buffer.append(lc_openImage);
318         buffer.append("','");
319         buffer.append(in_info.imageDirectory);
320         buffer.append("/");
321         buffer.append(lc_closeImage);
322         buffer.append("');");
323         buffer.append("\" target=\"treeFrame\">");
324     }
325     
326
327     /**
328      * Starts a row.
329      */

330     public void startRow(StringBuffer JavaDoc buffer) {
331         buffer.append("<tr>");
332     }
333
334     /**
335      * Compute the new styleClass:
336      * If the styleClass ends with a digit, increment the digit by one.
337      */

338     public String JavaDoc computeStyleClass(String JavaDoc in_styleClass) {
339         String JavaDoc lc_newStyleClass = in_styleClass;
340         if (in_styleClass.length()!=0) {
341             char lc_lastChar = in_styleClass.charAt(in_styleClass.length()-1);
342             if (Character.isDigit(lc_lastChar)) {
343             // Asume there won't be more than 9 defined styleClasses.
344
lc_newStyleClass = lc_newStyleClass.substring(0, lc_newStyleClass.length()-1) + (char)(lc_lastChar+1);
345             }
346         }
347         return lc_newStyleClass;
348     }
349     
350     public void renderActions(StringBuffer JavaDoc buffer, HttpServletRequest JavaDoc in_request, ServletContext JavaDoc in_context, TreeItemInfo in_info, MenuComponent in_item) throws JspException JavaDoc {
351         MenuAction[] lc_actions = in_item.getMenuAction();
352         buffer.append("<td align=\"right\" nowrap>");
353         int i;
354         for (i = 0; i < lc_actions.length; i++) {
355             MenuAction lc_menuAction = lc_actions[i];
356             String JavaDoc lc_href = lc_menuAction.getHref();
357             if (lc_href!=null) {
358                 String JavaDoc lc_computedHref = lc_href;
359                 if (lc_computedHref.indexOf('?')==-1) {
360                     lc_computedHref = lc_computedHref + "?path=" + in_info.path;
361                 }
362                 startLink(buffer, in_request, in_context, null, lc_computedHref, lc_menuAction.getTarget(), in_info.bundle, null);
363             }
364             buffer.append("<img SRC=\"");
365             buffer.append(in_info.imageDirectory);
366             buffer.append("/");
367             buffer.append(lc_menuAction.getImgSrc());
368             buffer.append("\" align=\"middle\" border=\"0\">");
369             if (lc_href!=null) {
370                 endLink(buffer);
371             }
372         }
373         buffer.append("</td>");
374     }
375
376 }
377
Popular Tags