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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26