📄 logondialog.java
字号:
package cn.com.chengang.sms.navigator;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
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.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import cn.com.chengang.sms.db.DbOperate;
import cn.com.chengang.sms.model.IUser;
import cn.com.chengang.sms.system.Context;
import cn.com.chengang.sms.system.SmsFactory;
public class LogonDialog extends Dialog {
private Text userIdText; // 用户名文本框
private Text passwordText;// 密码文本框
public LogonDialog(Shell parentShell) {
super(parentShell);
}
protected Control createDialogArea(Composite parent) {
getShell().setText("用户登录"); // 窗口标题
Composite topComp = new Composite(parent, SWT.NONE);
// 将topComp自身撑满整个Dialog(注:parent用的是GridLayout布局)
topComp.setLayoutData(new GridData(GridData.FILL_BOTH));
// topComp内的组件按照GridLayout来布局
topComp.setLayout(new GridLayout(2, false));
new Label(topComp, SWT.NONE).setText("用户名:");
userIdText = new Text(topComp, SWT.BORDER);
userIdText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
new Label(topComp, SWT.NONE).setText("密 码:");
passwordText = new Text(topComp, SWT.BORDER);
passwordText.setEchoChar('*');
passwordText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
return topComp;
}
// 单击对话框按钮时执行此方法
protected void buttonPressed(int buttonId) {
if (buttonId == IDialogConstants.OK_ID) {
String userId = userIdText.getText().trim();
String password = passwordText.getText().trim();
if (userId == null || userId.equals("")) {
MessageDialog.openWarning(getShell(), "", "用户名不能为空");
userIdText.setFocus(); // 设置焦点
return;
}
if (password == null || password.equals("")) {
MessageDialog.openWarning(getShell(), "", "密码不能为空");
passwordText.setFocus();
return;
}
// 到数据库中验证
DbOperate db = SmsFactory.getDbOperate();// 得到一个数据库操作类
IUser user = db.getUser(userId);// 从数据库中根据userId取得此用户
if (user == null) {
MessageDialog.openInformation(getShell(), "", "用户名不存在");
userIdText.setFocus();
userIdText.selectAll(); // 选择所有值
return;
}
String dbPassword = user.getPassword();
if (dbPassword == null || !dbPassword.equals(password)) {
MessageDialog.openInformation(getShell(), "", "密码错误");
passwordText.setFocus();
passwordText.selectAll();
return;
}
Context.getInstance().setCurrentUser(user); // 将当前登录用户保存起来
}
super.buttonPressed(buttonId);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -