/[schmitzm]/branches/2.4.x/src/skrueger/i8n/SwitchLanguageDialog.java
ViewVC logotype

Annotation of /branches/2.4.x/src/skrueger/i8n/SwitchLanguageDialog.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1294 - (hide annotations)
Mon Nov 22 12:00:28 2010 UTC (14 years, 3 months ago) by keeb
Original Path: trunk/src/skrueger/i8n/SwitchLanguageDialog.java
File size: 7699 byte(s)
refactored method such that it overrides getOkButton from superclass for convenience
1 alfonx 244 /*******************************************************************************
2     * Copyright (c) 2009 Martin O. J. Schmitz.
3     *
4     * This file is part of the SCHMITZM library - a collection of utility
5 alfonx 256 * classes based on Java 1.6, focusing (not only) on Java Swing
6 alfonx 244 * and the Geotools library.
7     *
8     * The SCHMITZM project is hosted at:
9     * http://wald.intevation.org/projects/schmitzm/
10     *
11     * This program is free software; you can redistribute it and/or
12     * modify it under the terms of the GNU Lesser General Public License
13     * as published by the Free Software Foundation; either version 3
14     * of the License, or (at your option) any later version.
15     *
16     * This program is distributed in the hope that it will be useful,
17     * but WITHOUT ANY WARRANTY; without even the implied warranty of
18     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19     * GNU General Public License for more details.
20     *
21     * You should have received a copy of the GNU Lesser General Public License (license.txt)
22     * 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     * or try this link: http://www.gnu.org/licenses/lgpl.html
25     *
26     * Contributors:
27     * Martin O. J. Schmitz - initial API and implementation
28 alfonx 862 * Stefan A. Tzeggai - additional utility classes
29 alfonx 244 ******************************************************************************/
30     package skrueger.i8n;
31    
32     import java.awt.Component;
33     import java.awt.GridBagConstraints;
34     import java.awt.GridBagLayout;
35     import java.awt.Insets;
36     import java.awt.event.ActionEvent;
37     import java.awt.event.ActionListener;
38 alfonx 1150 import java.util.ArrayList;
39 alfonx 244 import java.util.List;
40    
41     import javax.swing.ImageIcon;
42     import javax.swing.JButton;
43     import javax.swing.JComboBox;
44     import javax.swing.JLabel;
45     import javax.swing.JPanel;
46    
47 keeb 1290 import net.miginfocom.swing.MigLayout;
48    
49 alfonx 244 import org.apache.log4j.Logger;
50    
51     import schmitzm.swing.SwingUtil;
52 alfonx 1098 import skrueger.swing.AtlasDialog;
53 alfonx 244 import skrueger.swing.OkButton;
54     import skrueger.swing.TranslationEditJPanel;
55    
56     /**
57     * This dialog ask the user to select one of list of given languages. The dialog
58     * is modal and not visible after construction.
59     *
60 alfonx 1098 * @author Stefan A. Tzeggai
61 alfonx 244 */
62 alfonx 1098 public class SwitchLanguageDialog extends AtlasDialog {
63 keeb 1290 protected Logger LOGGER = Logger.getLogger(SwitchLanguageDialog.class);
64 alfonx 244
65 keeb 1290 private JPanel jContentPane = null;
66 alfonx 244
67 keeb 1290 private JLabel jLabelFlagimage = null;
68 alfonx 244
69 keeb 1290 private JPanel jPanel = null;
70 alfonx 244
71 keeb 1290 private JLabel jLabel = null;
72 keeb 1293
73 keeb 1294 private OkButton jButton = null;
74 alfonx 244
75 keeb 1290 private LanguagesComboBox jComboBox = null;
76 alfonx 244
77 keeb 1290 private final List<String> languages;
78 alfonx 244
79 keeb 1290 /**
80     * if <code>true</code>, the default locale will also be changed during a
81     * language selection
82     **/
83     protected boolean setLocale;
84 alfonx 244
85 keeb 1290 /**
86     * A dialog to select one of the available languages. If only one language
87     * is available, select it directly. Creating this object automatically
88     * makes it visible, unless there is only one language to choose from.. it
89     * that case it disposes itself automatically.
90     *
91     * @param setLocale if <code>true</code>, the default locale will also be
92     * changed during a language selection
93     */
94     public SwitchLanguageDialog(final Component owner,
95     final List<String> languages, boolean setLocale) {
96     super(owner);
97     this.languages = languages;
98     this.setLocale = setLocale;
99 alfonx 244
100 keeb 1290 if (languages == null || languages.size() == 0) {
101     // No language is available.
102     return;
103     }
104 alfonx 1098
105 keeb 1290 Translation.setActiveLang(languages.get(0), setLocale);
106     if (languages.size() == 1) {
107     // Only language one language is available. It has been selected
108     // automatically.
109     return;
110     }
111 alfonx 244
112 keeb 1290 initialize();
113     }
114 alfonx 244
115 keeb 1290 @Override
116     /**
117     * This modal dialog will not appear if there is <= one language to select from.
118     */
119     public void setVisible(boolean b) {
120     if (b == true && (languages == null || languages.size() <= 1)) {
121     return;
122     }
123     super.setVisible(b);
124     }
125 alfonx 244
126 keeb 1290 /**
127     * This method initializes this
128     *
129     * @return void
130     */
131     private void initialize() {
132     this.setContentPane(getJContentPane());
133 alfonx 244
134 keeb 1290 pack();
135 alfonx 1098
136 keeb 1290 SwingUtil.centerFrameOnScreenRandom(this);
137     setModal(true);
138     }
139 alfonx 244
140 keeb 1290 public boolean close() {
141     // Only close by ESC and window-close if a valid selection is made
142     if (jComboBox.getSelectedIndex() == languages.size()
143     || jComboBox.getSelectedIndex() == -1)
144     return false;
145     else
146     return super.close();
147     }
148 alfonx 607
149 keeb 1290 /**
150     * This method initializes jContentPane
151     *
152     * @return javax.swing.JPanel
153     */
154     private JPanel getJContentPane() {
155     if (jContentPane == null) {
156     jLabelFlagimage = new JLabel(new ImageIcon(
157     TranslationEditJPanel.class
158     .getResource("resource/flags.jpg")));
159     jContentPane = new JPanel();
160     MigLayout migLayout = new MigLayout("wrap 1", "[center]", "[]0[]");
161     jContentPane.setLayout(migLayout);
162     jContentPane.add(jLabelFlagimage, "north");
163     jContentPane.add(getLanguageCombobox());
164 keeb 1294 jContentPane.add(getOkButton(),
165 keeb 1290 "right, w 50!, gaptop 5, gapright 5, gapbottom 5");
166     }
167     return jContentPane;
168     }
169 keeb 1293
170     /**
171     * This method initializes the OkButton
172     *
173     * @return javax.swing.JButton
174     */
175 keeb 1294 @Override
176     protected OkButton getOkButton() {
177 keeb 1293 if (jButton == null) {
178     jButton = new OkButton();
179     jButton.setEnabled(false);
180 alfonx 244
181 keeb 1293 jButton.addActionListener(new ActionListener() {
182    
183     public void actionPerformed(ActionEvent e) {
184     dispose();
185     }
186    
187     });
188     }
189     return jButton;
190     }
191 keeb 1294
192 keeb 1293 /**
193 keeb 1290 * This method initializes the JPanel that carries the select language
194     * combobox
195     *
196     * @return javax.swing.JPanel
197     */
198     private JPanel getLanguageCombobox() {
199     if (jPanel == null) {
200     jLabel = new JLabel();
201     jLabel.setText("Select language: "); // i8n!?! Maybe replace with an
202     // icon of an index finger
203     jPanel = new JPanel();
204     jPanel.add(jLabel);
205     jPanel.add(getJComboBox());
206     }
207     return jPanel;
208     }
209 alfonx 1098
210 keeb 1290 /**
211     * This method initializes the Select Language Combobox
212     *
213     * @return javax.swing.JComboBox
214     */
215     private JComboBox getJComboBox() {
216     if (jComboBox == null) {
217     ArrayList<String> languagesPlusOne = new ArrayList<String>(
218     languages);
219     languagesPlusOne.add("?");
220 alfonx 244
221 keeb 1290 jComboBox = new LanguagesComboBox(languages);
222 alfonx 244
223 keeb 1290 SwingUtil.addMouseWheelForCombobox(jComboBox);
224     jComboBox.addActionListener(new ActionListener() {
225 alfonx 244
226 keeb 1290 public void actionPerformed(final ActionEvent e) {
227     if (jComboBox.getSelectedIndex() == languages.size()) {
228 keeb 1294 getOkButton().setEnabled(false);
229 keeb 1290 return;
230     }
231 alfonx 244
232 keeb 1290 String l = languages.get(jComboBox.getSelectedIndex());
233     try {
234     Translation.setActiveLang(l, setLocale);
235 alfonx 244
236 keeb 1294 getOkButton().setEnabled(true);
237 keeb 1290 } catch (java.lang.IllegalArgumentException ee) {
238     LOGGER.warn("The language " + l + " is not valid", ee);
239 keeb 1294 getOkButton().setEnabled(false);
240 keeb 1290 }
241 alfonx 244
242 keeb 1290 }
243 alfonx 244
244 keeb 1290 });
245     }
246     return jComboBox;
247     }
248 alfonx 244
249 alfonx 607 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26