• RCP之病人信息系统开发总结(9):MVC模式之View层—对话框


    对话框Dialog也是系统中很常见的重要部分,这里我总结一下我实现的登录对话框
      
    登录对话框的实现还是比较复杂的,需要注意的是它是在主程序窗口还没有创建之前创建的!但是构造方法中传入null竟然是可以的,牢记!
    还有,我实现的是三次登陆失败的话程序自动退出!如果输入的信息无法登陆的话,会重新弹出登录对话框
    点击了“OK”之后,对话框类check,但是它只是看是否为空,如果为空就提示不能为空(只有医生是可以登录的,他的用户名和密码都不为空)
    如果不为空,就返回了,
     
    package com.yinger.patientims.dialogs; 

    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.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 com.yinger.patientims.model.Doctor;

    /**
     * 登录对话框继承标题区域对话框(TitleAreaDialog)
     * 
     */


    // TitleAreaDialog : A dialog that has a title area for displaying a title and
    // an image as well as a common area for
    // displaying a description, a message, or an error message.
    public class LoginDialog extends TitleAreaDialog {

      private Text text_username;
      private Text text_password;
      private Doctor doctor = new Doctor();//首先构造一个Doctor

      public LoginDialog(Shell parentShell) {
        super(parentShell);
        //不能在这里对shell进行操作,因为这里shell还没有创建!    
      }

      // 这个方法是用来设置dialog里面的内容,实际上dialog上半部分的内容
      @Override
      protected Control createContents(Composite parent) {
        Control contents = super.createContents(parent); // 利用父类的方法!
        // 设置dialog上的标题
        setTitle("Add Your Login Information");
        // 设置标题下面的信息提示
        // setMessage("Attention:The textfield must't be blank!");
        setMessage("Attention: You just have three times to try!", IMessageProvider.INFORMATION);// 有了第二个参数就可以显示一个信息的图标
        // Minimal interface to a message provider. Used for dialog pages
        // which can provide a message with an icon.
        return contents;
      }

      // 这个方法也是用来设置dialog里面的内容,但是设置的是dialog下半部分的内容
      @Override
      protected Control createDialogArea(Composite parent) {
        Composite composite = new Composite(parent, SWT.NONE);
        composite.setLayout(new GridLayout(2, false));
        // composite.setLayout(new
        // GridLayout());//报错!原因是引错了包!一定要是引入swt中的GridLayout
        // The method setLayout(Layout) in the type Composite is not
        // applicable for the arguments (GridLayout)
        GridData layoutData = new GridData(GridData.FILL_HORIZONTAL); // 充满水平方向
        composite.setLayoutData(layoutData);

        Label usernameLabel = new Label(composite, SWT.NONE);
        usernameLabel.setText("UserName:");
        text_username = new Text(composite, SWT.BORDER);
        text_username.setLayoutData(layoutData);

        Label passwordLabel = new Label(composite, SWT.NONE);
        passwordLabel.setText("Password:");
        text_password = new Text(composite, SWT.BORDER);
        text_password.setLayoutData(layoutData);
        text_password.setEchoChar('*'); // 密码需要设置为显示 *

        return composite;
      }

      // 从Dialog类中继承过来的方法,用于处理按键事件,并调用其他的方法使窗口open方法有个返回值
      // 注意:这里是不要对登录进行实际的验证的!而只是在非空情况下返回用户按下的键!
      // Notifies that this dialog's button with the given id has been pressed.
      @Override
      protected void buttonPressed(int buttonId) {
        // 单击 OK
        if (IDialogConstants.OK_ID == buttonId) {
          if (!checkLogin()) {//如果填写的内容为空,则修改提示信息为一个错误信息,同时登录窗口不会关闭!
            return;
          }
          setLoginUser();//如果填写的内容不为空,那么将内容设置到doctor上面,并让窗体返回OK键!
          okPressed();// 向父窗体通知登录窗口返回的是OK按钮被按下,也就是窗口的返回值是OK键
          // Dialog :Notifies that the ok button of this dialog has been pressed.
          // 总结:如果调用了okPressed或者cancelPressed方法,那么对话框就要返回了,对话框会被dispose
          // 建议对话框和父窗体之间的通信可以在对话框中保存一个相应的对象,这样就可以在对话框dispose之前将那个对象的内容设置好!
          return;
        }
        // 单击 Cancel
        if (IDialogConstants.CANCEL_ID == buttonId) {
          cancelPressed();// Notifies that the cancel button of this dialog has been pressed.
        }
      }

      // 符合要求,设置登录的用户
      private void setLoginUser() {
    //    doctor = new Doctor();
        doctor.setUsername(text_username.getText());
        doctor.setPassword(text_password.getText());
      }

      // 检查登录信息,判断是否信息有效
      private boolean checkLogin() {
        String username = text_username.getText();
        String password = text_password.getText();
        // 不符合要求,显示错误信息并返回
        if (username == null || username.equals("") || password == null || password.equals("")) {
          setErrorMessage("Error:UserName and Password must'n be blank!");
          return false;
        }
        return true;
      }

      // 从Window类中继承过来的方法,用于在窗体显示时设置窗体的显示信息
      @Override
      protected void configureShell(Shell newShell) {
        super.configureShell(newShell);
        newShell.setText("User Login");
        newShell.setSize(400, 220);// 注意这里的高度,高度一定要足够的大,否则按钮可能就会看不到
        newShell.setLocation(400, 300);
        // newShell.setImage(new Image(null, "/icons/Home.ico"));
    //    newShell.setImage(getDefaultImage());// 设置图片失败,默认图片是系统默认的那个难看的程序图标
    //    newShell.setImage(new Image(null, System.getProperty("user.dir")+"\\icons\\User.ico"));
        //路径错了: E:\Java_Study\Eclipse\Eclipse for rcp\eclipse_rcp\icons\User.ico
    //    newShell.setImage(new Image(Display.getDefault(), ClassLoader.getSystemResourceAsStream("icons/User.ico")));
        //这样还是不行,display是null
      }

      // 从Window类中继承过来的方法,可以用来改变dialog的窗体的样式
      @Override
      protected int getShellStyle() {
        return super.getShellStyle();
        // return super.getShellStyle() | SWT.MAX | SWT.MIN; // 可以最大化和最小化
      }

      public Doctor getDoctor() {
        return doctor;
      }

      public void setDoctor(Doctor doctor) {
        this.doctor = doctor;
      }

    }
     
    与之搭配的Application类
     
    package com.yinger.patientims; 

    import org.eclipse.equinox.app.IApplication;
    import org.eclipse.equinox.app.IApplicationContext;
    import org.eclipse.jface.dialogs.IDialogConstants;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.ui.IWorkbench;
    import org.eclipse.ui.PlatformUI;

    import com.yinger.patientims.dialogs.LoginDialog;
    import com.yinger.patientims.model.Doctor;
    import com.yinger.patientims.util.DBUtil;

    /**
     * This class controls all aspects of the application's execution
     * Application类是RCP应用程序的主程序类,相当于控制器,类似于Main()方法
     * 该类负责创建一个工作台(Workbench),并且与ApplicationWorkbenchAdvisor类连接
     * 该类实现了IApplication接口,启动RCP应用程序之后该类首先被加载。 如果系统需要登录,通常是在这个类中插入登录模块 通常情况下这个类不需要改动
     */

    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(); // 创建Display对象
        try {
          // 启动之前先进行登录操作
          if (!login()) {
            return IApplication.EXIT_OK; // 没有登陆成功就正常退出
          }
          // 启动工作台,从而打开应用程序窗口
          int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
          if (returnCode == PlatformUI.RETURN_RESTART)
            return IApplication.EXIT_RESTART;
          else
            return IApplication.EXIT_OK;
        } finally {
          display.dispose();
        }

      }

      /*
       * (non-Javadoc)
       * 
       * @see org.eclipse.equinox.app.IApplication#stop()
       */

      public void stop() {
        if (!PlatformUI.isWorkbenchRunning())
          return;
        final IWorkbench workbench = PlatformUI.getWorkbench();
        final Display display = workbench.getDisplay();
        display.syncExec(new Runnable() {
          public void run() {
            if (!display.isDisposed()) // 关闭窗口之前先判断display是否被销毁了
              workbench.close();
          }
        });
      }

      // 登录验证
      public boolean login() {
        boolean flag = false;// 标志
        int tryCount = 0;//登录的次数
        while (!flag) {
          LoginDialog loginDialog = new LoginDialog(null);// null是可以的!
          Doctor doctor = new Doctor();
          loginDialog.setDoctor(doctor);// 这里很重要
          if (tryCount == 3) {//尝试登录了三次没有成功就退出
    //        loginDialog.setErrorMessage("Wrong Login Information!");
            return false;
          }
          int res = loginDialog.open();// 注意,可能用户只是点击了关闭!
          if (res == IDialogConstants.ABORT_ID || res == IDialogConstants.CANCEL_ID) {
            return false; // 关闭或者退出了就直接退出程序
          }
          if (res == IDialogConstants.OK_ID) {// Dialog返回之前会设置好内部的doctor对象
            doctor = DBUtil.getLoginInfo(doctor);// 此时右边的doctor对象有了username和password
          }
          // doctor肯定不为null(每次都是new Doctor()),那么就要看doctor的属性
          if (doctor.getName() != null) {// 如果用户输入了name和password之后,但是信息无效就会提示这个或者用户直接点击了关闭登录
            // MessageDialog.openInformation(null,
            // "Error Information", "Wrong Login Information!");
            // loginDialog.setErrorMessage("Wrong Login Information!");
            flag = true;// 这种情况下才是登录成功,否则继续进行登录
          }else {
            tryCount++;
          }
        }
        return true;
      }
    }
     
    验证用户
    使用自定义的数据库帮助类 DBUtil中的方法判断输入的信息是否可以登录
    package com.yinger.patientims.util; 

    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.text.SimpleDateFormat;

    import com.yinger.patientims.dao.DAOBase;
    import com.yinger.patientims.dao.DoctorDAO;
    import com.yinger.patientims.model.Doctor;
    import com.yinger.patientims.model.Patient;

    /**
     * 数据库操作的帮助类 
     *
     */

    public class DBUtil {

      private static DAOBase daoBase;
      static {
        daoBase = new DAOBase();
      }

      public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

      // 根据用户输入的信息判断信息是否正确有效
      public static Doctor getLoginInfo(Doctor doctor) {
        Connection connection = daoBase.getConnection();
        ResultSet resultSet;
        Statement statement;
        // SELECT * FROM t_doctor WHERE username='huatuo' and
        // password='1234'
        String sqlString = "SELECT * FROM t_doctor WHERE username='" + doctor.getUsername() + "' and password='" + doctor.getPassword() + "'";
        try {
          statement = connection.createStatement();
          resultSet = statement.executeQuery(sqlString);
          while (resultSet.next()) {
            doctor = DoctorDAO.setOneDoctor(resultSet);
          }
          statement.close();
        } catch (Exception e) {
          e.printStackTrace();
        } finally {
          try {
            connection.close();
          } catch (SQLException e) {
            e.printStackTrace();
          }
        }
        return doctor;
      }

      // 判断某个病床是否已经有病人了
      public static boolean validateSickBed(String dname, int bname, int rname) {
        Connection connection = daoBase.getConnection();
        ResultSet resultSet = null;
        Statement statement = null;
        // SELECT p.id FROM t_patient p,t_sickbed b,t_sickroom
        // r,t_department d
        // WHERE p.sickbed_id=b.id and b.sickroom_id=r.id and
        // r.department_id=d.id
        // and d.name='普外科' and b.sickbedno=2 and r.sickroomno=101
        String sql = "SELECT p.id FROM t_patient p,t_sickbed b,t_sickroom r,t_department d ";
        sql += " WHERE p.sickbed_id=b.id and b.sickroom_id=r.id and r.department_id=d.id ";
        sql += "and d.name='" + dname + "' and b.sickbedno=" + bname + " and r.sickroomno=" + rname + "";
        try {
          statement = connection.createStatement();
          resultSet = statement.executeQuery(sql);
          if (resultSet.next()) {// 存在病人
            return true;
          } else {
            return false;
          }
        } catch (Exception e) {
          e.printStackTrace();
        } finally {
          try {
            statement.close();
            connection.close();
          } catch (SQLException e) {
            e.printStackTrace();
          }
        }
        return false;// 默认是不存在
      }

      //根据已有的病人信息得到病床id---->这个方法已经没有用了!
      public static Long getSickBedId(Patient patient) {
        Connection connection = daoBase.getConnection();
        ResultSet resultSet = null;
        Statement statement = null;
        // SELECT b.id FROM t_sickbed b,t_sickroom r,t_department d
        // WHERE b.sickroom_id=r.id and r.department_id=d.id
        // and d.name='普外科' and b.sickbedno=2 and r.sickroomno=101
        String sql = "SELECT b.id FROM t_sickbed b,t_sickroom r,t_department d ";
        sql += " WHERE b.sickroom_id=r.id and r.department_id=d.id ";
        sql += "and d.name='" + patient.getDepartment().getName() + "' and b.sickbedno=" + patient.getSickbed().getSickBedNo() + " and r.sickroomno="
            + patient.getSickroom().getSickRoomNo() + "";
        try {
          statement = connection.createStatement();
          resultSet = statement.executeQuery(sql);
          if (resultSet.next()) { //注意,在resultset调用它的方法之前statement是不可以close的!
            return resultSet.getLong("id");
          }
        } catch (Exception e) {
          e.printStackTrace();
        } finally {
          try {
            statement.close();
            connection.close();
          } catch (SQLException e) {
            e.printStackTrace();
          }
        }
        return 0L;
      }

    }

     



  • 相关阅读:
    jmeter使用—响应断言
    jmeter使用—计数器的使用
    Jmeter使用—使用 HTTP代理服务器抓取接口
    jmeter使用—远程分布式
    linux sed
    perl 文件操作
    jenkins
    perl exit (Jenkins 判断构建是否完成)
    cs ds ss fs gs 段寄存器
    ‘桌面助手’端口不匹配
  • 原文地址:https://www.cnblogs.com/yinger/p/2255656.html
Copyright © 2020-2023  润新知