View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.example.demo;
21  
22  import org.apache.myfaces.tobago.internal.util.ObjectUtils;
23  import org.apache.myfaces.tobago.internal.util.StringUtils;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  import javax.enterprise.context.SessionScoped;
28  import javax.enterprise.event.Event;
29  import javax.faces.application.Application;
30  import javax.faces.application.FacesMessage;
31  import javax.faces.component.UIViewRoot;
32  import javax.faces.context.FacesContext;
33  import javax.faces.model.SelectItem;
34  import javax.inject.Inject;
35  import javax.inject.Named;
36  import javax.servlet.ServletContext;
37  import java.io.Serializable;
38  import java.lang.invoke.MethodHandles;
39  import java.net.MalformedURLException;
40  import java.net.URL;
41  import java.util.ArrayList;
42  import java.util.Iterator;
43  import java.util.List;
44  import java.util.Locale;
45  
46  @Named
47  @SessionScoped
48  public class LocaleController implements Serializable {
49  
50    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
51  
52    private Locale locale;
53  
54    @Inject
55    private Event<LocaleChanged> events;
56  
57    public LocaleController() {
58  
59      final FacesContext facesContext = FacesContext.getCurrentInstance();
60  
61      // locale
62  
63      final UIViewRoot viewRoot = facesContext.getViewRoot();
64      if (viewRoot != null) {
65        locale = viewRoot.getLocale();
66      } else {
67        locale = facesContext.getApplication().getDefaultLocale();
68      }
69    }
70  
71    public String submit() {
72      if (LOG.isDebugEnabled()) {
73        LOG.debug("submit locale");
74      }
75      final FacesContext facesContext = FacesContext.getCurrentInstance();
76      final UIViewRoot viewRoot = facesContext.getViewRoot();
77      if (viewRoot != null) {
78        return viewRoot.getViewId();
79      } else {
80        facesContext.addMessage(null,
81            new FacesMessage(FacesMessage.SEVERITY_WARN, "ViewRoot not found!", null));
82        return Outcome.CONCEPT_LOCALE.toString();
83      }
84    }
85  
86  // ///////////////////////////////////////////// logic
87  
88    public List<SelectItem> getLocaleItems() {
89      final FacesContext facesContext = FacesContext.getCurrentInstance();
90      final Application application = facesContext.getApplication();
91      final Locale defaultLocale = application.getDefaultLocale();
92      final Iterator supportedLocales = application.getSupportedLocales();
93  
94      boolean defaultInList = false;
95      final List<SelectItem> localeItems = new ArrayList<>();
96      while (supportedLocales.hasNext()) {
97        final Locale supported = (Locale) supportedLocales.next();
98        localeItems.add(createLocaleItem(supported));
99        if (supported.equals(defaultLocale)) {
100         defaultInList = true;
101       }
102     }
103     // If the default is already in the list, don't add it.
104     // Background: Must the default be in the supported list? Yes or No?
105     // This question is not specified explicit and different implemented in the RI and MyFaces
106     if (!defaultInList && defaultLocale != null) {
107       localeItems.add(0, createLocaleItem(defaultLocale));
108     }
109     return localeItems;
110   }
111 
112   private SelectItem createLocaleItem(final Locale localeItem) {
113     if (locale != null) {
114       return new SelectItem(localeItem, localeItem.getDisplayName(locale));
115     } else {
116       return new SelectItem(localeItem, localeItem.getDisplayName(localeItem));
117     }
118   }
119 
120   public Locale getLocale() {
121     return locale;
122   }
123 
124   public String getLocalizedLocale() {
125     if (locale != null) {
126       return locale.getDisplayName(locale);
127     } else {
128       return null;
129     }
130   }
131 
132   public String getImageCountryName() {
133     final String language = locale.getLanguage();
134     final String country = locale.getCountry();
135     final String suffix
136         = StringUtils.isNotBlank(language) && StringUtils.isNotBlank(country) ? "_" + language + "_" + country : "";
137     String url = "/image/country" + suffix + ".png";
138     final ServletContext servletContext =
139         (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
140     URL resource = null;
141     try {
142       resource = servletContext.getResource(url);
143     } catch (final MalformedURLException e) {
144       // ignore
145     }
146     if (resource == null) {
147       url = "/image/country.png";
148     }
149     return url;
150   }
151 
152   public void setLocale(final Locale locale) {
153     if (!ObjectUtils.equals(this.locale, locale)) {
154       events.fire(new LocaleChanged());
155     }
156     this.locale = locale;
157   }
158 
159 }