Coverage Report - org.homeunix.thecave.buddi.plugin.builtin.preference.LocalePreferences
 
Classes in this File Line Coverage Branch Coverage Complexity
LocalePreferences
73%
101/137
55%
32/58
5.375
LocalePreferences$1
100%
6/6
50%
1/2
5.375
LocalePreferences$2
100%
5/5
50%
1/2
5.375
 
 1  
 /*
 2  
  * Created on Aug 12, 2007 by wyatt
 3  
  */
 4  
 package org.homeunix.thecave.buddi.plugin.builtin.preference;
 5  
 
 6  
 import java.awt.Component;
 7  
 import java.awt.FlowLayout;
 8  
 import java.awt.event.ActionEvent;
 9  
 import java.awt.event.ActionListener;
 10  
 import java.io.File;
 11  
 import java.text.SimpleDateFormat;
 12  
 import java.util.Collections;
 13  
 import java.util.Date;
 14  
 import java.util.HashSet;
 15  
 import java.util.Set;
 16  
 import java.util.Vector;
 17  
 import java.util.logging.Logger;
 18  
 
 19  
 import javax.swing.Box;
 20  
 import javax.swing.BoxLayout;
 21  
 import javax.swing.DefaultComboBoxModel;
 22  
 import javax.swing.DefaultListCellRenderer;
 23  
 import javax.swing.JButton;
 24  
 import javax.swing.JCheckBox;
 25  
 import javax.swing.JLabel;
 26  
 import javax.swing.JList;
 27  
 import javax.swing.JOptionPane;
 28  
 import javax.swing.JPanel;
 29  
 
 30  
 import org.homeunix.thecave.buddi.Buddi;
 31  
 import org.homeunix.thecave.buddi.Const;
 32  
 import org.homeunix.thecave.buddi.i18n.BuddiKeys;
 33  
 import org.homeunix.thecave.buddi.i18n.BuddiLanguageEditor;
 34  
 import org.homeunix.thecave.buddi.i18n.BuddiLanguageEditor.BuddiLanguageEditorException;
 35  
 import org.homeunix.thecave.buddi.i18n.keys.ButtonKeys;
 36  
 import org.homeunix.thecave.buddi.model.prefs.PrefsModel;
 37  
 import org.homeunix.thecave.buddi.plugin.api.BuddiPreferencePlugin;
 38  
 import org.homeunix.thecave.buddi.plugin.api.util.TextFormatter;
 39  
 import org.homeunix.thecave.buddi.util.InternalFormatter;
 40  
 
 41  
 import ca.digitalcave.moss.swing.MossScrollingComboBox;
 42  
 import ca.digitalcave.moss.swing.exception.WindowOpenException;
 43  
 
 44  
 public class LocalePreferences extends BuddiPreferencePlugin implements ActionListener {
 45  
         public static final long serialVersionUID = 0; 
 46  
 
 47  
         private final MossScrollingComboBox language;
 48  
         private final MossScrollingComboBox dateFormat;
 49  
         private final MossScrollingComboBox currencyFormat;
 50  
         private final JCheckBox currencySymbolAfterAmount;
 51  
         private final JButton otherCurrencyButton;
 52  
         private final JButton otherDateFormatButton;
 53  
         private final JButton editLanguagesButton;
 54  
 
 55  
         private final DefaultComboBoxModel languageModel;
 56  
         private final DefaultComboBoxModel currencyModel;
 57  
         private final DefaultComboBoxModel dateFormatModel;
 58  
 
 59  695
         public LocalePreferences() {
 60  695
                 languageModel = new DefaultComboBoxModel();
 61  695
                 language = new MossScrollingComboBox(languageModel);
 62  695
                 currencyModel = new DefaultComboBoxModel();
 63  695
                 currencyFormat = new MossScrollingComboBox(currencyModel);
 64  695
                 currencySymbolAfterAmount = new JCheckBox(TextFormatter.getTranslation(BuddiKeys.PREFERENCE_SHOW_CURRENCY_SYMBOL_AFTER_AMOUNT));
 65  695
                 dateFormatModel = new DefaultComboBoxModel();
 66  695
                 dateFormat = new MossScrollingComboBox(dateFormatModel);
 67  695
                 otherCurrencyButton = new JButton(TextFormatter.getTranslation(ButtonKeys.BUTTON_OTHER));
 68  695
                 otherDateFormatButton = new JButton(TextFormatter.getTranslation(ButtonKeys.BUTTON_OTHER));
 69  695
                 editLanguagesButton = new JButton(TextFormatter.getTranslation(BuddiKeys.PREFERENCE_EDIT_LANGUAGES));
 70  695
         }
 71  
 
 72  
         @Override
 73  
         public JPanel getPreferencesPanel() {
 74  695
                 JPanel panel = new JPanel();
 75  695
                 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
 76  
 
 77  695
                 JPanel languagePanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
 78  695
                 JPanel dateFormatPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
 79  695
                 JPanel currencyFormatPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
 80  
 
 81  695
                 JLabel dateFormatLabel = new JLabel(TextFormatter.getTranslation(BuddiKeys.PREFERENCE_DATE_FORMAT));
 82  695
                 JLabel currencyFormatLabel = new JLabel(TextFormatter.getTranslation(BuddiKeys.PREFERENCE_CURRENCY));
 83  695
                 JLabel languageLabel = new JLabel(TextFormatter.getTranslation(BuddiKeys.PREFERENCE_LANGUAGE));
 84  
 
 85  
                 //Set up the language pulldown
 86  695
                 language.setRenderer(new DefaultListCellRenderer(){
 87  
                         public static final long serialVersionUID = 0;
 88  
                         public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
 89  20281
                                 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
 90  20281
                                 if (value instanceof String){
 91  20281
                                         String str = (String) value;
 92  20281
                                         this.setText(str.replaceAll("_", " "));
 93  
                                 }
 94  20281
                                 return this;
 95  
                         }
 96  
                 });
 97  
 
 98  
                 //Set up the date format pulldown
 99  695
                 dateFormat.setRenderer(new DefaultListCellRenderer(){
 100  
                         public static final long serialVersionUID = 0;
 101  
                         public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
 102  6415
                                 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
 103  
 
 104  6415
                                 if (value != null){
 105  6415
                                         this.setText(new SimpleDateFormat(value.toString()).format(new Date()));
 106  
                                 }
 107  
 
 108  6415
                                 return this;
 109  
                         }
 110  
                 });
 111  
 
 112  695
                 otherCurrencyButton.setPreferredSize(InternalFormatter.getButtonSize(otherCurrencyButton));
 113  695
                 otherDateFormatButton.setPreferredSize(InternalFormatter.getButtonSize(otherDateFormatButton));
 114  695
                 editLanguagesButton.setPreferredSize(InternalFormatter.getButtonSize(editLanguagesButton));
 115  
 
 116  695
                 otherCurrencyButton.addActionListener(this);
 117  695
                 otherDateFormatButton.addActionListener(this);
 118  695
                 editLanguagesButton.addActionListener(this);
 119  
 
 120  695
                 languagePanel.add(languageLabel);
 121  695
                 languagePanel.add(language);
 122  695
                 languagePanel.add(editLanguagesButton);
 123  
 
 124  695
                 dateFormatPanel.add(dateFormatLabel);
 125  695
                 dateFormatPanel.add(dateFormat);
 126  695
                 dateFormatPanel.add(otherDateFormatButton);
 127  
 
 128  695
                 currencyFormatPanel.add(currencyFormatLabel);
 129  695
                 currencyFormatPanel.add(currencyFormat);
 130  695
                 currencyFormatPanel.add(otherCurrencyButton);
 131  
 
 132  695
                 panel.add(languagePanel);                
 133  695
                 panel.add(dateFormatPanel);
 134  695
                 panel.add(currencyFormatPanel);
 135  695
                 panel.add(currencySymbolAfterAmount);
 136  695
                 panel.add(Box.createVerticalGlue());
 137  
 
 138  695
                 return panel;
 139  
         }
 140  
 
 141  
         public void load() {
 142  
 
 143  
                 //Set up currency model
 144  695
                 boolean customCurrency = true; //Assume custom until proved otherwise, below
 145  695
                 String currency = PrefsModel.getInstance().getCurrencySign();
 146  13900
                 for (String s : Const.CURRENCY_FORMATS) {
 147  13205
                         currencyModel.addElement(s);
 148  13205
                         if (s.equals(currency))
 149  695
                                 customCurrency = false;
 150  
                 }
 151  695
                 if (customCurrency){
 152  0
                         currencyModel.addElement(currency);
 153  
                 }
 154  
 
 155  
                 //Set up Date Format model
 156  695
                 boolean customDateFormat = true; //Assume custom until proved otherwise, below
 157  6255
                 for (String s : Const.DATE_FORMATS) {
 158  5560
                         dateFormatModel.addElement(s);
 159  5560
                         if (s.equals(PrefsModel.getInstance().getDateFormat()))
 160  695
                                 customDateFormat = false;
 161  
                 }
 162  695
                 if (customDateFormat){
 163  0
                         dateFormatModel.addElement(PrefsModel.getInstance().getDateFormat());
 164  
                 }
 165  
 
 166  
 
 167  
                 //Set up language model.  Look for all built in ones, and any extras.
 168  695
                 languageModel.removeAllElements();
 169  695
                 Set<String> languages = new HashSet<String>();
 170  
                 //Load all built in languages
 171  10425
                 for (String language : Const.BUNDLED_LANGUAGES) {
 172  9730
                         languages.add(language);                        
 173  
                 }
 174  
                 //Load all languages in the Languages folder
 175  695
                 File languageLocation = Buddi.getLanguagesFolder();
 176  695
                 if (languageLocation.exists() && languageLocation.isDirectory()){
 177  1390
                         for (File f: languageLocation.listFiles())
 178  695
                                 if (f.getName().endsWith(Const.LANGUAGE_EXTENSION))
 179  695
                                         languages.add(f.getName().replaceAll(Const.LANGUAGE_EXTENSION, ""));
 180  
                 }
 181  
                 //Load all found languages into language model
 182  695
                 Vector<String> languagesVector = new Vector<String>(languages);
 183  695
                 Collections.sort(languagesVector);
 184  695
                 for (String string : languagesVector) {
 185  9730
                         languageModel.addElement(string);
 186  
                 }
 187  
 
 188  
 
 189  
                 //Select from Preferencs
 190  695
                 dateFormat.setSelectedItem(PrefsModel.getInstance().getDateFormat());
 191  695
                 currencyFormat.setSelectedItem(PrefsModel.getInstance().getCurrencySign());
 192  695
                 currencySymbolAfterAmount.setSelected(PrefsModel.getInstance().isShowCurrencyAfterAmount());
 193  695
                 language.setSelectedItem(PrefsModel.getInstance().getLanguage());
 194  695
         }
 195  
 
 196  
         public boolean save() {
 197  43
                 boolean restart = false;
 198  43
                 if (!PrefsModel.getInstance().getDateFormat().equals(dateFormat.getSelectedItem().toString()))
 199  0
                         restart = true;
 200  43
                 if (!PrefsModel.getInstance().getLanguage().equals(language.getSelectedItem().toString()))
 201  0
                         restart = true;
 202  43
                 if (!PrefsModel.getInstance().getCurrencySign().equals(currencyFormat.getSelectedItem().toString()))
 203  0
                         restart = true;
 204  43
                 if (PrefsModel.getInstance().isShowCurrencyAfterAmount() != currencySymbolAfterAmount.isSelected())
 205  1
                         restart = true;
 206  
                 
 207  43
                 PrefsModel.getInstance().setDateFormat(dateFormat.getSelectedItem().toString());
 208  43
                 PrefsModel.getInstance().setCurrencySign(currencyFormat.getSelectedItem().toString());
 209  43
                 PrefsModel.getInstance().setShowCurrencyAfterAmount(currencySymbolAfterAmount.isSelected());
 210  43
                 PrefsModel.getInstance().setLanguage(language.getSelectedItem().toString());
 211  
 
 212  43
                 PrefsModel.getInstance().getTranslator().reloadLanguages();
 213  
                 
 214  43
                 return restart;
 215  
         }
 216  
 
 217  
         public void actionPerformed(ActionEvent e) {
 218  67
                 if (e.getSource().equals(otherCurrencyButton)){
 219  0
                         String newCurrency = JOptionPane.showInputDialog(
 220  
                                         null, 
 221  
                                         TextFormatter.getTranslation(BuddiKeys.PREFERENCE_ENTER_CURRENCY_SYMBOL), 
 222  
                                         TextFormatter.getTranslation(BuddiKeys.PREFERENCE_ENTER_CURRENCY_SYMBOL_TITLE), 
 223  
                                         JOptionPane.PLAIN_MESSAGE);
 224  
 
 225  0
                         if (newCurrency != null && newCurrency.length() > 0){
 226  0
                                 currencyModel.removeAllElements();
 227  
 
 228  
                                 //Set up currency lists
 229  0
                                 boolean customCurrency = true; //Assume custom until proved otherwise, below
 230  0
                                 for (String s : Const.CURRENCY_FORMATS) {
 231  0
                                         currencyModel.addElement(s);
 232  0
                                         if (s.equals(newCurrency)){
 233  0
                                                 customCurrency = false;
 234  0
                                                 Logger.getLogger(this.getClass().getName()).finest("Currency " + newCurrency + " already in list...");
 235  
                                         }
 236  
                                 }
 237  0
                                 if (customCurrency){
 238  0
                                         currencyModel.addElement(newCurrency);
 239  
                                 }
 240  
 
 241  0
                                 currencyFormat.setSelectedItem(newCurrency);
 242  0
                         }
 243  
                         else {
 244  0
                                 Logger.getLogger(this.getClass().getName()).finest("Invalid currency: '" + newCurrency + "'");
 245  
                         }        
 246  0
                 }
 247  67
                 else if (e.getSource().equals(otherDateFormatButton)){
 248  44
                         String newDateFormat = JOptionPane.showInputDialog(
 249  
                                         null, 
 250  
                                         TextFormatter.getTranslation(BuddiKeys.PREFERENCE_ENTER_DATE_FORMAT), 
 251  
                                         TextFormatter.getTranslation(BuddiKeys.PREFERENCE_ENTER_DATE_FORMAT_TITLE), 
 252  
                                         JOptionPane.PLAIN_MESSAGE);
 253  
 
 254  20
                         if (newDateFormat != null 
 255  
                                         && newDateFormat.length() > 0){
 256  
 
 257  
                                 //Test out the new format, to see if it complies with
 258  
                                 // the format rules.
 259  
                                 try {
 260  1
                                         new SimpleDateFormat(newDateFormat);
 261  
                                 }
 262  1
                                 catch (IllegalArgumentException iae){
 263  1
                                         String[] options = new String[1];
 264  1
                                         options[0] = TextFormatter.getTranslation(ButtonKeys.BUTTON_OK);
 265  
 
 266  1
                                         JOptionPane.showOptionDialog(
 267  
                                                         null, 
 268  
                                                         TextFormatter.getTranslation(BuddiKeys.PREFERENCE_ERROR_INCORRECT_FORMAT), 
 269  
                                                         TextFormatter.getTranslation(BuddiKeys.ERROR),
 270  
                                                         JOptionPane.DEFAULT_OPTION,
 271  
                                                         JOptionPane.ERROR_MESSAGE,
 272  
                                                         null,
 273  
                                                         options,
 274  
                                                         options[0]);
 275  0
                                         return;
 276  0
                                 }
 277  
 
 278  
 
 279  0
                                 dateFormatModel.removeAllElements();
 280  
 
 281  
                                 //Set up currency lists
 282  0
                                 boolean customDateFormat = true; //Assume custom until proved otherwise, below
 283  0
                                 for (String s : Const.DATE_FORMATS) {
 284  0
                                         dateFormatModel.addElement(s);
 285  0
                                         if (s.equals(newDateFormat)){
 286  0
                                                 customDateFormat = false;
 287  0
                                                 Logger.getLogger(this.getClass().getName()).finest("Date Format " + newDateFormat + " already in list...");
 288  
                                         }
 289  
                                 }
 290  0
                                 if (customDateFormat){
 291  0
                                         dateFormatModel.addElement(newDateFormat);
 292  
                                 }
 293  
 
 294  0
                                 dateFormatModel.setSelectedItem(newDateFormat);
 295  0
                         }
 296  
                         else {
 297  19
                                 Logger.getLogger(this.getClass().getName()).finest("Invalid Date Format: '" + newDateFormat + "'");
 298  
                         }
 299  19
                 }
 300  23
                 else if (e.getSource().equals(editLanguagesButton)){
 301  
                         try {
 302  23
                                 BuddiLanguageEditor ble = BuddiLanguageEditor.getInstance(language.getSelectedItem().toString());
 303  0
                                 ble.openWindow();
 304  
                         }
 305  0
                         catch (WindowOpenException woe){}
 306  0
                         catch (BuddiLanguageEditorException blee){}
 307  
                 }
 308  
 
 309  19
         }
 310  
 
 311  
         public String getName() {
 312  695
                 return BuddiKeys.LOCALE.toString();
 313  
         }
 314  
 }