1 |
package skrueger.i8n; |
/******************************************************************************* |
2 |
|
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
import java.awt.GridBagConstraints; |
* |
4 |
import java.awt.GridBagLayout; |
* This file is part of the SCHMITZM library - a collection of utility |
5 |
import java.awt.Insets; |
* classes based on Java 1.6, focusing (not only) on Java Swing |
6 |
import java.awt.Window; |
* and the Geotools library. |
7 |
import java.awt.event.ActionEvent; |
* |
8 |
import java.awt.event.ActionListener; |
* The SCHMITZM project is hosted at: |
9 |
import java.awt.event.MouseWheelListener; |
* http://wald.intevation.org/projects/schmitzm/ |
10 |
import java.util.List; |
* |
11 |
import java.util.Locale; |
* 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 |
import javax.swing.DefaultComboBoxModel; |
* as published by the Free Software Foundation; either version 3 |
14 |
import javax.swing.ImageIcon; |
* of the License, or (at your option) any later version. |
15 |
import javax.swing.JButton; |
* |
16 |
import javax.swing.JComboBox; |
* This program is distributed in the hope that it will be useful, |
17 |
import javax.swing.JDialog; |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
import javax.swing.JLabel; |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 |
import javax.swing.JPanel; |
* GNU General Public License for more details. |
20 |
|
* |
21 |
import org.apache.log4j.Logger; |
* 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 |
import schmitzm.swing.SwingUtil; |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
24 |
import skrueger.swing.OkButton; |
* or try this link: http://www.gnu.org/licenses/lgpl.html |
25 |
import skrueger.swing.TranslationEditJPanel; |
* |
26 |
|
* Contributors: |
27 |
public class SwitchLanguageDialog extends JDialog { |
* Martin O. J. Schmitz - initial API and implementation |
28 |
protected Logger LOGGER = Logger.getLogger(SwitchLanguageDialog.class); |
* Stefan A. Tzeggai - additional utility classes |
29 |
|
******************************************************************************/ |
30 |
private static final long serialVersionUID = 1L; |
package skrueger.i8n; |
31 |
|
|
32 |
private JPanel jContentPane = null; |
import java.awt.Component; |
33 |
|
import java.awt.GridBagConstraints; |
34 |
private JLabel jLabelFlagimage = null; |
import java.awt.GridBagLayout; |
35 |
|
import java.awt.Insets; |
36 |
private JPanel jPanel = null; |
import java.awt.event.ActionEvent; |
37 |
|
import java.awt.event.ActionListener; |
38 |
private JButton jButton = null; |
import java.awt.event.MouseWheelListener; |
39 |
|
import java.util.List; |
40 |
private JPanel jPanel1 = null; |
import java.util.Locale; |
41 |
|
|
42 |
private JLabel jLabel = null; |
import javax.swing.DefaultComboBoxModel; |
43 |
|
import javax.swing.ImageIcon; |
44 |
private JComboBox jComboBox = null; |
import javax.swing.JButton; |
45 |
|
import javax.swing.JComboBox; |
46 |
private final List<String> languages; |
import javax.swing.JLabel; |
47 |
|
import javax.swing.JPanel; |
48 |
/** |
|
49 |
* A dialog to select one of the available languages. If only one language |
import org.apache.log4j.Logger; |
50 |
* is available, select it directly. Creating this object automatically |
|
51 |
* makes it visible. |
import schmitzm.swing.SwingUtil; |
52 |
* |
import skrueger.swing.AtlasDialog; |
53 |
* @param owner |
import skrueger.swing.OkButton; |
54 |
* @param atlasConfig |
import skrueger.swing.TranslationEditJPanel; |
55 |
*/ |
|
56 |
public SwitchLanguageDialog(final Window owner, final List<String> languages) { |
/** |
57 |
super(owner); |
* This dialog ask the user to select one of list of given languages. The dialog |
58 |
this.languages = languages; |
* is modal and not visible after construction. |
59 |
|
* |
60 |
Translation.setActiveLang(languages.get(0)); |
* @author Stefan A. Tzeggai |
61 |
|
*/ |
62 |
if (languages.size() == 1) { |
public class SwitchLanguageDialog extends AtlasDialog { |
63 |
LOGGER.debug("Only language '" + languages.get(0) |
protected Logger LOGGER = Logger.getLogger(SwitchLanguageDialog.class); |
64 |
+ "' is available. It has been selected automatically."); |
|
65 |
return; |
private JPanel jContentPane = null; |
66 |
} |
|
67 |
|
private JLabel jLabelFlagimage = null; |
68 |
initialize(); |
|
69 |
|
private JPanel jPanel = null; |
70 |
setVisible(true); |
|
71 |
} |
private JButton jButton = null; |
72 |
|
|
73 |
/** |
private JPanel jPanel1 = null; |
74 |
* This method initializes this |
|
75 |
* |
private JLabel jLabel = null; |
76 |
* @return void |
|
77 |
*/ |
private JComboBox jComboBox = null; |
78 |
private void initialize() { |
|
79 |
this.setContentPane(getJContentPane()); |
private final List<String> languages; |
80 |
setModal(true); |
|
81 |
SwingUtil.centerFrameOnScreenRandom(this); |
/** |
82 |
pack(); |
* if <code>true</code>, the default locale will also be changed during a |
83 |
} |
* language selection |
84 |
|
**/ |
85 |
/** |
protected boolean setLocale; |
86 |
* This method initializes jContentPane |
|
87 |
* |
/** |
88 |
* @return javax.swing.JPanel |
* A dialog to select one of the available languages. If only one language |
89 |
*/ |
* is available, select it directly. Creating this object automatically |
90 |
private JPanel getJContentPane() { |
* makes it visible, unless there is only one language to choose from.. it |
91 |
if (jContentPane == null) { |
* that case it disposes itself automatically. |
92 |
final GridBagConstraints gridBagConstraints11 = new GridBagConstraints(); |
* |
93 |
gridBagConstraints11.gridx = 1; |
* @param setLocale |
94 |
gridBagConstraints11.fill = GridBagConstraints.HORIZONTAL; |
* if <code>true</code>, the default locale will also be changed |
95 |
gridBagConstraints11.gridy = 1; |
* during a language selection |
96 |
final GridBagConstraints gridBagConstraints3 = new GridBagConstraints(); |
*/ |
97 |
gridBagConstraints3.gridx = 1; |
public SwitchLanguageDialog(final Component owner, |
98 |
gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL; |
final List<String> languages, boolean setLocale) { |
99 |
gridBagConstraints3.gridy = 2; |
super(owner); |
100 |
final GridBagConstraints gridBagConstraints1 = new GridBagConstraints(); |
this.languages = languages; |
101 |
gridBagConstraints1.gridx = 0; |
this.setLocale = setLocale; |
102 |
gridBagConstraints1.fill = GridBagConstraints.BOTH; |
|
103 |
gridBagConstraints1.gridwidth = 2; |
if (languages == null || languages.size() == 0) { |
104 |
gridBagConstraints1.anchor = GridBagConstraints.NORTH; |
// No language is available. |
105 |
gridBagConstraints1.gridy = 0; |
return; |
106 |
jLabelFlagimage = new JLabel(new ImageIcon( |
} |
107 |
TranslationEditJPanel.class |
|
108 |
.getResource("resource/flags.jpg"))); |
Translation.setActiveLang(languages.get(0), setLocale); |
109 |
jContentPane = new JPanel(); |
if (languages.size() == 1) { |
110 |
jContentPane.setLayout(new GridBagLayout()); |
// Only language one language is available. It has been selected |
111 |
jContentPane.add(jLabelFlagimage, gridBagConstraints1); |
// automatically. |
112 |
jContentPane.add(getJPanel(), gridBagConstraints3); |
return; |
113 |
jContentPane.add(getJPanel1(), gridBagConstraints11); |
} |
114 |
} |
|
115 |
return jContentPane; |
initialize(); |
116 |
} |
} |
117 |
|
|
118 |
/** |
@Override |
119 |
* This method initializes jPanel |
/** |
120 |
* |
* This modal dialog will not appear if there is <= one language to select from. |
121 |
* @return javax.swing.JPanel |
*/ |
122 |
*/ |
public void setVisible(boolean b) { |
123 |
private JPanel getJPanel() { |
if (b == true && (languages == null || languages.size() <= 1)) { |
124 |
if (jPanel == null) { |
return; |
125 |
final GridBagConstraints gridBagConstraints4 = new GridBagConstraints(); |
} |
126 |
gridBagConstraints4.gridx = 0; |
super.setVisible(b); |
127 |
gridBagConstraints4.anchor = GridBagConstraints.EAST; |
} |
128 |
gridBagConstraints4.weightx = 1.0; |
|
129 |
gridBagConstraints4.insets = new Insets(5, 5, 5, 5); |
/** |
130 |
gridBagConstraints4.gridy = 0; |
* This method initializes this |
131 |
jPanel = new JPanel(); |
* |
132 |
jPanel.setLayout(new GridBagLayout()); |
* @return void |
133 |
jPanel.add(getJButton(), gridBagConstraints4); |
*/ |
134 |
} |
private void initialize() { |
135 |
return jPanel; |
this.setContentPane(getJContentPane()); |
136 |
} |
|
137 |
|
pack(); |
138 |
/** |
|
139 |
* This method initializes jButton |
SwingUtil.centerFrameOnScreenRandom(this); |
140 |
* |
setModal(true); |
141 |
* @return javax.swing.JButton |
} |
142 |
*/ |
|
143 |
private JButton getJButton() { |
public boolean close() { |
144 |
if (jButton == null) { |
// Only close by ESC and window-close if a valid selection is made |
145 |
jButton = new OkButton(); |
if (jComboBox.getSelectedIndex() == languages.size()) |
146 |
jButton.setEnabled(false); |
return false; |
147 |
|
else |
148 |
jButton.addActionListener(new ActionListener() { |
return super.close(); |
149 |
|
} |
150 |
public void actionPerformed(ActionEvent e) { |
|
151 |
dispose(); |
/** |
152 |
} |
* This method initializes jContentPane |
153 |
|
* |
154 |
}); |
* @return javax.swing.JPanel |
155 |
} |
*/ |
156 |
return jButton; |
private JPanel getJContentPane() { |
157 |
} |
if (jContentPane == null) { |
158 |
|
final GridBagConstraints gridBagConstraints11 = new GridBagConstraints(); |
159 |
/** |
gridBagConstraints11.gridx = 1; |
160 |
* This method initializes jPanel1 |
gridBagConstraints11.fill = GridBagConstraints.HORIZONTAL; |
161 |
* |
gridBagConstraints11.gridy = 1; |
162 |
* @return javax.swing.JPanel |
final GridBagConstraints gridBagConstraints3 = new GridBagConstraints(); |
163 |
*/ |
gridBagConstraints3.gridx = 1; |
164 |
private JPanel getJPanel1() { |
gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL; |
165 |
if (jPanel1 == null) { |
gridBagConstraints3.gridy = 2; |
166 |
final GridBagConstraints gridBagConstraints2 = new GridBagConstraints(); |
final GridBagConstraints gridBagConstraints1 = new GridBagConstraints(); |
167 |
gridBagConstraints2.fill = GridBagConstraints.VERTICAL; |
gridBagConstraints1.gridx = 0; |
168 |
gridBagConstraints2.gridy = 0; |
gridBagConstraints1.fill = GridBagConstraints.BOTH; |
169 |
gridBagConstraints2.weightx = 1.0; |
gridBagConstraints1.gridwidth = 2; |
170 |
gridBagConstraints2.insets = new Insets(5, 5, 5, 5); |
gridBagConstraints1.anchor = GridBagConstraints.NORTH; |
171 |
gridBagConstraints2.anchor = GridBagConstraints.WEST; |
gridBagConstraints1.gridy = 0; |
172 |
gridBagConstraints2.gridx = 1; |
jLabelFlagimage = new JLabel(new ImageIcon( |
173 |
final GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
TranslationEditJPanel.class |
174 |
gridBagConstraints.gridx = 0; |
.getResource("resource/flags.jpg"))); |
175 |
gridBagConstraints.insets = new Insets(0, 5, 0, 0); |
jContentPane = new JPanel(); |
176 |
gridBagConstraints.gridy = 0; |
jContentPane.setLayout(new GridBagLayout()); |
177 |
jLabel = new JLabel(); |
jContentPane.add(jLabelFlagimage, gridBagConstraints1); |
178 |
jLabel.setText("Select language: "); |
jContentPane.add(getJPanel(), gridBagConstraints3); |
179 |
jPanel1 = new JPanel(); |
jContentPane.add(getJPanel1(), gridBagConstraints11); |
180 |
jPanel1.setLayout(new GridBagLayout()); |
} |
181 |
jPanel1.add(jLabel, gridBagConstraints); |
return jContentPane; |
182 |
jPanel1.add(getJComboBox(), gridBagConstraints2); |
} |
183 |
} |
|
184 |
return jPanel1; |
/** |
185 |
} |
* This method initializes jPanel |
186 |
|
* |
187 |
/** |
* @return javax.swing.JPanel |
188 |
* This method initializes jComboBox |
*/ |
189 |
* |
private JPanel getJPanel() { |
190 |
* @return javax.swing.JComboBox |
if (jPanel == null) { |
191 |
*/ |
final GridBagConstraints gridBagConstraints4 = new GridBagConstraints(); |
192 |
private JComboBox getJComboBox() { |
gridBagConstraints4.gridx = 0; |
193 |
if (jComboBox == null) { |
gridBagConstraints4.anchor = GridBagConstraints.EAST; |
194 |
jComboBox = new JComboBox(); |
gridBagConstraints4.weightx = 1.0; |
195 |
|
gridBagConstraints4.insets = new Insets(5, 5, 5, 5); |
196 |
jComboBox.addMouseWheelListener(new MouseWheelListener() { |
gridBagConstraints4.gridy = 0; |
197 |
public void mouseWheelMoved(java.awt.event.MouseWheelEvent e) { |
jPanel = new JPanel(); |
198 |
|
jPanel.setLayout(new GridBagLayout()); |
199 |
if ((e.getWheelRotation() < 0)) { |
jPanel.add(getJButton(), gridBagConstraints4); |
200 |
if (jComboBox.getSelectedIndex() < jComboBox |
} |
201 |
.getItemCount() - 1) |
return jPanel; |
202 |
jComboBox.setSelectedIndex(jComboBox |
} |
203 |
.getSelectedIndex() + 1); |
|
204 |
} else { |
/** |
205 |
if (jComboBox.getSelectedIndex() > 0) |
* This method initializes jButton |
206 |
jComboBox.setSelectedIndex(jComboBox |
* |
207 |
.getSelectedIndex() - 1); |
* @return javax.swing.JButton |
208 |
} |
*/ |
209 |
} |
private JButton getJButton() { |
210 |
}); |
if (jButton == null) { |
211 |
|
jButton = new OkButton(); |
212 |
String[] langNames = new String[languages.size() + 1]; |
jButton.setEnabled(false); |
213 |
for (int i = 0; i < languages.size(); i++) { |
|
214 |
|
jButton.addActionListener(new ActionListener() { |
215 |
Locale locale = null; |
|
216 |
for (Locale l : Locale.getAvailableLocales()) { |
public void actionPerformed(ActionEvent e) { |
217 |
if (l.getLanguage().equals(languages.get(i))) { |
dispose(); |
218 |
locale = l; |
} |
219 |
} |
|
220 |
} |
}); |
221 |
|
} |
222 |
langNames[i] = locale.getDisplayLanguage(locale) + " / " |
return jButton; |
223 |
+ locale.getDisplayLanguage() + " / " |
} |
224 |
+ languages.get(i); |
|
225 |
} |
/** |
226 |
langNames[languages.size()] = "?"; |
* This method initializes jPanel1 |
227 |
|
* |
228 |
jComboBox.setModel(new DefaultComboBoxModel(langNames)); |
* @return javax.swing.JPanel |
229 |
jComboBox.setSelectedItem(langNames[languages.size()]); |
*/ |
230 |
|
private JPanel getJPanel1() { |
231 |
jComboBox.addActionListener(new ActionListener() { |
if (jPanel1 == null) { |
232 |
|
final GridBagConstraints gridBagConstraints2 = new GridBagConstraints(); |
233 |
public void actionPerformed(final ActionEvent e) { |
gridBagConstraints2.fill = GridBagConstraints.VERTICAL; |
234 |
if (jComboBox.getSelectedIndex() == languages.size()) { |
gridBagConstraints2.gridy = 0; |
235 |
getJButton().setEnabled(false); |
gridBagConstraints2.weightx = 1.0; |
236 |
return; |
gridBagConstraints2.insets = new Insets(5, 5, 5, 5); |
237 |
} |
gridBagConstraints2.anchor = GridBagConstraints.WEST; |
238 |
Translation.setActiveLang(languages.get(jComboBox |
gridBagConstraints2.gridx = 1; |
239 |
.getSelectedIndex())); |
final GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
240 |
getJButton().setEnabled(true); |
gridBagConstraints.gridx = 0; |
241 |
} |
gridBagConstraints.insets = new Insets(0, 5, 0, 0); |
242 |
|
gridBagConstraints.gridy = 0; |
243 |
}); |
jLabel = new JLabel(); |
244 |
} |
jLabel.setText("Select language: "); // i8n!?! Maybe replace with an |
245 |
return jComboBox; |
// icon of an index finger |
246 |
} |
jPanel1 = new JPanel(); |
247 |
|
jPanel1.setLayout(new GridBagLayout()); |
248 |
} // @jve:decl-index=0:visual-constraint="0,0" |
jPanel1.add(jLabel, gridBagConstraints); |
249 |
|
jPanel1.add(getJComboBox(), gridBagConstraints2); |
250 |
|
} |
251 |
|
return jPanel1; |
252 |
|
} |
253 |
|
|
254 |
|
/** |
255 |
|
* This method initializes jComboBox |
256 |
|
* |
257 |
|
* @return javax.swing.JComboBox |
258 |
|
*/ |
259 |
|
private JComboBox getJComboBox() { |
260 |
|
if (jComboBox == null) { |
261 |
|
jComboBox = new JComboBox(); |
262 |
|
|
263 |
|
jComboBox.addMouseWheelListener(new MouseWheelListener() { |
264 |
|
public void mouseWheelMoved(java.awt.event.MouseWheelEvent e) { |
265 |
|
|
266 |
|
if ((e.getWheelRotation() < 0)) { |
267 |
|
if (jComboBox.getSelectedIndex() < jComboBox |
268 |
|
.getItemCount() - 1) |
269 |
|
jComboBox.setSelectedIndex(jComboBox |
270 |
|
.getSelectedIndex() + 1); |
271 |
|
} else { |
272 |
|
if (jComboBox.getSelectedIndex() > 0) |
273 |
|
jComboBox.setSelectedIndex(jComboBox |
274 |
|
.getSelectedIndex() - 1); |
275 |
|
} |
276 |
|
} |
277 |
|
}); |
278 |
|
|
279 |
|
String[] langNames = new String[languages.size() + 1]; |
280 |
|
for (int i = 0; i < languages.size(); i++) { |
281 |
|
|
282 |
|
Locale locale = I8NUtil.getLocaleFor(languages.get(i)); |
283 |
|
|
284 |
|
langNames[i] = locale.getDisplayLanguage(locale) + " / " |
285 |
|
+ locale.getDisplayLanguage() + " / " |
286 |
|
+ languages.get(i); |
287 |
|
} |
288 |
|
langNames[languages.size()] = "?"; |
289 |
|
|
290 |
|
jComboBox.setModel(new DefaultComboBoxModel(langNames)); |
291 |
|
jComboBox.setSelectedItem(langNames[languages.size()]); |
292 |
|
|
293 |
|
jComboBox.addActionListener(new ActionListener() { |
294 |
|
|
295 |
|
public void actionPerformed(final ActionEvent e) { |
296 |
|
if (jComboBox.getSelectedIndex() == languages.size()) { |
297 |
|
getJButton().setEnabled(false); |
298 |
|
return; |
299 |
|
} |
300 |
|
|
301 |
|
String l = languages.get(jComboBox.getSelectedIndex()); |
302 |
|
try { |
303 |
|
Translation.setActiveLang(l, setLocale); |
304 |
|
|
305 |
|
getJButton().setEnabled(true); |
306 |
|
} catch (java.lang.IllegalArgumentException ee) { |
307 |
|
LOGGER.warn("The language " + l + " is not valid", ee); |
308 |
|
getJButton().setEnabled(false); |
309 |
|
} |
310 |
|
|
311 |
|
} |
312 |
|
|
313 |
|
}); |
314 |
|
} |
315 |
|
return jComboBox; |
316 |
|
} |
317 |
|
|
318 |
|
} |