www.pudn.com > TestToolsProject.rar > DataBaseConfigSetForm.java, change:2014-08-18,size:19062b


package com.mervyn.testtools; 
 
import java.awt.BorderLayout; 
import java.awt.Toolkit; 
 
import javax.swing.ImageIcon; 
import javax.swing.JFileChooser; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JFrame; 
import java.awt.Dimension; 
import javax.swing.JLabel; 
import java.awt.Rectangle; 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.RandomAccessFile; 
 
import javax.swing.JTextField; 
import javax.swing.JButton; 
 
import com.mervyn.testsource.CustomJOptionPane; 
import com.mervyn.testsource.MyFileFilter; 
 
public class DataBaseConfigSetForm extends JFrame { 
 
	private static final long serialVersionUID = 1L; 
 
	private JPanel jContentPane = null; 
 
	private JLabel jLabel = null; 
 
	private JLabel jLabel1 = null; 
 
	private static JTextField jTextField = null; 
 
	private static JTextField jTextField1 = null; 
 
	private JButton jButton = null; 
 
	private JButton jButton1 = null; 
 
	private JButton jButton2 = null; 
 
	private JButton jButton3 = null; 
	JFileChooser fc = new JFileChooser(); 
 
	private JButton jButton4 = null; 
	private String RemoteDatabasePath="",LocalDatabasePath="",LocalBackUPdatabasePath="",FiberHomeDatabasePath=""; 
	private String LocalIP="",RemoteIP="";  //  @jve:decl-index=0: 
 
	private JLabel jLabel2 = null; 
 
	private JLabel jLabel3 = null; 
 
	private JLabel jLabel4 = null; 
 
	private static JTextField jTextField2 = null; 
 
	private JButton jButton5 = null; 
 
	private static JTextField jTextField3 = null; 
 
	private static JTextField jTextField4 = null; 
 
	private JLabel jLabel5 = null; 
 
	private static JTextField jTextField5 = null; 
 
	private JButton jButton6 = null; 
	public String getLocalDatabasePath() { 
		return LocalDatabasePath; 
	} 
 
 
	public void setDatabasePath(){ 
		TestToolsPing Database=new TestToolsPing(); 
		LocalDatabasePath = Database.GetLocalDatabasePath(); 
		RemoteDatabasePath = Database.GetRemoteDatabasePath(); 
		LocalBackUPdatabasePath=Database.GetLocalBackUPDatabasePath(); 
		LocalIP=Database.GetLocalIP(); 
		RemoteIP=Database.GetRemoteIP(); 
		FiberHomeDatabasePath=Database.GetFiberHomeDatabasePath(); 
		 
	} 
 
 
	public String getRemoteDatabasePath() {  
		return RemoteDatabasePath; 
	} 
 
 
 
 
	/** 
	 * This is the default constructor 
	 */ 
	public DataBaseConfigSetForm() { 
		super(); 
		initialize(); 
		setDatabasePath(); 
		LoadDatabaseConfig(); 
		 
	} 
	public void LoadDatabaseConfig() 
	{ 
		jTextField.setText(LocalDatabasePath); 
		jTextField1.setText(RemoteDatabasePath); 
		jTextField2.setText(LocalBackUPdatabasePath); 
		jTextField3.setText(RemoteIP); 
		jTextField4.setText(LocalIP); 
		jTextField5.setText(FiberHomeDatabasePath); 
	} 
 
	/** 
	 * This method initializes this 
	 *  
	 * @return void 
	 */ 
	private void initialize() { 
		this.setSize(475, 293); 
		this.setContentPane(getJContentPane()); 
		this.setTitle("数据库配置"); 
		int windowWidth = this.getWidth(); // 获得窗口宽 
		int windowHeight = this.getHeight(); // 获得窗口高 
		Toolkit kit = Toolkit.getDefaultToolkit(); // 定义工具包 
		Dimension screenSize = kit.getScreenSize(); // 获取屏幕的尺寸 
		int screenWidth = screenSize.width; // 获取屏幕的宽 
		int screenHeight = screenSize.height; // 获取屏幕的高 
		this.setLocation(screenWidth / 2 - windowWidth / 2, screenHeight / 2 
				- windowHeight / 2);// 设置窗口居中显示 
	} 
	private String CreateFileSelect(String str) 
	{ 
		String ReturnValue=""; 
		try{ 
			File SelectFilePath = null; 
			fc.setMultiSelectionEnabled(false);// 选择模式,可选择文件和文件夹 
			fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 
			fc.setFileHidingEnabled(true); // 设置是否显示隐藏文件 
			fc.setAcceptAllFileFilterUsed(false); 
			fc.removeChoosableFileFilter(fc.getAcceptAllFileFilter());  //移去所有文件过滤器 
		    fc.addChoosableFileFilter(new MyFileFilter("mdb","Access文件")); 
		    int returnValue = fc.showOpenDialog(null); 
			if (returnValue == JFileChooser.APPROVE_OPTION) { 
				ReturnValue = fc.getSelectedFile().getPath(); 
			} 
			else 
			{ 
				ReturnValue=str; 
			} 
		}catch(NullPointerException NullFile) 
		{ 
			NullFile.printStackTrace(); 
			ReturnValue=str; 
		} 
		return ReturnValue; 
		 
	} 
 
	/** 
	 * This method initializes jContentPane 
	 *  
	 * @return javax.swing.JPanel 
	 */ 
	private JPanel getJContentPane() { 
		if (jContentPane == null) { 
			jLabel5 = new JLabel(); 
			jLabel5.setBounds(new Rectangle(12, 170, 82, 26)); 
			jLabel5.setText("烽火数据库:"); 
			jLabel4 = new JLabel(); 
			jLabel4.setBounds(new Rectangle(12, 77, 82, 26)); 
			jLabel4.setText("备份数据库:"); 
			jLabel3 = new JLabel(); 
			jLabel3.setBounds(new Rectangle(12, 43, 82, 26)); 
			jLabel3.setText("远程测试IP:"); 
			jLabel2 = new JLabel(); 
			jLabel2.setBounds(new Rectangle(12, 9, 82, 26)); 
			jLabel2.setText("本地测试IP:"); 
			jLabel1 = new JLabel(); 
			jLabel1.setBounds(new Rectangle(12, 141, 82, 26)); 
			jLabel1.setText("远程数据库:"); 
			jLabel = new JLabel(); 
			jLabel.setBounds(new Rectangle(12, 109, 82, 26)); 
			jLabel.setText("本地数据库:"); 
			jContentPane = new JPanel(); 
			jContentPane.setLayout(null); 
			jContentPane.add(jLabel, null); 
			jContentPane.add(jLabel1, null); 
			jContentPane.add(getJTextField(), null); 
			jContentPane.add(getJTextField1(), null); 
			jContentPane.add(getJButton(), null); 
			jContentPane.add(getJButton1(), null); 
			jContentPane.add(getJButton2(), null); 
			jContentPane.add(getJButton3(), null); 
			jContentPane.add(getJButton4(), null); 
			jContentPane.add(jLabel2, null); 
			jContentPane.add(jLabel3, null); 
			jContentPane.add(jLabel4, null); 
			jContentPane.add(getJTextField2(), null); 
			jContentPane.add(getJButton5(), null); 
			jContentPane.add(getJTextField3(), null); 
			jContentPane.add(getJTextField4(), null); 
			jContentPane.add(jLabel5, null); 
			jContentPane.add(getJTextField5(), null); 
			jContentPane.add(getJButton6(), null); 
		} 
		return jContentPane; 
	} 
 
	/** 
	 * This method initializes jTextField	 
	 * 	 
	 * @return javax.swing.JTextField	 
	 */ 
	private JTextField getJTextField() { 
		if (jTextField == null) { 
			jTextField = new JTextField(); 
			jTextField.setBounds(new Rectangle(98, 109, 287, 26)); 
		} 
		return jTextField; 
	} 
 
	/** 
	 * This method initializes jTextField1	 
	 * 	 
	 * @return javax.swing.JTextField	 
	 */ 
	private JTextField getJTextField1() { 
		if (jTextField1 == null) { 
			jTextField1 = new JTextField(); 
			jTextField1.setBounds(new Rectangle(98, 141, 287, 26)); 
		} 
		return jTextField1; 
	} 
 
	/** 
	 * This method initializes jButton	 
	 * 	 
	 * @return javax.swing.JButton	 
	 */ 
	private JButton getJButton() { 
		if (jButton == null) { 
			jButton = new JButton(); 
			jButton.setBounds(new Rectangle(385, 109, 70, 25)); 
			jButton.setText("浏览"); 
			jButton.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {				 
						jTextField.setText(CreateFileSelect(jTextField.getText()));		 
				} 
			}); 
		} 
		return jButton; 
	} 
 
	/** 
	 * This method initializes jButton1	 
	 * 	 
	 * @return javax.swing.JButton	 
	 */ 
	private JButton getJButton1() { 
		if (jButton1 == null) { 
			jButton1 = new JButton(); 
			jButton1.setBounds(new Rectangle(385, 141, 70, 25)); 
			jButton1.setText("浏览"); 
			jButton1.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) { 
						jTextField1.setText(CreateFileSelect(jTextField1.getText())); 
				} 
			}); 
		} 
		return jButton1; 
	} 
 
	/** 
	 * This method initializes jButton2	 
	 * 	 
	 * @return javax.swing.JButton	 
	 */ 
	private JButton getJButton2() { 
		if (jButton2 == null) { 
			jButton2 = new JButton(); 
			jButton2.setBounds(new Rectangle(189, 224, 89, 29)); 
			jButton2.setText("修改"); 
			jButton2.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) { 
					UpdateProgramConfigFile(); 
					TestToolsPing TestForm = new TestToolsPing(); 
					TestForm.setVisible(true); 
					setVisible(false); 
				} 
			}); 
		} 
		return jButton2; 
	} 
	public static void UpdateProgramConfigFile() 
	{ 
		File directory = new File("."); 
		CustomJOptionPane JOption=new CustomJOptionPane(); 
		String str=""; 
		try { 
			str=directory.getCanonicalPath()+"ico/stop_mac.png"; 
			boolean NotMessage=false; 
			String FilePath = directory.getCanonicalPath() 
			+ "\\ProgramConfigFile.ini"; 
			String encoding = "GBK"; 
			File file = new File(FilePath); 
				if (file.isFile() && file.exists()) { // 判断文件是否存在 
					InputStreamReader read = new InputStreamReader( 
							new FileInputStream(file), encoding);// 考虑到编码格式 
					BufferedReader bufferedReader = new BufferedReader(read); 
					String lineTxt = null; 
					int FlagValue=0; 
					String FileWriteStr=""; 
					DeleteFileSub(FilePath); 
					String BatchIP="",LocalIP="",RemoteIP="",LocalDataPath="",RemoteDataPath=""; 
					int i=0; 
					while ((lineTxt = bufferedReader.readLine()) != null) { 
						 
						if(lineTxt.contains("Batch open Telnet port IP:")) 
						{ 
							//System.out.println("IPO:"+lineTxt); 
							FileWriteStr=FileWriteStr+lineTxt+"\r\n"; 
							//System.out.println("a:"+FileWriteStr); 
						} 
						else if(lineTxt.contains("Local connectivity test IP address:")) 
						{ 
							FileWriteStr=FileWriteStr+"Local connectivity test IP address:"+jTextField4.getText()+"\r\n"; 
							//System.out.println("b:"+FileWriteStr); 
						} 
						else if(lineTxt.contains("Remote connectivity test IP address:")) 
						{ 
							FileWriteStr=FileWriteStr+"Remote connectivity test IP address:"+jTextField3.getText()+"\r\n"; 
							//System.out.println("c:"+FileWriteStr); 
						} 
						else if(lineTxt.contains("The local database path:")) 
						{ 
							FileWriteStr=FileWriteStr+"The local database path:"+jTextField.getText()+"\r\n"; 
							//System.out.println("d:"+FileWriteStr); 
						} 
						else if(lineTxt.contains("Remote database path:")) 
						{ 
							FileWriteStr=FileWriteStr+"Remote database path:"+jTextField1.getText()+"\r\n"; 
							//System.out.println("e:"+FileWriteStr); 
						} 
						else if(lineTxt.contains("The local backup database path:")) 
						{ 
							FileWriteStr=FileWriteStr+"The local backup database path:"+jTextField2.getText()+"\r\n"; 
							//System.out.println("e:"+FileWriteStr); 
						} 
						else if(lineTxt.contains("FiberHome database path:")) 
						{ 
							FileWriteStr=FileWriteStr+"FiberHome database path:"+jTextField5.getText()+"\r\n"; 
						} 
						else 
						{ 
							FileWriteStr=FileWriteStr+lineTxt+"\r\n"; 
						} 
					} 
					//System.out.println("Test!!"); 
					CreateFile(FilePath,FileWriteStr); 
					//System.out.println(FileWriteStr); 
					read.close(); 
					String PicturePath=directory.getCanonicalPath()+"/ico/CurrentSkinImage.png"; 
					String ShowMessage="数据库配置完成!!"; 
					String PromptMessage="提示信息"; 
					JOption.CreateJOpeionPane(PicturePath, ShowMessage, PromptMessage); 
					 
				}  
		} catch (Exception e) { 
			 
			String ShowMessage="读取文件内容出错!!"; 
			String PromptMessage="错误信息"; 
			JOption.CreateJOpeionPane(str, ShowMessage, PromptMessage); 
			e.printStackTrace(); 
		} 
	} 
	public static String StrCompareSub(String SourceStr, String Reference, 
			String EndFlag) { 
		String ReturnStrValue = ""; 
		int CompareStart = 0; 
		int CompareEnd = 0; 
		int ReferenceLength = Reference.length(); 
		int LocationNum = SourceStr.indexOf(Reference); 
		CompareStart = LocationNum + ReferenceLength; 
		CompareEnd = CompareStart; 
		while (!SourceStr.substring(CompareEnd, CompareEnd + 1).equals(EndFlag)) { 
			CompareEnd = CompareEnd + 1; 
		} 
		ReturnStrValue = SourceStr.substring(CompareStart, CompareEnd); 
		return ReturnStrValue; 
	} 
	public static void UpdateProgramConfigFileinitialize() 
	{ 
		try { 
			File directory = new File("."); 
			String FilePath = directory.getCanonicalPath() 
			+ "\\ProgramConfigFile.ini"; 
			String encoding = "GBK"; 
			File file = new File(FilePath); 
				if (file.isFile() && file.exists()) { // 判断文件是否存在 
					InputStreamReader read = new InputStreamReader( 
							new FileInputStream(file), encoding);// 考虑到编码格式 
					BufferedReader bufferedReader = new BufferedReader(read); 
					String lineTxt = null; 
					int FlagValue=0; 
					String FileWriteStr=""; 
					FileWriteStr=FileWriteStr+"#TestTools Program initialize setting....\r\n"; 
					FileWriteStr=FileWriteStr+"Batch open Telnet port IP:192.168.1.\r\n"; 
					FileWriteStr=FileWriteStr+"Local connectivity test IP address:192.168.1.1\r\n"; 
					FileWriteStr=FileWriteStr+"Remote connectivity test IP address:192.168.1.206\r\n"; 
					FileWriteStr=FileWriteStr+"The local database path:E:/VSTBackup/VSTDatabaseLocal.mdb\r\n"; 
					FileWriteStr=FileWriteStr+"The local backup database path:E:/VSTBackup/TestToolsPonBackup.mdb\r\n"; 
					FileWriteStr=FileWriteStr+"Remote database path:D:/AcessDataBase/pondata.mdb\r\n"; 
					FileWriteStr=FileWriteStr+"FiberHome database path:E:/VSTBackup/ProductManage.mdb\r\n"; 
					FileWriteStr=FileWriteStr+"#the end Program Config!!\r\n"; 
					DeleteFileSub(FilePath); 
					CreateFile(FilePath,FileWriteStr); 
					read.close(); 
					String PicturePath=directory.getCanonicalPath()+"/ico/CurrentSkinImage.png"; 
					String ShowMessage="数据库初始化配置完成!!"; 
					String PromptMessage="提示信息"; 
					CustomJOptionPane JOption=new CustomJOptionPane(); 
					JOption.CreateJOpeionPane(PicturePath, ShowMessage, PromptMessage); 
					 
				}  
		} catch (Exception e) { 
			JOptionPane.showMessageDialog(null, "写入文件内容出错", "错误信息", 
					JOptionPane.ERROR_MESSAGE); 
			e.printStackTrace(); 
		} 
	} 
	private static void DeleteFileSub(String FilePath) 
	{ 
		File DelFile = new File(FilePath); 
		boolean  IsFile=DelFile.delete(); 
	} 
	public static void CreateFile(String FilePath, String WriteStr) { 
		File CreateFileObject = new File(FilePath); 
		RandomAccessFile RCreateFile = null; 
		try { 
			RCreateFile = new RandomAccessFile(CreateFileObject, "rw"); 
			RCreateFile.writeBytes(WriteStr); 
		} catch (IOException e1) { 
			e1.printStackTrace(); 
		} finally { 
			if (RCreateFile != null) { 
				try { 
					RCreateFile.close(); 
				} catch (IOException e2) { 
					e2.printStackTrace(); 
				} 
			} 
		} 
	} 
	public static void readTxtFile(String filePath) { 
		try { 
			String encoding = "GBK"; 
			File file = new File(filePath); 
				if (file.isFile() && file.exists()) { // 判断文件是否存在 
					InputStreamReader read = new InputStreamReader( 
							new FileInputStream(file), encoding);// 考虑到编码格式 
					BufferedReader bufferedReader = new BufferedReader(read); 
					String lineTxt = null; 
					int FlagValue=0; 
					while ((lineTxt = bufferedReader.readLine()) != null) { 
						jTextField.setText(lineTxt); 
						jTextField1.setText(bufferedReader.readLine()); 
						break; 
					} 
					read.close(); 
				} else { 
					JOptionPane.showMessageDialog(null, "找不到指定的文件", "错误信息", 
							JOptionPane.ERROR_MESSAGE); 
				} 
			 
 
 
		} catch (Exception e) { 
			JOptionPane.showMessageDialog(null, "读取文件内容出错", "错误信息", 
					JOptionPane.ERROR_MESSAGE); 
			e.printStackTrace(); 
		} 
 
	} 
	/** 
	 * This method initializes jButton3	 
	 * 	 
	 * @return javax.swing.JButton	 
	 */ 
	private JButton getJButton3() { 
		if (jButton3 == null) { 
			jButton3 = new JButton(); 
			jButton3.setBounds(new Rectangle(367, 224, 89, 29)); 
			jButton3.setText("取消"); 
			jButton3.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) { 
					TestToolsPing TestForm = new TestToolsPing(); 
					TestForm.setVisible(true); 
					setVisible(false); 
					 
				} 
			}); 
		} 
		return jButton3; 
	} 
 
	/** 
	 * This method initializes jButton4	 
	 * 	 
	 * @return javax.swing.JButton	 
	 */ 
	private JButton getJButton4() { 
		if (jButton4 == null) { 
			jButton4 = new JButton(); 
			jButton4.setBounds(new Rectangle(278, 224, 89, 29)); 
			jButton4.setText("重置配置"); 
			jButton4.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) { 
					UpdateProgramConfigFileinitialize(); 
					setDatabasePath(); 
					LoadDatabaseConfig(); 
					TestToolsPing TestForm = new TestToolsPing(); 
					TestForm.setVisible(true); 
					setVisible(false); 
				} 
			}); 
		} 
		return jButton4; 
	} 
 
 
	/** 
	 * This method initializes jTextField2	 
	 * 	 
	 * @return javax.swing.JTextField	 
	 */ 
	private JTextField getJTextField2() { 
		if (jTextField2 == null) { 
			jTextField2 = new JTextField(); 
			jTextField2.setBounds(new Rectangle(98, 77, 287, 26)); 
		} 
		return jTextField2; 
	} 
 
 
	/** 
	 * This method initializes jButton5	 
	 * 	 
	 * @return javax.swing.JButton	 
	 */ 
	private JButton getJButton5() { 
		if (jButton5 == null) { 
			jButton5 = new JButton(); 
			jButton5.setBounds(new Rectangle(385, 76, 70, 25)); 
			jButton5.setText("浏览"); 
			jButton5.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) { 
					jTextField2.setText(CreateFileSelect(jTextField2.getText()));	 
				} 
			}); 
		} 
		return jButton5; 
	} 
 
 
	/** 
	 * This method initializes jTextField3	 
	 * 	 
	 * @return javax.swing.JTextField	 
	 */ 
	private JTextField getJTextField3() { 
		if (jTextField3 == null) { 
			jTextField3 = new JTextField(); 
			jTextField3.setBounds(new Rectangle(98, 43, 287, 26)); 
		} 
		return jTextField3; 
	} 
 
 
	/** 
	 * This method initializes jTextField4	 
	 * 	 
	 * @return javax.swing.JTextField	 
	 */ 
	private JTextField getJTextField4() { 
		if (jTextField4 == null) { 
			jTextField4 = new JTextField(); 
			jTextField4.setBounds(new Rectangle(98, 7, 287, 26)); 
		} 
		return jTextField4; 
	} 
 
 
	/** 
	 * This method initializes jTextField5	 
	 * 	 
	 * @return javax.swing.JTextField	 
	 */ 
	private JTextField getJTextField5() { 
		if (jTextField5 == null) { 
			jTextField5 = new JTextField(); 
			jTextField5.setBounds(new Rectangle(98, 171, 287, 26)); 
		} 
		return jTextField5; 
	} 
 
 
	/** 
	 * This method initializes jButton6	 
	 * 	 
	 * @return javax.swing.JButton	 
	 */ 
	private JButton getJButton6() { 
		if (jButton6 == null) { 
			jButton6 = new JButton(); 
			jButton6.setBounds(new Rectangle(385, 171, 70, 25)); 
			jButton6.setText("浏览"); 
			jButton6.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) { 
					jTextField5.setText(CreateFileSelect(jTextField1.getText())); 
				} 
			}); 
		} 
		return jButton6; 
	} 
 
}  //  @jve:decl-index=0:visual-constraint="10,10"