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