/[schmitzm]/trunk/src/skrueger/i8n/SwitchLanguageDialog.java
ViewVC logotype

Annotation of /trunk/src/skrueger/i8n/SwitchLanguageDialog.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1312 - (hide annotations)
Sun Nov 28 21:51:04 2010 UTC (14 years, 3 months ago) by alfonx
File size: 7580 byte(s)
* MailUtil kann jetzt SMTP mail verschicken.
* GP /AS versichen jetzt BugReports per SMTP wenn möglich, sonst über Desktop
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.event.ActionEvent;
34     import java.awt.event.ActionListener;
35 alfonx 1150 import java.util.ArrayList;
36 alfonx 244 import java.util.List;
37    
38     import javax.swing.ImageIcon;
39     import javax.swing.JComboBox;
40     import javax.swing.JLabel;
41     import javax.swing.JPanel;
42    
43 keeb 1290 import net.miginfocom.swing.MigLayout;
44    
45 alfonx 244 import org.apache.log4j.Logger;
46    
47     import schmitzm.swing.SwingUtil;
48 alfonx 1098 import skrueger.swing.AtlasDialog;
49 alfonx 244 import skrueger.swing.OkButton;
50     import skrueger.swing.TranslationEditJPanel;
51    
52     /**
53     * This dialog ask the user to select one of list of given languages. The dialog
54     * is modal and not visible after construction.
55     *
56 alfonx 1098 * @author Stefan A. Tzeggai
57 alfonx 244 */
58 alfonx 1098 public class SwitchLanguageDialog extends AtlasDialog {
59 keeb 1290 protected Logger LOGGER = Logger.getLogger(SwitchLanguageDialog.class);
60 alfonx 244
61 keeb 1290 private JPanel jContentPane = null;
62 alfonx 244
63 keeb 1290 private JLabel jLabelFlagimage = null;
64 alfonx 244
65 keeb 1290 private JPanel jPanel = null;
66 alfonx 244
67 keeb 1290 private JLabel jLabel = null;
68 keeb 1293
69 keeb 1294 private OkButton jButton = null;
70 alfonx 244
71 keeb 1290 private LanguagesComboBox jComboBox = null;
72 alfonx 244
73 keeb 1290 private final List<String> languages;
74 alfonx 244
75 keeb 1290 /**
76     * if <code>true</code>, the default locale will also be changed during a
77     * language selection
78     **/
79     protected boolean setLocale;
80 alfonx 244
81 keeb 1290 /**
82     * A dialog to select one of the available languages. If only one language
83     * is available, select it directly. Creating this object automatically
84     * makes it visible, unless there is only one language to choose from.. it
85     * that case it disposes itself automatically.
86     *
87     * @param setLocale if <code>true</code>, the default locale will also be
88     * changed during a language selection
89     */
90     public SwitchLanguageDialog(final Component owner,
91     final List<String> languages, boolean setLocale) {
92     super(owner);
93     this.languages = languages;
94     this.setLocale = setLocale;
95 alfonx 244
96 keeb 1290 if (languages == null || languages.size() == 0) {
97     // No language is available.
98     return;
99     }
100 alfonx 1098
101 keeb 1290 Translation.setActiveLang(languages.get(0), setLocale);
102     if (languages.size() == 1) {
103     // Only language one language is available. It has been selected
104     // automatically.
105     return;
106     }
107 alfonx 244
108 keeb 1290 initialize();
109     }
110 alfonx 244
111 keeb 1290 @Override
112     /**
113     * This modal dialog will not appear if there is <= one language to select from.
114     */
115     public void setVisible(boolean b) {
116     if (b == true && (languages == null || languages.size() <= 1)) {
117     return;
118     }
119     super.setVisible(b);
120     }
121 alfonx 244
122 keeb 1290 /**
123     * This method initializes this
124     *
125     * @return void
126     */
127     private void initialize() {
128     this.setContentPane(getJContentPane());
129 alfonx 244
130 keeb 1290 pack();
131 alfonx 1098
132 keeb 1290 SwingUtil.centerFrameOnScreenRandom(this);
133     setModal(true);
134     }
135 alfonx 244
136 keeb 1290 public boolean close() {
137     // Only close by ESC and window-close if a valid selection is made
138     if (jComboBox.getSelectedIndex() == languages.size()
139     || jComboBox.getSelectedIndex() == -1)
140     return false;
141     else
142     return super.close();
143     }
144 alfonx 607
145 keeb 1290 /**
146     * This method initializes jContentPane
147     *
148     * @return javax.swing.JPanel
149     */
150     private JPanel getJContentPane() {
151     if (jContentPane == null) {
152     jLabelFlagimage = new JLabel(new ImageIcon(
153     TranslationEditJPanel.class
154     .getResource("resource/flags.jpg")));
155     jContentPane = new JPanel();
156     MigLayout migLayout = new MigLayout("wrap 1", "[center]", "[]0[]");
157     jContentPane.setLayout(migLayout);
158     jContentPane.add(jLabelFlagimage, "north");
159     jContentPane.add(getLanguageCombobox());
160 keeb 1294 jContentPane.add(getOkButton(),
161 keeb 1290 "right, w 50!, gaptop 5, gapright 5, gapbottom 5");
162     }
163     return jContentPane;
164     }
165 keeb 1293
166     /**
167     * This method initializes the OkButton
168     *
169     * @return javax.swing.JButton
170     */
171 keeb 1294 @Override
172     protected OkButton getOkButton() {
173 keeb 1293 if (jButton == null) {
174     jButton = new OkButton();
175     jButton.setEnabled(false);
176 alfonx 244
177 keeb 1293 jButton.addActionListener(new ActionListener() {
178    
179     public void actionPerformed(ActionEvent e) {
180     dispose();
181     }
182    
183     });
184     }
185     return jButton;
186     }
187 keeb 1294
188 keeb 1293 /**
189 keeb 1290 * This method initializes the JPanel that carries the select language
190     * combobox
191     *
192     * @return javax.swing.JPanel
193     */
194     private JPanel getLanguageCombobox() {
195     if (jPanel == null) {
196     jLabel = new JLabel();
197     jLabel.setText("Select language: "); // i8n!?! Maybe replace with an
198     // icon of an index finger
199     jPanel = new JPanel();
200     jPanel.add(jLabel);
201     jPanel.add(getJComboBox());
202     }
203     return jPanel;
204     }
205 alfonx 1098
206 keeb 1290 /**
207     * This method initializes the Select Language Combobox
208     *
209     * @return javax.swing.JComboBox
210     */
211     private JComboBox getJComboBox() {
212     if (jComboBox == null) {
213     ArrayList<String> languagesPlusOne = new ArrayList<String>(
214     languages);
215     languagesPlusOne.add("?");
216 alfonx 244
217 keeb 1290 jComboBox = new LanguagesComboBox(languages);
218 alfonx 244
219 keeb 1290 SwingUtil.addMouseWheelForCombobox(jComboBox);
220     jComboBox.addActionListener(new ActionListener() {
221 alfonx 244
222 keeb 1290 public void actionPerformed(final ActionEvent e) {
223     if (jComboBox.getSelectedIndex() == languages.size()) {
224 keeb 1294 getOkButton().setEnabled(false);
225 keeb 1290 return;
226     }
227 alfonx 244
228 keeb 1290 String l = languages.get(jComboBox.getSelectedIndex());
229     try {
230     Translation.setActiveLang(l, setLocale);
231 alfonx 244
232 keeb 1294 getOkButton().setEnabled(true);
233 keeb 1290 } catch (java.lang.IllegalArgumentException ee) {
234     LOGGER.warn("The language " + l + " is not valid", ee);
235 keeb 1294 getOkButton().setEnabled(false);
236 keeb 1290 }
237 alfonx 244
238 keeb 1290 }
239 alfonx 244
240 keeb 1290 });
241     }
242     return jComboBox;
243     }
244 alfonx 244
245 alfonx 607 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26