KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > jsfext > component > factory > basic > LocalizedStringFactory


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.tools.jsfext.component.factory.basic;
24
25 import com.sun.enterprise.tools.jsfext.component.factory.ComponentFactoryBase;
26 import com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutComponent;
27
28 import com.sun.web.ui.util.MessageUtil;
29
30 import javax.faces.component.UIComponent;
31 import javax.faces.component.html.HtmlOutputText;
32 import javax.faces.context.FacesContext;
33
34
35 /**
36  * <p> This factory is responsible for creating a <code>UIComponent</code>
37  * that contains a localized message.</p>
38  *
39  * @author Ken Paulsen (ken.paulsen@sun.com)
40  */

41 public class LocalizedStringFactory extends ComponentFactoryBase {
42
43     /**
44      * <p> This is the factory method responsible for creating the
45      * <code>UIComponent</code>.</p>
46      *
47      * @param context The <code>FacesContext</code>
48      * @param descriptor The {@link LayoutComponent} descriptor associated
49      * with the requested <code>UIComponent</code>.
50      * @param parent The parent <code>UIComponent</code>
51      *
52      * @return The newly created <code>HtmlOutputText</code>.
53      */

54     public UIComponent create(FacesContext context, LayoutComponent descriptor, UIComponent parent) {
55     // First make sure we have all our input.
56
String JavaDoc baseName =
57         (String JavaDoc) descriptor.getEvaluatedOption(context, BASE_NAME, parent);
58     if (baseName == null) {
59         throw new IllegalArgumentException JavaDoc("'" + BASE_NAME
60             + "' is missing for LayoutComponent: '"
61             + descriptor.getUnevaluatedId() + "'!");
62     }
63     String JavaDoc key =
64         (String JavaDoc) descriptor.getEvaluatedOption(context, KEY, parent);
65     if (key == null) {
66         throw new IllegalArgumentException JavaDoc("'" + KEY
67             + "' is missing for LayoutComponent: '"
68             + descriptor.getUnevaluatedId() + "'!");
69     }
70
71     // parameters are optional
72
Object JavaDoc parameters =
73         descriptor.getEvaluatedOption(context, PARAMETERS, parent);
74     Object JavaDoc[] args = null;
75     if (parameters != null) {
76         // Convert from list to Object[]
77
if (parameters.getClass().isArray()) {
78         args = (Object JavaDoc[]) parameters;
79         } else {
80         // Not a list, just treat as a single value
81
args = new Object JavaDoc[] {parameters.toString()};
82         }
83     }
84
85     // Get the Localized Message
86
String JavaDoc value = MessageUtil.getMessage(context, baseName, key, args);
87
88     // Create the UIComponent
89
HtmlOutputText output = new HtmlOutputText();
90
91     // This needs to be done here (before setOptions) so that $...{...}
92
// expressions can be resolved... may want to defer these?
93
if (parent != null) {
94         addChild(context, descriptor, parent, output);
95     }
96
97     // Set the value
98
output.setValue(value);
99
100     // Set all the attributes / properties
101
setOptions(context, descriptor, output);
102
103     // Return the new UIComponent
104
return output;
105     }
106
107
108     /**
109      * The key for the option representing the base name of the
110      * ResourceBundle.
111      */

112     public static final String JavaDoc BASE_NAME = "baseName";
113
114     /**
115      * The key for the option representing the ResourceBundle key.
116      */

117     public static final String JavaDoc KEY = "key";
118
119     /**
120      * The key for the option representing the ResourceBundle entries
121      * parameters.
122      */

123     public static final String JavaDoc PARAMETERS = "parameters";
124 }
125
Popular Tags