KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > portal > alpharenderer > HtmlRenderer


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Sam
27  */

28
29
30 package com.caucho.portal.alpharenderer;
31
32 import com.caucho.portal.generic.AbstractRenderer;
33 import com.caucho.util.L10N;
34
35 import javax.portlet.*;
36 import java.io.IOException JavaDoc;
37 import java.io.PrintWriter JavaDoc;
38 import java.util.MissingResourceException JavaDoc;
39 import java.util.ResourceBundle JavaDoc;
40 import java.util.Set JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 /**
44  * Preferences:
45  *
46  * <dl>
47  * <dt>html.stylesheet
48  * <dd>the css stylesheet to use, overrides the value set with setStylesheet()
49  * </dl>
50  *
51  * ResourceBundle lookups:
52  *
53  * <dl>
54  * <dt>portletMode.<i>portletMode</i>.title
55  * <dd>The name to display when referring to the portletMode
56  * <dt>portletMode.<i>portletMode</i>.shortDescription
57  * <dd>A short description of the portlet mode
58  * <dt>windowState.<i>windowState</i>.title
59  * <dd>The name to display when referring to the windowState
60  * <dt>windowState.<i>windowState</i>.shortDescription
61  * <dd>A short description of the window state
62  * </dl>
63  */

64 public class HtmlRenderer
65   extends AbstractRenderer
66 {
67   private static L10N L = new L10N( HtmlRenderer.class );
68
69   static protected final Logger JavaDoc log =
70     Logger.getLogger( HtmlRenderer.class.getName() );
71
72   public final static String JavaDoc PREFERENCE_STYLESHEET = "html.stylesheet";
73
74   private String JavaDoc _pageTitle = "Resin Documentation";
75   private boolean _compact = false;
76   private String JavaDoc _doctype = "html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transistional.dtd\"";
77
78   private String JavaDoc _stylesheet = "portal.css";
79
80   private Location _titleLocation;
81   private Menu _windowStateMenu;
82   private Menu _portletModeMenu;
83
84   /**
85    * The title for the page, included in the html output as:
86    * <pre>
87    * &lt;head&gt;
88    * &lt;title&gt;<i>pageTitle</i>&lt;/title&gt;
89    * &lt;/head&gt;
90    * </pre>
91    */

92   public void setPageTitle( String JavaDoc pageTitle )
93   {
94     _pageTitle = pageTitle;
95   }
96
97   public String JavaDoc getPageTitle( )
98   {
99     return _pageTitle;
100   }
101
102   /**
103    * Set to true for compact output without newlines,
104    * default false.
105    */

106   public void setCompact( boolean compact )
107   {
108     _compact = compact;
109   }
110
111   public boolean isCompact()
112   {
113     return _compact;
114   }
115
116   /**
117    * The doctype.
118    * Default is <code>html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transistional.dtd</code>
119    */

120   public void setDoctype( String JavaDoc doctype )
121   {
122     _doctype = doctype;
123   }
124
125   public String JavaDoc getDoctype()
126   {
127     return _doctype;
128   }
129
130   /**
131    * The stylesheet to use, unless overridden with the portlet preference
132    * <code>css.stylesheet</code>. Default is "/portal.css". .
133    */

134   public void setStylesheet( String JavaDoc stylesheet )
135   {
136     _stylesheet = stylesheet;
137   }
138
139   public String JavaDoc getStylesheet()
140   {
141     return _stylesheet;
142   }
143
144   /**
145    * Location to place the title of the portlet:
146    * "hidden", "frame", "header" (default), or "footer".
147    */

148   public void setTitleLocation( String JavaDoc titleLocation )
149   {
150     _titleLocation = Location.getLocation( titleLocation );
151   }
152
153   public Location getTitleLocation()
154   {
155     return _titleLocation;
156   }
157
158   /**
159    * Default is an instance of {@link HtmlMenu}
160    */

161   public void setWindowStateMenu( Menu windowStateMenu )
162   {
163     _windowStateMenu = windowStateMenu;
164   }
165
166   public Menu getWindowStateMenu()
167   {
168     return _windowStateMenu;
169   }
170
171   /**
172    * Default is an instance of {@link HtmlMenu}
173    */

174   public void setPortletModeMenu( Menu portletModeMenu )
175   {
176     _portletModeMenu = portletModeMenu;
177   }
178
179   public Menu getPortletModeMenu()
180   {
181     return _portletModeMenu;
182   }
183
184   public void init()
185   {
186     if ( _windowStateMenu == null )
187       _windowStateMenu = new HtmlMenu();
188
189     if ( _portletModeMenu == null )
190       _portletModeMenu = new HtmlMenu();
191   }
192
193   protected void beginPage( PrintWriter JavaDoc out,
194                             RenderRequest request,
195                             String JavaDoc namespace )
196     throws IOException JavaDoc
197   {
198     PortletPreferences pref = request.getPreferences();
199     PortletResponse response = getRenderResponse( request );
200
201     String JavaDoc stylesheet = pref.getValue( PREFERENCE_STYLESHEET, _stylesheet );
202
203     String JavaDoc pageTitle = _pageTitle; // XXX: page title from ?
204

205     if ( _doctype != null && _doctype.length() > 0 ) {
206       out.print("<!DOCTYPE ");
207       out.print( _doctype );
208       out.print( '>' );
209
210       printNewline( out );
211     }
212
213     out.print( "<html>" );
214     printNewline( out );
215     out.print( "<head>" );
216     printNewline( out );
217
218     if ( pageTitle != null ) {
219       out.print( "<title>" + pageTitle + "</title>" );
220       printNewline( out );
221     }
222
223     if ( stylesheet != null && stylesheet.length() > 0 ) {
224       String JavaDoc cssUrl = response.encodeURL( stylesheet );
225       out.print( "<link rel='StyleSheet' HREF='" );
226       out.print( cssUrl );
227       out.print( "' type='text/css' media='all'/>" );
228       printNewline( out );
229     }
230
231     out.print( "</head>" );
232     printNewline( out );
233     out.print( "<body>" );
234     printNewline( out );
235   }
236
237   protected void beginWindow( PrintWriter JavaDoc out,
238                               RenderRequest request,
239                               String JavaDoc namespace )
240     throws IOException JavaDoc
241   {
242     ResourceBundle JavaDoc resourceBundle = getResourceBundle( request );
243
244
245     out.print( "<div class='portlet-frame ");
246     printEscaped( out, request.getPortletMode().toString() );
247     out.print( ' ' );
248     printEscaped( out, request.getWindowState().toString() );
249     out.print( "' id='" );
250     out.print( namespace );
251     out.print( "'>" );
252
253     if ( _titleLocation == Location.FRAME )
254       htmlTitle( out, request, namespace, resourceBundle );
255
256     if ( _portletModeMenu.getLocation() == Location.FRAME )
257       htmlPortletModeMenu( out, request, namespace, resourceBundle );
258
259     if ( _windowStateMenu.getLocation() == Location.FRAME )
260       htmlWindowStateMenu( out, request, namespace, resourceBundle );
261
262     printNewline( out );
263
264     out.print( "<div class='portlet-header'>" );
265
266     printNewline( out );
267
268     if ( _titleLocation == Location.HEADER )
269       htmlTitle( out, request, namespace, resourceBundle );
270
271     if ( _portletModeMenu.getLocation() == Location.HEADER )
272       htmlPortletModeMenu( out, request, namespace, resourceBundle );
273
274     if ( _windowStateMenu.getLocation() == Location.HEADER )
275       htmlWindowStateMenu( out, request, namespace, resourceBundle );
276
277     out.print( "</div>" ); // class='portlet-topbar'>
278
printNewline( out );
279
280     out.print( "<div class='portlet-content'>" );
281     printNewline( out );
282   }
283
284   protected void endWindow( PrintWriter JavaDoc out,
285                             RenderRequest request,
286                             String JavaDoc namespace )
287     throws IOException JavaDoc
288   {
289     ResourceBundle JavaDoc resourceBundle = getResourceBundle( request );
290
291     out.print( "</div>" );
292     printNewline( out );
293     out.print( "<div class='portlet-footer'>" );
294     printNewline( out );
295
296     if ( _titleLocation == Location.FOOTER )
297       htmlTitle( out, request, namespace, resourceBundle );
298
299     if ( _portletModeMenu.getLocation() == Location.FOOTER )
300       htmlPortletModeMenu( out, request, namespace, resourceBundle );
301
302     if ( _windowStateMenu.getLocation() == Location.FOOTER )
303       htmlWindowStateMenu( out, request, namespace, resourceBundle );
304
305     out.print( "</div>" ); // footer
306
printNewline( out );
307     out.print( "</div>" ); // frame
308
out.print( "<!-- " );
309     out.print( namespace );
310     out.print( " -->" );
311     printNewline( out );
312   }
313
314   protected void endPage( PrintWriter JavaDoc out,
315                           RenderRequest request,
316                           String JavaDoc namespace )
317     throws IOException JavaDoc
318   {
319     out.print( "</body>" );
320     printNewline( out );
321     out.print( "</html>" );
322     printNewline( out );
323   }
324
325   protected void printNewline( PrintWriter JavaDoc out )
326   {
327     if ( !_compact )
328       out.println();
329   }
330
331   protected void htmlTitle( PrintWriter JavaDoc out,
332                             RenderRequest request,
333                             String JavaDoc namespace,
334                             ResourceBundle JavaDoc resourceBundle )
335     throws IOException JavaDoc
336   {
337     String JavaDoc title = getTitle( request );
338
339     if ( title != null ) {
340       out.print( "<h1>" );
341       out.print( title );
342       out.print( "</h1>" );
343     }
344   }
345
346   protected void htmlPortletModeMenu( PrintWriter JavaDoc out,
347                                       RenderRequest request,
348                                       String JavaDoc namespace,
349                                       ResourceBundle JavaDoc resourceBundle )
350     throws IOException JavaDoc
351   {
352     PortletMode currentPortletMode = request.getPortletMode();
353     Set JavaDoc<PortletMode> portletModes = getPortletModes( request );
354
355     Menu.MenuRenderer menuRenderer = _portletModeMenu.createRenderer();
356
357     for ( PortletMode portletMode : portletModes ) {
358       String JavaDoc title = portletMode.toString();
359       String JavaDoc shortDescription = null;
360       String JavaDoc urlString = null;
361       boolean isSelected = true;
362
363       if ( ! portletMode.equals( currentPortletMode ) ) {
364         isSelected = false;
365         PortletURL url = createControlURL( request );
366
367         try {
368           url.setPortletMode( portletMode );
369         }
370         catch ( PortletModeException ex ) {
371           throw new RuntimeException JavaDoc( ex );
372         }
373
374         urlString = url.toString();
375       }
376
377       if ( resourceBundle != null ) {
378         StringBuffer JavaDoc key = new StringBuffer JavaDoc();
379
380         key.append( "portletMode." );
381         key.append( portletMode.toString() );
382         key.append( ".title" );
383
384         try {
385           title = resourceBundle.getString( key.toString() );
386         }
387         catch ( MissingResourceException JavaDoc ex ) {
388         }
389
390         key.setLength( 0 );
391
392         key.append( "portletMode." );
393         key.append( portletMode.toString() );
394         key.append( ".shortDescription" );
395
396         try {
397           shortDescription = resourceBundle.getString( key.toString() );
398         }
399         catch ( MissingResourceException JavaDoc ex ) {
400         }
401       }
402
403       menuRenderer.add( title, shortDescription, urlString, isSelected );
404     }
405
406     menuRenderer.print( out );
407   }
408
409   protected void htmlWindowStateMenu( PrintWriter JavaDoc out,
410                                       RenderRequest request,
411                                       String JavaDoc namespace,
412                                       ResourceBundle JavaDoc resourceBundle )
413     throws IOException JavaDoc
414   {
415     WindowState currentWindowState = request.getWindowState();
416     Set JavaDoc<WindowState> windowStates = getWindowStates( request );
417
418     Menu.MenuRenderer menuRenderer = _windowStateMenu.createRenderer();
419
420     for ( WindowState windowState : windowStates ) {
421       String JavaDoc title = windowState.toString();
422       String JavaDoc shortDescription = null;
423       String JavaDoc urlString = null;
424       boolean isSelected = true;
425
426       if ( ! windowState.equals( currentWindowState ) ) {
427         isSelected = false;
428         PortletURL url = createControlURL( request );
429
430         try {
431           url.setWindowState( windowState );
432         }
433         catch ( WindowStateException ex ) {
434           throw new RuntimeException JavaDoc( ex );
435         }
436
437         urlString = url.toString();
438       }
439
440       if ( resourceBundle != null ) {
441         StringBuffer JavaDoc key = new StringBuffer JavaDoc();
442
443         key.append( "windowState." );
444         key.append( windowState.toString() );
445         key.append( ".title" );
446
447         try {
448           title = resourceBundle.getString( key.toString() );
449         }
450         catch ( MissingResourceException JavaDoc ex ) {
451         }
452
453         key.setLength( 0 );
454
455         key.append( "windowState." );
456         key.append( windowState.toString() );
457         key.append( ".shortDescription" );
458
459         try {
460           shortDescription = resourceBundle.getString( key.toString() );
461         }
462         catch ( MissingResourceException JavaDoc ex ) {
463         }
464       }
465
466       menuRenderer.add( title, shortDescription, urlString, isSelected );
467     }
468
469     menuRenderer.print( out );
470   }
471 }
472
Popular Tags