/*
- * Copyright (C) 2016, 2017 Roland Häder
+ * Copyright (C) 2016 - 2020 Free Software Foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
package org.mxchange.jjobs.beans.localization;
import java.text.MessageFormat;
+import java.text.NumberFormat;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Locale;
import javax.inject.Named;
import org.mxchange.jcoreee.events.locale.LocaleChangeEvent;
import org.mxchange.jcoreee.events.locale.ObservableLocaleChangeEvent;
-import org.mxchange.jjobs.beans.BaseJobsController;
+import org.mxchange.jjobs.beans.BaseJobsBean;
import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent;
import org.mxchange.juserlogincore.events.logout.ObservableUserLogoutEvent;
*/
@Named ("localizationController")
@SessionScoped
-public class JobsLocalizationSessionBean extends BaseJobsController implements JobsLocalizationSessionController {
+public class JobsLocalizationSessionBean extends BaseJobsBean implements JobsLocalizationSessionController {
+
+ /**
+ * Number format
+ */
+ private static NumberFormat NUMBER_FORMAT;
/**
* Serial number
// Call super constructor
super();
- // Init list
+ // Init locale list
this.supportedLocales = new LinkedHashMap<>(2);
}
* @param event Event instance
*/
public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
- // event should not be null
+ // Event and contained entity instance should not be null
if (null == event) {
// Throw NPE
throw new NullPointerException("event is null"); //NOI18N
// Is the locale set?
if (event.getLoggedInUser().getUserLocale() instanceof Locale) {
// Get user local
- Locale userLocale = event.getLoggedInUser().getUserLocale();
+ final Locale userLocale = event.getLoggedInUser().getUserLocale();
// Change locale
- this.changeLocale(userLocale);
+ this.changeLocale(userLocale, Boolean.TRUE);
}
}
* @param event Event instance
*/
public void afterUserLogoutEvent (@Observes final ObservableUserLogoutEvent event) {
- // event should not be null
+ // Event and contained entity instance should not be null
if (null == event) {
// Throw NPE
throw new NullPointerException("event is null"); //NOI18N
Locale newLocale = null;
// Iterate over whole map
- for (Map.Entry<String, Locale> entry : this.getSupportedLocales().entrySet()) {
- Locale foundLocale = entry.getValue();
+ for (final Map.Entry<String, Locale> entry : this.getSupportedLocales().entrySet()) {
+ final Locale foundLocale = entry.getValue();
// Does the language match?
if (Objects.equals(foundLocale.toString(), this.getLocaleCode())) {
}
// Then change it
- this.changeLocale(newLocale);
+ this.changeLocale(newLocale, Boolean.TRUE);
+
+ // Change formatting
+ NUMBER_FORMAT = NumberFormat.getNumberInstance(newLocale);
}
- /**
- * Getter for locale
- * <p>
- * @return Locale
- */
+ @Override
+ public String formatCurrency (final Float amount) {
+ // Format amount
+ return NUMBER_FORMAT.format(amount);
+ }
+
+ @Override
public Locale getLocale () {
return this.locale;
}
@PostConstruct
public void init () {
// Get default locale
- Locale defaultLocale = FacesContext.getCurrentInstance().getApplication().getDefaultLocale();
+ final Locale defaultLocale = FacesContext.getCurrentInstance().getApplication().getDefaultLocale();
// Add it to list
this.getSupportedLocales().put(defaultLocale.toString(), defaultLocale);
// Get iterator from faces context
- Iterator<Locale> iterator = FacesContext.getCurrentInstance().getApplication().getSupportedLocales();
+ final Iterator<Locale> iterator = FacesContext.getCurrentInstance().getApplication().getSupportedLocales();
// Add all locales
while (iterator.hasNext()) {
// Is no country code found?
if (requestLocale.getCountry().isEmpty()) {
// Then try to find one, get language from it
- String language = requestLocale.getLanguage();
+ final String language = requestLocale.getLanguage();
Boolean found = Boolean.FALSE;
// Iterate over whole map
- for (Map.Entry<String, Locale> entry : this.getSupportedLocales().entrySet()) {
- String languageCode = entry.getKey();
- Locale foundLocale = entry.getValue();
+ for (final Map.Entry<String, Locale> entry : this.getSupportedLocales().entrySet()) {
+ final String languageCode = entry.getKey();
+ final Locale foundLocale = entry.getValue();
// Does the language match?
if (languageCode.startsWith(language)) {
}
// Change locale, may set same back in faces context, but triggers event
- this.changeLocale(requestLocale);
+ this.changeLocale(requestLocale, Boolean.FALSE);
+
+ // Initial formatting, may change when user chooses other locale
+ NUMBER_FORMAT = NumberFormat.getNumberInstance(this.locale);
}
/**
* This method makes sure that locales with at least language and country
* information are being changed to.
* <p>
- * @param locale New locale to set
+ * @param locale New locale to set
+ * @param notifyBeans Whether to notify other beans
*/
- private void changeLocale (final Locale locale) {
+ private void changeLocale (final Locale locale, final Boolean notifyBeans) {
// Is the locale language_country at least?
if (null == locale) {
// Throw NPE
// Inform faces context
FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
- // Fire event
- this.localeChangeEvent.fire(new LocaleChangeEvent(locale));
+ // Notify other beans?
+ if (notifyBeans) {
+ // Fire event
+ this.localeChangeEvent.fire(new LocaleChangeEvent(locale));
+ }
}
/**