001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package com.sun.rave.web.ui.util;
042:
043: import java.util.Locale;
044: import java.util.Map;
045:
046: import javax.faces.context.ExternalContext;
047: import javax.faces.context.FacesContext;
048: import com.sun.rave.web.ui.theme.Theme;
049: import com.sun.rave.web.ui.theme.ThemeFactory;
050: import com.sun.rave.web.ui.theme.ThemeManager;
051:
052: /**
053: * Utilities needed by Sun Web Components to
054: * retrieve an appropriate Theme.
055: * @author avk
056: */
057: public class ThemeUtilities {
058:
059: private final static boolean DEBUG = false;
060:
061: /**
062: * Use this method to retrieve a named Theme.
063: * @param context The FacesContext for the request.
064: * @return A Locale-specific Theme
065: */
066: public static Theme getTheme(FacesContext context) {
067:
068: if (DEBUG)
069: log("getTheme()");
070:
071: // First, get the ThemeManager
072: ThemeManager manager = null;
073:
074: ExternalContext appContext = context.getExternalContext();
075:
076: Object themeManagerObject = appContext.getApplicationMap().get(
077: ThemeManager.THEME_MANAGER);
078: if (themeManagerObject != null) {
079: if (DEBUG)
080: log("Found ThemeManager"); //NOI18N
081: if (themeManagerObject instanceof ThemeManager) {
082: manager = (ThemeManager) themeManagerObject;
083: } else {
084: StringBuffer msgBuffer = new StringBuffer(
085: "ERROR WHILE RETRIEVING THEME: ");
086: msgBuffer.append(" Session attribute of name ");
087: msgBuffer.append(Theme.THEME_ATTR);
088: msgBuffer.append(" is not of type Theme");
089: throw new RuntimeException(msgBuffer.toString());
090: }
091: } else {
092: if (DEBUG)
093: log("Initializing ThemeManager"); //NOI18N
094: manager = ThemeFactory.initializeThemeManager(appContext);
095: }
096:
097: Map sessionAttributes = appContext.getSessionMap();
098:
099: String themeName = null;
100: Object themeObject = sessionAttributes.get(Theme.THEME_ATTR);
101: if (themeObject != null) {
102: if (DEBUG)
103: log("Found themeName in session"); //NOI18N
104: themeName = themeObject.toString();
105: // Need to check this for Creator
106: if (themeName.length() == 0) {
107: themeName = manager.getDefaultThemeName();
108: sessionAttributes.put(Theme.THEME_ATTR, themeName);
109: }
110: } else {
111: if (DEBUG)
112: log("Getting default themename"); //NOI18N
113: themeName = manager.getDefaultThemeName();
114: sessionAttributes.put(Theme.THEME_ATTR, themeName);
115: }
116: if (DEBUG)
117: log("\tTheme name is " + themeName);
118: Locale locale = context.getViewRoot().getLocale();
119:
120: if (locale == null) {
121: locale = Locale.getDefault();
122: log("\tWARNING: couldn't get locale from the view root.");
123: log("\tUsing system locale " + locale.toString());
124: } else if (DEBUG) {
125: log("\tUsing locale from viewroot " + locale.toString());
126: }
127: return manager.getTheme(themeName, locale);
128:
129: }
130:
131: private static void log(String s) {
132: System.out.println(ThemeUtilities.class.getName() + "::" + s);
133: }
134: }
|