From 5b9931ba4581bb8d6f2ec5b798bca1a14ed9eacf Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 31 May 2009 16:38:13 +0000 Subject: [PATCH] * store available languages in .properties file instead of a .xml file because JAXB class-loading takes ~300ms --- .../filebot/ui/panel/subtitle/Language.java | 105 +++++++----------- .../ui/panel/subtitle/SubtitlePanel.java | 6 +- .../ui/panel/subtitle/language.properties | 43 +++++++ .../filebot/ui/panel/subtitle/languages.xml | 45 -------- 4 files changed, 85 insertions(+), 114 deletions(-) create mode 100644 source/net/sourceforge/filebot/ui/panel/subtitle/language.properties delete mode 100644 source/net/sourceforge/filebot/ui/panel/subtitle/languages.xml diff --git a/source/net/sourceforge/filebot/ui/panel/subtitle/Language.java b/source/net/sourceforge/filebot/ui/panel/subtitle/Language.java index dcae956d..74fbecc5 100644 --- a/source/net/sourceforge/filebot/ui/panel/subtitle/Language.java +++ b/source/net/sourceforge/filebot/ui/panel/subtitle/Language.java @@ -2,40 +2,21 @@ package net.sourceforge.filebot.ui.panel.subtitle; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Unmarshaller; -import javax.xml.bind.annotation.XmlAttribute; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; +import java.util.Comparator; +import java.util.MissingResourceException; +import java.util.ResourceBundle; +import java.util.Set; class Language { - @XmlAttribute(name = "name") - private String name; - - @XmlAttribute(name = "code") - private String code; - - - protected Language() { - // used by JAXB - } + private final String code; + private final String name; - public Language(String name, String code) { - this.name = name; + public Language(String code, String name) { this.code = code; - } - - - public String getName() { - return name; + this.name = name; } @@ -44,9 +25,8 @@ class Language { } - @Override - public Language clone() { - return new Language(name, code); + public String getName() { + return name; } @@ -56,50 +36,47 @@ class Language { } - public static Language getLanguage(String languageCode) { - for (Language language : Languages.getInstance().elements()) { - if (language.getCode().equalsIgnoreCase(languageCode)) - return language; + @Override + public Language clone() { + return new Language(code, name); + } + + + public static final Comparator ALPHABETIC_ORDER = new Comparator() { + + @Override + public int compare(Language o1, Language o2) { + return o1.name.compareToIgnoreCase(o2.name); + } + }; + + + public static Language getLanguage(String code) { + ResourceBundle bundle = ResourceBundle.getBundle(Language.class.getName()); + + try { + return new Language(code, bundle.getString(code)); + } catch (MissingResourceException e) { + // ignore } return null; } - public static List availableLanguages() { - return Collections.unmodifiableList(Arrays.asList(Languages.getInstance().elements())); - } - - - @XmlRootElement(name = "languages") - private static class Languages { + public static Language[] availableLanguages() { + ResourceBundle bundle = ResourceBundle.getBundle(Language.class.getName()); + Set languageCodeSet = bundle.keySet(); - @XmlElement(name = "language") - private Language[] elements; + Language[] languages = new Language[languageCodeSet.size()]; + int size = 0; - // keep singleton instance of all available languages - private static Languages instance; - - - public static Languages getInstance() { - if (instance == null) { - try { - Unmarshaller unmarshaller = JAXBContext.newInstance(Languages.class).createUnmarshaller(); - - // load languages from xml files - return (Languages) unmarshaller.unmarshal(Language.class.getResource("languages.xml")); - } catch (JAXBException e) { - throw new RuntimeException(e); - } - } - - return instance; + // fill languages array + for (String code : languageCodeSet) { + languages[size++] = new Language(code, bundle.getString(code)); } - - public Language[] elements() { - return elements; - } + return languages; } } diff --git a/source/net/sourceforge/filebot/ui/panel/subtitle/SubtitlePanel.java b/source/net/sourceforge/filebot/ui/panel/subtitle/SubtitlePanel.java index 0b63259c..24c44625 100644 --- a/source/net/sourceforge/filebot/ui/panel/subtitle/SubtitlePanel.java +++ b/source/net/sourceforge/filebot/ui/panel/subtitle/SubtitlePanel.java @@ -51,11 +51,7 @@ public class SubtitlePanel extends AbstractSearchPanel - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -