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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1294 - (show annotations)
Mon Nov 22 12:00:28 2010 UTC (14 years, 3 months ago) by keeb
File size: 7699 byte(s)
refactored method such that it overrides getOkButton from superclass for convenience
1 /*******************************************************************************
2 * Copyright (c) 2009 Martin O. J. Schmitz.
3 *
4 * This file is part of the SCHMITZM library - a collection of utility
5 * classes based on Java 1.6, focusing (not only) on Java Swing
6 * 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 * Stefan A. Tzeggai - additional utility classes
29 ******************************************************************************/
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 import java.util.ArrayList;
39 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 import net.miginfocom.swing.MigLayout;
48
49 import org.apache.log4j.Logger;
50
51 import schmitzm.swing.SwingUtil;
52 import skrueger.swing.AtlasDialog;
53 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 * @author Stefan A. Tzeggai
61 */
62 public class SwitchLanguageDialog extends AtlasDialog {
63 protected Logger LOGGER = Logger.getLogger(SwitchLanguageDialog.class);
64
65 private JPanel jContentPane = null;
66
67 private JLabel jLabelFlagimage = null;
68
69 private JPanel jPanel = null;
70
71 private JLabel jLabel = null;
72
73 private OkButton jButton = null;
74
75 private LanguagesComboBox jComboBox = null;
76
77 private final List<String> languages;
78
79 /**
80 * if <code>true</code>, the default locale will also be changed during a
81 * language selection
82 **/
83 protected boolean setLocale;
84
85 /**
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
100 if (languages == null || languages.size() == 0) {
101 // No language is available.
102 return;
103 }
104
105 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
112 initialize();
113 }
114
115 @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
126 /**
127 * This method initializes this
128 *
129 * @return void
130 */
131 private void initialize() {
132 this.setContentPane(getJContentPane());
133
134 pack();
135
136 SwingUtil.centerFrameOnScreenRandom(this);
137 setModal(true);
138 }
139
140 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
149 /**
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 jContentPane.add(getOkButton(),
165 "right, w 50!, gaptop 5, gapright 5, gapbottom 5");
166 }
167 return jContentPane;
168 }
169
170 /**
171 * This method initializes the OkButton
172 *
173 * @return javax.swing.JButton
174 */
175 @Override
176 protected OkButton getOkButton() {
177 if (jButton == null) {
178 jButton = new OkButton();
179 jButton.setEnabled(false);
180
181 jButton.addActionListener(new ActionListener() {
182
183 public void actionPerformed(ActionEvent e) {
184 dispose();
185 }
186
187 });
188 }
189 return jButton;
190 }
191
192 /**
193 * 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
210 /**
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
221 jComboBox = new LanguagesComboBox(languages);
222
223 SwingUtil.addMouseWheelForCombobox(jComboBox);
224 jComboBox.addActionListener(new ActionListener() {
225
226 public void actionPerformed(final ActionEvent e) {
227 if (jComboBox.getSelectedIndex() == languages.size()) {
228 getOkButton().setEnabled(false);
229 return;
230 }
231
232 String l = languages.get(jComboBox.getSelectedIndex());
233 try {
234 Translation.setActiveLang(l, setLocale);
235
236 getOkButton().setEnabled(true);
237 } catch (java.lang.IllegalArgumentException ee) {
238 LOGGER.warn("The language " + l + " is not valid", ee);
239 getOkButton().setEnabled(false);
240 }
241
242 }
243
244 });
245 }
246 return jComboBox;
247 }
248
249 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26