1 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 ; 37 import java.io.PrintWriter ; 38 import java.util.MissingResourceException ; 39 import java.util.ResourceBundle ; 40 import java.util.Set ; 41 import java.util.logging.Logger ; 42 43 64 public class HtmlRenderer 65 extends AbstractRenderer 66 { 67 private static L10N L = new L10N( HtmlRenderer.class ); 68 69 static protected final Logger log = 70 Logger.getLogger( HtmlRenderer.class.getName() ); 71 72 public final static String PREFERENCE_STYLESHEET = "html.stylesheet"; 73 74 private String _pageTitle = "Resin Documentation"; 75 private boolean _compact = false; 76 private String _doctype = "html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transistional.dtd\""; 77 78 private String _stylesheet = "portal.css"; 79 80 private Location _titleLocation; 81 private Menu _windowStateMenu; 82 private Menu _portletModeMenu; 83 84 92 public void setPageTitle( String pageTitle ) 93 { 94 _pageTitle = pageTitle; 95 } 96 97 public String getPageTitle( ) 98 { 99 return _pageTitle; 100 } 101 102 106 public void setCompact( boolean compact ) 107 { 108 _compact = compact; 109 } 110 111 public boolean isCompact() 112 { 113 return _compact; 114 } 115 116 120 public void setDoctype( String doctype ) 121 { 122 _doctype = doctype; 123 } 124 125 public String getDoctype() 126 { 127 return _doctype; 128 } 129 130 134 public void setStylesheet( String stylesheet ) 135 { 136 _stylesheet = stylesheet; 137 } 138 139 public String getStylesheet() 140 { 141 return _stylesheet; 142 } 143 144 148 public void setTitleLocation( String titleLocation ) 149 { 150 _titleLocation = Location.getLocation( titleLocation ); 151 } 152 153 public Location getTitleLocation() 154 { 155 return _titleLocation; 156 } 157 158 161 public void setWindowStateMenu( Menu windowStateMenu ) 162 { 163 _windowStateMenu = windowStateMenu; 164 } 165 166 public Menu getWindowStateMenu() 167 { 168 return _windowStateMenu; 169 } 170 171 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 out, 194 RenderRequest request, 195 String namespace ) 196 throws IOException 197 { 198 PortletPreferences pref = request.getPreferences(); 199 PortletResponse response = getRenderResponse( request ); 200 201 String stylesheet = pref.getValue( PREFERENCE_STYLESHEET, _stylesheet ); 202 203 String pageTitle = _pageTitle; 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 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 out, 238 RenderRequest request, 239 String namespace ) 240 throws IOException 241 { 242 ResourceBundle 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>" ); printNewline( out ); 279 280 out.print( "<div class='portlet-content'>" ); 281 printNewline( out ); 282 } 283 284 protected void endWindow( PrintWriter out, 285 RenderRequest request, 286 String namespace ) 287 throws IOException 288 { 289 ResourceBundle 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>" ); printNewline( out ); 307 out.print( "</div>" ); out.print( "<!-- " ); 309 out.print( namespace ); 310 out.print( " -->" ); 311 printNewline( out ); 312 } 313 314 protected void endPage( PrintWriter out, 315 RenderRequest request, 316 String namespace ) 317 throws IOException 318 { 319 out.print( "</body>" ); 320 printNewline( out ); 321 out.print( "</html>" ); 322 printNewline( out ); 323 } 324 325 protected void printNewline( PrintWriter out ) 326 { 327 if ( !_compact ) 328 out.println(); 329 } 330 331 protected void htmlTitle( PrintWriter out, 332 RenderRequest request, 333 String namespace, 334 ResourceBundle resourceBundle ) 335 throws IOException 336 { 337 String 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 out, 347 RenderRequest request, 348 String namespace, 349 ResourceBundle resourceBundle ) 350 throws IOException 351 { 352 PortletMode currentPortletMode = request.getPortletMode(); 353 Set <PortletMode> portletModes = getPortletModes( request ); 354 355 Menu.MenuRenderer menuRenderer = _portletModeMenu.createRenderer(); 356 357 for ( PortletMode portletMode : portletModes ) { 358 String title = portletMode.toString(); 359 String shortDescription = null; 360 String 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 ( ex ); 372 } 373 374 urlString = url.toString(); 375 } 376 377 if ( resourceBundle != null ) { 378 StringBuffer key = new StringBuffer (); 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 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 ex ) { 400 } 401 } 402 403 menuRenderer.add( title, shortDescription, urlString, isSelected ); 404 } 405 406 menuRenderer.print( out ); 407 } 408 409 protected void htmlWindowStateMenu( PrintWriter out, 410 RenderRequest request, 411 String namespace, 412 ResourceBundle resourceBundle ) 413 throws IOException 414 { 415 WindowState currentWindowState = request.getWindowState(); 416 Set <WindowState> windowStates = getWindowStates( request ); 417 418 Menu.MenuRenderer menuRenderer = _windowStateMenu.createRenderer(); 419 420 for ( WindowState windowState : windowStates ) { 421 String title = windowState.toString(); 422 String shortDescription = null; 423 String 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 ( ex ); 435 } 436 437 urlString = url.toString(); 438 } 439 440 if ( resourceBundle != null ) { 441 StringBuffer key = new StringBuffer (); 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 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 ex ) { 463 } 464 } 465 466 menuRenderer.add( title, shortDescription, urlString, isSelected ); 467 } 468 469 menuRenderer.print( out ); 470 } 471 } 472 | Popular Tags |