Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2282108
  • 博文数量: 321
  • 博客积分: 3440
  • 博客等级: 中校
  • 技术积分: 2992
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-24 09:08
个人简介

我就在这里

文章分类

全部博文(321)

文章存档

2015年(9)

2014年(84)

2013年(101)

2012年(25)

2011年(29)

2010年(21)

2009年(6)

2008年(23)

2007年(23)

分类: Java

2015-01-03 19:59:47

启动用户程序进行登录验证

   我们在某些情况下要求用户有相应的权限才能执行操作时,必须在程序启动是验证用户是否是合法的用户,这时我们可以在程序启动是弹出登录对话框,如果登录成功的话才可以进入系统,否则无法进入

  要在RCP程序中实现这样的功能很简单,我们只要在 Application.java 这个类的  start(IApplicationContext context)  方法中进行验证就行了,这个类在RCP程序中的作用是负责启动整个RCP 框架,相对于传统类的 main 方法,是程序的入口,

我们先新建一个 登录对话框,名称为 LoginDialog,界面元素很简单,只要一个输入用户名和密码的文本框,我们在这个对话框点击完成的时候进行验证用户名是否输入正确,如果输入是正确的话那么就调用  okPressed ()方法,否则的话就调用  cancelPressed (),我们通过判断对话框的返回代码来判断对话框是不是正常终止,是的话表示成功登录,否则表示登录失败或退出登录,下面是 LoginDialog.java类的内容:

  1. package org.vwpolo.rcp.example.dialogs;

  2. import org.eclipse.jface.dialogs.IDialogConstants;
  3. import org.eclipse.jface.dialogs.MessageDialog;
  4. import org.eclipse.jface.dialogs.TitleAreaDialog;
  5. import org.eclipse.swt.SWT;
  6. import org.eclipse.swt.graphics.Point;
  7. import org.eclipse.swt.layout.GridData;
  8. import org.eclipse.swt.layout.GridLayout;
  9. import org.eclipse.swt.widgets.Composite;
  10. import org.eclipse.swt.widgets.Control;
  11. import org.eclipse.swt.widgets.Label;
  12. import org.eclipse.swt.widgets.Shell;
  13. import org.eclipse.swt.widgets.Text;

  14. public class LoginDialog extends TitleAreaDialog {

  15.   private Text passwordText;
  16.   private Text userNameText;
  17.   /**
  18.    * Create the dialog
  19.    * @param parentShell
  20.    */
  21.   public LoginDialog(Shell parentShell) {
  22.     super(parentShell);
  23.   }

  24.   /**
  25.    * Create contents of the dialog
  26.    * @param parent
  27.    */
  28.   @Override
  29.   protected Control createDialogArea(Composite parent) {
  30.     Composite area = (Composite) super.createDialogArea(parent);
  31.     Composite container = new Composite(area, SWT.NONE);
  32.     final GridLayout gridLayout = new GridLayout();
  33.     gridLayout.numColumns = 2;
  34.     container.setLayout(gridLayout);
  35.     container.setLayoutData(new GridData(GridData.FILL_BOTH));

  36.     final Label label = new Label(container, SWT.NONE);
  37.     label.setText("用户名:");

  38.     userNameText = new Text(container, SWT.BORDER);
  39.     final GridData gd_userNameText = new GridData(SWT.FILL, SWT.CENTER, true, false);
  40.     userNameText.setLayoutData(gd_userNameText);

  41.     final Label label_1 = new Label(container, SWT.NONE);
  42.     label_1.setText("密码:");

  43.     passwordText = new Text(container, SWT.BORDER);
  44.     final GridData gd_passwordText = new GridData(SWT.FILL, SWT.CENTER, true, false);
  45.     passwordText.setLayoutData(gd_passwordText);
  46.     setTitle("欢迎登录XX系统");
  47.     setMessage("输入用户名和密码");
  48.     //
  49.     return area;
  50.   }

  51.   /**
  52.    * Create contents of the button bar
  53.    * @param parent
  54.    */
  55.   @Override
  56.   protected void createButtonsForButtonBar(Composite parent) {
  57.     createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
  58.     createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
  59.   }

  60.   /**
  61.    * Return the initial size of the dialog
  62.    */
  63.   @Override
  64.   protected Point getInitialSize() {
  65.     return new Point(354, 229);
  66.   }
  67.   protected void configureShell(Shell newShell) {
  68.     super.configureShell(newShell);
  69.     newShell.setText("欢迎登录");
  70.   }
  71.   protected void buttonPressed(int buttonId) {
  72.     if (buttonId == IDialogConstants.OK_ID) {
  73.       try {
  74.         login(userNameText.getText(),passwordText.getText());
  75.         okPressed();
  76.       } catch (Exception e) {
  77.         MessageDialog.openError(getShell(), "登录失败", e.getMessage());
  78.         e.printStackTrace();
  79.         return;
  80.       }
  81.     }
  82.     if (buttonId == IDialogConstants.CANCEL_ID) {
  83.       cancelPressed();
  84.     }
  85.     super.buttonPressed(buttonId);
  86.   }

  87.   /**
  88.    *

    判断是否登录成功。


  89.    * @author 刘绕兴
  90.    * @param userName
  91.    * @param password
  92.    * @throws Exception
  93.    */
  94.   private void login(String userName, String password) throws Exception {
  95.     if(userName.equals("vwpolo") && password.equals("123"))
  96.       return;
  97.     throw new Exception("错误的用户名和密码");
  98.   }

  99. }
Application.java 文件内容:

  1. package org.vwpolo.rcp.example;

  2. import org.eclipse.core.runtime.Platform;

  3. /**
  4.  * This class controls all aspects of the application's execution
  5.  */
  6. public class Application implements IApplication {

  7.     /* (non-Javadoc)
  8.      * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
  9.      */
  10.     public Object start(IApplicationContext context) throws Exception {
  11.     Display display = PlatformUI.createDisplay();
  12.     try {
  13.       Shell shell = Display.getCurrent().getActiveShell();

  14.       try {
  15.         if (!doLogin(shell)) {
  16.           Platform.endSplash();
  17.           return EXIT_OK;
  18.         }
  19.       }
  20.       finally {
  21.         if (shell != null) {
  22.           shell.dispose();
  23.         }
  24.       }
  25.       int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
  26.       if (returnCode == PlatformUI.RETURN_RESTART) {
  27.         return IApplication.EXIT_RESTART;
  28.       }
  29.       return IApplication.EXIT_OK;
  30.     }
  31.     finally {
  32.       display.dispose();
  33.     }
  34.   }

  35.      /**
  36.    *

    用户登录。


  37.    * @author vwpolo
  38.    * @param shell
  39.    * @return 判断登录是否成功
  40.    */
  41.   private boolean doLogin(Shell shell) {
  42.     LoginDialog dialog = new LoginDialog(shell);
  43.     int result = dialog.open();
  44.     if (result != Dialog.OK) {
  45.       return false;
  46.     }
  47.     return true;
  48.   }
  49.   
  50.     /* (non-Javadoc)
  51.      * @see org.eclipse.equinox.app.IApplication#stop()
  52.      */
  53.     public void stop() {
  54.         final IWorkbench workbench = PlatformUI.getWorkbench();
  55.         if (workbench == null)
  56.             return;
  57.         final Display display = workbench.getDisplay();
  58.         display.syncExec(new Runnable() {
  59.             public void run() {
  60.                 if (!display.isDisposed())
  61.                     workbench.close();
  62.             }
  63.         });
  64.     }
  65. }
在这里简化和很多功能,比如验证用户名和密码通过数据库进行验证,在这里简单起见就不弄这么复杂了


其实我们还可以通过扩展 org.eclipse.ui.startup 这个扩展点在启动是添加操作,两个的区别是上面这种方法是用的是和整个RCP启动框架的线程, startup方法会单独分配一个线程执行

转自:http://blog.csdn.net/vwpolo/article/details/2111881


阅读(2265) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~