/[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 256 by alfonx, Fri Jul 31 14:43:47 2009 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. Krüger - 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.Window;
37          private static final long serialVersionUID = 1L;  import java.awt.event.ActionEvent;
38    import java.awt.event.ActionListener;
39          private JPanel jContentPane = null;  import java.awt.event.MouseWheelListener;
40    import java.util.List;
41          private JLabel jLabelFlagimage = null;  import java.util.Locale;
42    
43          private JPanel jPanel = null;  import javax.swing.DefaultComboBoxModel;
44    import javax.swing.ImageIcon;
45          private JButton jButton = null;  import javax.swing.JButton;
46    import javax.swing.JComboBox;
47          private JPanel jPanel1 = null;  import javax.swing.JDialog;
48    import javax.swing.JLabel;
49          private JLabel jLabel = null;  import javax.swing.JPanel;
50    
51          private JComboBox jComboBox = null;  import org.apache.log4j.Logger;
52    
53          private final List<String> languages;  import schmitzm.swing.SwingUtil;
54    import skrueger.swing.OkButton;
55          /**  import skrueger.swing.TranslationEditJPanel;
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.   * This dialog ask the user to select one of list of given languages. The dialog
59           *   * is modal and not visible after construction.
60           * @param owner   *
61           * @param atlasConfig   * @author Stefan A. Krueger
62           */   */
63          public SwitchLanguageDialog(final Component owner,  public class SwitchLanguageDialog extends JDialog {
64                          final List<String> languages) {          protected Logger LOGGER = Logger.getLogger(SwitchLanguageDialog.class);
65                  super(SwingUtil.getParentWindow(owner));  
66                  this.languages = languages;          private static final long serialVersionUID = 1L;
67    
68                  Translation.setActiveLang(languages.get(0));          private JPanel jContentPane = null;
69    
70                  if (languages.size() == 1) {          private JLabel jLabelFlagimage = null;
71                          LOGGER.debug("Only language '" + languages.get(0)  
72                                          + "' is available. It has been selected automatically.");          private JPanel jPanel = null;
73                          return;  
74                  }          private JButton jButton = null;
75    
76                  initialize();          private JPanel jPanel1 = null;
77          }  
78            private JLabel jLabel = null;
79          /**  
80           * This method initializes this          private JComboBox jComboBox = null;
81           *  
82           * @return void          private final List<String> languages;
83           */  
84          private void initialize() {          /**
85                  this.setContentPane(getJContentPane());           * A dialog to select one of the available languages. If only one language
86                  setModal(true);           * is available, select it directly. Creating this object automatically
87                  SwingUtil.centerFrameOnScreenRandom(this);           * makes it visible.
88             *
89                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);           * @param owner
90             * @param atlasConfig
91                  pack();           */
92          }          public SwitchLanguageDialog(final Component owner,
93                            final List<String> languages) {
94          /**                  super(SwingUtil.getParentWindow(owner));
95           * This method initializes jContentPane                  this.languages = languages;
96           *  
97           * @return javax.swing.JPanel                  Translation.setActiveLang(languages.get(0));
98           */  
99          private JPanel getJContentPane() {                  if (languages.size() == 1) {
100                  if (jContentPane == null) {                          LOGGER.debug("Only language '" + languages.get(0)
101                          final GridBagConstraints gridBagConstraints11 = new GridBagConstraints();                                          + "' is available. It has been selected automatically.");
102                          gridBagConstraints11.gridx = 1;                          return;
103                          gridBagConstraints11.fill = GridBagConstraints.HORIZONTAL;                  }
104                          gridBagConstraints11.gridy = 1;  
105                          final GridBagConstraints gridBagConstraints3 = new GridBagConstraints();                  initialize();
106                          gridBagConstraints3.gridx = 1;          }
107                          gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;  
108                          gridBagConstraints3.gridy = 2;          /**
109                          final GridBagConstraints gridBagConstraints1 = new GridBagConstraints();           * This method initializes this
110                          gridBagConstraints1.gridx = 0;           *
111                          gridBagConstraints1.fill = GridBagConstraints.BOTH;           * @return void
112                          gridBagConstraints1.gridwidth = 2;           */
113                          gridBagConstraints1.anchor = GridBagConstraints.NORTH;          private void initialize() {
114                          gridBagConstraints1.gridy = 0;                  this.setContentPane(getJContentPane());
115                          jLabelFlagimage = new JLabel(new ImageIcon(                  setModal(true);
116                                          TranslationEditJPanel.class                  SwingUtil.centerFrameOnScreenRandom(this);
117                                                          .getResource("resource/flags.jpg")));  
118                          jContentPane = new JPanel();                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
119                          jContentPane.setLayout(new GridBagLayout());  
120                          jContentPane.add(jLabelFlagimage, gridBagConstraints1);                  pack();
121                          jContentPane.add(getJPanel(), gridBagConstraints3);          }
122                          jContentPane.add(getJPanel1(), gridBagConstraints11);  
123                  }          /**
124                  return jContentPane;           * This method initializes jContentPane
125          }           *
126             * @return javax.swing.JPanel
127          /**           */
128           * This method initializes jPanel          private JPanel getJContentPane() {
129           *                  if (jContentPane == null) {
130           * @return javax.swing.JPanel                          final GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
131           */                          gridBagConstraints11.gridx = 1;
132          private JPanel getJPanel() {                          gridBagConstraints11.fill = GridBagConstraints.HORIZONTAL;
133                  if (jPanel == null) {                          gridBagConstraints11.gridy = 1;
134                          final GridBagConstraints gridBagConstraints4 = new GridBagConstraints();                          final GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
135                          gridBagConstraints4.gridx = 0;                          gridBagConstraints3.gridx = 1;
136                          gridBagConstraints4.anchor = GridBagConstraints.EAST;                          gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
137                          gridBagConstraints4.weightx = 1.0;                          gridBagConstraints3.gridy = 2;
138                          gridBagConstraints4.insets = new Insets(5, 5, 5, 5);                          final GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
139                          gridBagConstraints4.gridy = 0;                          gridBagConstraints1.gridx = 0;
140                          jPanel = new JPanel();                          gridBagConstraints1.fill = GridBagConstraints.BOTH;
141                          jPanel.setLayout(new GridBagLayout());                          gridBagConstraints1.gridwidth = 2;
142                          jPanel.add(getJButton(), gridBagConstraints4);                          gridBagConstraints1.anchor = GridBagConstraints.NORTH;
143                  }                          gridBagConstraints1.gridy = 0;
144                  return jPanel;                          jLabelFlagimage = new JLabel(new ImageIcon(
145          }                                          TranslationEditJPanel.class
146                                                            .getResource("resource/flags.jpg")));
147          /**                          jContentPane = new JPanel();
148           * This method initializes jButton                          jContentPane.setLayout(new GridBagLayout());
149           *                          jContentPane.add(jLabelFlagimage, gridBagConstraints1);
150           * @return javax.swing.JButton                          jContentPane.add(getJPanel(), gridBagConstraints3);
151           */                          jContentPane.add(getJPanel1(), gridBagConstraints11);
152          private JButton getJButton() {                  }
153                  if (jButton == null) {                  return jContentPane;
154                          jButton = new OkButton();          }
155                          jButton.setEnabled(false);  
156            /**
157                          jButton.addActionListener(new ActionListener() {           * This method initializes jPanel
158             *
159                                  public void actionPerformed(ActionEvent e) {           * @return javax.swing.JPanel
160                                          dispose();           */
161                                  }          private JPanel getJPanel() {
162                    if (jPanel == null) {
163                          });                          final GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
164                  }                          gridBagConstraints4.gridx = 0;
165                  return jButton;                          gridBagConstraints4.anchor = GridBagConstraints.EAST;
166          }                          gridBagConstraints4.weightx = 1.0;
167                            gridBagConstraints4.insets = new Insets(5, 5, 5, 5);
168          /**                          gridBagConstraints4.gridy = 0;
169           * This method initializes jPanel1                          jPanel = new JPanel();
170           *                          jPanel.setLayout(new GridBagLayout());
171           * @return javax.swing.JPanel                          jPanel.add(getJButton(), gridBagConstraints4);
172           */                  }
173          private JPanel getJPanel1() {                  return jPanel;
174                  if (jPanel1 == null) {          }
175                          final GridBagConstraints gridBagConstraints2 = new GridBagConstraints();  
176                          gridBagConstraints2.fill = GridBagConstraints.VERTICAL;          /**
177                          gridBagConstraints2.gridy = 0;           * This method initializes jButton
178                          gridBagConstraints2.weightx = 1.0;           *
179                          gridBagConstraints2.insets = new Insets(5, 5, 5, 5);           * @return javax.swing.JButton
180                          gridBagConstraints2.anchor = GridBagConstraints.WEST;           */
181                          gridBagConstraints2.gridx = 1;          private JButton getJButton() {
182                          final GridBagConstraints gridBagConstraints = new GridBagConstraints();                  if (jButton == null) {
183                          gridBagConstraints.gridx = 0;                          jButton = new OkButton();
184                          gridBagConstraints.insets = new Insets(0, 5, 0, 0);                          jButton.setEnabled(false);
185                          gridBagConstraints.gridy = 0;  
186                          jLabel = new JLabel();                          jButton.addActionListener(new ActionListener() {
187                          jLabel.setText("Select language: "); // i8n!?! Maybe replace with an  
188                                                                                                          // icon of an index finger                                  public void actionPerformed(ActionEvent e) {
189                          jPanel1 = new JPanel();                                          dispose();
190                          jPanel1.setLayout(new GridBagLayout());                                  }
191                          jPanel1.add(jLabel, gridBagConstraints);  
192                          jPanel1.add(getJComboBox(), gridBagConstraints2);                          });
193                  }                  }
194                  return jPanel1;                  return jButton;
195          }          }
196    
197          /**          /**
198           * This method initializes jComboBox           * This method initializes jPanel1
199           *           *
200           * @return javax.swing.JComboBox           * @return javax.swing.JPanel
201           */           */
202          private JComboBox getJComboBox() {          private JPanel getJPanel1() {
203                  if (jComboBox == null) {                  if (jPanel1 == null) {
204                          jComboBox = new JComboBox();                          final GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
205                            gridBagConstraints2.fill = GridBagConstraints.VERTICAL;
206                          jComboBox.addMouseWheelListener(new MouseWheelListener() {                          gridBagConstraints2.gridy = 0;
207                                  public void mouseWheelMoved(java.awt.event.MouseWheelEvent e) {                          gridBagConstraints2.weightx = 1.0;
208                            gridBagConstraints2.insets = new Insets(5, 5, 5, 5);
209                                          if ((e.getWheelRotation() < 0)) {                          gridBagConstraints2.anchor = GridBagConstraints.WEST;
210                                                  if (jComboBox.getSelectedIndex() < jComboBox                          gridBagConstraints2.gridx = 1;
211                                                                  .getItemCount() - 1)                          final GridBagConstraints gridBagConstraints = new GridBagConstraints();
212                                                          jComboBox.setSelectedIndex(jComboBox                          gridBagConstraints.gridx = 0;
213                                                                          .getSelectedIndex() + 1);                          gridBagConstraints.insets = new Insets(0, 5, 0, 0);
214                                          } else {                          gridBagConstraints.gridy = 0;
215                                                  if (jComboBox.getSelectedIndex() > 0)                          jLabel = new JLabel();
216                                                          jComboBox.setSelectedIndex(jComboBox                          jLabel.setText("Select language: "); // i8n!?! Maybe replace with an
217                                                                          .getSelectedIndex() - 1);                                                                                                          // icon of an index finger
218                                          }                          jPanel1 = new JPanel();
219                                  }                          jPanel1.setLayout(new GridBagLayout());
220                          });                          jPanel1.add(jLabel, gridBagConstraints);
221                            jPanel1.add(getJComboBox(), gridBagConstraints2);
222                          String[] langNames = new String[languages.size() + 1];                  }
223                          for (int i = 0; i < languages.size(); i++) {                  return jPanel1;
224            }
225                                  Locale locale = I8NUtil.getLocaleFor(languages.get(i));  
226            /**
227                                  langNames[i] = locale.getDisplayLanguage(locale) + " / "           * This method initializes jComboBox
228                                                  + locale.getDisplayLanguage() + " / "           *
229                                                  + languages.get(i);           * @return javax.swing.JComboBox
230                          }           */
231                          langNames[languages.size()] = "?";          private JComboBox getJComboBox() {
232                    if (jComboBox == null) {
233                          jComboBox.setModel(new DefaultComboBoxModel(langNames));                          jComboBox = new JComboBox();
234                          jComboBox.setSelectedItem(langNames[languages.size()]);  
235                            jComboBox.addMouseWheelListener(new MouseWheelListener() {
236                          jComboBox.addActionListener(new ActionListener() {                                  public void mouseWheelMoved(java.awt.event.MouseWheelEvent e) {
237    
238                                  public void actionPerformed(final ActionEvent e) {                                          if ((e.getWheelRotation() < 0)) {
239                                          if (jComboBox.getSelectedIndex() == languages.size()) {                                                  if (jComboBox.getSelectedIndex() < jComboBox
240                                                  getJButton().setEnabled(false);                                                                  .getItemCount() - 1)
241                                                  return;                                                          jComboBox.setSelectedIndex(jComboBox
242                                          }                                                                          .getSelectedIndex() + 1);
243                                            } else {
244                                          String l = languages.get(jComboBox.getSelectedIndex());                                                  if (jComboBox.getSelectedIndex() > 0)
245                                          try {                                                          jComboBox.setSelectedIndex(jComboBox
246                                                  Translation.setActiveLang(l);                                                                          .getSelectedIndex() - 1);
247                                            }
248                                                  getJButton().setEnabled(true);                                  }
249                                          } catch (java.lang.IllegalArgumentException ee) {                          });
250                                                  LOGGER.warn("The language " + l + " is not valid", ee);  
251                                                  getJButton().setEnabled(false);                          String[] langNames = new String[languages.size() + 1];
252                                          }                          for (int i = 0; i < languages.size(); i++) {
253    
254                                  }                                  Locale locale = I8NUtil.getLocaleFor(languages.get(i));
255    
256                          });                                  langNames[i] = locale.getDisplayLanguage(locale) + " / "
257                  }                                                  + locale.getDisplayLanguage() + " / "
258                  return jComboBox;                                                  + languages.get(i);
259          }                          }
260                            langNames[languages.size()] = "?";
261  } // @jve:decl-index=0:visual-constraint="0,0"  
262                            jComboBox.setModel(new DefaultComboBoxModel(langNames));
263                            jComboBox.setSelectedItem(langNames[languages.size()]);
264    
265                            jComboBox.addActionListener(new ActionListener() {
266    
267                                    public void actionPerformed(final ActionEvent e) {
268                                            if (jComboBox.getSelectedIndex() == languages.size()) {
269                                                    getJButton().setEnabled(false);
270                                                    return;
271                                            }
272    
273                                            String l = languages.get(jComboBox.getSelectedIndex());
274                                            try {
275                                                    Translation.setActiveLang(l);
276    
277                                                    getJButton().setEnabled(true);
278                                            } catch (java.lang.IllegalArgumentException ee) {
279                                                    LOGGER.warn("The language " + l + " is not valid", ee);
280                                                    getJButton().setEnabled(false);
281                                            }
282    
283                                    }
284    
285                            });
286                    }
287                    return jComboBox;
288            }
289    
290    } // @jve:decl-index=0:visual-constraint="0,0"

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26