Package de.t68.utils.keyboard

Source Code of de.t68.utils.keyboard.LanguageMap

/**
* Copyright (c) 2009, Till Woitendorf (t68)

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:<BR>

1. Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.<BR>
2. Redistributions in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or other materials provided with
the distribution.<BR>

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Contact me via http://jvkeyboard.sourceforge.net if you discover any bugs or if you have a suggestion
*/

package de.t68.utils.keyboard;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

/**
* @author t68
*
*/
public class LanguageMap extends java.util.TreeMap<String, KeyboardLayout> {

    protected static final String[] CONFIGNAMES = {
        "Simplified Chinese.xml", "Traditional Chinese.xml", "Danish.xml", "Dutch.xml", "English.xml",
        "International English.xml", "Finnish.xml", "French.xml", "German.xml", "Italian.xml",
        "Japanese.xml", "Korean.xml", "Norwegian.xml", "Pig Latin.xml", "Portuguese.xml",
        "Portuguese (Brazil).xml", "Russian.xml", "Spanish.xml", "Spanish-HispanicAmerica.xml",
        "Swedish.xml", "Thai.xml", "Turkish.xml", "Ukrainian.xml" };

    protected static LanguageMap instance;

    public static LanguageMap getInstance()
    {
        if (instance == null)
            instance = new LanguageMap("config");
        return instance;
    }

    public static void main(String[] args)
    {
        for (KeyboardLayout layout : getInstance().values())
            try
            {
                Element root = layout.getAsElement();
                File file = new File(layout.name + ".xml");
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                Format format = Format.getPrettyFormat();
                format.setEncoding("utf-16");
                format.setTextMode(Format.TextMode.TRIM);
                XMLOutputter outputter = new XMLOutputter(format);
                outputter.output(new Document(root), fileOutputStream);
                fileOutputStream.close();
            } catch (Exception e)
            {
                e.printStackTrace();
            }
    }

    protected LanguageMap(String configFolder)
    {
        super();
        String p = "de/t68/utils/keyboard/config";
        for (String c : CONFIGNAMES)
            try
            {
                InputStream is = ClassLoader.getSystemResourceAsStream(p + "/" + c);
                KeyboardLayout layout = new KeyboardLayout(is);
                List<String> locale = layout.getLocaleString();
                for (String l : locale)
                    put(l, layout);
                is.close();
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        File config = new File(configFolder);
        File[] configs = config.listFiles(new java.io.FilenameFilter() {

            @Override
            public boolean accept(File dir, String name)
            {
                return name.indexOf("-keyboard.xml") >= 0;
            }
        });
        if (configs != null)
            for (File c : configs)
                try
                {
                    FileInputStream is = new FileInputStream(c);
                    KeyboardLayout layout = new KeyboardLayout(is);
                    List<String> locale = layout.getLocaleString();
                    for (String l : locale)
                        put(l, layout);
                    is.close();
                } catch (Exception e1)
                {
                    System.out.println("error parsing : " + c.getAbsolutePath());
                    e1.printStackTrace();
                }
    }

    /**
     * @return the {@link KeyboardLayout} for the current locale in the folloing search order<BR>
     *         - the keyboard layout for the current language and country<BR>
     *         - the layout for the current language<BR>
     *         - thr layout for this language and a random country<BR>
     *         - the layout for international english "en-US"<BR>
     *         - the first configured layout
     *
     * @throws NoSuchElementException
     *             if we have no configured layout
     */
    public KeyboardLayout getKeyboardLayoutForCurrentLocale()
    {
        String language = Locale.getDefault().getLanguage();
        String country = Locale.getDefault().getCountry();

        return getKeyboardLayoutForLocale(language, country);
    }

    /**
     * @param language
     * @param country
     * @return
     */
    public KeyboardLayout getKeyboardLayoutForLocale(String language, String country)
    {
        KeyboardLayout defaultLayout = get("international");
        KeyboardLayout current = get(language + "-" + country);
        if (current != null)
            return (KeyboardLayout) current.clone();
        current = get(language);
        if (current != null)
            return (KeyboardLayout) current.clone();
        for (Map.Entry<String, KeyboardLayout> e : entrySet())
            if (e.getKey().startsWith(language + "-"))
                return (KeyboardLayout) e.getValue().clone();
        return defaultLayout == null ? (KeyboardLayout) values().iterator().next().clone()
                : (KeyboardLayout) defaultLayout.clone();
    }

}
TOP

Related Classes of de.t68.utils.keyboard.LanguageMap

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.