1 |
alfonx |
244 |
/******************************************************************************* |
2 |
|
|
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
|
|
* |
4 |
|
|
* This file is part of the SCHMITZM library - a collection of utility |
5 |
alfonx |
256 |
* classes based on Java 1.6, focusing (not only) on Java Swing |
6 |
alfonx |
244 |
* 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 |
alfonx |
862 |
* Stefan A. Tzeggai - additional utility classes |
29 |
alfonx |
244 |
******************************************************************************/ |
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 |
alfonx |
1150 |
import java.util.ArrayList; |
39 |
alfonx |
244 |
import java.util.List; |
40 |
|
|
|
41 |
|
|
import javax.swing.ImageIcon; |
42 |
|
|
import javax.swing.JButton; |
43 |
|
|
import javax.swing.JComboBox; |
44 |
|
|
import javax.swing.JLabel; |
45 |
|
|
import javax.swing.JPanel; |
46 |
|
|
|
47 |
|
|
import org.apache.log4j.Logger; |
48 |
|
|
|
49 |
|
|
import schmitzm.swing.SwingUtil; |
50 |
alfonx |
1098 |
import skrueger.swing.AtlasDialog; |
51 |
alfonx |
244 |
import skrueger.swing.OkButton; |
52 |
|
|
import skrueger.swing.TranslationEditJPanel; |
53 |
|
|
|
54 |
|
|
/** |
55 |
|
|
* This dialog ask the user to select one of list of given languages. The dialog |
56 |
|
|
* is modal and not visible after construction. |
57 |
|
|
* |
58 |
alfonx |
1098 |
* @author Stefan A. Tzeggai |
59 |
alfonx |
244 |
*/ |
60 |
alfonx |
1098 |
public class SwitchLanguageDialog extends AtlasDialog { |
61 |
alfonx |
244 |
protected Logger LOGGER = Logger.getLogger(SwitchLanguageDialog.class); |
62 |
|
|
|
63 |
|
|
private JPanel jContentPane = null; |
64 |
|
|
|
65 |
|
|
private JLabel jLabelFlagimage = null; |
66 |
|
|
|
67 |
|
|
private JPanel jPanel = null; |
68 |
|
|
|
69 |
|
|
private JButton jButton = null; |
70 |
|
|
|
71 |
|
|
private JPanel jPanel1 = null; |
72 |
|
|
|
73 |
|
|
private JLabel jLabel = null; |
74 |
|
|
|
75 |
alfonx |
1150 |
private LanguagesComboBox jComboBox = null; |
76 |
alfonx |
244 |
|
77 |
|
|
private final List<String> languages; |
78 |
|
|
|
79 |
|
|
/** |
80 |
alfonx |
1098 |
* if <code>true</code>, the default locale will also be changed during a |
81 |
|
|
* language selection |
82 |
|
|
**/ |
83 |
|
|
protected boolean setLocale; |
84 |
|
|
|
85 |
|
|
/** |
86 |
alfonx |
244 |
* A dialog to select one of the available languages. If only one language |
87 |
|
|
* is available, select it directly. Creating this object automatically |
88 |
alfonx |
607 |
* makes it visible, unless there is only one language to choose from.. it |
89 |
alfonx |
724 |
* that case it disposes itself automatically. |
90 |
alfonx |
1098 |
* |
91 |
|
|
* @param setLocale |
92 |
|
|
* if <code>true</code>, the default locale will also be changed |
93 |
|
|
* during a language selection |
94 |
alfonx |
244 |
*/ |
95 |
|
|
public SwitchLanguageDialog(final Component owner, |
96 |
alfonx |
1098 |
final List<String> languages, boolean setLocale) { |
97 |
|
|
super(owner); |
98 |
alfonx |
244 |
this.languages = languages; |
99 |
alfonx |
1098 |
this.setLocale = setLocale; |
100 |
alfonx |
244 |
|
101 |
alfonx |
1098 |
if (languages == null || languages.size() == 0) { |
102 |
|
|
// No language is available. |
103 |
|
|
return; |
104 |
|
|
} |
105 |
alfonx |
244 |
|
106 |
alfonx |
1098 |
Translation.setActiveLang(languages.get(0), setLocale); |
107 |
alfonx |
244 |
if (languages.size() == 1) { |
108 |
alfonx |
1098 |
// Only language one language is available. It has been selected |
109 |
|
|
// automatically. |
110 |
alfonx |
244 |
return; |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
initialize(); |
114 |
|
|
} |
115 |
|
|
|
116 |
alfonx |
1098 |
@Override |
117 |
alfonx |
244 |
/** |
118 |
alfonx |
1098 |
* This modal dialog will not appear if there is <= one language to select from. |
119 |
|
|
*/ |
120 |
|
|
public void setVisible(boolean b) { |
121 |
|
|
if (b == true && (languages == null || languages.size() <= 1)) { |
122 |
|
|
return; |
123 |
|
|
} |
124 |
|
|
super.setVisible(b); |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
/** |
128 |
alfonx |
244 |
* This method initializes this |
129 |
|
|
* |
130 |
|
|
* @return void |
131 |
|
|
*/ |
132 |
|
|
private void initialize() { |
133 |
|
|
this.setContentPane(getJContentPane()); |
134 |
|
|
|
135 |
|
|
pack(); |
136 |
alfonx |
607 |
|
137 |
|
|
SwingUtil.centerFrameOnScreenRandom(this); |
138 |
|
|
setModal(true); |
139 |
alfonx |
244 |
} |
140 |
|
|
|
141 |
alfonx |
1098 |
public boolean close() { |
142 |
|
|
// Only close by ESC and window-close if a valid selection is made |
143 |
alfonx |
1150 |
if (jComboBox.getSelectedIndex() == languages.size() |
144 |
|
|
|| jComboBox.getSelectedIndex() == -1) |
145 |
alfonx |
1098 |
return false; |
146 |
|
|
else |
147 |
|
|
return super.close(); |
148 |
|
|
} |
149 |
|
|
|
150 |
alfonx |
244 |
/** |
151 |
|
|
* This method initializes jContentPane |
152 |
|
|
* |
153 |
|
|
* @return javax.swing.JPanel |
154 |
|
|
*/ |
155 |
|
|
private JPanel getJContentPane() { |
156 |
|
|
if (jContentPane == null) { |
157 |
|
|
final GridBagConstraints gridBagConstraints11 = new GridBagConstraints(); |
158 |
|
|
gridBagConstraints11.gridx = 1; |
159 |
|
|
gridBagConstraints11.fill = GridBagConstraints.HORIZONTAL; |
160 |
|
|
gridBagConstraints11.gridy = 1; |
161 |
|
|
final GridBagConstraints gridBagConstraints3 = new GridBagConstraints(); |
162 |
|
|
gridBagConstraints3.gridx = 1; |
163 |
|
|
gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL; |
164 |
|
|
gridBagConstraints3.gridy = 2; |
165 |
|
|
final GridBagConstraints gridBagConstraints1 = new GridBagConstraints(); |
166 |
|
|
gridBagConstraints1.gridx = 0; |
167 |
|
|
gridBagConstraints1.fill = GridBagConstraints.BOTH; |
168 |
|
|
gridBagConstraints1.gridwidth = 2; |
169 |
|
|
gridBagConstraints1.anchor = GridBagConstraints.NORTH; |
170 |
|
|
gridBagConstraints1.gridy = 0; |
171 |
|
|
jLabelFlagimage = new JLabel(new ImageIcon( |
172 |
|
|
TranslationEditJPanel.class |
173 |
|
|
.getResource("resource/flags.jpg"))); |
174 |
|
|
jContentPane = new JPanel(); |
175 |
|
|
jContentPane.setLayout(new GridBagLayout()); |
176 |
|
|
jContentPane.add(jLabelFlagimage, gridBagConstraints1); |
177 |
|
|
jContentPane.add(getJPanel(), gridBagConstraints3); |
178 |
|
|
jContentPane.add(getJPanel1(), gridBagConstraints11); |
179 |
|
|
} |
180 |
|
|
return jContentPane; |
181 |
|
|
} |
182 |
|
|
|
183 |
|
|
/** |
184 |
|
|
* This method initializes jPanel |
185 |
|
|
* |
186 |
|
|
* @return javax.swing.JPanel |
187 |
|
|
*/ |
188 |
|
|
private JPanel getJPanel() { |
189 |
|
|
if (jPanel == null) { |
190 |
|
|
final GridBagConstraints gridBagConstraints4 = new GridBagConstraints(); |
191 |
|
|
gridBagConstraints4.gridx = 0; |
192 |
|
|
gridBagConstraints4.anchor = GridBagConstraints.EAST; |
193 |
|
|
gridBagConstraints4.weightx = 1.0; |
194 |
|
|
gridBagConstraints4.insets = new Insets(5, 5, 5, 5); |
195 |
|
|
gridBagConstraints4.gridy = 0; |
196 |
|
|
jPanel = new JPanel(); |
197 |
|
|
jPanel.setLayout(new GridBagLayout()); |
198 |
|
|
jPanel.add(getJButton(), gridBagConstraints4); |
199 |
|
|
} |
200 |
|
|
return jPanel; |
201 |
|
|
} |
202 |
|
|
|
203 |
|
|
/** |
204 |
|
|
* This method initializes jButton |
205 |
|
|
* |
206 |
|
|
* @return javax.swing.JButton |
207 |
|
|
*/ |
208 |
|
|
private JButton getJButton() { |
209 |
|
|
if (jButton == null) { |
210 |
|
|
jButton = new OkButton(); |
211 |
|
|
jButton.setEnabled(false); |
212 |
|
|
|
213 |
|
|
jButton.addActionListener(new ActionListener() { |
214 |
|
|
|
215 |
|
|
public void actionPerformed(ActionEvent e) { |
216 |
|
|
dispose(); |
217 |
|
|
} |
218 |
|
|
|
219 |
|
|
}); |
220 |
|
|
} |
221 |
|
|
return jButton; |
222 |
|
|
} |
223 |
|
|
|
224 |
|
|
/** |
225 |
|
|
* This method initializes jPanel1 |
226 |
|
|
* |
227 |
|
|
* @return javax.swing.JPanel |
228 |
|
|
*/ |
229 |
|
|
private JPanel getJPanel1() { |
230 |
|
|
if (jPanel1 == null) { |
231 |
|
|
final GridBagConstraints gridBagConstraints2 = new GridBagConstraints(); |
232 |
|
|
gridBagConstraints2.fill = GridBagConstraints.VERTICAL; |
233 |
|
|
gridBagConstraints2.gridy = 0; |
234 |
|
|
gridBagConstraints2.weightx = 1.0; |
235 |
|
|
gridBagConstraints2.insets = new Insets(5, 5, 5, 5); |
236 |
|
|
gridBagConstraints2.anchor = GridBagConstraints.WEST; |
237 |
|
|
gridBagConstraints2.gridx = 1; |
238 |
|
|
final GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
239 |
|
|
gridBagConstraints.gridx = 0; |
240 |
|
|
gridBagConstraints.insets = new Insets(0, 5, 0, 0); |
241 |
|
|
gridBagConstraints.gridy = 0; |
242 |
|
|
jLabel = new JLabel(); |
243 |
|
|
jLabel.setText("Select language: "); // i8n!?! Maybe replace with an |
244 |
alfonx |
607 |
// icon of an index finger |
245 |
alfonx |
244 |
jPanel1 = new JPanel(); |
246 |
|
|
jPanel1.setLayout(new GridBagLayout()); |
247 |
|
|
jPanel1.add(jLabel, gridBagConstraints); |
248 |
|
|
jPanel1.add(getJComboBox(), gridBagConstraints2); |
249 |
|
|
} |
250 |
|
|
return jPanel1; |
251 |
|
|
} |
252 |
|
|
|
253 |
|
|
/** |
254 |
|
|
* This method initializes jComboBox |
255 |
|
|
* |
256 |
|
|
* @return javax.swing.JComboBox |
257 |
|
|
*/ |
258 |
|
|
private JComboBox getJComboBox() { |
259 |
|
|
if (jComboBox == null) { |
260 |
alfonx |
1150 |
ArrayList<String> languagesPlusOne = new ArrayList<String>( |
261 |
|
|
languages); |
262 |
|
|
languagesPlusOne.add("?"); |
263 |
alfonx |
244 |
|
264 |
alfonx |
1150 |
jComboBox = new LanguagesComboBox(languages); |
265 |
alfonx |
244 |
|
266 |
alfonx |
1150 |
SwingUtil.addMouseWheelForCombobox(jComboBox); |
267 |
alfonx |
244 |
jComboBox.addActionListener(new ActionListener() { |
268 |
|
|
|
269 |
|
|
public void actionPerformed(final ActionEvent e) { |
270 |
|
|
if (jComboBox.getSelectedIndex() == languages.size()) { |
271 |
|
|
getJButton().setEnabled(false); |
272 |
|
|
return; |
273 |
|
|
} |
274 |
|
|
|
275 |
|
|
String l = languages.get(jComboBox.getSelectedIndex()); |
276 |
|
|
try { |
277 |
alfonx |
1098 |
Translation.setActiveLang(l, setLocale); |
278 |
alfonx |
244 |
|
279 |
|
|
getJButton().setEnabled(true); |
280 |
|
|
} catch (java.lang.IllegalArgumentException ee) { |
281 |
|
|
LOGGER.warn("The language " + l + " is not valid", ee); |
282 |
|
|
getJButton().setEnabled(false); |
283 |
|
|
} |
284 |
|
|
|
285 |
|
|
} |
286 |
|
|
|
287 |
|
|
}); |
288 |
|
|
} |
289 |
|
|
return jComboBox; |
290 |
|
|
} |
291 |
|
|
|
292 |
alfonx |
607 |
} |