⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 resource.java

📁 优秀的MPEG2-TS流分析软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * @(#)Resource.java - resource and i18n
 *
 * Copyright (c) 2004-2005 by pstorch, All rights reserved.
 * 
 * This file is part of X, a free Java based demux utility.
 * X is intended for educational purposes only, as a non-commercial test project.
 * It may not be used otherwise. Most parts are only experimental.
 * 
 *
 * This program is free software; you can redistribute it free of charge
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

package net.sourceforge.dvb.projectx.common;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JOptionPane;
import javax.swing.JRadioButtonMenuItem;

/**
 * Project-X resource and localization handling.
 * 
 * @author Peter Storch
 */
public class Resource {
	
	/** the prefix of all pjx resource files */
	private static final String PJX_RESOURCE_PREFIX = "pjxresources";

	/** current working directory */
	public static final String workdir = System.getProperty("user.dir");
	
	/** os dependent file separator */
	public static final String filesep = System.getProperty("file.separator");

	/** the users locale */
	private static Locale locale = null;

	/** the default resource bundle */
	private static ResourceBundle defaultResource = null;

	/** the resource bundle for the current users locale or language setting */
	private static ResourceBundle resource = null;
	
	/**
	 * Loads a resource bundle for the given locale.
	 * 
	 * @param locale
	 * @return ResourceBundle
	 */
	private static ResourceBundle loadResourceBundle(Locale locale) throws MissingResourceException
	{
		ResourceBundle newBundle = null;
		String resourceName = PJX_RESOURCE_PREFIX + "_" + locale.getLanguage() + ".properties";
		
		// first we try to find one in the current working directory
		try 
		{
			File file = new File(workdir + filesep + resourceName);
			if (file.exists() && file.canRead())
			{
				newBundle = new PropertyResourceBundle(new FileInputStream(file));
				return newBundle;
			}
		} 
		catch (Exception e) 
		{
			// shit happens, go on and try to find one in our jar file
		}
		
		try 
		{
			URL url = Resource.class.getClassLoader().getResource(resourceName);
			newBundle = new PropertyResourceBundle(url.openStream());
		} 
		catch (Exception e) 
		{
			throw new MissingResourceException("couldn't find " + resourceName, Resource.class.getName(), resourceName);
		}
		
		return newBundle;
	}
	
	static{
		// the default resource must be available
		defaultResource = loadResourceBundle(Locale.ENGLISH);
		try 
		{
			// now try to load the resource bundle form the users locale
			resource = loadResourceBundle(Locale.getDefault());
		} catch (MissingResourceException e) {
			// our fallback is english
			resource = defaultResource;
		}
	}
		
	/**
	 * Constructor of Resource.
	 */
	private Resource()
	{
		// singleton
	}
	
	/**
	 * Loads Language from ini file.
	 * 
	 * @param filename Name of the inifile.
	 */
	public static void loadLang(String inifile)
	{
		try 
		{
			if (new File(inifile).exists())
			{
				BufferedReader inis = new BufferedReader(new FileReader(inifile));
				String line=null; 
			
				while ((line = inis.readLine()) != null)
				{
					// look for the line with the language information
					if (line.startsWith("lang="))
					{
						String lang = line.substring(5);
						locale=new Locale(lang, "");
			try {
				resource = loadResourceBundle(locale);
			} catch (MissingResourceException e) {
				// our fallback is english
				resource = defaultResource;
			}
						
						// we have found what we need, stop reading this file
						break;
					}
				}
				inis.close();
			}
		}
		catch (IOException e1)
		{
			//DM25072004 081.7 int07 add
			System.out.println(resource.getString("msg.loadlang.error") + " " + e1);
		}
	}
		
	/**
	 * Saves the language information.
	 * 
	 * @param pw
	 */
	public static void saveLang(PrintWriter pw)
	{
		if (locale != null)
		{
			pw.println("// language");
			pw.println("lang="+locale);
		}
	}
		
	/**
	 * Gets a String from the Resource file. If the key is not found, the key
	 * itself is returned as text.
	 * 
	 * @param key
	 * @return String
	 */
	public static String getString(String key)
	{
		String text = null;
		try 
		{
			text = resource.getString(key);
		} 
		catch (MissingResourceException e) 
		{
			try 
			{
				// fallback to defaultResource
				text = defaultResource.getString(key);
			} 
			catch (MissingResourceException e2) 
			{
				System.out.println("ResourceKey '" + key + "' not found in pjxresources");
			}
		}
		
		// use key as text as fallback
		if (text == null)
		{
			text = "?" + key + "?";
		}
		
		return text;
	}

	/**
	 * Returns a resource String as a String Array of lines.
	 * 
	 * @return String[]
	 */
	public static String[] getStringByLines(String key)
	{
		List lines = new ArrayList();
		StringTokenizer st = new StringTokenizer(getString(key), "\n");
		while (st.hasMoreTokens())
		{
			lines.add(st.nextToken());
		}
		
		return (String[])lines.toArray(new String[0]);
	}

	/**
	 * Gets a String from the resource and inserts optional arguments.
	 * 
	 * @param key
	 * @param args
	 * @return
	 */
	public static String getString(String key, Object args[])
	{
		return MessageFormat.format(getString(key), args);
	}
	
	/**
	 * Gets a String from the resource and inserts an optional argument.
	 * 
	 * @param key
	 * @param args
	 * @return
	 */
	public static String getString(String key, Object arg)
	{
		return MessageFormat.format(getString(key), new Object[]{arg});
	}

	/**
	 * Gets a String from the resource and inserts two optional arguments.
	 * 
	 * @param key
	 * @param arg1
	 * @param arg2
	 * @return
	 */
	public static String getString(String key, Object arg1, Object arg2)
	{
		return MessageFormat.format(getString(key), new Object[]{arg1, arg2});
	}

	/**
	 * Gets a String from the resource and inserts three optional arguments.
	 * 
	 * @param key
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 * @return
	 */
	public static String getString(String key, Object arg1, Object arg2, Object arg3)
	{
		return MessageFormat.format(getString(key), new Object[]{arg1, arg2, arg3});
	}

	/**
	 * Gets a String from the resource and inserts four optional arguments.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -