KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > menu > MenuRenderer


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.menu;
22
23 import com.methodhead.tree.FoldingTreeNode;
24
25 /**
26   <p>
27     Renders a DHTML menu using the HVMenu 5.5
28     (http://www.dynamicdrive.com/dynamicindex1/hvmenu/). The
29     menu is logically defined using menu classes from the
30     <tt>java.awt</tt> package. Assemble <tt>Menu</tt>s and
31     <tt>MenuItem</tt>s almost as you would in a GUI application.
32     Only a few of the <tt>awt</tt> methods are important
33     however:
34   </p>
35   <ul>
36     <li><tt>MenuItem.setLabel()</tt>: Sets the menu item's label.</li>
37     <li>
38       <tt>MenuItem.setActionCommand()</tt>: Sets the menu
39       item's URL; set to an empty string if it should not link to
40       anything.
41     </li>
42   </ul>
43   <p>
44     Also, <tt>MenuBar</tt> is not used at all. Instead, the
45     menu bar is represented by the root <tt>Menu</tt>.
46     <tt>Menu</tt> offers more methods to manage it contents.
47   </p>
48   <p>
49     HVMenu offers many configuration options. This renderer
50     does not manage those options. Instead, this class renders
51     only the menu definitions. Therefore, other script must be
52     present for this to work right: the variables that define
53     the look and operation of the menus and the menu script
54     itself.
55   </p>
56  */

57 public class MenuRenderer {
58
59   // constructors /////////////////////////////////////////////////////////////
60

61   // constants ////////////////////////////////////////////////////////////////
62

63   // classes //////////////////////////////////////////////////////////////////
64

65   // methods //////////////////////////////////////////////////////////////////
66

67   /**
68    * Returns the JavaScript variable name for a menu item at
69    * <tt>path</tt> (e.g., a path of <tt>{1, 2}</tt> results
70    * in <tt>Menu1_2</tt>).
71    */

72   protected static String JavaDoc getVarName(
73     int[] path ) {
74
75     String JavaDoc html = "Menu";
76
77     for ( int i = 0; i < path.length; i++ ) {
78       if ( i == 0 )
79         html += path[ i ];
80       else
81         html += "_" + path[ i ];
82     }
83
84     return html;
85   }
86
87   /**
88    * For the menu items in <tt>menu</tt>, returns the length of the longest
89    * label.
90    */

91   protected static int getLongestLabelLength(
92     FoldingTreeNode menu ) {
93
94     int len = 0;
95     int tmp = 0;
96
97     for ( int i = 0; i < menu.getChildCount(); i++ ) {
98
99       String JavaDoc label = ( ( FoldingTreeNode )menu.getChildAt( i ) ).getLabel();
100       if ( label == null )
101         label = "";
102
103       tmp = label.length();
104       if ( tmp > len )
105         len = tmp;
106     }
107
108     return len;
109   }
110
111   protected void getMenuHtml(
112     StringBuffer JavaDoc buf,
113     FoldingTreeNode menu,
114     int[] path,
115     int width,
116     int height ) {
117
118     //
119
// prepend baseUrl if menu item has an action command
120
//
121
String JavaDoc url = menu.getUrl();
122     if ( url.length() > 0 )
123       url = baseUrl_ + url;
124
125     //
126
// render the html
127
//
128
String JavaDoc label = menu.getLabel();
129     if ( label == null )
130       label = "null label";
131
132     buf.append(
133       getVarName( path ) + "=new Array(\"" + label + "\",\"" +
134       url + "\",\"\"," + menu.getChildCount() +
135       "," + height + "," + width + ");\n" );
136
137     //
138
// recurse into menu items
139
//
140
if ( menu.getChildCount() > 0 ) {
141
142       //
143
// calculate the menu width
144
//
145
width = getLongestLabelLength( menu ) * fontWidthHint_;
146
147       //
148
// allocate a new path array
149
//
150
int[] menupath = new int[ path.length + 1 ];
151       System.arraycopy( path, 0, menupath, 0, path.length );
152       int last = menupath.length - 1;
153
154       //
155
// get menu html
156
//
157
for ( int i = 0; i < menu.getChildCount(); i++ ) {
158         menupath[ last ]++;
159         getMenuHtml(
160           buf,
161           ( FoldingTreeNode )menu.getChildAt( i ),
162           menupath,
163           width,
164           height );
165       }
166     }
167   }
168
169   /**
170    * Returns a string containing the render menu
171    * definitions.
172    */

173   public String JavaDoc renderMenu(
174     FoldingTreeNode menu ) {
175
176     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
177
178     buf.append( "NoOffFirstLineMenus=" + menu.getChildCount() + ";\n" );
179
180     int[] path = new int[ 1 ];
181     for ( int i = 0; i < menu.getChildCount(); i++ ) {
182       path[ 0 ] = i + 1;
183       FoldingTreeNode submenu = ( FoldingTreeNode )menu.getChildAt( i );
184
185       //
186
// check for a missing label
187
//
188
String JavaDoc label = submenu.getLabel();
189       if ( label == null )
190         label = "null label";
191
192       getMenuHtml(
193         buf,
194         submenu,
195         path,
196         fontWidthHint_ * label.length(),
197         fontHeightHint_ );
198     }
199
200     return buf.toString();
201   }
202
203   // properties ///////////////////////////////////////////////////////////////
204

205   /**
206    * Sets the approximate width of characters in the font used in the menu
207    * (defaults to 10).
208    */

209   public void setFontWidthHint(
210     int width ) {
211
212     fontWidthHint_ = width;
213   }
214
215   /**
216    * Sets the approximate height of characters in the font used in the menu
217    * (defaults to 20).
218    */

219   public void setFontHeightHint(
220     int height ) {
221
222     fontHeightHint_ = height;
223   }
224
225   /**
226    * Sets the base url prepended to all action commands.
227    */

228   public void setBaseUrl(
229     String JavaDoc baseUrl ) {
230
231     baseUrl_ = baseUrl;
232   }
233
234   // attributes ///////////////////////////////////////////////////////////////
235

236   protected int fontWidthHint_ = 10;
237   protected int fontHeightHint_ = 20;
238   protected String JavaDoc baseUrl_ = "";
239 }
240
Popular Tags