📄 resource.java
字号:
/*
* This program is free software; you can redistribute it 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* Created on 2006/3/06
*
* @Author: Xiaojun Chen
* $Revision$ 1.0
*
*/
package eti.bi.common.Locale;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Locale;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import eti.bi.alphaminer.core.observer.Observer;
import eti.bi.common.MLPropertyResourceBundle;
import eti.bi.common.PluginResource;
import eti.bi.common.System.SysLog;
public final class Resource{
public final static String CHANGE_LOCALE = "change locale";
private static locale locale;
private static MLPropertyResourceBundle pr = new MLPropertyResourceBundle();
private static LinkedList<PluginResource> paths = new LinkedList<PluginResource>();
private static LinkedList<Observer> observers = new LinkedList<Observer>();
public static void setCurrentLocale(locale currentlocale) {
locale = currentlocale;
}
public static synchronized void initialize() throws Exception{
if(locale==null) {
throw new NullPointerException("The Locale is null!");
}
String path = locale.getFile();
String charsetName = locale.getCharsetName();
String filename = locale.getBasePath()+File.separator+path;
JarFile jarFile = new JarFile(filename);
JarEntry jarEntry = jarFile.getJarEntry("language.properties");
if(jarEntry==null) {
throw new NullPointerException("The language.properties hasn't been found!");
}
InputStream in = jarFile.getInputStream(jarEntry);
pr.addResource(in,charsetName);
in.close();
//other resource
int size = paths.size();
PluginResource plr;
JarFile jarfile;
JarEntry jarentry;
path += "!/language.properties";
for(int i=0;i<size;i++){
plr = paths.get(i);
try {
jarfile = plr.jarfile;
jarentry = jarfile.getJarEntry(getLocalePathForPlugin(plr.localepath)+"language.properties");
in = jarfile.getInputStream(jarentry);
pr.addResource(in,charsetName);
in.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
jarfile = null;
jarentry = null;
in.close();
}
public static void switchLocale() throws Exception {
initialize();
sendNotify();
}
/**
* Get the string for a give key from resource
* @param key the key for the desired string
* @param defStr the default string to return if not found the string for this key
* @return the string for the given key. If not found the string for this key, just return defStr
* */
public static synchronized String srcStr(String key,String defStr) {
String ret = "";
try {
ret = pr.getString(key);
if(ret==null){
return defStr;
}
return ret;
} catch (Exception e) {
SysLog.error(null,e);
return defStr;
}
}
/**
* Get the string for a give key from resource
* @param key the key for the desired string
* @return the string for the given key. If not found the string for this key, just return key
* */
public static synchronized String srcStr(String key) {
String ret = "";
try {
ret = pr.getString(key);
if(ret==null){
return key;
}
return ret;
} catch (Exception e) {
SysLog.error(null,e);
return key;
}
}
public static locale getLocale(){
return locale;
}
public static String getBasePath() {
return locale.getBasePath();
}
public static String getFile() {
return locale.getFile();
}
public static String getWholePath() {
return locale.getBasePath()+"/"+locale.getFile();
}
public static String getCharsetName() {
return locale.getCharsetName();
}
public static String getLocalePath(String localedir){
if(localedir==null){
return null;
}
return localedir+File.separator + locale.getFile()+"/";
}
public static String getLocalePathForPlugin(String localedir){
if(localedir==null){
return null;
}
String file = locale.getFile();
if(file!=null && file.indexOf(".jar")>0){
file = file.substring(0, file.length()-4);
}
return localedir+ "/" + file+"/";
}
public static String getLocalePathinJar(String localedir){
if(localedir==null){
return null;
}
if(locale.getLocaleStrng()!=null) {
return localedir+"/" + locale.getLocaleStrng()+"/";
}
String file = locale.getFile();
if(file==null) {
return null;
}
if(file.endsWith(".jar")) {
file = file.substring(0, file.length()-4);
}
return localedir+"/" + file+"/";
}
public static URL getflashImageURL() throws MalformedURLException {
return eti.bi.common.SystemAdapter.JarURL(getWholePath()+"!/splash.png");
}
public static void registerPluginResource(PluginResource resourcepath) throws IOException{
if(resourcepath==null){
throw new IOException("No file defined");
}
if(!resourcepath.isvalid()){
return;
}
JarEntry jarentry;
JarFile jarfile = resourcepath.jarfile;
jarentry = jarfile.getJarEntry(getLocalePathForPlugin(resourcepath.localepath)+"language.properties");
InputStream in = jarfile.getInputStream(jarentry);
if(locale!=null){
pr.addResource(in, locale.getCharsetName());
}
jarfile = null;
in.close();
paths.add(resourcepath);
}
public static void close(){
}
/**
* register locale observer
* */
public static void registerInterest(Observer a_Observer) {
observers.add(a_Observer);
}
public static void removeIntereat(Observer a_Observer) {
observers.remove(a_Observer);
}
/**
* send locale notify to all observer
* */
private static void sendNotify() {
Iterator<Observer> iterator = observers.iterator();
while(iterator.hasNext()) {
iterator.next().sendNotify(CHANGE_LOCALE);
}
}
/**
* @param aLocale a Locale
* @return <code>true<code> if aLocale is equal to the current Locale
* */
public static boolean equalLocale(Locale aLocale) {
if(aLocale==null) {
return false;
}
String ls = locale.getLocaleStrng();
if(ls==null) {
return false;
}
if(ls.equals(aLocale.getCountry())||ls.equals(aLocale.getLanguage())||ls.equals(aLocale.toString())) {
return true;
}
else {
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -