001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2007
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.faces.components;
034:
035: import com.flexive.faces.FxJsfUtils;
036: import com.flexive.shared.exceptions.FxNotFoundException;
037:
038: import javax.faces.application.ViewHandler;
039: import javax.faces.component.UIOutput;
040: import javax.faces.context.FacesContext;
041: import static javax.faces.context.FacesContext.getCurrentInstance;
042: import javax.faces.context.ResponseWriter;
043: import java.io.IOException;
044: import java.util.ArrayList;
045: import java.util.List;
046:
047: /**
048: * Renders all weblet resources required by the current page.
049: *
050: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
051: * @version $Rev: 1 $
052: */
053: public class WriteWebletIncludes extends UIOutput {
054: private static final String REQ_WEBLETS = WriteWebletIncludes.class
055: .getName()
056: + ".WEBLETS";
057:
058: private List<String> weblets = new ArrayList<String>();
059:
060: public WriteWebletIncludes() {
061: // add flexive shared files
062: weblets
063: .add("com.flexive.faces.weblets/js/flexiveComponents.js");
064: weblets.add("com.flexive.faces.weblets/css/components.css");
065: if (FxJsfUtils.getRequest() != null) {
066: FxJsfUtils.getRequest().setAttribute(REQ_WEBLETS, weblets);
067: }
068: }
069:
070: /**
071: * Add the given weblet resource for the current page.
072: *
073: * @param weblet the weblet to be added, e.g. "com.flexive.faces.web/css/components.css"
074: */
075: @SuppressWarnings({"unchecked"})
076: public static void addWebletInclude(String weblet) {
077: final List<String> requestWeblets = (List<String>) FxJsfUtils
078: .getRequest().getAttribute(REQ_WEBLETS);
079: if (requestWeblets == null) {
080: throw new FxNotFoundException(
081: "ex.jsf.webletIncludes.noTagFound")
082: .asRuntimeException();
083: }
084: requestWeblets.add(weblet);
085: }
086:
087: @Override
088: public void encodeBegin(FacesContext facesContext)
089: throws IOException {
090: super .encodeBegin(facesContext);
091: final ViewHandler viewHandler = FacesContext
092: .getCurrentInstance().getApplication().getViewHandler();
093: final ResponseWriter out = facesContext.getResponseWriter();
094: for (String weblet : weblets) {
095: final String url = viewHandler.getResourceURL(
096: getCurrentInstance(), "weblet://" + weblet);
097: if (weblet.endsWith(".js")) {
098: out
099: .write("\n <script type=\"text/javascript\" src=\"");
100: out.write(url);
101: out.write("\"> </script>");
102: } else if (weblet.endsWith(".css")) {
103: out
104: .write("\n <link rel=\"stylesheet\" type=\"text/css\" href=\"");
105: out.write(url);
106: out.write("\"/>");
107: }
108: }
109:
110: }
111: }
|