001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Sam
027: */
028:
029: package com.caucho.portal.alpharenderer;
030:
031: import com.caucho.portal.generic.AbstractRenderer;
032: import com.caucho.util.L10N;
033:
034: import javax.portlet.*;
035: import java.io.IOException;
036: import java.io.PrintWriter;
037: import java.util.MissingResourceException;
038: import java.util.ResourceBundle;
039: import java.util.Set;
040: import java.util.logging.Logger;
041:
042: /**
043: * Preferences:
044: *
045: * <dl>
046: * <dt>html.stylesheet
047: * <dd>the css stylesheet to use, overrides the value set with setStylesheet()
048: * </dl>
049: *
050: * ResourceBundle lookups:
051: *
052: * <dl>
053: * <dt>portletMode.<i>portletMode</i>.title
054: * <dd>The name to display when referring to the portletMode
055: * <dt>portletMode.<i>portletMode</i>.shortDescription
056: * <dd>A short description of the portlet mode
057: * <dt>windowState.<i>windowState</i>.title
058: * <dd>The name to display when referring to the windowState
059: * <dt>windowState.<i>windowState</i>.shortDescription
060: * <dd>A short description of the window state
061: * </dl>
062: */
063: public class HtmlRenderer extends AbstractRenderer {
064: private static L10N L = new L10N(HtmlRenderer.class);
065:
066: static protected final Logger log = Logger
067: .getLogger(HtmlRenderer.class.getName());
068:
069: public final static String PREFERENCE_STYLESHEET = "html.stylesheet";
070:
071: private String _pageTitle = "Resin Documentation";
072: private boolean _compact = false;
073: private String _doctype = "html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transistional.dtd\"";
074:
075: private String _stylesheet = "portal.css";
076:
077: private Location _titleLocation;
078: private Menu _windowStateMenu;
079: private Menu _portletModeMenu;
080:
081: /**
082: * The title for the page, included in the html output as:
083: * <pre>
084: * <head>
085: * <title><i>pageTitle</i></title>
086: * </head>
087: * </pre>
088: */
089: public void setPageTitle(String pageTitle) {
090: _pageTitle = pageTitle;
091: }
092:
093: public String getPageTitle() {
094: return _pageTitle;
095: }
096:
097: /**
098: * Set to true for compact output without newlines,
099: * default false.
100: */
101: public void setCompact(boolean compact) {
102: _compact = compact;
103: }
104:
105: public boolean isCompact() {
106: return _compact;
107: }
108:
109: /**
110: * The doctype.
111: * Default is <code>html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transistional.dtd</code>
112: */
113: public void setDoctype(String doctype) {
114: _doctype = doctype;
115: }
116:
117: public String getDoctype() {
118: return _doctype;
119: }
120:
121: /**
122: * The stylesheet to use, unless overridden with the portlet preference
123: * <code>css.stylesheet</code>. Default is "/portal.css". .
124: */
125: public void setStylesheet(String stylesheet) {
126: _stylesheet = stylesheet;
127: }
128:
129: public String getStylesheet() {
130: return _stylesheet;
131: }
132:
133: /**
134: * Location to place the title of the portlet:
135: * "hidden", "frame", "header" (default), or "footer".
136: */
137: public void setTitleLocation(String titleLocation) {
138: _titleLocation = Location.getLocation(titleLocation);
139: }
140:
141: public Location getTitleLocation() {
142: return _titleLocation;
143: }
144:
145: /**
146: * Default is an instance of {@link HtmlMenu}
147: */
148: public void setWindowStateMenu(Menu windowStateMenu) {
149: _windowStateMenu = windowStateMenu;
150: }
151:
152: public Menu getWindowStateMenu() {
153: return _windowStateMenu;
154: }
155:
156: /**
157: * Default is an instance of {@link HtmlMenu}
158: */
159: public void setPortletModeMenu(Menu portletModeMenu) {
160: _portletModeMenu = portletModeMenu;
161: }
162:
163: public Menu getPortletModeMenu() {
164: return _portletModeMenu;
165: }
166:
167: public void init() {
168: if (_windowStateMenu == null)
169: _windowStateMenu = new HtmlMenu();
170:
171: if (_portletModeMenu == null)
172: _portletModeMenu = new HtmlMenu();
173: }
174:
175: protected void beginPage(PrintWriter out, RenderRequest request,
176: String namespace) throws IOException {
177: PortletPreferences pref = request.getPreferences();
178: PortletResponse response = getRenderResponse(request);
179:
180: String stylesheet = pref.getValue(PREFERENCE_STYLESHEET,
181: _stylesheet);
182:
183: String pageTitle = _pageTitle; // XXX: page title from ?
184:
185: if (_doctype != null && _doctype.length() > 0) {
186: out.print("<!DOCTYPE ");
187: out.print(_doctype);
188: out.print('>');
189:
190: printNewline(out);
191: }
192:
193: out.print("<html>");
194: printNewline(out);
195: out.print("<head>");
196: printNewline(out);
197:
198: if (pageTitle != null) {
199: out.print("<title>" + pageTitle + "</title>");
200: printNewline(out);
201: }
202:
203: if (stylesheet != null && stylesheet.length() > 0) {
204: String cssUrl = response.encodeURL(stylesheet);
205: out.print("<link rel='StyleSheet' href='");
206: out.print(cssUrl);
207: out.print("' type='text/css' media='all'/>");
208: printNewline(out);
209: }
210:
211: out.print("</head>");
212: printNewline(out);
213: out.print("<body>");
214: printNewline(out);
215: }
216:
217: protected void beginWindow(PrintWriter out, RenderRequest request,
218: String namespace) throws IOException {
219: ResourceBundle resourceBundle = getResourceBundle(request);
220:
221: out.print("<div class='portlet-frame ");
222: printEscaped(out, request.getPortletMode().toString());
223: out.print(' ');
224: printEscaped(out, request.getWindowState().toString());
225: out.print("' id='");
226: out.print(namespace);
227: out.print("'>");
228:
229: if (_titleLocation == Location.FRAME)
230: htmlTitle(out, request, namespace, resourceBundle);
231:
232: if (_portletModeMenu.getLocation() == Location.FRAME)
233: htmlPortletModeMenu(out, request, namespace, resourceBundle);
234:
235: if (_windowStateMenu.getLocation() == Location.FRAME)
236: htmlWindowStateMenu(out, request, namespace, resourceBundle);
237:
238: printNewline(out);
239:
240: out.print("<div class='portlet-header'>");
241:
242: printNewline(out);
243:
244: if (_titleLocation == Location.HEADER)
245: htmlTitle(out, request, namespace, resourceBundle);
246:
247: if (_portletModeMenu.getLocation() == Location.HEADER)
248: htmlPortletModeMenu(out, request, namespace, resourceBundle);
249:
250: if (_windowStateMenu.getLocation() == Location.HEADER)
251: htmlWindowStateMenu(out, request, namespace, resourceBundle);
252:
253: out.print("</div>"); // class='portlet-topbar'>
254: printNewline(out);
255:
256: out.print("<div class='portlet-content'>");
257: printNewline(out);
258: }
259:
260: protected void endWindow(PrintWriter out, RenderRequest request,
261: String namespace) throws IOException {
262: ResourceBundle resourceBundle = getResourceBundle(request);
263:
264: out.print("</div>");
265: printNewline(out);
266: out.print("<div class='portlet-footer'>");
267: printNewline(out);
268:
269: if (_titleLocation == Location.FOOTER)
270: htmlTitle(out, request, namespace, resourceBundle);
271:
272: if (_portletModeMenu.getLocation() == Location.FOOTER)
273: htmlPortletModeMenu(out, request, namespace, resourceBundle);
274:
275: if (_windowStateMenu.getLocation() == Location.FOOTER)
276: htmlWindowStateMenu(out, request, namespace, resourceBundle);
277:
278: out.print("</div>"); // footer
279: printNewline(out);
280: out.print("</div>"); // frame
281: out.print("<!-- ");
282: out.print(namespace);
283: out.print(" -->");
284: printNewline(out);
285: }
286:
287: protected void endPage(PrintWriter out, RenderRequest request,
288: String namespace) throws IOException {
289: out.print("</body>");
290: printNewline(out);
291: out.print("</html>");
292: printNewline(out);
293: }
294:
295: protected void printNewline(PrintWriter out) {
296: if (!_compact)
297: out.println();
298: }
299:
300: protected void htmlTitle(PrintWriter out, RenderRequest request,
301: String namespace, ResourceBundle resourceBundle)
302: throws IOException {
303: String title = getTitle(request);
304:
305: if (title != null) {
306: out.print("<h1>");
307: out.print(title);
308: out.print("</h1>");
309: }
310: }
311:
312: protected void htmlPortletModeMenu(PrintWriter out,
313: RenderRequest request, String namespace,
314: ResourceBundle resourceBundle) throws IOException {
315: PortletMode currentPortletMode = request.getPortletMode();
316: Set<PortletMode> portletModes = getPortletModes(request);
317:
318: Menu.MenuRenderer menuRenderer = _portletModeMenu
319: .createRenderer();
320:
321: for (PortletMode portletMode : portletModes) {
322: String title = portletMode.toString();
323: String shortDescription = null;
324: String urlString = null;
325: boolean isSelected = true;
326:
327: if (!portletMode.equals(currentPortletMode)) {
328: isSelected = false;
329: PortletURL url = createControlURL(request);
330:
331: try {
332: url.setPortletMode(portletMode);
333: } catch (PortletModeException ex) {
334: throw new RuntimeException(ex);
335: }
336:
337: urlString = url.toString();
338: }
339:
340: if (resourceBundle != null) {
341: StringBuffer key = new StringBuffer();
342:
343: key.append("portletMode.");
344: key.append(portletMode.toString());
345: key.append(".title");
346:
347: try {
348: title = resourceBundle.getString(key.toString());
349: } catch (MissingResourceException ex) {
350: }
351:
352: key.setLength(0);
353:
354: key.append("portletMode.");
355: key.append(portletMode.toString());
356: key.append(".shortDescription");
357:
358: try {
359: shortDescription = resourceBundle.getString(key
360: .toString());
361: } catch (MissingResourceException ex) {
362: }
363: }
364:
365: menuRenderer.add(title, shortDescription, urlString,
366: isSelected);
367: }
368:
369: menuRenderer.print(out);
370: }
371:
372: protected void htmlWindowStateMenu(PrintWriter out,
373: RenderRequest request, String namespace,
374: ResourceBundle resourceBundle) throws IOException {
375: WindowState currentWindowState = request.getWindowState();
376: Set<WindowState> windowStates = getWindowStates(request);
377:
378: Menu.MenuRenderer menuRenderer = _windowStateMenu
379: .createRenderer();
380:
381: for (WindowState windowState : windowStates) {
382: String title = windowState.toString();
383: String shortDescription = null;
384: String urlString = null;
385: boolean isSelected = true;
386:
387: if (!windowState.equals(currentWindowState)) {
388: isSelected = false;
389: PortletURL url = createControlURL(request);
390:
391: try {
392: url.setWindowState(windowState);
393: } catch (WindowStateException ex) {
394: throw new RuntimeException(ex);
395: }
396:
397: urlString = url.toString();
398: }
399:
400: if (resourceBundle != null) {
401: StringBuffer key = new StringBuffer();
402:
403: key.append("windowState.");
404: key.append(windowState.toString());
405: key.append(".title");
406:
407: try {
408: title = resourceBundle.getString(key.toString());
409: } catch (MissingResourceException ex) {
410: }
411:
412: key.setLength(0);
413:
414: key.append("windowState.");
415: key.append(windowState.toString());
416: key.append(".shortDescription");
417:
418: try {
419: shortDescription = resourceBundle.getString(key
420: .toString());
421: } catch (MissingResourceException ex) {
422: }
423: }
424:
425: menuRenderer.add(title, shortDescription, urlString,
426: isSelected);
427: }
428:
429: menuRenderer.print(out);
430: }
431: }
|