001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * TimeZoneDialog.java
028: *
029: * Created on 17 novembre 2004, 1.07
030: *
031: */
032:
033: package it.businesslogic.ireport.gui.locale;
034:
035: import it.businesslogic.ireport.gui.sheet.Tag;
036: import it.businesslogic.ireport.util.I18n;
037: import it.businesslogic.ireport.util.Misc;
038: import java.util.Locale;
039: import java.util.Vector;
040: import javax.swing.event.DocumentEvent;
041: import javax.swing.event.DocumentListener;
042:
043: /**
044: *
045: * @author Administrator
046: */
047: public class LocaleSelectorDialog extends javax.swing.JDialog {
048:
049: private int dialogResult = javax.swing.JOptionPane.CANCEL_OPTION;
050:
051: private Locale selectedLocale = null;
052: private boolean updating = false;
053:
054: /** Creates new form NewLocaleFileDialog */
055: public LocaleSelectorDialog(java.awt.Dialog parent, boolean modal) {
056: super (parent, modal);
057: initAll();
058: }
059:
060: public LocaleSelectorDialog(java.awt.Frame parent, boolean modal) {
061: super (parent, modal);
062: initAll();
063: }
064:
065: public void initAll() {
066: initComponents();
067:
068: Locale[] locales = Locale.getAvailableLocales();
069: java.util.Vector v = new java.util.Vector();
070: v.add(I18n.getString("timezone.default", "Default"));
071: for (int i = 0; i < locales.length; ++i) {
072: v.add(new Tag(locales[i], locales[i].getDisplayName(I18n
073: .getCurrentLocale())));
074: }
075:
076: jListLocales.setListData(v);
077:
078: jListLocales.setSelectedIndex(0);
079:
080: it.businesslogic.ireport.util.Misc.centerFrame(this );
081:
082: javax.swing.KeyStroke escape = javax.swing.KeyStroke
083: .getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0,
084: false);
085: javax.swing.Action escapeAction = new javax.swing.AbstractAction() {
086: public void actionPerformed(java.awt.event.ActionEvent e) {
087: jButton2ActionPerformed(e);
088: }
089: };
090:
091: DocumentListener dl = new DocumentListener() {
092: public void changedUpdate(DocumentEvent e) {
093: updateCustomLocale();
094: }
095:
096: public void insertUpdate(DocumentEvent e) {
097: updateCustomLocale();
098: }
099:
100: public void removeUpdate(DocumentEvent e) {
101: updateCustomLocale();
102: }
103: };
104:
105: jTextFieldCountry.getDocument().addDocumentListener(dl);
106: jTextFieldLanguage.getDocument().addDocumentListener(dl);
107: jTextFieldVariant.getDocument().addDocumentListener(dl);
108:
109: getRootPane().getInputMap(
110: javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(
111: escape, "ESCAPE");
112: getRootPane().getActionMap().put("ESCAPE", escapeAction);
113:
114: applyI18n();
115: //to make the default button ...
116: this .getRootPane().setDefaultButton(this .jButton1);
117: }
118:
119: public int getDialogResult() {
120: return dialogResult;
121: }
122:
123: public void setDialogResult(int dialogResult) {
124: this .dialogResult = dialogResult;
125: }
126:
127: /** This method is called from within the constructor to
128: * initialize the form.
129: * WARNING: Do NOT modify this code. The content of this method is
130: * always regenerated by the Form Editor.
131: */
132: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
133: private void initComponents() {
134: java.awt.GridBagConstraints gridBagConstraints;
135:
136: jTabbedPane1 = new javax.swing.JTabbedPane();
137: jPanel2 = new javax.swing.JPanel();
138: jScrollPane1 = new javax.swing.JScrollPane();
139: jListLocales = new javax.swing.JList();
140: jPanelCustom = new javax.swing.JPanel();
141: jLabelLanguage = new javax.swing.JLabel();
142: jTextFieldLanguage = new javax.swing.JTextField();
143: jLabelLanguageTip = new javax.swing.JLabel();
144: jLabelCountry = new javax.swing.JLabel();
145: jTextFieldCountry = new javax.swing.JTextField();
146: jLabelCountryTip = new javax.swing.JLabel();
147: jLabelVariant = new javax.swing.JLabel();
148: jTextFieldVariant = new javax.swing.JTextField();
149: jLabelVariantTip = new javax.swing.JLabel();
150: jPanel3 = new javax.swing.JPanel();
151: jPanel1 = new javax.swing.JPanel();
152: jButtonDefault = new javax.swing.JButton();
153: jButton1 = new javax.swing.JButton();
154: jButton2 = new javax.swing.JButton();
155:
156: getContentPane().setLayout(new java.awt.GridBagLayout());
157:
158: setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
159: jPanel2.setLayout(new java.awt.GridBagLayout());
160:
161: jPanel2.setMinimumSize(new java.awt.Dimension(350, 80));
162: jPanel2.setPreferredSize(new java.awt.Dimension(400, 300));
163: jListLocales
164: .addListSelectionListener(new javax.swing.event.ListSelectionListener() {
165: public void valueChanged(
166: javax.swing.event.ListSelectionEvent evt) {
167: jListLocalesValueChanged(evt);
168: }
169: });
170:
171: jScrollPane1.setViewportView(jListLocales);
172:
173: gridBagConstraints = new java.awt.GridBagConstraints();
174: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
175: gridBagConstraints.weightx = 1.0;
176: gridBagConstraints.weighty = 1.0;
177: gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
178: jPanel2.add(jScrollPane1, gridBagConstraints);
179:
180: jTabbedPane1.addTab("Predefined locales", jPanel2);
181: jPanel2.getAccessibleContext().setAccessibleName("Locale");
182:
183: jPanelCustom.setLayout(new java.awt.GridBagLayout());
184:
185: jLabelLanguage.setText("Language");
186: gridBagConstraints = new java.awt.GridBagConstraints();
187: gridBagConstraints.gridx = 0;
188: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
189: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
190: gridBagConstraints.insets = new java.awt.Insets(16, 8, 0, 4);
191: jPanelCustom.add(jLabelLanguage, gridBagConstraints);
192:
193: jTextFieldLanguage
194: .setMinimumSize(new java.awt.Dimension(90, 19));
195: jTextFieldLanguage.setPreferredSize(new java.awt.Dimension(90,
196: 19));
197: gridBagConstraints = new java.awt.GridBagConstraints();
198: gridBagConstraints.gridx = 1;
199: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
200: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
201: gridBagConstraints.insets = new java.awt.Insets(14, 0, 0, 4);
202: jPanelCustom.add(jTextFieldLanguage, gridBagConstraints);
203:
204: jLabelLanguageTip
205: .setText("<html>ISO Language Code (lower case, two-letter code. i.e. \"en\")");
206: gridBagConstraints = new java.awt.GridBagConstraints();
207: gridBagConstraints.gridx = 2;
208: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
209: gridBagConstraints.weightx = 1.0;
210: gridBagConstraints.insets = new java.awt.Insets(16, 0, 0, 8);
211: jPanelCustom.add(jLabelLanguageTip, gridBagConstraints);
212:
213: jLabelCountry.setText("Country");
214: gridBagConstraints = new java.awt.GridBagConstraints();
215: gridBagConstraints.gridx = 0;
216: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
217: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
218: gridBagConstraints.insets = new java.awt.Insets(6, 8, 0, 4);
219: jPanelCustom.add(jLabelCountry, gridBagConstraints);
220:
221: jTextFieldCountry
222: .setMinimumSize(new java.awt.Dimension(90, 19));
223: jTextFieldCountry.setPreferredSize(new java.awt.Dimension(90,
224: 19));
225: gridBagConstraints = new java.awt.GridBagConstraints();
226: gridBagConstraints.gridx = 1;
227: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
228: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
229: gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 4);
230: jPanelCustom.add(jTextFieldCountry, gridBagConstraints);
231:
232: jLabelCountryTip
233: .setText("<html>Optional ISO Country Code (upper case, two-letter code, i.e. \"US\")");
234: gridBagConstraints = new java.awt.GridBagConstraints();
235: gridBagConstraints.gridx = 2;
236: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
237: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
238: gridBagConstraints.weightx = 1.0;
239: gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 8);
240: jPanelCustom.add(jLabelCountryTip, gridBagConstraints);
241:
242: jLabelVariant.setText("Variant");
243: gridBagConstraints = new java.awt.GridBagConstraints();
244: gridBagConstraints.gridx = 0;
245: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
246: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
247: gridBagConstraints.insets = new java.awt.Insets(6, 8, 0, 4);
248: jPanelCustom.add(jLabelVariant, gridBagConstraints);
249:
250: jTextFieldVariant
251: .setMinimumSize(new java.awt.Dimension(90, 19));
252: jTextFieldVariant.setPreferredSize(new java.awt.Dimension(90,
253: 19));
254: gridBagConstraints = new java.awt.GridBagConstraints();
255: gridBagConstraints.gridx = 1;
256: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
257: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
258: gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 4);
259: jPanelCustom.add(jTextFieldVariant, gridBagConstraints);
260:
261: jLabelVariantTip.setText("<html>Optional specific code");
262: gridBagConstraints = new java.awt.GridBagConstraints();
263: gridBagConstraints.gridx = 2;
264: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
265: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
266: gridBagConstraints.weightx = 1.0;
267: gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 8);
268: jPanelCustom.add(jLabelVariantTip, gridBagConstraints);
269:
270: gridBagConstraints = new java.awt.GridBagConstraints();
271: gridBagConstraints.gridx = 0;
272: gridBagConstraints.weighty = 1.0;
273: jPanelCustom.add(jPanel3, gridBagConstraints);
274:
275: jTabbedPane1.addTab("Custom locale", jPanelCustom);
276:
277: gridBagConstraints = new java.awt.GridBagConstraints();
278: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
279: gridBagConstraints.weighty = 1.0;
280: gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
281: getContentPane().add(jTabbedPane1, gridBagConstraints);
282:
283: jPanel1.setLayout(new java.awt.GridBagLayout());
284:
285: jButtonDefault.setText("System default");
286: jButtonDefault
287: .addActionListener(new java.awt.event.ActionListener() {
288: public void actionPerformed(
289: java.awt.event.ActionEvent evt) {
290: jButton1ActionPerformed1(evt);
291: }
292: });
293:
294: gridBagConstraints = new java.awt.GridBagConstraints();
295: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
296: gridBagConstraints.weightx = 1.0;
297: gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 4);
298: jPanel1.add(jButtonDefault, gridBagConstraints);
299:
300: jButton1.setText("OK");
301: jButton1.setEnabled(false);
302: jButton1.addActionListener(new java.awt.event.ActionListener() {
303: public void actionPerformed(java.awt.event.ActionEvent evt) {
304: jButton1ActionPerformed(evt);
305: }
306: });
307:
308: gridBagConstraints = new java.awt.GridBagConstraints();
309: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
310: gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 4);
311: jPanel1.add(jButton1, gridBagConstraints);
312:
313: jButton2.setText("Cancel");
314: jButton2.addActionListener(new java.awt.event.ActionListener() {
315: public void actionPerformed(java.awt.event.ActionEvent evt) {
316: jButton2ActionPerformed(evt);
317: }
318: });
319:
320: gridBagConstraints = new java.awt.GridBagConstraints();
321: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
322: gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 4);
323: jPanel1.add(jButton2, gridBagConstraints);
324:
325: gridBagConstraints = new java.awt.GridBagConstraints();
326: gridBagConstraints.gridx = 0;
327: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
328: gridBagConstraints.weightx = 1.0;
329: getContentPane().add(jPanel1, gridBagConstraints);
330:
331: pack();
332: }// </editor-fold>//GEN-END:initComponents
333:
334: private void jButton1ActionPerformed1(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed1
335:
336: this .setSelectedLocale(Locale.getDefault());
337: jButton1ActionPerformed(evt);
338:
339: }//GEN-LAST:event_jButton1ActionPerformed1
340:
341: private void jListLocalesValueChanged(
342: javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jListLocalesValueChanged
343:
344: updating = true;
345: try {
346: if (jListLocales.getSelectedIndex() >= 0) {
347: jButton1.setEnabled(true);
348: if (jListLocales.getSelectedIndex() > 0) {
349: Locale locale = (Locale) ((Tag) jListLocales
350: .getSelectedValue()).getValue();
351: jTextFieldLanguage.setText(locale.getLanguage());
352: jTextFieldCountry.setText(locale.getCountry());
353: jTextFieldVariant.setText(locale.getVariant());
354: } else {
355: jTextFieldLanguage.setText("");
356: jTextFieldCountry.setText("");
357: jTextFieldVariant.setText("");
358: }
359: }
360: } finally {
361: updating = false;
362: }
363:
364: }//GEN-LAST:event_jListLocalesValueChanged
365:
366: private void updateCustomLocale() {
367: if (updating)
368: return;
369: jListLocales.clearSelection();
370:
371: if (jTextFieldLanguage.getText().trim().length() == 0) {
372: jButton1.setEnabled(false);
373: }
374: }
375:
376: private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
377: this .setDialogResult(javax.swing.JOptionPane.CANCEL_OPTION);
378: this .setVisible(false);
379: this .dispose();
380: }//GEN-LAST:event_jButton2ActionPerformed
381:
382: private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
383:
384: if (jListLocales.getSelectedIndex() < 0) {
385: this .selectedLocale = null;
386: String language = jTextFieldLanguage.getText().trim();
387: String country = jTextFieldCountry.getText().trim();
388: String variant = jTextFieldVariant.getText().trim();
389:
390: if (language != null && language.trim().length() > 0) {
391: if (country != null && country.trim().length() > 0) {
392: if (variant != null && variant.trim().length() > 0) {
393: selectedLocale = new Locale(language, country,
394: variant);
395: } else {
396: selectedLocale = new Locale(language, country);
397: }
398: } else {
399: selectedLocale = new Locale(language);
400: }
401: }
402: } else if (jListLocales.getSelectedIndex() == 0) {
403: this .selectedLocale = null;
404: } else {
405: this .selectedLocale = (Locale) ((Tag) jListLocales
406: .getSelectedValue()).getValue();
407: }
408:
409: this .setDialogResult(javax.swing.JOptionPane.OK_OPTION);
410:
411: this .setVisible(false);
412: this .dispose();
413: }//GEN-LAST:event_jButton1ActionPerformed
414:
415: // Variables declaration - do not modify//GEN-BEGIN:variables
416: private javax.swing.JButton jButton1;
417: private javax.swing.JButton jButton2;
418: private javax.swing.JButton jButtonDefault;
419: private javax.swing.JLabel jLabelCountry;
420: private javax.swing.JLabel jLabelCountryTip;
421: private javax.swing.JLabel jLabelLanguage;
422: private javax.swing.JLabel jLabelLanguageTip;
423: private javax.swing.JLabel jLabelVariant;
424: private javax.swing.JLabel jLabelVariantTip;
425: private javax.swing.JList jListLocales;
426: private javax.swing.JPanel jPanel1;
427: private javax.swing.JPanel jPanel2;
428: private javax.swing.JPanel jPanel3;
429: private javax.swing.JPanel jPanelCustom;
430: private javax.swing.JScrollPane jScrollPane1;
431: private javax.swing.JTabbedPane jTabbedPane1;
432: private javax.swing.JTextField jTextFieldCountry;
433: private javax.swing.JTextField jTextFieldLanguage;
434: private javax.swing.JTextField jTextFieldVariant;
435:
436: // End of variables declaration//GEN-END:variables
437:
438: public void applyI18n() {
439: // Start autogenerated code ----------------------
440: jButton1.setText(I18n.getString("localeSelectorDialog.button1",
441: "OK"));
442: jButton2.setText(I18n.getString("localeSelectorDialog.button2",
443: "Cancel"));
444:
445: jButton1.setMnemonic(I18n.getString(
446: "localeSelectorDialog.button1Mnemonic", "o").charAt(0));
447: jButton2.setMnemonic(I18n.getString(
448: "localeSelectorDialog.button2Mnemonic", "c").charAt(0));
449:
450: jButtonDefault
451: .setText(I18n.getString(
452: "localeSelectorDialog.buttonDefault",
453: "System default"));
454:
455: setTitle(I18n.getString("localeSelectorDialog.title",
456: "Locale selection"));
457: jLabelCountry.setText(I18n.getString(
458: "localeSelectorDialog.labelCountry", "Country"));
459: jLabelLanguage.setText(I18n.getString(
460: "localeSelectorDialog.labelLanguage", "Language"));
461: jLabelVariant.setText(I18n.getString(
462: "localeSelectorDialog.labelVariant", "Variant"));
463:
464: jLabelCountryTip
465: .setText("<html>"
466: + I18n
467: .getString(
468: "localeSelectorDialog.labelCountryTip",
469: "Optional ISO Country Code (upper case, two-letter code, i.e. \"US\")"));
470: jLabelLanguageTip
471: .setText("<html>"
472: + I18n
473: .getString(
474: "localeSelectorDialog.labelLanguageTip",
475: "ISO Language Code (lower case, two-letter code. i.e. \"en\")"));
476: jLabelVariantTip.setText("<html>"
477: + I18n.getString(
478: "localeSelectorDialog.labelVariantTip",
479: "Optional specific code"));
480:
481: jTabbedPane1.setTitleAt(0, I18n.getString(
482: "localeSelectorDialog.tab.predefinedLocales",
483: "Predefined locales"));
484: jTabbedPane1.setTitleAt(1, I18n.getString(
485: "localeSelectorDialog.tab.customeLocale",
486: "Custom locale"));
487:
488: }
489:
490: public Locale getSelectedLocale() {
491: return selectedLocale;
492: }
493:
494: public void setSelectedLocale(Locale selectedLocale) {
495: this .selectedLocale = selectedLocale;
496:
497: for (int i = 1; i < jListLocales.getModel().getSize(); ++i) {
498: Locale loc = (Locale) ((Tag) jListLocales.getModel()
499: .getElementAt(i)).getValue();
500: if (loc.equals(selectedLocale)) {
501: jListLocales.setSelectedIndex(i);
502: jListLocales.ensureIndexIsVisible(i);
503: return;
504: }
505: }
506:
507: jListLocales.clearSelection();
508: jTextFieldLanguage.setText(selectedLocale.getLanguage());
509: jTextFieldCountry.setText(selectedLocale.getCountry());
510: jTextFieldVariant.setText(selectedLocale.getVariant());
511: jTabbedPane1.setSelectedIndex(1);
512:
513: }
514:
515: public String getSelectedLocaleId() {
516: Locale loc = getSelectedLocale();
517: if (loc == null)
518: return null;
519: String s = loc.getLanguage();
520: if (loc.getCountry().length() > 0) {
521: s += "_" + loc.getCountry();
522: if (loc.getVariant().length() > 0) {
523: s += "_" + loc.getVariant();
524: }
525: }
526: return s;
527: }
528:
529: public void setSelectedLocaleId(String s) {
530: if (s == null)
531: return;
532: Locale newLocale = Misc.getLocaleFromString(s, null);
533:
534: if (newLocale != null) {
535: setSelectedLocale(newLocale);
536: }
537: }
538:
539: }
|