/* LanguageTool, a natural language style checker 
 * Copyright (C) 2011 Daniel Naber (http://www.danielnaber.de)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */
package org.languagetool.gui;

import org.languagetool.Language;

import javax.swing.*;
import java.util.*;

/**
 * Combo box with list of available languages.
 */
public class LanguageComboBox extends JComboBox {

  private final ResourceBundle messages;

  public LanguageComboBox(ResourceBundle messages) {
    this.messages = messages;
    populateLanguageBox();
  }

  void populateLanguageBox() {
    removeAllItems();
    final List<I18nLanguage> i18nLanguages = getAllLanguages();
    preselectDefaultLanguage(i18nLanguages);
  }

  private List<I18nLanguage> getAllLanguages() {
    final List<I18nLanguage> i18nLanguages = new ArrayList<I18nLanguage>();
    for (Language language : Language.LANGUAGES) {
      if (language != Language.DEMO) {
        i18nLanguages.add(new I18nLanguage(language, messages));
      }
    }
    Collections.sort(i18nLanguages);
    return i18nLanguages;
  }

  private void preselectDefaultLanguage(List<I18nLanguage> i18nLanguages) {
    final String defaultLocale = Locale.getDefault().getLanguage();
    final String defaultGuiLocale = getDefaultGuiLanguage(defaultLocale);
    for (final I18nLanguage i18nLanguage : i18nLanguages) {
      addItem(i18nLanguage);
      if (i18nLanguage.toString().equals(defaultGuiLocale)) {
        setSelectedItem(i18nLanguage);
      }
    }
  }

  private String getDefaultGuiLanguage(String defaultLocale) {
    String defaultGuiLocale = null;
    try {
      defaultGuiLocale = messages.getString(defaultLocale);
    } catch (final MissingResourceException e) {
      // language not supported, so don't select a default
    }
    return defaultGuiLocale;
  }

}

@@ -1851,24 +1851,27 @@
 ring default
+Gui
 Locale = Loc
@@ -1871,15 +1871,8 @@
 e = 
-Locale.
 getD
@@ -1881,14 +1881,11 @@
 ault
-().get
+Gui
 Lang
@@ -1893,47 +1893,15 @@
 age(
-);%0A    final String defaultGui
 Locale
- = 
+.
 getD
@@ -1910,33 +1910,10 @@
 ault
-GuiLanguage(defaultLocale
+()
 );%0A 
@@ -2159,22 +2159,22 @@
 anguage(
-String
+Locale
  default
@@ -2286,16 +2286,65 @@
 ltLocale
+.getLanguage() + %22-%22 + defaultLocale.getCountry()
 );%0A    %7D
@@ -2395,16 +2395,227 @@
       //
+ this specific language/variant combination is not supported%0A      try %7B%0A        defaultGuiLocale = messages.getString(defaultLocale.getLanguage());%0A      %7D catch (final MissingResourceException e2) %7B%0A        //
  languag
@@ -2657,16 +2657,24 @@
 default%0A
+      %7D%0A
     %7D%0A  

check the user's country setting to choose a more specific default