启动用户程序进行登录验证
我们在某些情况下要求用户有相应的权限才能执行操作时,必须在程序启动是验证用户是否是合法的用户,这时我们可以在程序启动是弹出登录对话框,如果登录成功的话才可以进入系统,否则无法进入
要在RCP程序中实现这样的功能很简单,我们只要在 Application.java 这个类的 start(IApplicationContext context) 方法中进行验证就行了,这个类在RCP程序中的作用是负责启动整个RCP 框架,相对于传统类的 main 方法,是程序的入口,
我们先新建一个 登录对话框,名称为 LoginDialog,界面元素很简单,只要一个输入用户名和密码的文本框,我们在这个对话框点击完成的时候进行验证用户名是否输入正确,如果输入是正确的话那么就调用 okPressed ()方法,否则的话就调用 cancelPressed (),我们通过判断对话框的返回代码来判断对话框是不是正常终止,是的话表示成功登录,否则表示登录失败或退出登录,下面是 LoginDialog.java类的内容:
-
package org.vwpolo.rcp.example.dialogs;
-
-
import org.eclipse.jface.dialogs.IDialogConstants;
-
import org.eclipse.jface.dialogs.MessageDialog;
-
import org.eclipse.jface.dialogs.TitleAreaDialog;
-
import org.eclipse.swt.SWT;
-
import org.eclipse.swt.graphics.Point;
-
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;
-
-
public class LoginDialog extends TitleAreaDialog {
-
-
private Text passwordText;
-
private Text userNameText;
-
/**
-
* Create the dialog
-
* @param parentShell
-
*/
-
public LoginDialog(Shell parentShell) {
-
super(parentShell);
-
}
-
-
/**
-
* Create contents of the dialog
-
* @param parent
-
*/
-
@Override
-
protected Control createDialogArea(Composite parent) {
-
Composite area = (Composite) super.createDialogArea(parent);
-
Composite container = new Composite(area, SWT.NONE);
-
final GridLayout gridLayout = new GridLayout();
-
gridLayout.numColumns = 2;
-
container.setLayout(gridLayout);
-
container.setLayoutData(new GridData(GridData.FILL_BOTH));
-
-
final Label label = new Label(container, SWT.NONE);
-
label.setText("用户名:");
-
-
userNameText = new Text(container, SWT.BORDER);
-
final GridData gd_userNameText = new GridData(SWT.FILL, SWT.CENTER, true, false);
-
userNameText.setLayoutData(gd_userNameText);
-
-
final Label label_1 = new Label(container, SWT.NONE);
-
label_1.setText("密码:");
-
-
passwordText = new Text(container, SWT.BORDER);
-
final GridData gd_passwordText = new GridData(SWT.FILL, SWT.CENTER, true, false);
-
passwordText.setLayoutData(gd_passwordText);
-
setTitle("欢迎登录XX系统");
-
setMessage("输入用户名和密码");
-
//
-
return area;
-
}
-
-
/**
-
* Create contents of the button bar
-
* @param parent
-
*/
-
@Override
-
protected void createButtonsForButtonBar(Composite parent) {
-
createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
-
createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
-
}
-
-
/**
-
* Return the initial size of the dialog
-
*/
-
@Override
-
protected Point getInitialSize() {
-
return new Point(354, 229);
-
}
-
protected void configureShell(Shell newShell) {
-
super.configureShell(newShell);
-
newShell.setText("欢迎登录");
-
}
-
protected void buttonPressed(int buttonId) {
-
if (buttonId == IDialogConstants.OK_ID) {
-
try {
-
login(userNameText.getText(),passwordText.getText());
-
okPressed();
-
} catch (Exception e) {
-
MessageDialog.openError(getShell(), "登录失败", e.getMessage());
-
e.printStackTrace();
-
return;
-
}
-
}
-
if (buttonId == IDialogConstants.CANCEL_ID) {
-
cancelPressed();
-
}
-
super.buttonPressed(buttonId);
-
}
-
-
/**
-
*
判断是否登录成功。
-
* @author 刘绕兴
-
* @param userName
-
* @param password
-
* @throws Exception
-
*/
-
private void login(String userName, String password) throws Exception {
-
if(userName.equals("vwpolo") && password.equals("123"))
-
return;
-
throw new Exception("错误的用户名和密码");
-
}
-
-
}
Application.java 文件内容:
-
package org.vwpolo.rcp.example;
-
-
import org.eclipse.core.runtime.Platform;
-
-
/**
-
* This class controls all aspects of the application's execution
-
*/
-
public class Application implements IApplication {
-
-
/* (non-Javadoc)
-
* @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
-
*/
-
public Object start(IApplicationContext context) throws Exception {
-
Display display = PlatformUI.createDisplay();
-
try {
-
Shell shell = Display.getCurrent().getActiveShell();
-
-
try {
-
if (!doLogin(shell)) {
-
Platform.endSplash();
-
return EXIT_OK;
-
}
-
}
-
finally {
-
if (shell != null) {
-
shell.dispose();
-
}
-
}
-
int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
-
if (returnCode == PlatformUI.RETURN_RESTART) {
-
return IApplication.EXIT_RESTART;
-
}
-
return IApplication.EXIT_OK;
-
}
-
finally {
-
display.dispose();
-
}
-
}
-
-
/**
-
*
用户登录。
-
* @author vwpolo
-
* @param shell
-
* @return 判断登录是否成功
-
*/
-
private boolean doLogin(Shell shell) {
-
LoginDialog dialog = new LoginDialog(shell);
-
int result = dialog.open();
-
if (result != Dialog.OK) {
-
return false;
-
}
-
return true;
-
}
-
-
/* (non-Javadoc)
-
* @see org.eclipse.equinox.app.IApplication#stop()
-
*/
-
public void stop() {
-
final IWorkbench workbench = PlatformUI.getWorkbench();
-
if (workbench == null)
-
return;
-
final Display display = workbench.getDisplay();
-
display.syncExec(new Runnable() {
-
public void run() {
-
if (!display.isDisposed())
-
workbench.close();
-
}
-
});
-
}
-
}
在这里简化和很多功能,比如验证用户名和密码通过数据库进行验证,在这里简单起见就不弄这么复杂了
其实我们还可以通过扩展 org.eclipse.ui.startup 这个扩展点在启动是添加操作,两个的区别是上面这种方法是用的是和整个RCP启动框架的线程, startup方法会单独分配一个线程执行
转自:http://blog.csdn.net/vwpolo/article/details/2111881
阅读(2369) | 评论(0) | 转发(0) |