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

Legend:
Removed from v.140  
changed lines
  Added in v.1097

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26