/[schmitzm]/trunk/src/skrueger/i8n/SwitchLanguageDialog.java
ViewVC logotype

Annotation of /trunk/src/skrueger/i8n/SwitchLanguageDialog.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1100 - (hide annotations)
Mon Oct 11 00:07:14 2010 UTC (14 years, 4 months ago) by alfonx
File size: 9351 byte(s)
Geopublisher can now switch languages for the GUI and the atlas data mostly independent.
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     import java.awt.event.MouseWheelListener;
39     import java.util.List;
40     import java.util.Locale;
41    
42     import javax.swing.DefaultComboBoxModel;
43     import javax.swing.ImageIcon;
44     import javax.swing.JButton;
45     import javax.swing.JComboBox;
46     import javax.swing.JLabel;
47     import javax.swing.JPanel;
48    
49     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 alfonx 244 protected Logger LOGGER = Logger.getLogger(SwitchLanguageDialog.class);
64    
65     private JPanel jContentPane = null;
66    
67     private JLabel jLabelFlagimage = null;
68    
69     private JPanel jPanel = null;
70    
71     private JButton jButton = null;
72    
73     private JPanel jPanel1 = null;
74    
75     private JLabel jLabel = null;
76    
77     private JComboBox jComboBox = null;
78    
79     private final List<String> languages;
80    
81     /**
82 alfonx 1098 * if <code>true</code>, the default locale will also be changed during a
83     * language selection
84     **/
85     protected boolean setLocale;
86    
87     /**
88 alfonx 244 * A dialog to select one of the available languages. If only one language
89     * is available, select it directly. Creating this object automatically
90 alfonx 607 * makes it visible, unless there is only one language to choose from.. it
91 alfonx 724 * that case it disposes itself automatically.
92 alfonx 1098 *
93     * @param setLocale
94     * if <code>true</code>, the default locale will also be changed
95     * during a language selection
96 alfonx 244 */
97     public SwitchLanguageDialog(final Component owner,
98 alfonx 1098 final List<String> languages, boolean setLocale) {
99     super(owner);
100 alfonx 244 this.languages = languages;
101 alfonx 1098 this.setLocale = setLocale;
102 alfonx 244
103 alfonx 1098 if (languages == null || languages.size() == 0) {
104     // No language is available.
105     return;
106     }
107 alfonx 244
108 alfonx 1098 Translation.setActiveLang(languages.get(0), setLocale);
109 alfonx 244 if (languages.size() == 1) {
110 alfonx 1098 // Only language one language is available. It has been selected
111     // automatically.
112 alfonx 244 return;
113     }
114    
115     initialize();
116     }
117    
118 alfonx 1098 @Override
119 alfonx 244 /**
120 alfonx 1098 * 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 alfonx 244 * This method initializes this
131     *
132     * @return void
133     */
134     private void initialize() {
135     this.setContentPane(getJContentPane());
136    
137     pack();
138 alfonx 607
139     SwingUtil.centerFrameOnScreenRandom(this);
140     setModal(true);
141 alfonx 244 }
142    
143 alfonx 1098 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 alfonx 244 /**
152     * This method initializes jContentPane
153     *
154     * @return javax.swing.JPanel
155     */
156     private JPanel getJContentPane() {
157     if (jContentPane == null) {
158     final GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
159     gridBagConstraints11.gridx = 1;
160     gridBagConstraints11.fill = GridBagConstraints.HORIZONTAL;
161     gridBagConstraints11.gridy = 1;
162     final GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
163     gridBagConstraints3.gridx = 1;
164     gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
165     gridBagConstraints3.gridy = 2;
166     final GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
167     gridBagConstraints1.gridx = 0;
168     gridBagConstraints1.fill = GridBagConstraints.BOTH;
169     gridBagConstraints1.gridwidth = 2;
170     gridBagConstraints1.anchor = GridBagConstraints.NORTH;
171     gridBagConstraints1.gridy = 0;
172     jLabelFlagimage = new JLabel(new ImageIcon(
173     TranslationEditJPanel.class
174     .getResource("resource/flags.jpg")));
175     jContentPane = new JPanel();
176     jContentPane.setLayout(new GridBagLayout());
177     jContentPane.add(jLabelFlagimage, gridBagConstraints1);
178     jContentPane.add(getJPanel(), gridBagConstraints3);
179     jContentPane.add(getJPanel1(), gridBagConstraints11);
180     }
181     return jContentPane;
182     }
183    
184     /**
185     * This method initializes jPanel
186     *
187     * @return javax.swing.JPanel
188     */
189     private JPanel getJPanel() {
190     if (jPanel == null) {
191     final GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
192     gridBagConstraints4.gridx = 0;
193     gridBagConstraints4.anchor = GridBagConstraints.EAST;
194     gridBagConstraints4.weightx = 1.0;
195     gridBagConstraints4.insets = new Insets(5, 5, 5, 5);
196     gridBagConstraints4.gridy = 0;
197     jPanel = new JPanel();
198     jPanel.setLayout(new GridBagLayout());
199     jPanel.add(getJButton(), gridBagConstraints4);
200     }
201     return jPanel;
202     }
203    
204     /**
205     * This method initializes jButton
206     *
207     * @return javax.swing.JButton
208     */
209     private JButton getJButton() {
210     if (jButton == null) {
211     jButton = new OkButton();
212     jButton.setEnabled(false);
213    
214     jButton.addActionListener(new ActionListener() {
215    
216     public void actionPerformed(ActionEvent e) {
217     dispose();
218     }
219    
220     });
221     }
222     return jButton;
223     }
224    
225     /**
226     * This method initializes jPanel1
227     *
228     * @return javax.swing.JPanel
229     */
230     private JPanel getJPanel1() {
231     if (jPanel1 == null) {
232     final GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
233     gridBagConstraints2.fill = GridBagConstraints.VERTICAL;
234     gridBagConstraints2.gridy = 0;
235     gridBagConstraints2.weightx = 1.0;
236     gridBagConstraints2.insets = new Insets(5, 5, 5, 5);
237     gridBagConstraints2.anchor = GridBagConstraints.WEST;
238     gridBagConstraints2.gridx = 1;
239     final GridBagConstraints gridBagConstraints = new GridBagConstraints();
240     gridBagConstraints.gridx = 0;
241     gridBagConstraints.insets = new Insets(0, 5, 0, 0);
242     gridBagConstraints.gridy = 0;
243     jLabel = new JLabel();
244     jLabel.setText("Select language: "); // i8n!?! Maybe replace with an
245 alfonx 607 // icon of an index finger
246 alfonx 244 jPanel1 = new JPanel();
247     jPanel1.setLayout(new GridBagLayout());
248     jPanel1.add(jLabel, gridBagConstraints);
249     jPanel1.add(getJComboBox(), gridBagConstraints2);
250     }
251     return jPanel1;
252     }
253    
254     /**
255     * This method initializes jComboBox
256     *
257     * @return javax.swing.JComboBox
258     */
259     private JComboBox getJComboBox() {
260     if (jComboBox == null) {
261     jComboBox = new JComboBox();
262    
263     jComboBox.addMouseWheelListener(new MouseWheelListener() {
264     public void mouseWheelMoved(java.awt.event.MouseWheelEvent e) {
265    
266     if ((e.getWheelRotation() < 0)) {
267     if (jComboBox.getSelectedIndex() < jComboBox
268     .getItemCount() - 1)
269     jComboBox.setSelectedIndex(jComboBox
270     .getSelectedIndex() + 1);
271     } else {
272     if (jComboBox.getSelectedIndex() > 0)
273     jComboBox.setSelectedIndex(jComboBox
274     .getSelectedIndex() - 1);
275     }
276     }
277     });
278    
279     String[] langNames = new String[languages.size() + 1];
280     for (int i = 0; i < languages.size(); i++) {
281    
282 alfonx 1100 Locale locale = I8NUtil.getFirstLocaleForLang(languages.get(i));
283 alfonx 244
284     langNames[i] = locale.getDisplayLanguage(locale) + " / "
285     + locale.getDisplayLanguage() + " / "
286     + languages.get(i);
287     }
288     langNames[languages.size()] = "?";
289    
290     jComboBox.setModel(new DefaultComboBoxModel(langNames));
291     jComboBox.setSelectedItem(langNames[languages.size()]);
292    
293     jComboBox.addActionListener(new ActionListener() {
294    
295     public void actionPerformed(final ActionEvent e) {
296     if (jComboBox.getSelectedIndex() == languages.size()) {
297     getJButton().setEnabled(false);
298     return;
299     }
300    
301     String l = languages.get(jComboBox.getSelectedIndex());
302     try {
303 alfonx 1098 Translation.setActiveLang(l, setLocale);
304 alfonx 244
305     getJButton().setEnabled(true);
306     } catch (java.lang.IllegalArgumentException ee) {
307     LOGGER.warn("The language " + l + " is not valid", ee);
308     getJButton().setEnabled(false);
309     }
310    
311     }
312    
313     });
314     }
315     return jComboBox;
316     }
317    
318 alfonx 607 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26