/[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

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

Legend:
Removed from v.124  
changed lines
  Added in v.621

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26