001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2017-2018 Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Affero General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 * 
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Affero General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.webconsole.base.events;
020
021import java.util.Locale;
022import org.jgrapes.core.Event;
023import org.jgrapes.webconsole.base.RenderSupport;
024
025/**
026 * Signals that the locale for the web console has changed. Should be handled
027 * by web console components that support localization by updating 
028 * the representation.
029 * 
030 * ![Event Sequence](SetLocale.svg)
031 * 
032 * @startuml SetLocale.svg
033 * hide footbox
034 * 
035 * Browser -> WebConsole: "setLocale"
036 * activate WebConsole
037 * activate ConsoleWeblet
038 * WebConsole -> ConsoleWeblet: SetLocale
039 * deactivate ConsoleWeblet
040 * loop for conlets with l10n support
041 *     WebConsole -> Conlet: SetLocale
042 * end loop
043 * deactivate WebConsole
044 * actor Framework
045 * Framework -> ConsoleWeblet: SetLocaleDone
046 * activate ConsoleWeblet
047 * opt reload requested
048 *     ConsoleWeblet -> Browser: "reload"
049 * end
050 * deactivate ConsoleWeblet
051 * 
052 * @enduml
053 */
054public class SetLocale extends Event<Void> {
055
056    private final RenderSupport renderSupport;
057    private final Locale locale;
058    private boolean reload;
059
060    /**
061     * Creates a new event.
062     *
063     * @param renderSupport the render support
064     * @param locale the locale to set
065     * @param reload the reload
066     */
067    public SetLocale(RenderSupport renderSupport, Locale locale,
068            boolean reload) {
069        this.renderSupport = renderSupport;
070        this.locale = locale;
071        this.reload = reload;
072        new SetLocaleCompleted(this);
073    }
074
075    /**
076     * Returns the render support.
077     * 
078     * @return the render support
079     */
080    public RenderSupport renderSupport() {
081        return renderSupport;
082    }
083
084    /**
085     * Returns the locale to set.
086     * 
087     * @return the locale
088     */
089    public Locale locale() {
090        return locale;
091    }
092
093    /**
094     * Returns `true` if the web console needs to be reloaded after
095     * changing the locale.
096     *
097     * @return true, if reload is required
098     */
099    public boolean reload() {
100        return reload;
101    }
102
103    /**
104     * Sets the reload flag. Used by web console components that cannot 
105     * dynamically update their content to the new locale.
106     * 
107     * For optimized behavior, web console components should check 
108     * {@link #reload()} before generating events that update the content 
109     * dynamically. Web console component that invoke this method 
110     * should define a handler with a higher priority. 
111     */
112    public void forceReload() {
113        this.reload = true;
114    }
115
116}