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. Krüger - additional utility classes |
29 |
******************************************************************************/ |
30 |
package skrueger.i8n; |
31 |
|
32 |
import java.awt.Component; |
33 |
import java.awt.GridBagConstraints; |
34 |
import java.awt.GridBagLayout; |
35 |
import java.awt.Insets; |
36 |
import java.awt.event.ActionEvent; |
37 |
import java.awt.event.ActionListener; |
38 |
import java.awt.event.MouseWheelListener; |
39 |
import java.util.List; |
40 |
import java.util.Locale; |
41 |
|
42 |
import javax.swing.DefaultComboBoxModel; |
43 |
import javax.swing.ImageIcon; |
44 |
import javax.swing.JButton; |
45 |
import javax.swing.JComboBox; |
46 |
import javax.swing.JDialog; |
47 |
import javax.swing.JLabel; |
48 |
import javax.swing.JPanel; |
49 |
|
50 |
import org.apache.log4j.Logger; |
51 |
|
52 |
import schmitzm.swing.SwingUtil; |
53 |
import skrueger.swing.OkButton; |
54 |
import skrueger.swing.TranslationEditJPanel; |
55 |
|
56 |
/** |
57 |
* This dialog ask the user to select one of list of given languages. The dialog |
58 |
* is modal and not visible after construction. |
59 |
* |
60 |
* @author Stefan A. Krueger |
61 |
*/ |
62 |
public class SwitchLanguageDialog extends JDialog { |
63 |
protected Logger LOGGER = Logger.getLogger(SwitchLanguageDialog.class); |
64 |
|
65 |
private static final long serialVersionUID = 1L; |
66 |
|
67 |
private JPanel jContentPane = null; |
68 |
|
69 |
private JLabel jLabelFlagimage = null; |
70 |
|
71 |
private JPanel jPanel = null; |
72 |
|
73 |
private JButton jButton = null; |
74 |
|
75 |
private JPanel jPanel1 = null; |
76 |
|
77 |
private JLabel jLabel = null; |
78 |
|
79 |
private JComboBox jComboBox = null; |
80 |
|
81 |
private final List<String> languages; |
82 |
|
83 |
/** |
84 |
* A dialog to select one of the available languages. If only one language |
85 |
* is available, select it directly. Creating this object automatically |
86 |
* makes it visible. |
87 |
* |
88 |
* @param owner |
89 |
* @param atlasConfig |
90 |
*/ |
91 |
public SwitchLanguageDialog(final Component owner, |
92 |
final List<String> languages) { |
93 |
super(SwingUtil.getParentWindow(owner)); |
94 |
this.languages = languages; |
95 |
|
96 |
Translation.setActiveLang(languages.get(0)); |
97 |
|
98 |
if (languages.size() == 1) { |
99 |
LOGGER.debug("Only language '" + languages.get(0) |
100 |
+ "' is available. It has been selected automatically."); |
101 |
return; |
102 |
} |
103 |
|
104 |
initialize(); |
105 |
} |
106 |
|
107 |
/** |
108 |
* This method initializes this |
109 |
* |
110 |
* @return void |
111 |
*/ |
112 |
private void initialize() { |
113 |
this.setContentPane(getJContentPane()); |
114 |
setModal(true); |
115 |
SwingUtil.centerFrameOnScreenRandom(this); |
116 |
|
117 |
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
118 |
|
119 |
pack(); |
120 |
} |
121 |
|
122 |
/** |
123 |
* This method initializes jContentPane |
124 |
* |
125 |
* @return javax.swing.JPanel |
126 |
*/ |
127 |
private JPanel getJContentPane() { |
128 |
if (jContentPane == null) { |
129 |
final GridBagConstraints gridBagConstraints11 = new GridBagConstraints(); |
130 |
gridBagConstraints11.gridx = 1; |
131 |
gridBagConstraints11.fill = GridBagConstraints.HORIZONTAL; |
132 |
gridBagConstraints11.gridy = 1; |
133 |
final GridBagConstraints gridBagConstraints3 = new GridBagConstraints(); |
134 |
gridBagConstraints3.gridx = 1; |
135 |
gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL; |
136 |
gridBagConstraints3.gridy = 2; |
137 |
final GridBagConstraints gridBagConstraints1 = new GridBagConstraints(); |
138 |
gridBagConstraints1.gridx = 0; |
139 |
gridBagConstraints1.fill = GridBagConstraints.BOTH; |
140 |
gridBagConstraints1.gridwidth = 2; |
141 |
gridBagConstraints1.anchor = GridBagConstraints.NORTH; |
142 |
gridBagConstraints1.gridy = 0; |
143 |
jLabelFlagimage = new JLabel(new ImageIcon( |
144 |
TranslationEditJPanel.class |
145 |
.getResource("resource/flags.jpg"))); |
146 |
jContentPane = new JPanel(); |
147 |
jContentPane.setLayout(new GridBagLayout()); |
148 |
jContentPane.add(jLabelFlagimage, gridBagConstraints1); |
149 |
jContentPane.add(getJPanel(), gridBagConstraints3); |
150 |
jContentPane.add(getJPanel1(), gridBagConstraints11); |
151 |
} |
152 |
return jContentPane; |
153 |
} |
154 |
|
155 |
/** |
156 |
* This method initializes jPanel |
157 |
* |
158 |
* @return javax.swing.JPanel |
159 |
*/ |
160 |
private JPanel getJPanel() { |
161 |
if (jPanel == null) { |
162 |
final GridBagConstraints gridBagConstraints4 = new GridBagConstraints(); |
163 |
gridBagConstraints4.gridx = 0; |
164 |
gridBagConstraints4.anchor = GridBagConstraints.EAST; |
165 |
gridBagConstraints4.weightx = 1.0; |
166 |
gridBagConstraints4.insets = new Insets(5, 5, 5, 5); |
167 |
gridBagConstraints4.gridy = 0; |
168 |
jPanel = new JPanel(); |
169 |
jPanel.setLayout(new GridBagLayout()); |
170 |
jPanel.add(getJButton(), gridBagConstraints4); |
171 |
} |
172 |
return jPanel; |
173 |
} |
174 |
|
175 |
/** |
176 |
* This method initializes jButton |
177 |
* |
178 |
* @return javax.swing.JButton |
179 |
*/ |
180 |
private JButton getJButton() { |
181 |
if (jButton == null) { |
182 |
jButton = new OkButton(); |
183 |
jButton.setEnabled(false); |
184 |
|
185 |
jButton.addActionListener(new ActionListener() { |
186 |
|
187 |
public void actionPerformed(ActionEvent e) { |
188 |
dispose(); |
189 |
} |
190 |
|
191 |
}); |
192 |
} |
193 |
return jButton; |
194 |
} |
195 |
|
196 |
/** |
197 |
* This method initializes jPanel1 |
198 |
* |
199 |
* @return javax.swing.JPanel |
200 |
*/ |
201 |
private JPanel getJPanel1() { |
202 |
if (jPanel1 == null) { |
203 |
final GridBagConstraints gridBagConstraints2 = new GridBagConstraints(); |
204 |
gridBagConstraints2.fill = GridBagConstraints.VERTICAL; |
205 |
gridBagConstraints2.gridy = 0; |
206 |
gridBagConstraints2.weightx = 1.0; |
207 |
gridBagConstraints2.insets = new Insets(5, 5, 5, 5); |
208 |
gridBagConstraints2.anchor = GridBagConstraints.WEST; |
209 |
gridBagConstraints2.gridx = 1; |
210 |
final GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
211 |
gridBagConstraints.gridx = 0; |
212 |
gridBagConstraints.insets = new Insets(0, 5, 0, 0); |
213 |
gridBagConstraints.gridy = 0; |
214 |
jLabel = new JLabel(); |
215 |
jLabel.setText("Select language: "); // i8n!?! Maybe replace with an |
216 |
// icon of an index finger |
217 |
jPanel1 = new JPanel(); |
218 |
jPanel1.setLayout(new GridBagLayout()); |
219 |
jPanel1.add(jLabel, gridBagConstraints); |
220 |
jPanel1.add(getJComboBox(), gridBagConstraints2); |
221 |
} |
222 |
return jPanel1; |
223 |
} |
224 |
|
225 |
/** |
226 |
* This method initializes jComboBox |
227 |
* |
228 |
* @return javax.swing.JComboBox |
229 |
*/ |
230 |
private JComboBox getJComboBox() { |
231 |
if (jComboBox == null) { |
232 |
jComboBox = new JComboBox(); |
233 |
|
234 |
jComboBox.addMouseWheelListener(new MouseWheelListener() { |
235 |
public void mouseWheelMoved(java.awt.event.MouseWheelEvent e) { |
236 |
|
237 |
if ((e.getWheelRotation() < 0)) { |
238 |
if (jComboBox.getSelectedIndex() < jComboBox |
239 |
.getItemCount() - 1) |
240 |
jComboBox.setSelectedIndex(jComboBox |
241 |
.getSelectedIndex() + 1); |
242 |
} else { |
243 |
if (jComboBox.getSelectedIndex() > 0) |
244 |
jComboBox.setSelectedIndex(jComboBox |
245 |
.getSelectedIndex() - 1); |
246 |
} |
247 |
} |
248 |
}); |
249 |
|
250 |
String[] langNames = new String[languages.size() + 1]; |
251 |
for (int i = 0; i < languages.size(); i++) { |
252 |
|
253 |
Locale locale = I8NUtil.getLocaleFor(languages.get(i)); |
254 |
|
255 |
langNames[i] = locale.getDisplayLanguage(locale) + " / " |
256 |
+ locale.getDisplayLanguage() + " / " |
257 |
+ languages.get(i); |
258 |
} |
259 |
langNames[languages.size()] = "?"; |
260 |
|
261 |
jComboBox.setModel(new DefaultComboBoxModel(langNames)); |
262 |
jComboBox.setSelectedItem(langNames[languages.size()]); |
263 |
|
264 |
jComboBox.addActionListener(new ActionListener() { |
265 |
|
266 |
public void actionPerformed(final ActionEvent e) { |
267 |
if (jComboBox.getSelectedIndex() == languages.size()) { |
268 |
getJButton().setEnabled(false); |
269 |
return; |
270 |
} |
271 |
|
272 |
String l = languages.get(jComboBox.getSelectedIndex()); |
273 |
try { |
274 |
Translation.setActiveLang(l); |
275 |
|
276 |
getJButton().setEnabled(true); |
277 |
} catch (java.lang.IllegalArgumentException ee) { |
278 |
LOGGER.warn("The language " + l + " is not valid", ee); |
279 |
getJButton().setEnabled(false); |
280 |
} |
281 |
|
282 |
} |
283 |
|
284 |
}); |
285 |
} |
286 |
return jComboBox; |
287 |
} |
288 |
|
289 |
} // @jve:decl-index=0:visual-constraint="0,0" |