2 |
* Copyright (c) 2009 Martin O. J. Schmitz. |
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
* |
* |
4 |
* This file is part of the SCHMITZM library - a collection of utility |
* This file is part of the SCHMITZM library - a collection of utility |
5 |
* classes based on Java 1.6, focussing (not only) on Java Swing |
* classes based on Java 1.6, focusing (not only) on Java Swing |
6 |
* and the Geotools library. |
* and the Geotools library. |
7 |
* |
* |
8 |
* The SCHMITZM project is hosted at: |
* The SCHMITZM project is hosted at: |
25 |
* |
* |
26 |
* Contributors: |
* Contributors: |
27 |
* Martin O. J. Schmitz - initial API and implementation |
* Martin O. J. Schmitz - initial API and implementation |
28 |
* Stefan A. Krüger - additional utility classes |
* Stefan A. Tzeggai - additional utility classes |
29 |
******************************************************************************/ |
******************************************************************************/ |
30 |
package skrueger.i8n; |
package skrueger.i8n; |
31 |
|
|
33 |
import java.awt.GridBagConstraints; |
import java.awt.GridBagConstraints; |
34 |
import java.awt.GridBagLayout; |
import java.awt.GridBagLayout; |
35 |
import java.awt.Insets; |
import java.awt.Insets; |
|
import java.awt.Window; |
|
36 |
import java.awt.event.ActionEvent; |
import java.awt.event.ActionEvent; |
37 |
import java.awt.event.ActionListener; |
import java.awt.event.ActionListener; |
38 |
import java.awt.event.MouseWheelListener; |
import java.util.ArrayList; |
39 |
import java.util.List; |
import java.util.List; |
|
import java.util.Locale; |
|
40 |
|
|
|
import javax.swing.DefaultComboBoxModel; |
|
41 |
import javax.swing.ImageIcon; |
import javax.swing.ImageIcon; |
42 |
import javax.swing.JButton; |
import javax.swing.JButton; |
43 |
import javax.swing.JComboBox; |
import javax.swing.JComboBox; |
|
import javax.swing.JDialog; |
|
44 |
import javax.swing.JLabel; |
import javax.swing.JLabel; |
45 |
import javax.swing.JPanel; |
import javax.swing.JPanel; |
46 |
|
|
47 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
48 |
|
|
49 |
import schmitzm.swing.SwingUtil; |
import schmitzm.swing.SwingUtil; |
50 |
|
import skrueger.swing.AtlasDialog; |
51 |
import skrueger.swing.OkButton; |
import skrueger.swing.OkButton; |
52 |
import skrueger.swing.TranslationEditJPanel; |
import skrueger.swing.TranslationEditJPanel; |
53 |
|
|
55 |
* This dialog ask the user to select one of list of given languages. The dialog |
* This dialog ask the user to select one of list of given languages. The dialog |
56 |
* is modal and not visible after construction. |
* is modal and not visible after construction. |
57 |
* |
* |
58 |
* @author Stefan A. Krueger |
* @author Stefan A. Tzeggai |
59 |
*/ |
*/ |
60 |
public class SwitchLanguageDialog extends JDialog { |
public class SwitchLanguageDialog extends AtlasDialog { |
61 |
protected Logger LOGGER = Logger.getLogger(SwitchLanguageDialog.class); |
protected Logger LOGGER = Logger.getLogger(SwitchLanguageDialog.class); |
62 |
|
|
|
private static final long serialVersionUID = 1L; |
|
|
|
|
63 |
private JPanel jContentPane = null; |
private JPanel jContentPane = null; |
64 |
|
|
65 |
private JLabel jLabelFlagimage = null; |
private JLabel jLabelFlagimage = null; |
72 |
|
|
73 |
private JLabel jLabel = null; |
private JLabel jLabel = null; |
74 |
|
|
75 |
private JComboBox jComboBox = null; |
private LanguagesComboBox jComboBox = null; |
76 |
|
|
77 |
private final List<String> languages; |
private final List<String> languages; |
78 |
|
|
79 |
/** |
/** |
80 |
|
* if <code>true</code>, the default locale will also be changed during a |
81 |
|
* language selection |
82 |
|
**/ |
83 |
|
protected boolean setLocale; |
84 |
|
|
85 |
|
/** |
86 |
* A dialog to select one of the available languages. If only one language |
* A dialog to select one of the available languages. If only one language |
87 |
* is available, select it directly. Creating this object automatically |
* is available, select it directly. Creating this object automatically |
88 |
* makes it visible. |
* makes it visible, unless there is only one language to choose from.. it |
89 |
|
* that case it disposes itself automatically. |
90 |
* |
* |
91 |
* @param owner |
* @param setLocale |
92 |
* @param atlasConfig |
* if <code>true</code>, the default locale will also be changed |
93 |
|
* during a language selection |
94 |
*/ |
*/ |
95 |
public SwitchLanguageDialog(final Component owner, |
public SwitchLanguageDialog(final Component owner, |
96 |
final List<String> languages) { |
final List<String> languages, boolean setLocale) { |
97 |
super(SwingUtil.getParentWindow(owner)); |
super(owner); |
98 |
this.languages = languages; |
this.languages = languages; |
99 |
|
this.setLocale = setLocale; |
100 |
|
|
101 |
Translation.setActiveLang(languages.get(0)); |
if (languages == null || languages.size() == 0) { |
102 |
|
// No language is available. |
103 |
|
return; |
104 |
|
} |
105 |
|
|
106 |
|
Translation.setActiveLang(languages.get(0), setLocale); |
107 |
if (languages.size() == 1) { |
if (languages.size() == 1) { |
108 |
LOGGER.debug("Only language '" + languages.get(0) |
// Only language one language is available. It has been selected |
109 |
+ "' is available. It has been selected automatically."); |
// automatically. |
110 |
return; |
return; |
111 |
} |
} |
112 |
|
|
113 |
initialize(); |
initialize(); |
114 |
} |
} |
115 |
|
|
116 |
|
@Override |
117 |
|
/** |
118 |
|
* 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 |
* This method initializes this |
* This method initializes this |
129 |
* |
* |
131 |
*/ |
*/ |
132 |
private void initialize() { |
private void initialize() { |
133 |
this.setContentPane(getJContentPane()); |
this.setContentPane(getJContentPane()); |
|
setModal(true); |
|
|
SwingUtil.centerFrameOnScreenRandom(this); |
|
|
|
|
|
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
|
134 |
|
|
135 |
pack(); |
pack(); |
136 |
|
|
137 |
|
SwingUtil.centerFrameOnScreenRandom(this); |
138 |
|
setModal(true); |
139 |
|
} |
140 |
|
|
141 |
|
public boolean close() { |
142 |
|
// Only close by ESC and window-close if a valid selection is made |
143 |
|
if (jComboBox.getSelectedIndex() == languages.size() |
144 |
|
|| jComboBox.getSelectedIndex() == -1) |
145 |
|
return false; |
146 |
|
else |
147 |
|
return super.close(); |
148 |
} |
} |
149 |
|
|
150 |
/** |
/** |
241 |
gridBagConstraints.gridy = 0; |
gridBagConstraints.gridy = 0; |
242 |
jLabel = new JLabel(); |
jLabel = new JLabel(); |
243 |
jLabel.setText("Select language: "); // i8n!?! Maybe replace with an |
jLabel.setText("Select language: "); // i8n!?! Maybe replace with an |
244 |
// icon of an index finger |
// icon of an index finger |
245 |
jPanel1 = new JPanel(); |
jPanel1 = new JPanel(); |
246 |
jPanel1.setLayout(new GridBagLayout()); |
jPanel1.setLayout(new GridBagLayout()); |
247 |
jPanel1.add(jLabel, gridBagConstraints); |
jPanel1.add(jLabel, gridBagConstraints); |
257 |
*/ |
*/ |
258 |
private JComboBox getJComboBox() { |
private JComboBox getJComboBox() { |
259 |
if (jComboBox == null) { |
if (jComboBox == null) { |
260 |
jComboBox = new JComboBox(); |
ArrayList<String> languagesPlusOne = new ArrayList<String>( |
261 |
|
languages); |
262 |
jComboBox.addMouseWheelListener(new MouseWheelListener() { |
languagesPlusOne.add("?"); |
|
public void mouseWheelMoved(java.awt.event.MouseWheelEvent e) { |
|
|
|
|
|
if ((e.getWheelRotation() < 0)) { |
|
|
if (jComboBox.getSelectedIndex() < jComboBox |
|
|
.getItemCount() - 1) |
|
|
jComboBox.setSelectedIndex(jComboBox |
|
|
.getSelectedIndex() + 1); |
|
|
} else { |
|
|
if (jComboBox.getSelectedIndex() > 0) |
|
|
jComboBox.setSelectedIndex(jComboBox |
|
|
.getSelectedIndex() - 1); |
|
|
} |
|
|
} |
|
|
}); |
|
|
|
|
|
String[] langNames = new String[languages.size() + 1]; |
|
|
for (int i = 0; i < languages.size(); i++) { |
|
|
|
|
|
Locale locale = I8NUtil.getLocaleFor(languages.get(i)); |
|
|
|
|
|
langNames[i] = locale.getDisplayLanguage(locale) + " / " |
|
|
+ locale.getDisplayLanguage() + " / " |
|
|
+ languages.get(i); |
|
|
} |
|
|
langNames[languages.size()] = "?"; |
|
263 |
|
|
264 |
jComboBox.setModel(new DefaultComboBoxModel(langNames)); |
jComboBox = new LanguagesComboBox(languages); |
|
jComboBox.setSelectedItem(langNames[languages.size()]); |
|
265 |
|
|
266 |
|
SwingUtil.addMouseWheelForCombobox(jComboBox); |
267 |
jComboBox.addActionListener(new ActionListener() { |
jComboBox.addActionListener(new ActionListener() { |
268 |
|
|
269 |
public void actionPerformed(final ActionEvent e) { |
public void actionPerformed(final ActionEvent e) { |
274 |
|
|
275 |
String l = languages.get(jComboBox.getSelectedIndex()); |
String l = languages.get(jComboBox.getSelectedIndex()); |
276 |
try { |
try { |
277 |
Translation.setActiveLang(l); |
Translation.setActiveLang(l, setLocale); |
278 |
|
|
279 |
getJButton().setEnabled(true); |
getJButton().setEnabled(true); |
280 |
} catch (java.lang.IllegalArgumentException ee) { |
} catch (java.lang.IllegalArgumentException ee) { |
289 |
return jComboBox; |
return jComboBox; |
290 |
} |
} |
291 |
|
|
292 |
} // @jve:decl-index=0:visual-constraint="0,0" |
} |