1 |
alfonx |
244 |
/******************************************************************************* |
2 |
|
|
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
|
|
* |
4 |
|
|
* This file is part of the SCHMITZM library - a collection of utility |
5 |
alfonx |
256 |
* classes based on Java 1.6, focusing (not only) on Java Swing |
6 |
alfonx |
244 |
* and the Geotools library. |
7 |
|
|
* |
8 |
|
|
* The SCHMITZM project is hosted at: |
9 |
|
|
* http://wald.intevation.org/projects/schmitzm/ |
10 |
|
|
* |
11 |
|
|
* This program is free software; you can redistribute it and/or |
12 |
|
|
* modify it under the terms of the GNU Lesser General Public License |
13 |
|
|
* as published by the Free Software Foundation; either version 3 |
14 |
|
|
* of the License, or (at your option) any later version. |
15 |
|
|
* |
16 |
|
|
* This program is distributed in the hope that it will be useful, |
17 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 |
|
|
* GNU General Public License for more details. |
20 |
|
|
* |
21 |
|
|
* You should have received a copy of the GNU Lesser General Public License (license.txt) |
22 |
|
|
* along with this program; if not, write to the Free Software |
23 |
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
24 |
|
|
* or try this link: http://www.gnu.org/licenses/lgpl.html |
25 |
|
|
* |
26 |
|
|
* Contributors: |
27 |
|
|
* Martin O. J. Schmitz - initial API and implementation |
28 |
alfonx |
862 |
* Stefan A. Tzeggai - additional utility classes |
29 |
alfonx |
244 |
******************************************************************************/ |
30 |
|
|
package skrueger.i8n; |
31 |
|
|
|
32 |
|
|
import java.awt.Component; |
33 |
|
|
import java.awt.GridBagConstraints; |
34 |
|
|
import java.awt.GridBagLayout; |
35 |
|
|
import java.awt.Insets; |
36 |
|
|
import java.awt.event.ActionEvent; |
37 |
|
|
import java.awt.event.ActionListener; |
38 |
alfonx |
1150 |
import java.util.ArrayList; |
39 |
alfonx |
244 |
import java.util.List; |
40 |
|
|
|
41 |
|
|
import javax.swing.ImageIcon; |
42 |
|
|
import javax.swing.JButton; |
43 |
|
|
import javax.swing.JComboBox; |
44 |
|
|
import javax.swing.JLabel; |
45 |
|
|
import javax.swing.JPanel; |
46 |
|
|
|
47 |
keeb |
1290 |
import net.miginfocom.swing.MigLayout; |
48 |
|
|
|
49 |
alfonx |
244 |
import org.apache.log4j.Logger; |
50 |
|
|
|
51 |
|
|
import schmitzm.swing.SwingUtil; |
52 |
alfonx |
1098 |
import skrueger.swing.AtlasDialog; |
53 |
alfonx |
244 |
import skrueger.swing.OkButton; |
54 |
|
|
import skrueger.swing.TranslationEditJPanel; |
55 |
|
|
|
56 |
|
|
/** |
57 |
|
|
* This dialog ask the user to select one of list of given languages. The dialog |
58 |
|
|
* is modal and not visible after construction. |
59 |
|
|
* |
60 |
alfonx |
1098 |
* @author Stefan A. Tzeggai |
61 |
alfonx |
244 |
*/ |
62 |
alfonx |
1098 |
public class SwitchLanguageDialog extends AtlasDialog { |
63 |
keeb |
1290 |
protected Logger LOGGER = Logger.getLogger(SwitchLanguageDialog.class); |
64 |
alfonx |
244 |
|
65 |
keeb |
1290 |
private JPanel jContentPane = null; |
66 |
alfonx |
244 |
|
67 |
keeb |
1290 |
private JLabel jLabelFlagimage = null; |
68 |
alfonx |
244 |
|
69 |
keeb |
1290 |
private JPanel jPanel = null; |
70 |
alfonx |
244 |
|
71 |
keeb |
1290 |
private JLabel jLabel = null; |
72 |
alfonx |
244 |
|
73 |
keeb |
1290 |
private LanguagesComboBox jComboBox = null; |
74 |
alfonx |
244 |
|
75 |
keeb |
1290 |
private final List<String> languages; |
76 |
alfonx |
244 |
|
77 |
keeb |
1290 |
/** |
78 |
|
|
* if <code>true</code>, the default locale will also be changed during a |
79 |
|
|
* language selection |
80 |
|
|
**/ |
81 |
|
|
protected boolean setLocale; |
82 |
alfonx |
244 |
|
83 |
keeb |
1290 |
/** |
84 |
|
|
* A dialog to select one of the available languages. If only one language |
85 |
|
|
* is available, select it directly. Creating this object automatically |
86 |
|
|
* makes it visible, unless there is only one language to choose from.. it |
87 |
|
|
* that case it disposes itself automatically. |
88 |
|
|
* |
89 |
|
|
* @param setLocale if <code>true</code>, the default locale will also be |
90 |
|
|
* changed during a language selection |
91 |
|
|
*/ |
92 |
|
|
public SwitchLanguageDialog(final Component owner, |
93 |
|
|
final List<String> languages, boolean setLocale) { |
94 |
|
|
super(owner); |
95 |
|
|
this.languages = languages; |
96 |
|
|
this.setLocale = setLocale; |
97 |
alfonx |
244 |
|
98 |
keeb |
1290 |
if (languages == null || languages.size() == 0) { |
99 |
|
|
// No language is available. |
100 |
|
|
return; |
101 |
|
|
} |
102 |
alfonx |
1098 |
|
103 |
keeb |
1290 |
Translation.setActiveLang(languages.get(0), setLocale); |
104 |
|
|
if (languages.size() == 1) { |
105 |
|
|
// Only language one language is available. It has been selected |
106 |
|
|
// automatically. |
107 |
|
|
return; |
108 |
|
|
} |
109 |
alfonx |
244 |
|
110 |
keeb |
1290 |
initialize(); |
111 |
|
|
} |
112 |
alfonx |
244 |
|
113 |
keeb |
1290 |
@Override |
114 |
|
|
/** |
115 |
|
|
* This modal dialog will not appear if there is <= one language to select from. |
116 |
|
|
*/ |
117 |
|
|
public void setVisible(boolean b) { |
118 |
|
|
if (b == true && (languages == null || languages.size() <= 1)) { |
119 |
|
|
return; |
120 |
|
|
} |
121 |
|
|
super.setVisible(b); |
122 |
|
|
} |
123 |
alfonx |
244 |
|
124 |
keeb |
1290 |
/** |
125 |
|
|
* This method initializes this |
126 |
|
|
* |
127 |
|
|
* @return void |
128 |
|
|
*/ |
129 |
|
|
private void initialize() { |
130 |
|
|
this.setContentPane(getJContentPane()); |
131 |
alfonx |
244 |
|
132 |
keeb |
1290 |
pack(); |
133 |
alfonx |
1098 |
|
134 |
keeb |
1290 |
SwingUtil.centerFrameOnScreenRandom(this); |
135 |
|
|
setModal(true); |
136 |
|
|
} |
137 |
alfonx |
244 |
|
138 |
keeb |
1290 |
public boolean close() { |
139 |
|
|
// Only close by ESC and window-close if a valid selection is made |
140 |
|
|
if (jComboBox.getSelectedIndex() == languages.size() |
141 |
|
|
|| jComboBox.getSelectedIndex() == -1) |
142 |
|
|
return false; |
143 |
|
|
else |
144 |
|
|
return super.close(); |
145 |
|
|
} |
146 |
alfonx |
607 |
|
147 |
keeb |
1290 |
/** |
148 |
|
|
* This method initializes jContentPane |
149 |
|
|
* |
150 |
|
|
* @return javax.swing.JPanel |
151 |
|
|
*/ |
152 |
|
|
private JPanel getJContentPane() { |
153 |
|
|
if (jContentPane == null) { |
154 |
|
|
jLabelFlagimage = new JLabel(new ImageIcon( |
155 |
|
|
TranslationEditJPanel.class |
156 |
|
|
.getResource("resource/flags.jpg"))); |
157 |
|
|
jContentPane = new JPanel(); |
158 |
|
|
MigLayout migLayout = new MigLayout("wrap 1", "[center]", "[]0[]"); |
159 |
|
|
jContentPane.setLayout(migLayout); |
160 |
|
|
jContentPane.add(jLabelFlagimage, "north"); |
161 |
|
|
jContentPane.add(getLanguageCombobox()); |
162 |
|
|
jContentPane.add(getOkButton(), // AtlasDialog.getOkButton() |
163 |
|
|
"right, w 50!, gaptop 5, gapright 5, gapbottom 5"); |
164 |
|
|
} |
165 |
|
|
return jContentPane; |
166 |
|
|
} |
167 |
alfonx |
244 |
|
168 |
keeb |
1290 |
/** |
169 |
|
|
* This method initializes the JPanel that carries the select language |
170 |
|
|
* combobox |
171 |
|
|
* |
172 |
|
|
* @return javax.swing.JPanel |
173 |
|
|
*/ |
174 |
|
|
private JPanel getLanguageCombobox() { |
175 |
|
|
if (jPanel == null) { |
176 |
|
|
jLabel = new JLabel(); |
177 |
|
|
jLabel.setText("Select language: "); // i8n!?! Maybe replace with an |
178 |
|
|
// icon of an index finger |
179 |
|
|
jPanel = new JPanel(); |
180 |
|
|
jPanel.add(jLabel); |
181 |
|
|
jPanel.add(getJComboBox()); |
182 |
|
|
} |
183 |
|
|
return jPanel; |
184 |
|
|
} |
185 |
alfonx |
1098 |
|
186 |
keeb |
1290 |
/** |
187 |
|
|
* This method initializes the Select Language Combobox |
188 |
|
|
* |
189 |
|
|
* @return javax.swing.JComboBox |
190 |
|
|
*/ |
191 |
|
|
private JComboBox getJComboBox() { |
192 |
|
|
if (jComboBox == null) { |
193 |
|
|
ArrayList<String> languagesPlusOne = new ArrayList<String>( |
194 |
|
|
languages); |
195 |
|
|
languagesPlusOne.add("?"); |
196 |
alfonx |
244 |
|
197 |
keeb |
1290 |
jComboBox = new LanguagesComboBox(languages); |
198 |
alfonx |
244 |
|
199 |
keeb |
1290 |
SwingUtil.addMouseWheelForCombobox(jComboBox); |
200 |
|
|
jComboBox.addActionListener(new ActionListener() { |
201 |
alfonx |
244 |
|
202 |
keeb |
1290 |
public void actionPerformed(final ActionEvent e) { |
203 |
|
|
if (jComboBox.getSelectedIndex() == languages.size()) { |
204 |
|
|
getOkButton().setEnabled(false); |
205 |
|
|
return; |
206 |
|
|
} |
207 |
alfonx |
244 |
|
208 |
keeb |
1290 |
String l = languages.get(jComboBox.getSelectedIndex()); |
209 |
|
|
try { |
210 |
|
|
Translation.setActiveLang(l, setLocale); |
211 |
alfonx |
244 |
|
212 |
keeb |
1290 |
getOkButton().setEnabled(true); |
213 |
|
|
} catch (java.lang.IllegalArgumentException ee) { |
214 |
|
|
LOGGER.warn("The language " + l + " is not valid", ee); |
215 |
|
|
getOkButton().setEnabled(false); |
216 |
|
|
} |
217 |
alfonx |
244 |
|
218 |
keeb |
1290 |
} |
219 |
alfonx |
244 |
|
220 |
keeb |
1290 |
}); |
221 |
|
|
} |
222 |
|
|
return jComboBox; |
223 |
|
|
} |
224 |
alfonx |
244 |
|
225 |
alfonx |
607 |
} |