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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 207 by alfonx, Thu Jul 9 20:14:21 2009 UTC revision 1098 by alfonx, Sun Oct 10 22:52:16 2010 UTC
# Line 1  Line 1 
1  package skrueger.i8n;  /*******************************************************************************
2     * Copyright (c) 2009 Martin O. J. Schmitz.
3  import java.awt.Component;   *
4  import java.awt.GridBagConstraints;   * This file is part of the SCHMITZM library - a collection of utility
5  import java.awt.GridBagLayout;   * classes based on Java 1.6, focusing (not only) on Java Swing
6  import java.awt.Insets;   * and the Geotools library.
7  import java.awt.Window;   *
8  import java.awt.event.ActionEvent;   * The SCHMITZM project is hosted at:
9  import java.awt.event.ActionListener;   * http://wald.intevation.org/projects/schmitzm/
10  import java.awt.event.MouseWheelListener;   *
11  import java.util.List;   * This program is free software; you can redistribute it and/or
12  import java.util.Locale;   * modify it under the terms of the GNU Lesser General Public License
13     * as published by the Free Software Foundation; either version 3
14  import javax.swing.DefaultComboBoxModel;   * of the License, or (at your option) any later version.
15  import javax.swing.ImageIcon;   *
16  import javax.swing.JButton;   * This program is distributed in the hope that it will be useful,
17  import javax.swing.JComboBox;   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  import javax.swing.JDialog;   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  import javax.swing.JLabel;   * GNU General Public License for more details.
20  import javax.swing.JPanel;   *
21     * You should have received a copy of the GNU Lesser General Public License (license.txt)
22  import org.apache.log4j.Logger;   * 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  import schmitzm.swing.SwingUtil;   * or try this link: http://www.gnu.org/licenses/lgpl.html
25  import skrueger.swing.OkButton;   *
26  import skrueger.swing.TranslationEditJPanel;   * Contributors:
27     *     Martin O. J. Schmitz - initial API and implementation
28  /**   *     Stefan A. Tzeggai - additional utility classes
29   * This dialog ask the user to select one of list of given languages. The dialog   ******************************************************************************/
30   * is modal and not visible after construction.  package skrueger.i8n;
31   *  
32   * @author Stefan A. Krueger  import java.awt.Component;
33   */  import java.awt.GridBagConstraints;
34  public class SwitchLanguageDialog extends JDialog {  import java.awt.GridBagLayout;
35          protected Logger LOGGER = Logger.getLogger(SwitchLanguageDialog.class);  import java.awt.Insets;
36    import java.awt.event.ActionEvent;
37          private static final long serialVersionUID = 1L;  import java.awt.event.ActionListener;
38    import java.awt.event.MouseWheelListener;
39          private JPanel jContentPane = null;  import java.util.List;
40    import java.util.Locale;
41          private JLabel jLabelFlagimage = null;  
42    import javax.swing.DefaultComboBoxModel;
43          private JPanel jPanel = null;  import javax.swing.ImageIcon;
44    import javax.swing.JButton;
45          private JButton jButton = null;  import javax.swing.JComboBox;
46    import javax.swing.JLabel;
47          private JPanel jPanel1 = null;  import javax.swing.JPanel;
48    
49          private JLabel jLabel = null;  import org.apache.log4j.Logger;
50    
51          private JComboBox jComboBox = null;  import schmitzm.swing.SwingUtil;
52    import skrueger.swing.AtlasDialog;
53          private final List<String> languages;  import skrueger.swing.OkButton;
54    import skrueger.swing.TranslationEditJPanel;
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   * This dialog ask the user to select one of list of given languages. The dialog
58           * makes it visible.   * is modal and not visible after construction.
59           *   *
60           * @param owner   * @author Stefan A. Tzeggai
61           * @param atlasConfig   */
62           */  public class SwitchLanguageDialog extends AtlasDialog {
63          public SwitchLanguageDialog(final Component owner,          protected Logger LOGGER = Logger.getLogger(SwitchLanguageDialog.class);
64                          final List<String> languages) {  
65                  super(SwingUtil.getParentWindow(owner));          private JPanel jContentPane = null;
66                  this.languages = languages;  
67            private JLabel jLabelFlagimage = null;
68                  Translation.setActiveLang(languages.get(0));  
69            private JPanel jPanel = null;
70                  if (languages.size() == 1) {  
71                          LOGGER.debug("Only language '" + languages.get(0)          private JButton jButton = null;
72                                          + "' is available. It has been selected automatically.");  
73                          return;          private JPanel jPanel1 = null;
74                  }  
75            private JLabel jLabel = null;
76                  initialize();  
77          }          private JComboBox jComboBox = null;
78    
79          /**          private final List<String> languages;
80           * This method initializes this  
81           *          /**
82           * @return void           * if <code>true</code>, the default locale will also be changed during a
83           */           * language selection
84          private void initialize() {           **/
85                  this.setContentPane(getJContentPane());          protected boolean setLocale;
86                  setModal(true);  
87                  SwingUtil.centerFrameOnScreenRandom(this);          /**
88             * A dialog to select one of the available languages. If only one language
89                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);           * is available, select it directly. Creating this object automatically
90             * makes it visible, unless there is only one language to choose from.. it
91                  pack();           * that case it disposes itself automatically.
92          }           *
93             * @param setLocale
94          /**           *            if <code>true</code>, the default locale will also be changed
95           * This method initializes jContentPane           *            during a language selection
96           *           */
97           * @return javax.swing.JPanel          public SwitchLanguageDialog(final Component owner,
98           */                          final List<String> languages, boolean setLocale) {
99          private JPanel getJContentPane() {                  super(owner);
100                  if (jContentPane == null) {                  this.languages = languages;
101                          final GridBagConstraints gridBagConstraints11 = new GridBagConstraints();                  this.setLocale = setLocale;
102                          gridBagConstraints11.gridx = 1;  
103                          gridBagConstraints11.fill = GridBagConstraints.HORIZONTAL;                  if (languages == null || languages.size() == 0) {
104                          gridBagConstraints11.gridy = 1;                          // No language is available.
105                          final GridBagConstraints gridBagConstraints3 = new GridBagConstraints();                          return;
106                          gridBagConstraints3.gridx = 1;                  }
107                          gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;  
108                          gridBagConstraints3.gridy = 2;                  Translation.setActiveLang(languages.get(0), setLocale);
109                          final GridBagConstraints gridBagConstraints1 = new GridBagConstraints();                  if (languages.size() == 1) {
110                          gridBagConstraints1.gridx = 0;                          // Only language one language is available. It has been selected
111                          gridBagConstraints1.fill = GridBagConstraints.BOTH;                          // automatically.
112                          gridBagConstraints1.gridwidth = 2;                          return;
113                          gridBagConstraints1.anchor = GridBagConstraints.NORTH;                  }
114                          gridBagConstraints1.gridy = 0;  
115                          jLabelFlagimage = new JLabel(new ImageIcon(                  initialize();
116                                          TranslationEditJPanel.class          }
117                                                          .getResource("resource/flags.jpg")));  
118                          jContentPane = new JPanel();          @Override
119                          jContentPane.setLayout(new GridBagLayout());          /**
120                          jContentPane.add(jLabelFlagimage, gridBagConstraints1);           * This modal dialog will not appear if there is <= one language to select from.
121                          jContentPane.add(getJPanel(), gridBagConstraints3);           */
122                          jContentPane.add(getJPanel1(), gridBagConstraints11);          public void setVisible(boolean b) {
123                  }                  if (b == true && (languages == null || languages.size() <= 1)) {
124                  return jContentPane;                          return;
125          }                  }
126                    super.setVisible(b);
127          /**          }
128           * This method initializes jPanel  
129           *          /**
130           * @return javax.swing.JPanel           * This method initializes this
131           */           *
132          private JPanel getJPanel() {           * @return void
133                  if (jPanel == null) {           */
134                          final GridBagConstraints gridBagConstraints4 = new GridBagConstraints();          private void initialize() {
135                          gridBagConstraints4.gridx = 0;                  this.setContentPane(getJContentPane());
136                          gridBagConstraints4.anchor = GridBagConstraints.EAST;  
137                          gridBagConstraints4.weightx = 1.0;                  pack();
138                          gridBagConstraints4.insets = new Insets(5, 5, 5, 5);  
139                          gridBagConstraints4.gridy = 0;                  SwingUtil.centerFrameOnScreenRandom(this);
140                          jPanel = new JPanel();                  setModal(true);
141                          jPanel.setLayout(new GridBagLayout());          }
142                          jPanel.add(getJButton(), gridBagConstraints4);  
143                  }          public boolean close() {
144                  return jPanel;                  // 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           * This method initializes jButton                          return super.close();
149           *          }
150           * @return javax.swing.JButton  
151           */          /**
152          private JButton getJButton() {           * This method initializes jContentPane
153                  if (jButton == null) {           *
154                          jButton = new OkButton();           * @return javax.swing.JPanel
155                          jButton.setEnabled(false);           */
156            private JPanel getJContentPane() {
157                          jButton.addActionListener(new ActionListener() {                  if (jContentPane == null) {
158                            final GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
159                                  public void actionPerformed(ActionEvent e) {                          gridBagConstraints11.gridx = 1;
160                                          dispose();                          gridBagConstraints11.fill = GridBagConstraints.HORIZONTAL;
161                                  }                          gridBagConstraints11.gridy = 1;
162                            final GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
163                          });                          gridBagConstraints3.gridx = 1;
164                  }                          gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
165                  return jButton;                          gridBagConstraints3.gridy = 2;
166          }                          final GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
167                            gridBagConstraints1.gridx = 0;
168          /**                          gridBagConstraints1.fill = GridBagConstraints.BOTH;
169           * This method initializes jPanel1                          gridBagConstraints1.gridwidth = 2;
170           *                          gridBagConstraints1.anchor = GridBagConstraints.NORTH;
171           * @return javax.swing.JPanel                          gridBagConstraints1.gridy = 0;
172           */                          jLabelFlagimage = new JLabel(new ImageIcon(
173          private JPanel getJPanel1() {                                          TranslationEditJPanel.class
174                  if (jPanel1 == null) {                                                          .getResource("resource/flags.jpg")));
175                          final GridBagConstraints gridBagConstraints2 = new GridBagConstraints();                          jContentPane = new JPanel();
176                          gridBagConstraints2.fill = GridBagConstraints.VERTICAL;                          jContentPane.setLayout(new GridBagLayout());
177                          gridBagConstraints2.gridy = 0;                          jContentPane.add(jLabelFlagimage, gridBagConstraints1);
178                          gridBagConstraints2.weightx = 1.0;                          jContentPane.add(getJPanel(), gridBagConstraints3);
179                          gridBagConstraints2.insets = new Insets(5, 5, 5, 5);                          jContentPane.add(getJPanel1(), gridBagConstraints11);
180                          gridBagConstraints2.anchor = GridBagConstraints.WEST;                  }
181                          gridBagConstraints2.gridx = 1;                  return jContentPane;
182                          final GridBagConstraints gridBagConstraints = new GridBagConstraints();          }
183                          gridBagConstraints.gridx = 0;  
184                          gridBagConstraints.insets = new Insets(0, 5, 0, 0);          /**
185                          gridBagConstraints.gridy = 0;           * This method initializes jPanel
186                          jLabel = new JLabel();           *
187                          jLabel.setText("Select language: "); // i8n!?! Maybe replace with an           * @return javax.swing.JPanel
188                                                                                                          // icon of an index finger           */
189                          jPanel1 = new JPanel();          private JPanel getJPanel() {
190                          jPanel1.setLayout(new GridBagLayout());                  if (jPanel == null) {
191                          jPanel1.add(jLabel, gridBagConstraints);                          final GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
192                          jPanel1.add(getJComboBox(), gridBagConstraints2);                          gridBagConstraints4.gridx = 0;
193                  }                          gridBagConstraints4.anchor = GridBagConstraints.EAST;
194                  return jPanel1;                          gridBagConstraints4.weightx = 1.0;
195          }                          gridBagConstraints4.insets = new Insets(5, 5, 5, 5);
196                            gridBagConstraints4.gridy = 0;
197          /**                          jPanel = new JPanel();
198           * This method initializes jComboBox                          jPanel.setLayout(new GridBagLayout());
199           *                          jPanel.add(getJButton(), gridBagConstraints4);
200           * @return javax.swing.JComboBox                  }
201           */                  return jPanel;
202          private JComboBox getJComboBox() {          }
203                  if (jComboBox == null) {  
204                          jComboBox = new JComboBox();          /**
205             * This method initializes jButton
206                          jComboBox.addMouseWheelListener(new MouseWheelListener() {           *
207                                  public void mouseWheelMoved(java.awt.event.MouseWheelEvent e) {           * @return javax.swing.JButton
208             */
209                                          if ((e.getWheelRotation() < 0)) {          private JButton getJButton() {
210                                                  if (jComboBox.getSelectedIndex() < jComboBox                  if (jButton == null) {
211                                                                  .getItemCount() - 1)                          jButton = new OkButton();
212                                                          jComboBox.setSelectedIndex(jComboBox                          jButton.setEnabled(false);
213                                                                          .getSelectedIndex() + 1);  
214                                          } else {                          jButton.addActionListener(new ActionListener() {
215                                                  if (jComboBox.getSelectedIndex() > 0)  
216                                                          jComboBox.setSelectedIndex(jComboBox                                  public void actionPerformed(ActionEvent e) {
217                                                                          .getSelectedIndex() - 1);                                          dispose();
218                                          }                                  }
219                                  }  
220                          });                          });
221                    }
222                          String[] langNames = new String[languages.size() + 1];                  return jButton;
223                          for (int i = 0; i < languages.size(); i++) {          }
224    
225                                  Locale locale = I8NUtil.getLocaleFor(languages.get(i));          /**
226             * This method initializes jPanel1
227                                  langNames[i] = locale.getDisplayLanguage(locale) + " / "           *
228                                                  + locale.getDisplayLanguage() + " / "           * @return javax.swing.JPanel
229                                                  + languages.get(i);           */
230                          }          private JPanel getJPanel1() {
231                          langNames[languages.size()] = "?";                  if (jPanel1 == null) {
232                            final GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
233                          jComboBox.setModel(new DefaultComboBoxModel(langNames));                          gridBagConstraints2.fill = GridBagConstraints.VERTICAL;
234                          jComboBox.setSelectedItem(langNames[languages.size()]);                          gridBagConstraints2.gridy = 0;
235                            gridBagConstraints2.weightx = 1.0;
236                          jComboBox.addActionListener(new ActionListener() {                          gridBagConstraints2.insets = new Insets(5, 5, 5, 5);
237                            gridBagConstraints2.anchor = GridBagConstraints.WEST;
238                                  public void actionPerformed(final ActionEvent e) {                          gridBagConstraints2.gridx = 1;
239                                          if (jComboBox.getSelectedIndex() == languages.size()) {                          final GridBagConstraints gridBagConstraints = new GridBagConstraints();
240                                                  getJButton().setEnabled(false);                          gridBagConstraints.gridx = 0;
241                                                  return;                          gridBagConstraints.insets = new Insets(0, 5, 0, 0);
242                                          }                          gridBagConstraints.gridy = 0;
243                            jLabel = new JLabel();
244                                          String l = languages.get(jComboBox.getSelectedIndex());                          jLabel.setText("Select language: "); // i8n!?! Maybe replace with an
245                                          try {                          // icon of an index finger
246                                                  Translation.setActiveLang(l);                          jPanel1 = new JPanel();
247                            jPanel1.setLayout(new GridBagLayout());
248                                                  getJButton().setEnabled(true);                          jPanel1.add(jLabel, gridBagConstraints);
249                                          } catch (java.lang.IllegalArgumentException ee) {                          jPanel1.add(getJComboBox(), gridBagConstraints2);
250                                                  LOGGER.warn("The language " + l + " is not valid", ee);                  }
251                                                  getJButton().setEnabled(false);                  return jPanel1;
252                                          }          }
253    
254                                  }          /**
255             * This method initializes jComboBox
256                          });           *
257                  }           * @return javax.swing.JComboBox
258                  return jComboBox;           */
259          }          private JComboBox getJComboBox() {
260                    if (jComboBox == null) {
261  } // @jve:decl-index=0:visual-constraint="0,0"                          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                                    Locale locale = I8NUtil.getLocaleFor(languages.get(i));
283    
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                                                    Translation.setActiveLang(l, setLocale);
304    
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    }

Legend:
Removed from v.207  
changed lines
  Added in v.1098

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26