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

📄 dbpreferencepage.java

📁 一个专家资料的管理系统
💻 JAVA
字号:
package cn.com.likai.mms.preferences;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import cn.com.likai.mms.Activator;
import cn.com.likai.mms.db.ConnectManager;

public class DBPreferencePage extends PreferencePage implements IWorkbenchPreferencePage,ModifyListener{
	//为文本框定义五个键值
	public static final String CLASSNAME_KEY = "$CLASSNAME_KEY";
	public static final String URL_KEY = "$URL_KEY";
	public static final String USERNAME_KEY = "$USERNAME_KEY";
	public static final String PASSWORD_KEY = "$PASSWORD_KEY";
	public static final String ARCHIVE_EDITOR_RS_NUM_KEY = "ARCHIVE_EDITOR_RS_NUM_KEY";
	
	//为文本框值定义五个默认值
	private static final String CLASSNAME_DEFAULT = "com.mysql.jdbc.Driver";
	private static final String URL_DEFAULT = "jdbc:mysql://localhost/mms";
	private static final String USERNAME_DEFAULT = "root";
	private static final String PASSWORD_DEFAULT = "123456";
	private static final String ARCHIVE_EDITOR_RS_NUM_DEFAULT = "18";
	
	//定义五个文本框
	private Text classNameText,urlText,usernameText,passwordText,archiveText;
	//定义一个IPreferenceStore对象
	private IPreferenceStore ps;
	//定义并创建一个验证监听器.功能:只能输入数字
	private VerifyListener verifyListener = new VerifyListener(){
		public void verifyText(VerifyEvent e){
			e.doit = ("0123456789".indexOf(e.text)>=0);
		}
	};
	
	//接口IWorkbenchPreferencePage方法,负责初始化.在此方法中设置一个
	//PreferenceStore对象,由此对象提供文本框值的读入/写出方法
	public void init(IWorkbench workbench){
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
	}
	
	//父类的界面创建方法
	protected Control createContents(Composite parent){
		ps = getPreferenceStore();
		Composite topComp = new Composite(parent,SWT.NONE);
		topComp.setLayout(new GridLayout());
		createDbConnectGroup(topComp);
		createTableRsCountGroup(topComp);
		return topComp;
	}
	
	//创建数据库连接组
	private void createDbConnectGroup(Composite topComp){
		Group group = new Group(topComp,SWT.NONE);
		group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		group.setLayout(new GridLayout(2,false));
		group.setText("数据库连接");
		classNameText = createText(group,"ClassName:",CLASSNAME_KEY,CLASSNAME_DEFAULT);
		urlText = createText(group,"URL:",URL_KEY,URL_DEFAULT);
	    usernameText = createText(group,"用户名:",USERNAME_KEY,USERNAME_DEFAULT);
	    passwordText = createText(group,"密码:",PASSWORD_KEY,PASSWORD_DEFAULT);
	    passwordText.setEchoChar('*');
	}
	
	//表格记录显示条数组
	private void createTableRsCountGroup(Composite topComp){
		Group group = new Group(topComp,SWT.NONE);
		group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		group.setLayout(new GridLayout(2,false));
		group.setText("表格记录显示条数");
		archiveText = createText(group,"档案管理表格每页显示记录条数:",ARCHIVE_EDITOR_RS_NUM_KEY,ARCHIVE_EDITOR_RS_NUM_DEFAULT);
		archiveText.addVerifyListener(verifyListener);//只能输入数字
	}
	//创建文本框,包括前面的标签
	private Text createText(Composite comp,String label,String saveKey,String defaultValue){
		new Label(comp,SWT.NONE).setText(label);
		Text text = new Text(comp,SWT.BORDER);
		text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		//用key取出以前保存的值,如果取出值为空或空字串,则返回默认值(defaultValue)
		String value = ps.getString(saveKey);
		text.setText(isEmpty(value)?defaultValue:value);
		text.addModifyListener(this);
		return text;
	}
	
	//判断传入的字符串是否为空值或空字串或空格
	private boolean isEmpty(String str){
		return str == null||str.trim().equals("");
	}
	
	//本类实现ModifyListener接口的方法,当3个文本框发生修改时将执行此方法
	//方法中对输入值进行了验证并将"确定","应用"两按钮使能
	public void modifyText(ModifyEvent e){
		String errorStr = null;//将原错误信息清空
		if(isEmpty(classNameText.getText())){
			errorStr = "ClassName不能为空!";
		}else if(isEmpty(urlText.getText())){
			errorStr = "URL不能为空!";
		}else if(isEmpty(usernameText.getText())){
			errorStr = "用户名不能为空!";
		}else if(isEmpty(passwordText.getText())){
			errorStr = "密码不能为空!";
		}else if(isEmpty(archiveText.getText())){
			errorStr = "档案管理表格每页显示记录数不能为空!";
		}
		setErrorMessage(errorStr);//errorStr = null时复原为正常的提示文字
		setValid(errorStr == null);//确定按钮
		getApplyButton().setEnabled(errorStr == null);//应用按钮
	}
	
	//父类方法,单击"复原缺省值"按钮时将执行此方法,取出默认值设置到文本框中
	protected void performDefaults(){
		classNameText.setText(CLASSNAME_DEFAULT);
		urlText.setText(URL_DEFAULT);
		usernameText.setText(USERNAME_DEFAULT);
		passwordText.setText(PASSWORD_DEFAULT);
		archiveText.removeVerifyListener(verifyListener);
		archiveText.setText(ARCHIVE_EDITOR_RS_NUM_DEFAULT);
		archiveText.addVerifyListener(verifyListener);
	}
	
	//父类方法,单击"应用"按钮时执行此方法,将文本框值保存并弹出成功的提示信息
	protected void preformApply(){
		doSave();//自定义方法
	}
	
	//父类方法,单击"确定"按钮时执行此方法,将文本框值保存并弹跳出成功的提示信息
	public boolean performOk(){
		doSave();
		ConnectManager.closeConnection();
		return true;
	}
	
	//保存文本框的值
	private void doSave(){
		ps.setValue(CLASSNAME_KEY,classNameText.getText());
		ps.setValue(URL_KEY, urlText.getText());
		ps.setValue(USERNAME_KEY, usernameText.getText());
		ps.setValue(PASSWORD_KEY, passwordText.getText());
		ps.setValue(ARCHIVE_EDITOR_RS_NUM_KEY, archiveText.getText());
	}

}

⌨️ 快捷键说明

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