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 |
|
|
43 |
import javax.swing.ImageIcon; |
import javax.swing.ImageIcon; |
44 |
import javax.swing.JButton; |
import javax.swing.JButton; |
45 |
import javax.swing.JComboBox; |
import javax.swing.JComboBox; |
|
import javax.swing.JDialog; |
|
46 |
import javax.swing.JLabel; |
import javax.swing.JLabel; |
47 |
import javax.swing.JPanel; |
import javax.swing.JPanel; |
48 |
|
|
49 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
50 |
|
|
51 |
import schmitzm.swing.SwingUtil; |
import schmitzm.swing.SwingUtil; |
52 |
|
import skrueger.swing.AtlasDialog; |
53 |
import skrueger.swing.OkButton; |
import skrueger.swing.OkButton; |
54 |
import skrueger.swing.TranslationEditJPanel; |
import skrueger.swing.TranslationEditJPanel; |
55 |
|
|
57 |
* 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 |
58 |
* is modal and not visible after construction. |
* is modal and not visible after construction. |
59 |
* |
* |
60 |
* @author Stefan A. Krueger |
* @author Stefan A. Tzeggai |
61 |
*/ |
*/ |
62 |
public class SwitchLanguageDialog extends JDialog { |
public class SwitchLanguageDialog extends AtlasDialog { |
63 |
protected Logger LOGGER = Logger.getLogger(SwitchLanguageDialog.class); |
protected Logger LOGGER = Logger.getLogger(SwitchLanguageDialog.class); |
64 |
|
|
|
private static final long serialVersionUID = 1L; |
|
|
|
|
65 |
private JPanel jContentPane = null; |
private JPanel jContentPane = null; |
66 |
|
|
67 |
private JLabel jLabelFlagimage = null; |
private JLabel jLabelFlagimage = null; |
79 |
private final List<String> languages; |
private final List<String> languages; |
80 |
|
|
81 |
/** |
/** |
82 |
|
* if <code>true</code>, the default locale will also be changed during a |
83 |
|
* language selection |
84 |
|
**/ |
85 |
|
protected boolean setLocale; |
86 |
|
|
87 |
|
/** |
88 |
* 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 |
89 |
* is available, select it directly. Creating this object automatically |
* is available, select it directly. Creating this object automatically |
90 |
* makes it visible. |
* makes it visible, unless there is only one language to choose from.. it |
91 |
|
* that case it disposes itself automatically. |
92 |
* |
* |
93 |
* @param owner |
* @param setLocale |
94 |
* @param atlasConfig |
* if <code>true</code>, the default locale will also be changed |
95 |
|
* during a language selection |
96 |
*/ |
*/ |
97 |
public SwitchLanguageDialog(final Component owner, |
public SwitchLanguageDialog(final Component owner, |
98 |
final List<String> languages) { |
final List<String> languages, boolean setLocale) { |
99 |
super(SwingUtil.getParentWindow(owner)); |
super(owner); |
100 |
this.languages = languages; |
this.languages = languages; |
101 |
|
this.setLocale = setLocale; |
102 |
|
|
103 |
Translation.setActiveLang(languages.get(0)); |
if (languages == null || languages.size() == 0) { |
104 |
|
// No language is available. |
105 |
|
return; |
106 |
|
} |
107 |
|
|
108 |
|
Translation.setActiveLang(languages.get(0), setLocale); |
109 |
if (languages.size() == 1) { |
if (languages.size() == 1) { |
110 |
LOGGER.debug("Only language '" + languages.get(0) |
// Only language one language is available. It has been selected |
111 |
+ "' is available. It has been selected automatically."); |
// automatically. |
112 |
return; |
return; |
113 |
} |
} |
114 |
|
|
115 |
initialize(); |
initialize(); |
116 |
} |
} |
117 |
|
|
118 |
|
@Override |
119 |
|
/** |
120 |
|
* This modal dialog will not appear if there is <= one language to select from. |
121 |
|
*/ |
122 |
|
public void setVisible(boolean b) { |
123 |
|
if (b == true && (languages == null || languages.size() <= 1)) { |
124 |
|
return; |
125 |
|
} |
126 |
|
super.setVisible(b); |
127 |
|
} |
128 |
|
|
129 |
/** |
/** |
130 |
* This method initializes this |
* This method initializes this |
131 |
* |
* |
133 |
*/ |
*/ |
134 |
private void initialize() { |
private void initialize() { |
135 |
this.setContentPane(getJContentPane()); |
this.setContentPane(getJContentPane()); |
|
setModal(true); |
|
|
SwingUtil.centerFrameOnScreenRandom(this); |
|
|
|
|
|
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
|
136 |
|
|
137 |
pack(); |
pack(); |
138 |
|
|
139 |
|
SwingUtil.centerFrameOnScreenRandom(this); |
140 |
|
setModal(true); |
141 |
|
} |
142 |
|
|
143 |
|
public boolean close() { |
144 |
|
// Only close by ESC and window-close if a valid selection is made |
145 |
|
if (jComboBox.getSelectedIndex() == languages.size()) |
146 |
|
return false; |
147 |
|
else |
148 |
|
return super.close(); |
149 |
} |
} |
150 |
|
|
151 |
/** |
/** |
242 |
gridBagConstraints.gridy = 0; |
gridBagConstraints.gridy = 0; |
243 |
jLabel = new JLabel(); |
jLabel = new JLabel(); |
244 |
jLabel.setText("Select language: "); // i8n!?! Maybe replace with an |
jLabel.setText("Select language: "); // i8n!?! Maybe replace with an |
245 |
// icon of an index finger |
// icon of an index finger |
246 |
jPanel1 = new JPanel(); |
jPanel1 = new JPanel(); |
247 |
jPanel1.setLayout(new GridBagLayout()); |
jPanel1.setLayout(new GridBagLayout()); |
248 |
jPanel1.add(jLabel, gridBagConstraints); |
jPanel1.add(jLabel, gridBagConstraints); |
279 |
String[] langNames = new String[languages.size() + 1]; |
String[] langNames = new String[languages.size() + 1]; |
280 |
for (int i = 0; i < languages.size(); i++) { |
for (int i = 0; i < languages.size(); i++) { |
281 |
|
|
282 |
Locale locale = I8NUtil.getLocaleFor(languages.get(i)); |
Locale locale = I8NUtil.getFirstLocaleForLang(languages.get(i)); |
283 |
|
|
284 |
langNames[i] = locale.getDisplayLanguage(locale) + " / " |
langNames[i] = locale.getDisplayLanguage(locale) + " / " |
285 |
+ locale.getDisplayLanguage() + " / " |
+ locale.getDisplayLanguage() + " / " |
300 |
|
|
301 |
String l = languages.get(jComboBox.getSelectedIndex()); |
String l = languages.get(jComboBox.getSelectedIndex()); |
302 |
try { |
try { |
303 |
Translation.setActiveLang(l); |
Translation.setActiveLang(l, setLocale); |
304 |
|
|
305 |
getJButton().setEnabled(true); |
getJButton().setEnabled(true); |
306 |
} catch (java.lang.IllegalArgumentException ee) { |
} catch (java.lang.IllegalArgumentException ee) { |
315 |
return jComboBox; |
return jComboBox; |
316 |
} |
} |
317 |
|
|
318 |
} // @jve:decl-index=0:visual-constraint="0,0" |
} |