1 |
/******************************************************************************* |
2 |
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
* |
4 |
* This file is part of the SCHMITZM library - a collection of utility |
5 |
* classes based on Java 1.6, focusing (not only) on Java Swing |
6 |
* and the Geotools library. |
7 |
* |
8 |
* The SCHMITZM project is hosted at: |
9 |
* http://wald.intevation.org/projects/schmitzm/ |
10 |
* |
11 |
* This program is free software; you can redistribute it and/or |
12 |
* modify it under the terms of the GNU Lesser General Public License |
13 |
* as published by the Free Software Foundation; either version 3 |
14 |
* of the License, or (at your option) any later version. |
15 |
* |
16 |
* This program is distributed in the hope that it will be useful, |
17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 |
* GNU General Public License for more details. |
20 |
* |
21 |
* You should have received a copy of the GNU Lesser General Public License (license.txt) |
22 |
* along with this program; if not, write to the Free Software |
23 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
24 |
* or try this link: http://www.gnu.org/licenses/lgpl.html |
25 |
* |
26 |
* Contributors: |
27 |
* Martin O. J. Schmitz - initial API and implementation |
28 |
* Stefan A. Tzeggai - additional utility classes |
29 |
******************************************************************************/ |
30 |
package skrueger.i8n; |
31 |
|
32 |
import java.awt.Component; |
33 |
import java.awt.event.ActionEvent; |
34 |
import java.awt.event.ActionListener; |
35 |
import java.util.ArrayList; |
36 |
import java.util.List; |
37 |
|
38 |
import javax.swing.ImageIcon; |
39 |
import javax.swing.JComboBox; |
40 |
import javax.swing.JLabel; |
41 |
import javax.swing.JPanel; |
42 |
|
43 |
import net.miginfocom.swing.MigLayout; |
44 |
|
45 |
import org.apache.log4j.Logger; |
46 |
|
47 |
import schmitzm.swing.SwingUtil; |
48 |
import skrueger.swing.AtlasDialog; |
49 |
import skrueger.swing.OkButton; |
50 |
import skrueger.swing.TranslationEditJPanel; |
51 |
|
52 |
/** |
53 |
* This dialog ask the user to select one of list of given languages. The dialog |
54 |
* is modal and not visible after construction. |
55 |
* |
56 |
* @author Stefan A. Tzeggai |
57 |
*/ |
58 |
public class SwitchLanguageDialog extends AtlasDialog { |
59 |
protected Logger LOGGER = Logger.getLogger(SwitchLanguageDialog.class); |
60 |
|
61 |
private JPanel jContentPane = null; |
62 |
|
63 |
private JLabel jLabelFlagimage = null; |
64 |
|
65 |
private JPanel jPanel = null; |
66 |
|
67 |
private JLabel jLabel = null; |
68 |
|
69 |
private OkButton jButton = null; |
70 |
|
71 |
private LanguagesComboBox jComboBox = null; |
72 |
|
73 |
private final List<String> languages; |
74 |
|
75 |
/** |
76 |
* if <code>true</code>, the default locale will also be changed during a |
77 |
* language selection |
78 |
**/ |
79 |
protected boolean setLocale; |
80 |
|
81 |
/** |
82 |
* A dialog to select one of the available languages. If only one language |
83 |
* is available, select it directly. Creating this object automatically |
84 |
* makes it visible, unless there is only one language to choose from.. it |
85 |
* that case it disposes itself automatically. |
86 |
* |
87 |
* @param setLocale if <code>true</code>, the default locale will also be |
88 |
* changed during a language selection |
89 |
*/ |
90 |
public SwitchLanguageDialog(final Component owner, |
91 |
final List<String> languages, boolean setLocale) { |
92 |
super(owner); |
93 |
this.languages = languages; |
94 |
this.setLocale = setLocale; |
95 |
|
96 |
if (languages == null || languages.size() == 0) { |
97 |
// No language is available. |
98 |
return; |
99 |
} |
100 |
|
101 |
Translation.setActiveLang(languages.get(0), setLocale); |
102 |
if (languages.size() == 1) { |
103 |
// Only language one language is available. It has been selected |
104 |
// automatically. |
105 |
return; |
106 |
} |
107 |
|
108 |
initialize(); |
109 |
} |
110 |
|
111 |
@Override |
112 |
/** |
113 |
* This modal dialog will not appear if there is <= one language to select from. |
114 |
*/ |
115 |
public void setVisible(boolean b) { |
116 |
if (b == true && (languages == null || languages.size() <= 1)) { |
117 |
return; |
118 |
} |
119 |
super.setVisible(b); |
120 |
} |
121 |
|
122 |
/** |
123 |
* This method initializes this |
124 |
* |
125 |
* @return void |
126 |
*/ |
127 |
private void initialize() { |
128 |
this.setContentPane(getJContentPane()); |
129 |
|
130 |
pack(); |
131 |
|
132 |
SwingUtil.centerFrameOnScreenRandom(this); |
133 |
setModal(true); |
134 |
} |
135 |
|
136 |
public boolean close() { |
137 |
// Only close by ESC and window-close if a valid selection is made |
138 |
if (jComboBox.getSelectedIndex() == languages.size() |
139 |
|| jComboBox.getSelectedIndex() == -1) |
140 |
return false; |
141 |
else |
142 |
return super.close(); |
143 |
} |
144 |
|
145 |
/** |
146 |
* This method initializes jContentPane |
147 |
* |
148 |
* @return javax.swing.JPanel |
149 |
*/ |
150 |
private JPanel getJContentPane() { |
151 |
if (jContentPane == null) { |
152 |
jLabelFlagimage = new JLabel(new ImageIcon( |
153 |
TranslationEditJPanel.class |
154 |
.getResource("resource/flags.jpg"))); |
155 |
jContentPane = new JPanel(); |
156 |
MigLayout migLayout = new MigLayout("wrap 1", "[center]", "[]0[]"); |
157 |
jContentPane.setLayout(migLayout); |
158 |
jContentPane.add(jLabelFlagimage, "north"); |
159 |
jContentPane.add(getLanguageCombobox()); |
160 |
jContentPane.add(getOkButton(), |
161 |
"right, w 50!, gaptop 5, gapright 5, gapbottom 5"); |
162 |
} |
163 |
return jContentPane; |
164 |
} |
165 |
|
166 |
/** |
167 |
* This method initializes the OkButton |
168 |
* |
169 |
* @return javax.swing.JButton |
170 |
*/ |
171 |
@Override |
172 |
protected OkButton getOkButton() { |
173 |
if (jButton == null) { |
174 |
jButton = new OkButton(); |
175 |
jButton.setEnabled(false); |
176 |
|
177 |
jButton.addActionListener(new ActionListener() { |
178 |
|
179 |
public void actionPerformed(ActionEvent e) { |
180 |
dispose(); |
181 |
} |
182 |
|
183 |
}); |
184 |
} |
185 |
return jButton; |
186 |
} |
187 |
|
188 |
/** |
189 |
* This method initializes the JPanel that carries the select language |
190 |
* combobox |
191 |
* |
192 |
* @return javax.swing.JPanel |
193 |
*/ |
194 |
private JPanel getLanguageCombobox() { |
195 |
if (jPanel == null) { |
196 |
jLabel = new JLabel(); |
197 |
jLabel.setText("Select language: "); // i8n!?! Maybe replace with an |
198 |
// icon of an index finger |
199 |
jPanel = new JPanel(); |
200 |
jPanel.add(jLabel); |
201 |
jPanel.add(getJComboBox()); |
202 |
} |
203 |
return jPanel; |
204 |
} |
205 |
|
206 |
/** |
207 |
* This method initializes the Select Language Combobox |
208 |
* |
209 |
* @return javax.swing.JComboBox |
210 |
*/ |
211 |
private JComboBox getJComboBox() { |
212 |
if (jComboBox == null) { |
213 |
ArrayList<String> languagesPlusOne = new ArrayList<String>( |
214 |
languages); |
215 |
languagesPlusOne.add("?"); |
216 |
|
217 |
jComboBox = new LanguagesComboBox(languages); |
218 |
|
219 |
SwingUtil.addMouseWheelForCombobox(jComboBox); |
220 |
jComboBox.addActionListener(new ActionListener() { |
221 |
|
222 |
public void actionPerformed(final ActionEvent e) { |
223 |
if (jComboBox.getSelectedIndex() == languages.size()) { |
224 |
getOkButton().setEnabled(false); |
225 |
return; |
226 |
} |
227 |
|
228 |
String l = languages.get(jComboBox.getSelectedIndex()); |
229 |
try { |
230 |
Translation.setActiveLang(l, setLocale); |
231 |
|
232 |
getOkButton().setEnabled(true); |
233 |
} catch (java.lang.IllegalArgumentException ee) { |
234 |
LOGGER.warn("The language " + l + " is not valid", ee); |
235 |
getOkButton().setEnabled(false); |
236 |
} |
237 |
|
238 |
} |
239 |
|
240 |
}); |
241 |
} |
242 |
return jComboBox; |
243 |
} |
244 |
|
245 |
} |