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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26