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