/* * File : Messages.java * Created : 14-sep-2001 11:22 * By : fbusquets * * JClic - Authoring and playing system for educational activities * * Copyright (C) 2000 - 2005 Francesc Busquets & Departament * d'Educacio de la Generalitat de Catalunya * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details (see the LICENSE file). */ package edu.xtec.util; import java.awt.BorderLayout; import java.text.Collator; import java.text.DateFormat; import java.awt.Component; import java.util.HashMap; import java.util.StringTokenizer; import java.util.Locale; import java.util.Date; import java.util.TreeMap; import javax.swing.JOptionPane; import java.io.File; import java.util.Vector; import java.util.Iterator; import javax.swing.JComponent; import java.awt.GridBagLayout; import java.awt.GridBagConstraints; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.SwingConstants; import javax.swing.JTextField; import javax.swing.JPasswordField; import javax.swing.JDialog; import javax.swing.SwingUtilities; /** * * @author Francesc Busquets (fbusquets@xtec.net) * @version 1.0 */ public class Messages { public static final String LANGUAGE="language", COUNTRY="country", VARIANT="variant"; public static final String BASIC_BUNDLE="messages.BasicMessages"; public static final String MESSAGES="messages"; public static final String ERROR="ERROR", WARNING="WARNING"; public static final int OK=0; // o public static final int YES=1; // y public static final int RETRY=2; // r public static final int NO=3; // n public static final int IGNORE=4; // i public static final int CANCEL=5; // c public static final int YES_TO_ALL=6; // Y public static final int NO_TO_ALL=7; // N private static final int NUM_BUTTONS=8; private static final String BTN_KEYS="oyrnicYN"; private static final String[] BTN_CODES={"OK", "YES", "RETRY", "NO", "IGNORE", "CANCEL", "YES_TO_ALL", "NO_TO_ALL"}; private String[] dlgButtons; public static final int MAX_PASSWORD_LENGTH=24; private Locale currentLocale; private MultiBundle messages; private Collator collator; private java.text.NumberFormat numberFormat; private java.text.NumberFormat percentFormat; public static final String OPTIONS_DELIMITER=","; public Messages(String bundle){ init(bundle, null, null, null); } public Messages(String bundle, String options){ StringTokenizer st=new StringTokenizer(options, OPTIONS_DELIMITER); init(bundle, st.hasMoreTokens() ? st.nextToken() : null, st.hasMoreTokens() ? st.nextToken() : null, st.hasMoreTokens() ? st.nextToken() : null); } public Messages(String bundle, java.util.HashMap options){ init(bundle, (String)options.get(LANGUAGE), (String)options.get(COUNTRY), (String)options.get(VARIANT)); } public Messages(String bundle, String language, String country, String variant) { init(bundle, language, country, variant); } public static Messages getMessages(HashMap options, String bundle){ Messages msg=(Messages)options.get(MESSAGES); if(msg==null){ String language=(String)options.get(LANGUAGE); //System.out.println("language getMessages: " + language); if(language==null){ JOptionPane pane=new JOptionPane("Please select your language:", JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION); //pane.setSelectionValues(DESCRIPTIVE_LANGUAGE_CODES); pane.setSelectionValues(getDescriptiveLanguageCodes(null)); pane.setWantsInput(true); String systemlanguage=Locale.getDefault().getLanguage(); //System.out.println("System Language: " + systemlanguage); //String selectedlang=(systemlanguage.equals("qc")) ? "qcv" : systemlanguage; //System.out.println("Selected Language: " + systemlanguage); //String initialSelection=getDescriptiveLanguageCode(selectedlang); if (System.getenv("LANG").contains("valenci")) { System.out.println("eieieieiei"); systemlanguage="qcv"; } else { System.out.println("0e0e0e0e0"); } String initialSelection=getDescriptiveLanguageCode(systemlanguage); pane.setInitialSelectionValue(initialSelection); showDlg((Component)options.get(Options.MAIN_PARENT_COMPONENT), pane, "Language selecion"); String sel=(String)pane.getInputValue(); if(sel==null) sel=initialSelection; options.put(LANGUAGE, getLanguageFromDescriptive(sel)); } msg=new Messages(bundle, options); options.put(MESSAGES, msg); Locale.setDefault(msg.getLocale()); } else if(bundle!=null){ msg.setLocale(options); msg.addBundle(bundle); } return msg; } public void init(String bundle, String language, String country, String variant) { setLocale(language, country, variant); //System.out.println("language: " +language); //System.out.println("country: " + country); //System.out.println("variant: "+ variant); addBundle(bundle); addBundle(BASIC_BUNDLE); getDlgButtons(true); } public void setLocale(java.util.HashMap options){ setLocale( (String)options.get(LANGUAGE), (String)options.get(COUNTRY), (String)options.get(VARIANT)); } public void setLocale(String language, String country, String variant) { Locale l=null; if(country==null) country=""; if(language==null || language.length()==0) l= (currentLocale==null ? Locale.getDefault() : currentLocale); else if(variant==null || variant.length()==0) l=new Locale(language, country); else l=new Locale(language, country, variant); if(!l.equals(currentLocale)){ currentLocale=l; numberFormat=java.text.NumberFormat.getInstance(currentLocale); percentFormat=java.text.NumberFormat.getPercentInstance(currentLocale); collator=null; if(messages!=null){ messages.setLocale(currentLocale); getDlgButtons(true); } } } public String[] getDlgButtons(boolean update){ if(update || dlgButtons==null){ dlgButtons=new String[NUM_BUTTONS]; for(int i=0; i0) result=String.copyValueOf(pwch); } else result=textField.getText(); } return result; } public boolean showInputDlg(Component parent, String[] msgKeys, String[] shortPromptKeys, JComponent[] promptObjects, String titleKey){ Vector v=new Vector(); if(msgKeys!=null){ for(int i=0; ii){ JLabel lb=new JLabel(get(shortPromptKeys[i])); lb.setLabelFor(promptObjects[i]); lb.setHorizontalAlignment(SwingConstants.LEFT); c.gridwidth=GridBagConstraints.RELATIVE; gridBag.setConstraints(lb, c); panel.add(lb); } c.gridwidth=GridBagConstraints.REMAINDER; gridBag.setConstraints(promptObjects[i], c); panel.add(promptObjects[i]); } v.add(panel); } } String title= (titleKey!=null ? get(titleKey) : ""); NarrowOptionPane pane=new NarrowOptionPane(60, v.toArray(), JOptionPane.QUESTION_MESSAGE, JOptionPane.DEFAULT_OPTION, null, parseButtons("oc")); return getFeedback(parent, pane, title)==OK; } public boolean showInputDlg(Component parent, JComponent mainComponent, String titleKey){ return showInputDlg(parent, mainComponent, titleKey, "oc"); } public boolean showInputDlg(Component parent, JComponent mainComponent, String titleKey, String buttons){ return showInputDlg(parent, mainComponent, titleKey, buttons, false); } public boolean showInputDlg(Component parent, JComponent mainComponent, String titleKey, String buttons, boolean centerOnParent){ InputDlg dlg=new InputDlg(parent, titleKey, buttons, mainComponent, centerOnParent); return dlg.getFeedback()==OK; //String title= (titleKey!=null ? get(titleKey) : ""); //NarrowOptionPane pane=new NarrowOptionPane(60, mainComponent, JOptionPane.PLAIN_MESSAGE, JOptionPane.DEFAULT_OPTION, null, parseButtons(buttons)); //return getFeedback(parent, pane, title)==OK; } class InputDlg extends JDialog implements java.awt.event.ActionListener{ int result=CANCEL; Component parent; boolean centerOnParent; InputDlg(Component parent, String titleKey, String buttons, JComponent mainComponent, boolean centerOnParent){ // 26-jan-06 - Modified to solve bug #73, reported by Jorda Polo // Compile error in gcj 4.0.3: // "Can't reference 'this' before the superclass constructor has been called." // OLD CODE: // super(JOptionPane.getFrameForComponent(parent), titleKey!=null ? get(titleKey) : "", true); // NEW CODE: // Split in two steps: // 1 - call super with 'owner' and 'modal' parameters // 2 - if 'titleKey' not null, set title super(JOptionPane.getFrameForComponent(parent), true); if(titleKey!=null) setTitle(get(titleKey)); // -------- this.parent=parent; this.centerOnParent=centerOnParent; setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); getContentPane().setLayout(new BorderLayout(10, 10)); if(mainComponent!=null) getContentPane().add(mainComponent, BorderLayout.CENTER); JButton[] btn=parseJButtons(buttons); JPanel btnPanel=new JPanel(); JButton defaultBtn=null; for(int i=0; i=3600){ sb.append(v/3600).append("h"); } if(v>=60){ sb.append((v%3600)/60).append("'"); } sb.append(v%60).append("\""); return sb.substring(0); } /* Since the Java specification does not include the ISO 639-2 three-letter * language codes, we provide this HashMap to support it. The list will be * expanded as new translations of JClic where created. */ public static final HashMap ISO_639_2_CODES=new HashMap(); static{ ISO_639_2_CODES.put("ast","asturianu"); ISO_639_2_CODES.put("qcv","valenciĆ "); } public static String getDescriptiveLanguageCode(String languageCode){ String result=null; if(languageCode!=null){ result=(String)ISO_639_2_CODES.get(languageCode); if(result==null){ Locale lx=new Locale(languageCode, ""); result=lx.getDisplayName(Locale.getDefault()); } result=result+" ("+languageCode+")"; //System.out.println("Resultado language: " + result); } return result; } public static String getLanguageFromDescriptive(String descriptive){ String result=null; int p=-1; if(descriptive!=null && (descriptive=descriptive.trim()).length()>4 && (p=descriptive.lastIndexOf('('))>0){ result=descriptive.substring(p+1, descriptive.length()-1); } return result; } public static final String[] getDescriptiveLanguageCodes(Locale inLocale){ Locale dl=Locale.getDefault(); String[] lc=Locale.getISOLanguages(); TreeMap tree = new TreeMap(); for(int i=0; i