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

📄 preferencesmanager.java

📁 是一款国外的网络游戏平台的源码*不是类似浩方那种虚拟局域网技术
💻 JAVA
字号:
/*
 * Created on Apr 25, 2006
 */
package org.GTADS.client.preferences;



import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Properties;

/**
 * @author Administrator
 *
 *
 * Client Side Preferences
 */
public class PreferencesManager {
/*
 * Client side preferences to alter the behavior of the client
 * 
 * Options:
 * 
 * - Language Settings
 * - Geo Location
 * - Proxy Port
 * - Server List File
 * - TimeStamps?
 * 
 *  
 */
	
	private static PreferencesManager instance = null;
	private Properties SystemConfiguration;
	private String configFile = "config.dat";
	private String DEFAULT_CHATROOM = "#GTADS";
	private int DEFAULT_PROXYPORT = 8000;
	private String DEFAULT_LOCALE = "en";
	private String TIME_STAMPS = "yes";
	
	public static PreferencesManager getInstance(){
		if (instance == null){
			instance = new PreferencesManager();
		}
		return instance;
	}
	
	public static void clearInstance(){
		if (instance != null){
			instance = null;
		}
	}
	
	public PreferencesManager(){
		SystemConfiguration = new Properties();
		init();
	}
	
	private void init(){
		// Read in keys and values
		loadInPreferences();
	}
	
	private void loadInPreferences(){
		String buffer = new String();
		try {
			File configData = new File(configFile);
			FileReader configReader = new FileReader(configData);
			BufferedReader inputBuffer = new BufferedReader(configReader);
			
			boolean isEOF = false;
			
			while (!isEOF){
				buffer = inputBuffer.readLine();
				if (buffer == null)
					isEOF = true;
				else {
					String[] inputKeyValue = buffer.split("=");
					if (inputKeyValue.length == 2){
						SystemConfiguration.setProperty(inputKeyValue[0].toUpperCase(),inputKeyValue[1]);
					}
				}
			}
			inputBuffer.close();
		} catch (FileNotFoundException fnf){
		} catch (IOException ioe){
		}
		return;
	}
	
	protected boolean writeOutPreferences(){
		// Read in file, ignore comments
		// Find where original key=values are and replace them
		// For all else append
		ArrayList configFileData = new ArrayList();
		
		try {
			File configData = new File(configFile);
			DataInputStream configDataStream = new DataInputStream(new FileInputStream(configData));
			String temp = new String();
			boolean isEOF = false;
			
			while (!isEOF){
				temp = configDataStream.readLine();
				if (temp == null)
					isEOF = true;
				else
					configFileData.add(temp);
			}
		} catch (FileNotFoundException fne){
			return false;
		}
		catch (IOException ioe){
			return false;
		}
		
		// Analyze the file and then write
		ArrayList otherValues = new ArrayList();
		for (int i = 0; i < configFileData.size(); i++){
			String temp = configFileData.get(i).toString();
			String[] keyValue;
			
			if (!(temp.startsWith(";") || temp.equals(""))){
				keyValue = temp.split("=");
				if (keyValue.length > 0){
					if (SystemConfiguration.containsKey(keyValue[0].toUpperCase())){
						configFileData.set(i, keyValue[0] + "=" + this.getValue(keyValue[0]));
					}
					else {
						if (keyValue[0] != null){
							otherValues.add(keyValue[0] + "=" + this.getValue(keyValue[0]));
						}
					}
				}
			}
		}
			Enumeration e = SystemConfiguration.keys();
			int count = 0;
			
			while (e.hasMoreElements()){
				String aKey = e.nextElement().toString();
				
				for (int i = 0; i < configFileData.size(); i++){
					String temp = configFileData.get(i).toString();
					if (!(temp.startsWith(";") || temp.equals(""))){
						String[] splitString = temp.split("=");
						if (splitString.length > 0){
							if (splitString[0].equalsIgnoreCase(aKey)){
								count++;
							}
						}
					}	
				}
				if (count == 0){
					otherValues.add(aKey + "=" + SystemConfiguration.getProperty(aKey, ""));
				}
				count = 0;
			}
		
		configFileData.addAll(otherValues); // Append new key values
		
		try {
			File configData = new File(configFile);
			FileWriter configWriter = new FileWriter(configData);
			BufferedWriter outputBuffer = new BufferedWriter(configWriter);
	
			for (int i = 0; i < configFileData.size(); i++){
				outputBuffer.write(configFileData.get(i).toString());
				outputBuffer.newLine();
			}
			outputBuffer.close();
		} catch (IOException ioe){
			return false;
		}
		return true;
	}
	
	public String getLocale(){
		String locale = SystemConfiguration.getProperty("LANGUAGE", "en");
		return locale;
	}
	
	public String getValue(String key) {
		String value = SystemConfiguration.getProperty(key.toUpperCase(),"");
		return value;
	}
	
	public void setValue(String key, String value){
		SystemConfiguration.setProperty(key.toUpperCase(), value);
	}
	
	public String getKey(String value) {
		Enumeration e = (Enumeration)SystemConfiguration.keys();
		String objectValue = new String();
		String key = new String();
		
		while (e.hasMoreElements()){
			objectValue = (String)e.nextElement();
			
			if (SystemConfiguration.getProperty(objectValue, "").equalsIgnoreCase(value)){
				key = objectValue;
				return key;
			}
		}
		return key;
	}
	
	public int getProxyPort(){
		int defaultPort = DEFAULT_PROXYPORT;
		Integer proxyPortInt = new Integer(defaultPort);
		String proxyPortString = SystemConfiguration.getProperty("PROXY PORT", defaultPort + "");
		
		try {
			if (proxyPortString != null) {
				proxyPortInt = Integer.valueOf(proxyPortString);
			}
		} catch (NumberFormatException nfe) {
			return proxyPortInt.intValue();
		}
		return proxyPortInt.intValue();
	}
	
	public String getDefaultChatroom(){
		String defaultChatroom = new String();
		defaultChatroom = SystemConfiguration.getProperty("DEFAULT ROOM", DEFAULT_CHATROOM);
		
		return defaultChatroom;
	}
	
	public String getDefaultUsername(){
		String defaultUser = new String();
		defaultUser = SystemConfiguration.getProperty("USERNAME", "");
		
		return defaultUser;
	}
	
	public String getDefaultPassword(){
		String defaultPassword = new String();
		if (getPasswordSaved()){
			defaultPassword = SystemConfiguration.getProperty("PASSWORD","");
		}
		
		return defaultPassword;
	}
	
	public boolean getPasswordSaved(){
		return SystemConfiguration.getProperty("SAVE PASSWORD", "NO").equalsIgnoreCase("YES") ? true : false;
	}
	
	public boolean getTimeStamps(){
		return SystemConfiguration.getProperty("TIMESTAMPS", TIME_STAMPS).equalsIgnoreCase("YES") ? true : false;
	}
}

⌨️ 快捷键说明

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