Ejemplo n.º 1
0
 public UserBrowserModel() {
   UserBrowsingManager manager = new UserBrowsingManager();
   pUser = manager.getUser();
 }
Ejemplo n.º 2
0
  public UserBrowsing() {

    setTitle(MessageBundle.getMessage("angal.menu.usersbrowser"));
    myFrame = this;

    // addWindowListener(this);

    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    Toolkit kit = Toolkit.getDefaultToolkit();
    Dimension screensize = kit.getScreenSize();
    pfrmWidth = screensize.width / 2 + 100;
    pfrmHeight = screensize.height / 2;
    setBounds(screensize.width / 4, screensize.height / 4, pfrmWidth, pfrmHeight);

    model = new UserBrowserModel();
    table = new JTable(model);
    table.getColumnModel().getColumn(0).setPreferredWidth(pColumwidth[0]);
    table.getColumnModel().getColumn(1).setPreferredWidth(pColumwidth[1]);
    table.getColumnModel().getColumn(2).setPreferredWidth(pColumwidth[2]);
    // table.getColumnModel().getColumn(3).setPreferredWidth(pColumwidth[3]);

    scrollPane = new JScrollPane(table);
    add(scrollPane, BorderLayout.CENTER);

    JPanel buttonPanel = new JPanel();

    selectlabel = new JLabel(MessageBundle.getMessage("angal.menu.selectgroup"));
    buttonPanel.add(selectlabel);

    pbox = new JComboBox();
    pbox.addItem(MessageBundle.getMessage("angal.menu.all"));
    ArrayList<UserGroup> group = manager.getUserGroup();
    for (UserGroup elem : group) {
      pbox.addItem(elem);
    }
    pbox.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            pSelection = pbox.getSelectedItem().toString();
            if (pSelection.compareTo("ALL") == 0) model = new UserBrowserModel();
            else model = new UserBrowserModel(pSelection);
            model.fireTableDataChanged();
            table.updateUI();
          }
        });
    buttonPanel.add(pbox);

    JButton buttonNew = new JButton(MessageBundle.getMessage("angal.common.new"));
    buttonNew.setMnemonic(KeyEvent.VK_N);
    buttonNew.addActionListener(
        new ActionListener() {

          public void actionPerformed(ActionEvent event) {
            user = new User("", "", "", "");
            new UserEdit(myFrame, user, true);
          }
        });
    buttonPanel.add(buttonNew);

    JButton buttonEdit = new JButton(MessageBundle.getMessage("angal.common.edit"));
    buttonEdit.setMnemonic(KeyEvent.VK_E);
    buttonEdit.addActionListener(
        new ActionListener() {

          public void actionPerformed(ActionEvent event) {
            if (table.getSelectedRow() < 0) {
              JOptionPane.showMessageDialog(
                  null,
                  MessageBundle.getMessage("angal.common.pleaseselectarow"),
                  MessageBundle.getMessage("angal.hospital"),
                  JOptionPane.PLAIN_MESSAGE);
              return;
            } else {
              selectedrow = table.getSelectedRow();
              user = (User) (((UserBrowserModel) model).getValueAt(table.getSelectedRow(), -1));
              new UserEdit(myFrame, user, false);
            }
          }
        });
    buttonPanel.add(buttonEdit);

    JButton buttonResetPassword = new JButton(MessageBundle.getMessage("angal.menu.resetpassword"));
    buttonResetPassword.setMnemonic(KeyEvent.VK_R);
    buttonResetPassword.addActionListener(
        new ActionListener() {

          public void actionPerformed(ActionEvent event) {
            if (table.getSelectedRow() < 0) {
              JOptionPane.showMessageDialog(
                  null,
                  MessageBundle.getMessage("angal.common.pleaseselectarow"),
                  MessageBundle.getMessage("angal.hospital"),
                  JOptionPane.PLAIN_MESSAGE);
              return;
            } else {
              selectedrow = table.getSelectedRow();
              user = (User) (((UserBrowserModel) model).getValueAt(table.getSelectedRow(), -1));

              // 1. Insert new password
              JPasswordField pwd = new JPasswordField(10);
              pwd.addAncestorListener(
                  new AncestorListener() {

                    public void ancestorRemoved(AncestorEvent event) {}

                    public void ancestorMoved(AncestorEvent event) {}

                    public void ancestorAdded(AncestorEvent event) {
                      event.getComponent().requestFocusInWindow();
                    }
                  });
              String newPassword = "";
              while (newPassword.equals("")) {
                int action =
                    JOptionPane.showConfirmDialog(
                        UserBrowsing.this,
                        pwd,
                        MessageBundle.getMessage(
                            "angal.menu.onepleaseinsertnewpassowrdminsixdigits"),
                        JOptionPane.OK_CANCEL_OPTION);
                if (action == JOptionPane.CANCEL_OPTION) return;
                newPassword = new String(pwd.getPassword());
                if (newPassword == null || newPassword.equals("") || newPassword.length() < 6) {
                  JOptionPane.showMessageDialog(
                      UserBrowsing.this,
                      MessageBundle.getMessage(
                          "angal.menu.pleaseinsertavalidpasswordminsixdigits"));
                  newPassword = "";
                  pwd.setText("");
                }
              }

              // 2. Retype new password
              pwd.setText("");
              int action =
                  JOptionPane.showConfirmDialog(
                      UserBrowsing.this,
                      pwd,
                      MessageBundle.getMessage("angal.menu.twopleaserepeatpassword"),
                      JOptionPane.OK_CANCEL_OPTION);
              if (action == JOptionPane.CANCEL_OPTION) return;
              String newPassword2 = new String(pwd.getPassword());

              // 3. Check & Save
              if (!newPassword.equals(newPassword2)) {
                JOptionPane.showMessageDialog(
                    UserBrowsing.this,
                    MessageBundle.getMessage("angal.menu.retypeerrorpleaseretry"));
                return;
              }
              if (newPassword != null && !newPassword.equals("") && newPassword.length() >= 6) {
                String hashed = BCrypt.hashpw(new String(newPassword), BCrypt.gensalt());
                user.setPasswd(hashed);
                if (manager.updatePassword(user))
                  JOptionPane.showMessageDialog(
                      UserBrowsing.this,
                      MessageBundle.getMessage("angal.menu.thepasswordhasbeenchanged"));
              } else {
                JOptionPane.showMessageDialog(
                    UserBrowsing.this,
                    MessageBundle.getMessage("angal.menu.pleaseinsertavalidpasswordminsixdigits"));
              }
            }
          }
        });
    buttonPanel.add(buttonResetPassword);

    JButton buttonDelete = new JButton(MessageBundle.getMessage("angal.common.delete"));
    buttonDelete.setMnemonic(KeyEvent.VK_D);
    buttonDelete.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            if (table.getSelectedRow() < 0) {
              JOptionPane.showMessageDialog(
                  null,
                  MessageBundle.getMessage("angal.common.pleaseselectarow"),
                  MessageBundle.getMessage("angal.hospital"),
                  JOptionPane.PLAIN_MESSAGE);
              return;
            } else {
              UserBrowsingManager manager = new UserBrowsingManager();
              User m = (User) (((UserBrowserModel) model).getValueAt(table.getSelectedRow(), -1));
              int n =
                  JOptionPane.showConfirmDialog(
                      null,
                      MessageBundle.getMessage("angal.menu.deleteuser")
                          + " \""
                          + m.getUserName()
                          + "\" ?",
                      MessageBundle.getMessage("angal.hospital"),
                      JOptionPane.YES_NO_OPTION);

              if ((n == JOptionPane.YES_OPTION) && (manager.deleteUser(m))) {
                pUser.remove(table.getSelectedRow());
                model.fireTableDataChanged();
                table.updateUI();
              }
            }
          }
        });
    buttonPanel.add(buttonDelete);

    JButton buttonClose = new JButton(MessageBundle.getMessage("angal.common.close"));
    buttonClose.setMnemonic(KeyEvent.VK_C);
    buttonClose.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            dispose();
          }
        });
    buttonPanel.add(buttonClose);

    add(buttonPanel, BorderLayout.SOUTH);
    setVisible(true);
  }