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

📄 localesmanager.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 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/09/04
 *
 * @Author: Xiaojun Chen
 * $Revision$ 1.0
 *
 */
package eti.bi.alphaminer.Locale;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import eti.bi.alphaminer.ui.ApplicationWindow;
import eti.bi.alphaminer.ui.dialog.MessageDialog;
import eti.bi.common.Locale.Locales;
import eti.bi.common.Locale.LocalesLoader;
import eti.bi.common.Locale.Resource;
import eti.bi.common.Locale.locale;
import eti.bi.common.System.SysConfig;
import eti.bi.common.System.SysLog;
import eti.bi.exception.SysException;

public class LocalesManager {
	private static ApplicationWindow m_container;
	private static boolean hasload = false;
	private static Locales locales;
	private static LocaleConfiguration localeConfiguration = null;

	public static void setContainer(ApplicationWindow applicationWindow) {
		if (m_container == null) {
			m_container = applicationWindow;
		}
	}

	public static void load() throws SysException {
		if (hasload && locales != null) {
			return;
		}
		LocalesLoader.setDefinitionFile(SysConfig.getProperty("localesdefinition"));
		LocalesLoader.setBasePath(SysConfig.getLocaleHomen());
		File definitionFile = new File(LocalesLoader.getDefinitionFile());
		if (definitionFile.isFile()) {
			LocalesLoader.loadLocales();
			locales = LocalesLoader.getLocales();
		}
		if (locales == null) {
			locales = new Locales();
		}
		// add default
		if (locales.size() == 0) {
			// TODO AUTO Scan
			File localehome = new File(SysConfig.getLocaleHomen());
			File[] localejars = localehome.listFiles(new FilenameFilter() {
				public boolean accept(File dir, String name) {
					if (name.endsWith(".jar")) {
						return true;
					}
					return false;
				}
			});
			if (!(localejars == null || localejars.length == 0)) {
				for (int i = 0; i < localejars.length; i++) {
					try {
						locales.AddLocale(getLocalefromjar(localejars[i]));
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		hasload = true;
	}

	public static locale getLocalefromjar(File jarfile) throws IOException {
		JarFile jar = new JarFile(jarfile);
		Manifest manifest = jar.getManifest();
		Attributes atts = manifest.getMainAttributes();
		locale newlocale = new locale(atts.getValue("name"), atts.getValue("localString"), jarfile.getParent(), jarfile.getName()
				, atts.getValue("description"), atts.getValue("charsetName"));
		if (!newlocale.isvalid()) {
			SysLog.error("The locale jar" + jarfile.getAbsolutePath()
					+ " hasn't manifest or the manifest has bad format!");
			return null;
		}
		return newlocale;
	}

	public static Locales getLocales() {
		return locales;
	}

	public static locale getLocale(int index) {
		if (locales == null) {
			return null;
		}
		return locales.getLocale(index);
	}

	public static String[] getLocalesNames() {
		if (locales != null) {
			return locales.getLocalesNames();
		} else {
			return new String[0];
		}
	}

	public static String[][] getLocalesNameLocale() {
		if (locales != null) {
			return locales.getLocalesNameLocale();
		} else {
			return new String[0][0];
		}
	}

	public static int getCountofLocales() {
		if (locales == null) {
			return 0;
		} else {
			return locales.size();
		}
	}

	public static boolean hasLocale(locale locale) {
		if (locales == null) {
			return false;
		} else {
			return locales.hasLocale(locale);
		}
	}

	public static int CurrentlocaleIndex() {
		if (locales != null) {
			return locales.CurrentlocalesIndex();
		} else {
			return -1;
		}
	}

	public static String CurrentLocaleFile() {
		if (locales != null) {
			return locales.CurrentLocale().getFile();
		}
		return null;
	}

	public static locale getCurrentLocale() {
		if (locales != null) {
			return locales.CurrentLocale();
		}
		return null;
	}

	/**
	 * @author Xiaojun Chen
	 * @param reload
	 *            true if the current workspace need reloaded
	 */
	public static boolean SwitchLocale(int index, boolean reload) {
		if (index == CurrentlocaleIndex()) {
			if (!reload) {
				return true;
			}
		}
		if (locales == null) {
			return false;
		}
		if (!locales.SwitchCurrentlocale(index)) {
			return false;
		}
		Resource.setCurrentLocale(getCurrentLocale());
		try {
			Resource.switchLocale();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		if (!writeXML()) {
			return false;
		}
		return true;
	}

	/**
	 * @author Xiaojun Chen reload the current workspace
	 */
	public static boolean ReloadLocale() {
		return SwitchLocale(CurrentlocaleIndex(), true);
	}

	@SuppressWarnings("deprecation")
	public static void configuration() {
		if (localeConfiguration == null) {
			localeConfiguration = new LocaleConfiguration();
		}
		locales.resetChangeMonitor();
		int formWidth = localeConfiguration.getWidth();
		int formHeight = localeConfiguration.getHeight();
		Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
		int formLocationX = (((screenDim.width - formWidth) / 2) < m_container.getLocation().x) ? m_container
				.getLocation().x : (screenDim.width - formWidth) / 2;
		int formLocationY = ((screenDim.height / 2 - formHeight) < m_container.getLocation().y) ? m_container
				.getLocation().y : (screenDim.height / 2 - formHeight);
		localeConfiguration.setLocation(formLocationX, formLocationY);
		localeConfiguration.show();
	}

	/**
	 * @author Xiaojun Chen load by LocaleConfiguration.close()
	 */
	public static void configurationExit() {
		m_container.updateLocales();
		if(locales.needreload()) {
			ReloadLocale();
		}
	}

	public static boolean deleteLocale(int index) {
		if (locales == null) {
			return false;
		}
		if (!locales.deleteLocale(index)) {
			return false;
		}
		if (!writeXML()) {
			return false;
		}
		return true;
	}

	public static boolean updateLocale(int index, locale Locale) {
		if (locales == null) {
			return false;
		}
		if (!locales.updateLocale(index, Locale)) {
			return false;
		}
		if (!writeXML()) {
			return false;
		}
		return true;
	}

	public static boolean addLocale(int index, locale Locale, Component parent) {
		if (locales == null||Locale==null) {
			return false;
		}
		
		//if the same name locale exists, tell the user
		if(locales.existsLocales(Locale.getName())) {
			MessageDialog.showMessageDialog(parent, "The locale with the name of '"+Locale.getName()+"' has already existed!");
			return false;
		}
		
		//copy file to Locale home
		if(!Locale.getBasePath().equals(SysConfig.getLocaleHomen())) {
			File file = new File(Locale.getBasePath()+"/"+Locale.getFile());
			File newFile = new File(SysConfig.getLocaleHomen()+File.separator+file.getName());
			InputStream in = null;
			OutputStream ou = null;
			try {
				in = new FileInputStream(file);
				
				ou = new FileOutputStream(newFile);
				byte[] bytes = new byte[5000];
				int read = 0;
				int size = in.available();
				int hasread = 0;
				int step = 0;
				
				while(hasread<size) {
					read=0;
					step = 5000>(size-hasread)?(size-hasread):5000;
					while(read<step) {
						read += in.read(bytes, read, step-read);
					}
					hasread += read;
					ou.write(bytes, 0, read);
				}
			}
			catch(Exception e) {
				e.printStackTrace();
				MessageDialog.showMessageDialog(parent, e.getMessage());
				return false;
			}
			finally {
				try{
					if(in!=null) {
						in.close();
					}
					if(ou!=null) {
						ou.close();
					}
				}
				catch(Exception e) {
					e.printStackTrace();
				}
				
			}
		}
		
		if (!locales.AddLocale(Locale, index)) {
			return false;
		}
		
		if (!writeXML()) {
			return false;
		}
		return true;
	}

	public static locale ImportLocale(Component parent) throws IOException {
		File aFile = null;
		JFileChooser filechooser = new JFileChooser();
		filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		filechooser.setFileFilter(new FileFilter() {
			@Override
			public boolean accept(File f) {
				if (f.isDirectory() || f.getName().endsWith(".jar")) {
					return true;
				}
				return false;
			}

			@Override
			public String getDescription() {
				return ".jar";
			}
		});
		if (filechooser.showOpenDialog(parent) != JFileChooser.APPROVE_OPTION) {
			return null;
		}
		aFile = filechooser.getSelectedFile();
		if (aFile == null || (!aFile.exists() || (!aFile.getName().endsWith(".jar")))) {
			return null;
		}
		return getLocalefromjar(aFile);
	}

	/**
	 * return if writing Locales configuration file succeed
	 */
	public static boolean writeXML() {
		if (locales == null) {
			return false;
		}
		// backup
		BufferedReader definitionFile = null;
		StringBuffer sb = new StringBuffer();
		FileWriter definition;
		try {
			definitionFile = new BufferedReader(new FileReader(LocalesLoader.getDefinitionFile()));
			String aLine = null;
			while ((aLine = definitionFile.readLine()) != null) {
				sb.append(aLine + "\n");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (definitionFile != null) {
					definitionFile.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		}
		BufferedWriter bfw = null;
		try {
			definition = new FileWriter(LocalesLoader.getDefinitionFile());
			bfw = new BufferedWriter(definition);
			bfw.write(locales.toXML());
			return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			// recover
			e.printStackTrace();
			if (sb.length() > 0) {
				try {
					definition = new FileWriter(LocalesLoader.getDefinitionFile());
					bfw = new BufferedWriter(definition);
					bfw.write(sb.toString());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			return false;
		} finally {
			try {
				if (bfw != null) {
					bfw.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		}
	}
}

⌨️ 快捷键说明

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