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 org.netbeans.modules.visualweb.web.ui.dt.component.util;
042:
043: import com.sun.rave.web.ui.util.MessageUtil;
044: import java.util.Locale;
045: import javax.faces.context.FacesContext;
046:
047: /**
048: * Provides access to design-time resources with a Bundle-DT baseName.
049: *
050: * @author Edwin Goei
051: */
052: public class DesignMessageUtil {
053:
054: /**
055: * Get a message from a design-time resource bundle.
056: *
057: * @param clazz
058: * class determines package where resources are located
059: * @param key
060: * The key for the desired string.
061: * @return localized String
062: */
063: public static String getMessage(Class clazz, String key) {
064: return getMessage(clazz, key, null);
065: }
066:
067: /**
068: * Get a formatted message from a design-time resource bundle.
069: *
070: * @param clazz
071: * class determines package where resources are located
072: * @param key
073: * The key for the desired string.
074: * @param args
075: * The arguments to be inserted into the string.
076: * @return localized String
077: */
078: public static String getMessage(Class clazz, String key,
079: Object args[]) {
080: String baseName = clazz.getPackage().getName() + ".Bundle-DT";
081:
082: // XXX webui-designtime is module jar now, and current (wrong) impl
083: // of project classloader doesn't know about it.
084: // TODO This arch (missing arch) needs to be revised together with
085: // new impl of project classloaders.
086: // return MessageUtil.getMessage(baseName, key, args);
087: return MessageUtil.getMessage(getLocale(), baseName, key, args,
088: DesignMessageUtil.class.getClassLoader());
089: }
090:
091: // XXX Copy from webui/runtime/library MessageUtils.
092: // TODO Avoid copy/paste antipattern.
093: private static Locale getLocale() {
094: //FacesContext context = FacesContext.getCurrentInstance();
095: //if (context == null) {
096: //return Locale.getDefault();
097: //}
098:
099: //Locale locale = null;
100:
101: // context.getViewRoot() may not have been initialized at this point.
102: //if (context.getViewRoot() != null)
103: //locale = context.getViewRoot().getLocale();
104:
105: //return (locale != null) ? locale : Locale.getDefault();
106:
107: // Do not depend on FacesContext to get the locale. Here is the problem
108: // When Design time container initializes it creates the RI ConfigListener,
109: // which in turn instantiates all the Renderers in the Render Kit
110: // Some design time renderer calls this class to loaded messages
111: // This causes problem because Faces Context is not yet initialized and throws
112: // Exception. Since this is a design time only renderer, I'm trying to fix the problem
113: // using just Locale.getDefault() - Winston
114: return Locale.getDefault();
115: }
116: }
|