/[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 1293 - (hide annotations)
Mon Nov 22 11:55:02 2010 UTC (14 years, 3 months ago) by keeb
Original Path: trunk/src/skrueger/i8n/SwitchLanguageDialog.java
File size: 7700 byte(s)
Fixed regression where OkButton could not be set as disabled/enabled
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     private JButton 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 1293 jContentPane.add(getJButton(), // AtlasDialog.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     private JButton getJButton() {
176     if (jButton == null) {
177     jButton = new OkButton();
178     jButton.setEnabled(false);
179 alfonx 244
180 keeb 1293 jButton.addActionListener(new ActionListener() {
181    
182     public void actionPerformed(ActionEvent e) {
183     dispose();
184     }
185    
186     });
187     }
188     return jButton;
189     }
190     /**
191 keeb 1290 * 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 alfonx 1098
208 keeb 1290 /**
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 alfonx 244
219 keeb 1290 jComboBox = new LanguagesComboBox(languages);
220 alfonx 244
221 keeb 1290 SwingUtil.addMouseWheelForCombobox(jComboBox);
222     jComboBox.addActionListener(new ActionListener() {
223 alfonx 244
224 keeb 1290 public void actionPerformed(final ActionEvent e) {
225     if (jComboBox.getSelectedIndex() == languages.size()) {
226 keeb 1293 getJButton().setEnabled(false);
227 keeb 1290 return;
228     }
229 alfonx 244
230 keeb 1290 String l = languages.get(jComboBox.getSelectedIndex());
231     try {
232     Translation.setActiveLang(l, setLocale);
233 alfonx 244
234 keeb 1293 getJButton().setEnabled(true);
235 keeb 1290 } catch (java.lang.IllegalArgumentException ee) {
236     LOGGER.warn("The language " + l + " is not valid", ee);
237 keeb 1293 getJButton().setEnabled(false);
238 keeb 1290 }
239 alfonx 244
240 keeb 1290 }
241 alfonx 244
242 keeb 1290 });
243     }
244     return jComboBox;
245     }
246 alfonx 244
247 alfonx 607 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26