Update year in copyright headers.
[jSite.git] / src / main / java / de / todesbaum / jsite / i18n / I18nContainer.java
1 /*
2  * jSite - I18nContainer.java - Copyright © 2007–2014 David Roden
3  *
4  * This program is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 package de.todesbaum.jsite.i18n;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.Iterator;
24 import java.util.List;
25
26 /**
27  * Container that collects {@link Runnable}s that change the texts of GUI
28  * components when the current locale has changed.
29  *
30  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
31  */
32 public class I18nContainer implements Iterable<Runnable> {
33
34         /** The container singleton. */
35         private static final I18nContainer singleton = new I18nContainer();
36
37         /** The list of runnables that change texts. */
38         private final List<Runnable> i18nRunnables = Collections.synchronizedList(new ArrayList<Runnable>());
39
40         /**
41          * The list of runnables that change texts and run after
42          * {@link #i18nRunnables}.
43          */
44         private final List<Runnable> i18nPostRunnables = Collections.synchronizedList(new ArrayList<Runnable>());
45
46         /**
47          * Returns the singleton instance.
48          *
49          * @return The singleton instance
50          */
51         public static I18nContainer getInstance() {
52                 return singleton;
53         }
54
55         /**
56          * Registers an i18n runnable that is run when the current locale has
57          * changed.
58          *
59          * @param i18nRunnable
60          *            The runnable to register
61          */
62         public void registerRunnable(Runnable i18nRunnable) {
63                 i18nRunnables.add(i18nRunnable);
64         }
65
66         /**
67          * Registers a {@link Runnable} that changes texts when the current locale
68          * has changed and runs after {@link #i18nRunnables} have run.
69          *
70          * @param i18nPostRunnable
71          *            The runnable to register
72          */
73         public void registerPostRunnable(Runnable i18nPostRunnable) {
74                 i18nPostRunnables.add(i18nPostRunnable);
75         }
76
77         /**
78          * {@inheritDoc}
79          * <p>
80          * Returns a combined list of {@link #i18nRunnables} and
81          * {@link #i18nPostRunnables}, in that order.
82          */
83         @Override
84         public Iterator<Runnable> iterator() {
85                 List<Runnable> allRunnables = new ArrayList<Runnable>();
86                 allRunnables.addAll(i18nRunnables);
87                 allRunnables.addAll(i18nPostRunnables);
88                 return allRunnables.iterator();
89         }
90
91 }