/[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 1293 - (show annotations)
Mon Nov 22 11:55:02 2010 UTC (14 years, 3 months ago) by keeb
File size: 7700 byte(s)
Fixed regression where OkButton could not be set as disabled/enabled
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 JButton 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(getJButton(), // AtlasDialog.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 private JButton getJButton() {
176 if (jButton == null) {
177 jButton = new OkButton();
178 jButton.setEnabled(false);
179
180 jButton.addActionListener(new ActionListener() {
181
182 public void actionPerformed(ActionEvent e) {
183 dispose();
184 }
185
186 });
187 }
188 return jButton;
189 }
190 /**
191 * This method initializes the JPanel that carries the select language
192 * combobox
193 *
194 * @return javax.swing.JPanel
195 */
196 private JPanel getLanguageCombobox() {
197 if (jPanel == null) {
198 jLabel = new JLabel();
199 jLabel.setText("Select language: "); // i8n!?! Maybe replace with an
200 // icon of an index finger
201 jPanel = new JPanel();
202 jPanel.add(jLabel);
203 jPanel.add(getJComboBox());
204 }
205 return jPanel;
206 }
207
208 /**
209 * This method initializes the Select Language Combobox
210 *
211 * @return javax.swing.JComboBox
212 */
213 private JComboBox getJComboBox() {
214 if (jComboBox == null) {
215 ArrayList<String> languagesPlusOne = new ArrayList<String>(
216 languages);
217 languagesPlusOne.add("?");
218
219 jComboBox = new LanguagesComboBox(languages);
220
221 SwingUtil.addMouseWheelForCombobox(jComboBox);
222 jComboBox.addActionListener(new ActionListener() {
223
224 public void actionPerformed(final ActionEvent e) {
225 if (jComboBox.getSelectedIndex() == languages.size()) {
226 getJButton().setEnabled(false);
227 return;
228 }
229
230 String l = languages.get(jComboBox.getSelectedIndex());
231 try {
232 Translation.setActiveLang(l, setLocale);
233
234 getJButton().setEnabled(true);
235 } catch (java.lang.IllegalArgumentException ee) {
236 LOGGER.warn("The language " + l + " is not valid", ee);
237 getJButton().setEnabled(false);
238 }
239
240 }
241
242 });
243 }
244 return jComboBox;
245 }
246
247 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26