📄 modifyarchivedialog.java
字号:
package com.zdh.sms.editor;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
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 com.zdh.sms.model.Course;
import com.zdh.sms.model.IUser;
import com.zdh.sms.model.SchoolClass;
import com.zdh.sms.model.Student;
import com.zdh.sms.model.Teacher;
import com.zdh.sms.system.CourseComposite;
import com.zdh.sms.system.SmsUtil;
//修改用户信息的对话框
public class ModifyArchiveDialog extends TitleAreaDialog {
private IUser user; // 用户实体
private Label idLabel; // id字段值
private Label latestOnlinLabel; // 最后登录时间
private Text useridText; // 用户名
private Text passwordText; // 密码
private Text nameText; // 姓名
private Label schoolClassLabel; // 班级标签
private Combo schoolClassCombo; // 班级文本框
private CourseComposite courseComp; // 课程面板
public ModifyArchiveDialog(Shell parentShell) {
super(parentShell);
}
protected Control createDialogArea(Composite parent) {
getShell().setText("用户档案");
setTitle("修改用户档案");
setMessage("请输入新的用户信息", IMessageProvider.INFORMATION);
Composite topComp = new Composite(parent, SWT.NONE);
// 设置基板topComp在对话框中的位置
topComp.setLayoutData(new GridData(GridData.CENTER, GridData.CENTER,
true, true));
topComp.setLayout(new GridLayout(2, false));// 网格布局,2列
// 创建界面组件
Composite comp1 = new Composite(topComp, SWT.NONE);
comp1.setLayoutData(new GridData(GridData.FILL_BOTH));
comp1.setLayout(new GridLayout(2, false));// 网格布局,2列
new Label(comp1, SWT.NONE).setText("ID:");
idLabel = new Label(comp1, SWT.NONE);
new Label(comp1, SWT.NONE).setText("用户名:");
useridText = createText(comp1, SWT.BORDER);
new Label(comp1, SWT.NONE).setText("密码:");
passwordText = createText(comp1, SWT.BORDER);
new Label(comp1, SWT.NONE).setText("姓名:");
nameText = createText(comp1, SWT.BORDER);
{
schoolClassLabel = new Label(comp1, SWT.NONE);
schoolClassLabel.setText("班级:");
Composite classComp = new Composite(comp1, SWT.NONE);
classComp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
classComp.setLayout(new FillLayout());
schoolClassCombo = SmsUtil.createSchoolClassCombo(classComp,
SWT.BORDER | SWT.READ_ONLY);
}
new Label(comp1, SWT.NONE).setText("最近登录:");
latestOnlinLabel = new Label(comp1, SWT.NONE);
courseComp = new CourseComposite(topComp, SWT.NONE);// 创建课程面板
// 设值
setValue(user);
return topComp;
}
// 给界面设初值
private void setValue(IUser user) {
if (user == null)
return;
idLabel.setText(String.valueOf(user.getId()));
latestOnlinLabel.setText(SmsUtil.dateToLongStr(user.getLatestOnline()));
useridText.setText(user.getUserId());
passwordText.setText(user.getPassword());
nameText.setText(user.getName());
if (user instanceof Student) {
Student o = (Student) user;
SchoolClass schoolClass = o.getSchoolclass();
schoolClassCombo.setText(schoolClass.getName());
schoolClassCombo.setData(schoolClass.getName(), schoolClass);
courseComp.setVisible(false);
} else if (user instanceof Teacher) {
Teacher o = (Teacher) user;
for (Course course : o.getCourses())
courseComp.add(course);
schoolClassLabel.setVisible(false);// 隐藏班级
schoolClassCombo.setVisible(false);
}
}
// 得到一个固定长度的Text框
private Text createText(Composite comp, int style) {
Text text = new Text(comp, style);
text.setLayoutData(new GridData(100, -1));
return text;
}
// 对话框按钮单击执行的方法
protected void buttonPressed(int buttonId) {
if (buttonId == IDialogConstants.OK_ID) {
if (!checkValue())
return;
getValue(user);
}
super.buttonPressed(buttonId);
}
// 检查输入值的合法性,返回false表示不合法
private boolean checkValue() {
if (useridText.getText().trim().equals("")) {
setErrorMessage("用户名不能为空");
return false;
}
if (passwordText.getText().trim().equals("")) {
setErrorMessage("密码不能为空");
return false;
}
if (nameText.getText().trim().equals("")) {
setErrorMessage("姓名不能为空");
return false;
}
return true;
}
// 将界面的值更新到传入的user参数
private void getValue(IUser user) {
if (user instanceof Teacher) { // 设置新的课程值
Teacher teacher = (Teacher) user;
teacher.clearCourses(); // 先清空
for (String courseName : courseComp.getItems()) {
Course course = courseComp.getData(courseName);
teacher.addCourse(course);
}
} else if (user instanceof Student) { // 设置新的班级值
String sel = schoolClassCombo.getText();
SchoolClass schoolclass = (SchoolClass) schoolClassCombo
.getData(sel);
((Student) user).setSchoolclass(schoolclass);
}
user.setId(new Long(idLabel.getText()));
user.setUserId(useridText.getText());
user.setPassword(passwordText.getText());
user.setName(nameText.getText());
user.setLatestOnline(user.getLatestOnline());
}
// 改变对话框的大小
protected Point getInitialSize() {
Point p = super.getInitialSize();
p.x = 400;
return p;
}
public IUser getUser() {
return user;
}
public void setUser(IUser user) {
this.user = user;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -