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(owner instanceof Window ? (Window) owner : SwingUtil |
66 |
.getParentWindow(owner)); |
67 |
this.languages = languages; |
68 |
|
69 |
Translation.setActiveLang(languages.get(0)); |
70 |
|
71 |
if (languages.size() == 1) { |
72 |
LOGGER.debug("Only language '" + languages.get(0) |
73 |
+ "' is available. It has been selected automatically."); |
74 |
return; |
75 |
} |
76 |
|
77 |
initialize(); |
78 |
} |
79 |
|
80 |
/** |
81 |
* This method initializes this |
82 |
* |
83 |
* @return void |
84 |
*/ |
85 |
private void initialize() { |
86 |
this.setContentPane(getJContentPane()); |
87 |
setModal(true); |
88 |
SwingUtil.centerFrameOnScreenRandom(this); |
89 |
|
90 |
setDefaultCloseOperation( JDialog.DO_NOTHING_ON_CLOSE); |
91 |
|
92 |
pack(); |
93 |
} |
94 |
|
95 |
/** |
96 |
* This method initializes jContentPane |
97 |
* |
98 |
* @return javax.swing.JPanel |
99 |
*/ |
100 |
private JPanel getJContentPane() { |
101 |
if (jContentPane == null) { |
102 |
final GridBagConstraints gridBagConstraints11 = new GridBagConstraints(); |
103 |
gridBagConstraints11.gridx = 1; |
104 |
gridBagConstraints11.fill = GridBagConstraints.HORIZONTAL; |
105 |
gridBagConstraints11.gridy = 1; |
106 |
final GridBagConstraints gridBagConstraints3 = new GridBagConstraints(); |
107 |
gridBagConstraints3.gridx = 1; |
108 |
gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL; |
109 |
gridBagConstraints3.gridy = 2; |
110 |
final GridBagConstraints gridBagConstraints1 = new GridBagConstraints(); |
111 |
gridBagConstraints1.gridx = 0; |
112 |
gridBagConstraints1.fill = GridBagConstraints.BOTH; |
113 |
gridBagConstraints1.gridwidth = 2; |
114 |
gridBagConstraints1.anchor = GridBagConstraints.NORTH; |
115 |
gridBagConstraints1.gridy = 0; |
116 |
jLabelFlagimage = new JLabel(new ImageIcon( |
117 |
TranslationEditJPanel.class |
118 |
.getResource("resource/flags.jpg"))); |
119 |
jContentPane = new JPanel(); |
120 |
jContentPane.setLayout(new GridBagLayout()); |
121 |
jContentPane.add(jLabelFlagimage, gridBagConstraints1); |
122 |
jContentPane.add(getJPanel(), gridBagConstraints3); |
123 |
jContentPane.add(getJPanel1(), gridBagConstraints11); |
124 |
} |
125 |
return jContentPane; |
126 |
} |
127 |
|
128 |
/** |
129 |
* This method initializes jPanel |
130 |
* |
131 |
* @return javax.swing.JPanel |
132 |
*/ |
133 |
private JPanel getJPanel() { |
134 |
if (jPanel == null) { |
135 |
final GridBagConstraints gridBagConstraints4 = new GridBagConstraints(); |
136 |
gridBagConstraints4.gridx = 0; |
137 |
gridBagConstraints4.anchor = GridBagConstraints.EAST; |
138 |
gridBagConstraints4.weightx = 1.0; |
139 |
gridBagConstraints4.insets = new Insets(5, 5, 5, 5); |
140 |
gridBagConstraints4.gridy = 0; |
141 |
jPanel = new JPanel(); |
142 |
jPanel.setLayout(new GridBagLayout()); |
143 |
jPanel.add(getJButton(), gridBagConstraints4); |
144 |
} |
145 |
return jPanel; |
146 |
} |
147 |
|
148 |
/** |
149 |
* This method initializes jButton |
150 |
* |
151 |
* @return javax.swing.JButton |
152 |
*/ |
153 |
private JButton getJButton() { |
154 |
if (jButton == null) { |
155 |
jButton = new OkButton(); |
156 |
jButton.setEnabled(false); |
157 |
|
158 |
jButton.addActionListener(new ActionListener() { |
159 |
|
160 |
public void actionPerformed(ActionEvent e) { |
161 |
dispose(); |
162 |
} |
163 |
|
164 |
}); |
165 |
} |
166 |
return jButton; |
167 |
} |
168 |
|
169 |
/** |
170 |
* This method initializes jPanel1 |
171 |
* |
172 |
* @return javax.swing.JPanel |
173 |
*/ |
174 |
private JPanel getJPanel1() { |
175 |
if (jPanel1 == null) { |
176 |
final GridBagConstraints gridBagConstraints2 = new GridBagConstraints(); |
177 |
gridBagConstraints2.fill = GridBagConstraints.VERTICAL; |
178 |
gridBagConstraints2.gridy = 0; |
179 |
gridBagConstraints2.weightx = 1.0; |
180 |
gridBagConstraints2.insets = new Insets(5, 5, 5, 5); |
181 |
gridBagConstraints2.anchor = GridBagConstraints.WEST; |
182 |
gridBagConstraints2.gridx = 1; |
183 |
final GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
184 |
gridBagConstraints.gridx = 0; |
185 |
gridBagConstraints.insets = new Insets(0, 5, 0, 0); |
186 |
gridBagConstraints.gridy = 0; |
187 |
jLabel = new JLabel(); |
188 |
jLabel.setText("Select language: "); //i8n!?! Maybe replace with an 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 |
245 |
.getSelectedIndex()); |
246 |
try { |
247 |
Translation.setActiveLang(l); |
248 |
|
249 |
getJButton().setEnabled(true); |
250 |
} catch (java.lang.IllegalArgumentException ee) { |
251 |
LOGGER.warn("The language "+l+" is not valid",ee); |
252 |
getJButton().setEnabled(false); |
253 |
} |
254 |
|
255 |
} |
256 |
|
257 |
}); |
258 |
} |
259 |
return jComboBox; |
260 |
} |
261 |
|
262 |
} // @jve:decl-index=0:visual-constraint="0,0" |