Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Select |
|
| 2.75;2.75 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to you under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | ||
18 | package org.apache.shale.usecases.locale; | |
19 | ||
20 | import java.util.Iterator; | |
21 | import java.util.Locale; | |
22 | ||
23 | import javax.faces.application.FacesMessage; | |
24 | import javax.faces.context.FacesContext; | |
25 | ||
26 | import org.apache.commons.logging.Log; | |
27 | import org.apache.commons.logging.LogFactory; | |
28 | import org.apache.shale.util.Messages; | |
29 | import org.apache.shale.view.AbstractViewController; | |
30 | ||
31 | /** | |
32 | * <p>ViewController to select the <code>Locale</code> to be used for | |
33 | * localizing responses.</p> | |
34 | * | |
35 | * $Id: Select.java 464373 2006-10-16 04:21:54Z rahul $ | |
36 | */ | |
37 | 5 | public class Select extends AbstractViewController { |
38 | ||
39 | ||
40 | // ------------------------------------------------------ Manifest Constants | |
41 | ||
42 | ||
43 | /** | |
44 | * <p>Logical outcome indicating that a locale was NOT successfully | |
45 | * selected.</p> | |
46 | */ | |
47 | static final String FAILURE = "locale$failure"; | |
48 | ||
49 | ||
50 | /** | |
51 | * <p>Logical outcome indicating that a locale was successfully selected.</p> | |
52 | */ | |
53 | static final String SUCCESS = "locale$success"; | |
54 | ||
55 | ||
56 | // -------------------------------------------------------- Static Variables | |
57 | ||
58 | ||
59 | /** | |
60 | * <p>The <code>Log</code> instance for this class.</p> | |
61 | */ | |
62 | 2 | private static final Log log = LogFactory.getLog(Select.class); |
63 | ||
64 | ||
65 | /** | |
66 | * <p>Localized messages for this application.</p> | |
67 | */ | |
68 | 1 | private static Messages messages = |
69 | new Messages("org.apache.shale.usecases.view.Bundle"); | |
70 | ||
71 | ||
72 | // -------------------------------------------------------------- Properties | |
73 | ||
74 | ||
75 | /** | |
76 | * <p>The Stringified definition of the <code>Locale</code> to be used | |
77 | * for localizing responses.</p> | |
78 | */ | |
79 | 5 | private String locale = null; |
80 | 5 | public String getLocale() { return this.locale; } |
81 | 4 | public void setLocale(String locale) { this.locale = locale; } |
82 | ||
83 | ||
84 | // -------------------------------------------------- Event Handling Methods | |
85 | ||
86 | ||
87 | /** | |
88 | * <p>Set the new <code>Locale</code>, based on the value selected | |
89 | * by the user.</p> | |
90 | */ | |
91 | public String select() { | |
92 | ||
93 | 3 | FacesContext context = FacesContext.getCurrentInstance(); |
94 | ||
95 | // Identify the locale String specified by the user | |
96 | 3 | String localeString = getLocale(); |
97 | 3 | if ((localeString == null) || (localeString.length() < 1)) { |
98 | 1 | log.error(messages.getMessage("select.missing")); |
99 | 1 | context.addMessage(null, |
100 | new FacesMessage(messages.getMessage("select.missing"))); | |
101 | 1 | return null; |
102 | } | |
103 | ||
104 | // Match it to a Locale supported by this application | |
105 | 2 | Iterator locales = context.getApplication().getSupportedLocales(); |
106 | 8 | while (locales.hasNext()) { |
107 | 7 | Locale locale = (Locale) locales.next(); |
108 | 7 | if (localeString.equals(locale.toString())) { |
109 | 1 | if (log.isDebugEnabled()) { |
110 | log.debug(messages.getMessage("select.selected", | |
111 | new Object[] { localeString })); | |
112 | } | |
113 | 1 | context.getViewRoot().setLocale(locale); |
114 | 1 | return SUCCESS; |
115 | } | |
116 | 6 | } |
117 | ||
118 | // Error condition - no matching locale in the supported list | |
119 | 1 | String text = messages.getMessage("select.mismatch", |
120 | new Object[] { localeString }); | |
121 | 1 | log.warn(text); |
122 | 1 | context.addMessage(null, new FacesMessage(text)); |
123 | 1 | return FAILURE; |
124 | ||
125 | } | |
126 | ||
127 | ||
128 | // -------------------------------------------------- ViewController Methods | |
129 | ||
130 | ||
131 | /** | |
132 | * <p>Set the value of the <code>locale</code> property based on the | |
133 | * <code>Locale</code> in the current view.</p> | |
134 | */ | |
135 | public void prerender() { | |
136 | ||
137 | 1 | Locale locale = |
138 | FacesContext.getCurrentInstance().getViewRoot().getLocale(); | |
139 | 1 | if (log.isTraceEnabled()) { |
140 | log.trace(messages.getMessage("select.prerender", | |
141 | new Object[] { locale })); | |
142 | } | |
143 | 1 | setLocale(locale.toString()); |
144 | ||
145 | 1 | } |
146 | ||
147 | ||
148 | } |