1.
在Application中有启动RCP的入口
@Override
public Object start(final IApplicationContext context) throws Exception {
final Display display = PlatformUI.createDisplay();
final Shell shell = Display.getCurrent().getActiveShell();
try {// 打开 登录框
final LoginDialog loginDialog = new LoginDialog(shell);
final int result = loginDialog.open();
// final int result = 0;
if (result != Window.OK) {
return EXIT_OK;// 如果 没点击Dialog的OK,就return掉,不执行后面打开软件了。
}
} finally {
if (shell != null) {
shell.dispose();
}
}
try {// 打开 软件界面
final int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
if (returnCode == PlatformUI.RETURN_RESTART) {
return IApplication.EXIT_RESTART;
} else {
return IApplication.EXIT_OK;
}
} finally {
display.dispose();
}
}
UI设计
public class LoginDialog extends Dialog {
private Text txtUserName;
private Text txtPassword;
private Button btnLogin;
private Button btnRegister;
private Label lblForgetPassword;
private final Color colorBackground = SWTResourceManager.getColor(102, 204, 255);
private final Color colorRed = SWTResourceManager.getColor(SWT.COLOR_RED);
private Label lblLoginWarn;
private final String strLongSpace = " ";
public LoginDialog(final Shell parentShell) {
super(parentShell);
}
@Override
protected Control createDialogArea(final Composite parent) {
final Font fontBold = SWTResourceManager.getFont("微软雅黑", 9, SWT.BOLD);
final Font fontNomal = SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL);
final Font fontItalic = SWTResourceManager.getFont("微软雅黑", 9, SWT.ITALIC);
final Image imageBackground = ResourceManager.getPluginImage("com.hirain.fm.ui", "icons/login.jpg");
parent.setBackground(colorBackground);
final GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 11;
parent.setLayout(gridLayout);
parent.setLayoutData(new GridData(GridData.FILL_BOTH));
// 图片
final Label lblImage = new Label(parent, SWT.NONE);
lblImage.setImage(imageBackground);
lblImage.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false, 11, 1));
final Label lblImageBottomSpace = new Label(parent, SWT.NONE);
lblImageBottomSpace.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, false, false, 11, 1));
// 登录行
final Label lblUserLeftSpace = new Label(parent, SWT.NONE);
lblUserLeftSpace.setBackground(colorBackground);
lblUserLeftSpace.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
final Label lblUserName = new Label(parent, SWT.NONE);
lblUserName.setBackground(colorBackground);
lblUserName.setText("用户名:");
lblUserName.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
txtUserName = new Text(parent, SWT.BORDER);
final GridData gdUserNameText = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
gdUserNameText.widthHint = 120;
txtUserName.setLayoutData(gdUserNameText);
final Label lblPassword = new Label(parent, SWT.NONE);
lblPassword.setBackground(colorBackground);
lblPassword.setText("密 码:");
lblPassword.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
txtPassword = new Text(parent, SWT.BORDER | SWT.PASSWORD);
final GridData gdPasswordText = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
gdPasswordText.widthHint = 120;
txtPassword.setLayoutData(gdPasswordText);
btnLogin = new Button(parent, SWT.FLAT);
btnLogin.setText("登录");
final GridData gdLoginBtn = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
gdLoginBtn.widthHint = 80;
btnLogin.setLayoutData(gdLoginBtn);
final Label lblSpaceBtn = new Label(parent, SWT.NONE);
lblSpaceBtn.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
btnRegister = new Button(parent, SWT.NONE);
final GridData gdRegisterBtn = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
gdRegisterBtn.widthHint = 80;
btnRegister.setLayoutData(gdRegisterBtn);
btnRegister.setText("注册");
final Label lblSpaceRight = new Label(parent, SWT.NONE);
lblSpaceRight.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, false, 1, 1));
// 提示 行
lblLoginWarn = new Label(parent, SWT.CENTER);
lblLoginWarn.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, false, false, 11, 2));
lblLoginWarn.setBackground(colorBackground);
lblLoginWarn.setForeground(colorRed);
lblLoginWarn.setText(strLongSpace);
// 须知 行
final Label lblNoteSpace = new Label(parent, SWT.NONE);
lblNoteSpace.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 3, 3));
final Label lblLoginNote = new Label(parent, SWT.NONE);
lblLoginNote.setBackground(colorBackground);
lblLoginNote.setForeground(colorRed);
lblLoginNote.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 3, 1));
lblLoginNote.setText("登录须知:");
lblLoginNote.setFont(fontBold);
final Label lblNoteRightSpace = new Label(parent, SWT.NONE);
lblNoteRightSpace.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 5, 1));
final Label lblNote1 = new Label(parent, SWT.NONE);
lblNote1.setBackground(colorBackground);
lblNote1.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 3, 1));
lblNote1.setText("1.未注册用户请先注册再使用!");
lblNote1.setFont(fontNomal);
final Label lblNoteSpace1 = new Label(parent, SWT.NONE);
lblNoteSpace1.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 5, 1));
final Label lblNote2 = new Label(parent, SWT.NONE);
lblNote2.setBackground(colorBackground);
lblNote2.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1));
lblNote2.setText("2.如忘记密码,可以点击");
lblNote2.setFont(fontNomal);
lblForgetPassword = new Label(parent, SWT.NONE);
lblForgetPassword.setBackground(colorBackground);
lblForgetPassword.setForeground(colorRed);
lblForgetPassword.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 2, 1));
lblForgetPassword.setText("找回密码");
lblForgetPassword.setFont(fontItalic);
final Label lblNoteSpace2 = new Label(parent, SWT.NONE);
lblNoteSpace2.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 5, 1));
addListener();
return parent;
}
private void addListener() {
btnLogin.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
final String userName = txtUserName.getText().trim();
final String password = txtPassword.getText().trim();
// TODO 查询数据库
if (userName.equals("1") && password.equals("1")) {
lblLoginWarn.setText(strLongSpace);
// 如果 admin
// 如果user
} else {
lblLoginWarn.setText("密码错误 !");
final Job job = new Job("") {
@Override
protected IStatus run(final IProgressMonitor monitor) {
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
if (!lblLoginWarn.isDisposed()) {
lblLoginWarn.setText(strLongSpace);
}
}
});
return Status.OK_STATUS;
}
};
job.schedule(1000);
return;
}
close();
}
});
btnRegister.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
// TODO 弹框:注册页面
}
});
lblForgetPassword.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(final MouseEvent e) {
// TODO 弹框:找回密码页面
}
});
}
/**
* 去掉Dilog默认带的确定、取消按钮
*/
@Override
protected void createButtonsForButtonBar(final Composite parent) {
parent.setBackground(colorBackground);
}
@Override
protected Point getInitialSize() {
final Rectangle area = Display.getDefault().getClientArea();
return new Point(area.width / 2, area.height / 2);
}
@Override
protected Point getInitialLocation(Point initialSize) {
initialSize = new Point(0, 0);
return super.getInitialLocation(initialSize);
}
@Override
protected void configureShell(final Shell newShell) {
super.configureShell(newShell);
newShell.setText("欢迎登录 - 软件");
}
}
2.