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