UserGroupEditor.java
0001 /*  UserGroupEditor.java
0002  *
0003  *  Copyright (c) 1995-2010, The University of Sheffield. See the file
0004  *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
0005  *
0006  *  This file is part of GATE (see http://gate.ac.uk/), and is free
0007  *  software, licenced under the GNU Library General Public License,
0008  *  Version 2, June 1991 (in the distribution as file licence.html,
0009  *  and also available at http://gate.ac.uk/gate/licence.html).
0010  *
0011  *  Kalina Bontcheva,  03/October/2001
0012  *
0013  *  $Id: UserGroupEditor.java 12006 2009-12-01 17:24:28Z thomas_heitz $
0014  *
0015  */
0016 
0017 
0018 package gate.gui;
0019 
0020 import java.awt.*;
0021 import java.awt.event.*;
0022 import java.util.ArrayList;
0023 import java.util.Iterator;
0024 
0025 import javax.swing.*;
0026 import javax.swing.event.ListSelectionEvent;
0027 import javax.swing.event.ListSelectionListener;
0028 
0029 import junit.framework.Assert;
0030 
0031 import gate.*;
0032 import gate.security.*;
0033 import gate.util.GateRuntimeException;
0034 import gate.util.Out;
0035 
0036 
0037 public class UserGroupEditor extends JComponent {
0038   protected JPanel jPanel1 = new JPanel();
0039   protected JPanel jPanel2 = new JPanel();
0040   protected JList firstList = new JList();
0041   protected JList secondList = new JList();
0042   protected CardLayout cardLayout1 = new CardLayout();
0043   protected JRadioButton displayUsersFirst = new JRadioButton();
0044   protected JRadioButton displayGroupsFirst = new JRadioButton();
0045 
0046   protected Session session;
0047   protected AccessController controller;
0048 
0049   protected boolean usersFirst = true;
0050   protected JButton exitButton = new JButton();
0051   protected JPopupMenu userMenu = new JPopupMenu();
0052   protected JPopupMenu groupMenu = new JPopupMenu();
0053 
0054   public UserGroupEditor(AccessController ac, Session theSession) {
0055     try {
0056       jbInit();
0057     }
0058     catch(Exception e) {
0059       e.printStackTrace();
0060     }
0061 
0062     this.session = theSession;
0063     this.controller = ac;
0064 
0065     showUsersFirst();
0066 
0067   }
0068 
0069   public static void main(String[] argsthrows Exception {
0070     Gate.init();
0071 
0072     JFrame frame = new JFrame();
0073 
0074     java.util.List dbPaths = new ArrayList();
0075     DataStoreRegister reg = Gate.getDataStoreRegister();
0076     Iterator keyIter = DataStoreRegister.getConfigData().keySet().iterator();
0077     while (keyIter.hasNext()) {
0078       String keyName = (StringkeyIter.next();
0079       if (keyName.startsWith("url"))
0080         dbPaths.add(DataStoreRegister.getConfigData().get(keyName));
0081     }
0082     if (dbPaths.isEmpty())
0083       throw new
0084         GateRuntimeException("Oracle URL not configured in gate.xml");
0085     //by default make it the first
0086     String storageURL = (String)dbPaths.get(0);
0087     if (dbPaths.size() 1) {
0088       Object[] paths = dbPaths.toArray();
0089       Object answer = JOptionPane.showInputDialog(
0090                           frame,
0091                           "Select a database",
0092                           "GATE", JOptionPane.QUESTION_MESSAGE,
0093                           null, paths,
0094                           paths[0]);
0095       if (answer != null)
0096         storageURL = (Stringanswer;
0097       else
0098         return;
0099     }
0100 
0101 //    AccessController ac = new AccessControllerImpl(urlString);
0102     AccessController ac = Factory.createAccessController(storageURL);
0103     Assert.assertNotNull(ac);
0104     ac.open();
0105 
0106     Session mySession = null;
0107 
0108     try {
0109       mySession = login(ac, frame.getContentPane());
0110     catch (gate.security.SecurityException ex) {
0111         JOptionPane.showMessageDialog(
0112           frame,
0113           "To use this tool you must login as a user "
0114             "with administrative rights!",
0115           "Login error",
0116           JOptionPane.ERROR_MESSAGE
0117           );
0118       ac.close();
0119       System.exit(-1);
0120     }
0121 
0122     if (! ac.isValidSession(mySession)){
0123       JOptionPane.showMessageDialog(
0124         frame,
0125         "Incorrect session obtained. "
0126           "Probably there is a problem with the database!",
0127         "Login error",
0128         JOptionPane.ERROR_MESSAGE
0129         );
0130       ac.close();
0131       System.exit(-1);
0132     }
0133 
0134     if (!mySession.isPrivilegedSession()) {
0135         JOptionPane.showMessageDialog(
0136           frame,
0137           "Insufficient priviliges to edit/view groups and users!",
0138           "Login error",
0139           JOptionPane.ERROR_MESSAGE
0140           );
0141       ac.close();
0142       System.exit(-1);
0143     }
0144 
0145     //INITIALISE THE FRAME, ETC.
0146     frame.setEnabled(true);
0147     frame.setTitle("GATE User/Group Administration Tool");
0148     frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
0149 
0150 
0151     UserGroupEditor userGroupEditor1 = new UserGroupEditor(ac, mySession);
0152 
0153     //Put the bean in a scroll pane.
0154     frame.getContentPane().add(userGroupEditor1, BorderLayout.CENTER);
0155 
0156     //DISPLAY FRAME
0157     frame.pack();
0158     frame.setSize(800600);
0159     frame.setVisible(true);
0160 
0161   }
0162 
0163   public static Session login(AccessController ac, Component parent)
0164                           throws  gate.persist.PersistenceException,
0165                                   gate.security.SecurityException {
0166     String userName = "";
0167     String userPass = "";
0168     String group = "";
0169 
0170     JPanel listPanel = new JPanel();
0171     listPanel.setLayout(new BoxLayout(listPanel,BoxLayout.X_AXIS));
0172 
0173     JPanel panel1 = new JPanel();
0174     panel1.setLayout(new BoxLayout(panel1,BoxLayout.Y_AXIS));
0175     panel1.add(new JLabel("User name: "));
0176     panel1.add(new JLabel("Password: "));
0177     panel1.add(new JLabel("Group: "));
0178 
0179     JPanel panel2 = new JPanel();
0180     panel2.setLayout(new BoxLayout(panel2,BoxLayout.Y_AXIS));
0181     JTextField usrField = new JTextField(30);
0182     panel2.add(usrField);
0183     JPasswordField pwdField = new JPasswordField(30);
0184     panel2.add(pwdField);
0185     JTextField grpField = new JTextField(30);
0186     panel2.add(grpField);
0187 
0188     listPanel.add(panel1);
0189     listPanel.add(Box.createHorizontalStrut(20));
0190     listPanel.add(panel2);
0191 
0192     if(OkCancelDialog.showDialogparent,
0193                                   listPanel,
0194                                   "Please enter login details")){
0195       userName = usrField.getText();
0196       userPass = new String(pwdField.getPassword());
0197       group = grpField.getText();
0198       if (userName.equals(""|| userPass.equals(""|| group.equals("")) {
0199         JOptionPane.showMessageDialog(
0200           parent,
0201           "You must provide non-empty user name, password and group!",
0202           "Login error",
0203           JOptionPane.ERROR_MESSAGE
0204           );
0205         System.exit(-1);
0206       }
0207     }
0208 
0209     return ac.login(userName, userPass, ac.findGroup(group).getID());
0210   }
0211 
0212 
0213   private void jbInit() throws Exception {
0214     this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
0215 //    jPanel2.setLayout(new BorderLayout(40, 40));
0216     jPanel2.setLayout(new BoxLayout(jPanel2,BoxLayout.X_AXIS));
0217 
0218 
0219 //    jPanel1.setSize(800, 100);
0220 //    jPanel2.setSize(800, 500);
0221 
0222     displayUsersFirst.setText("Show all users");
0223     displayUsersFirst.setToolTipText("");
0224     displayUsersFirst.setActionCommand("usersFirst");
0225     displayUsersFirst.setSelected(true);
0226     displayUsersFirst.addItemListener(new java.awt.event.ItemListener() {
0227       public void itemStateChanged(ItemEvent e) {
0228         displayUsersFirst_itemStateChanged(e);
0229       }
0230     });
0231     displayGroupsFirst.setText("Show all groups");
0232     displayGroupsFirst.setActionCommand("groupsFirst");
0233 
0234     this.add(jPanel1, null);
0235     ButtonGroup group = new ButtonGroup();
0236     group.add(displayUsersFirst);
0237     group.add(displayGroupsFirst);
0238     this.add(jPanel1);
0239     jPanel1.add(displayUsersFirst);
0240     jPanel1.add(Box.createHorizontalStrut(50));
0241     jPanel1.add(displayGroupsFirst);
0242 
0243     this.add(jPanel2, null);
0244     jPanel2.add(new JScrollPane(firstList), BorderLayout.WEST);
0245     jPanel2.add(Box.createHorizontalStrut(50));
0246     jPanel2.add(new JScrollPane(secondList), BorderLayout.EAST);
0247     firstList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
0248     firstList.setModel(new DefaultListModel());
0249     firstList.addMouseListener(new MouseAdapter() {
0250       public void mouseClicked(MouseEvent e) {
0251         listRightMouseClick(e);
0252       }//mouse clicked
0253     });
0254     firstList.getSelectionModel().addListSelectionListener(
0255       new ListSelectionListener() {
0256         public void valueChanged(ListSelectionEvent e) {
0257           firstListItemSelected(e);
0258         }//
0259       }//the selection listener
0260     );
0261     secondList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
0262     secondList.setModel(new DefaultListModel());
0263     secondList.addMouseListener(new MouseAdapter() {
0264       public void mouseClicked(MouseEvent e) {
0265         listRightMouseClick(e);
0266       }//mouse clicked
0267     });
0268 
0269     this.add(Box.createVerticalGlue());
0270 
0271     this.add(exitButton);
0272     exitButton.setText("Exit");
0273     exitButton.addActionListener(new ActionListener() {
0274       public void actionPerformed(ActionEvent e) {
0275         try {
0276           controller.close();
0277         catch (gate.persist.PersistenceException ex) {
0278           Out.prln(ex.getMessage());
0279         }
0280         System.exit(0);
0281       //actionPerformed
0282     });
0283     this.add(Box.createVerticalStrut(50));
0284 
0285   }
0286 
0287   private void showUsersFirst() {
0288     DefaultListModel firstListData = (DefaultListModelfirstList.getModel();
0289     DefaultListModel secondListData = (DefaultListModelsecondList.getModel();
0290     firstListData.clear();
0291     secondListData.clear();
0292 
0293     readUsers(firstListData, firstList);
0294   }
0295 
0296   private void readUsers(DefaultListModel listModel, JList list) {
0297     //get the names of all users
0298     try {
0299       java.util.List users = controller.listUsers();
0300       for (int i = 0; i < users.size(); i++)
0301         listModel.addElement(users.get(i));
0302       list.setModel(listModel);
0303     catch (gate.persist.PersistenceException ex) {
0304       throw new gate.util.GateRuntimeException("Cannot read users!");
0305     }
0306 
0307   }//readUsers
0308 
0309   private void showGroupsFirst() {
0310     DefaultListModel firstListData = (DefaultListModelfirstList.getModel();
0311     DefaultListModel secondListData = (DefaultListModelsecondList.getModel();
0312     firstListData.clear();
0313     secondListData.clear();
0314 
0315     readGroups(firstListData, firstList);
0316   }
0317 
0318   private void readGroups(DefaultListModel listModel, JList list) {
0319     //get the names of all groups
0320     try {
0321       java.util.List groups = controller.listGroups();
0322       for (int i = 0; i < groups.size(); i++)
0323         listModel.addElement(groups.get(i));
0324       list.setModel(listModel);
0325     catch (gate.persist.PersistenceException ex) {
0326       throw new gate.util.GateRuntimeException("Cannot read groups!");
0327     }
0328 
0329   }//readGroups
0330 
0331   void displayUsersFirst_itemStateChanged(ItemEvent e) {
0332     if (e.getStateChange() == ItemEvent.DESELECTED) {
0333       if (!usersFirst)
0334         return;
0335       displayGroupsFirst.setSelected(true);
0336       if (usersFirst)  //if it used to be users first, we need to change
0337         showGroupsFirst();
0338       usersFirst = false;
0339     else {
0340       if (usersFirst)
0341         return;
0342       displayGroupsFirst.setSelected(false);
0343       if (! usersFirst)
0344         showUsersFirst();
0345       usersFirst = true;
0346     }
0347   //display users first (de-)selected
0348 
0349   void listRightMouseClick(MouseEvent e) {
0350         //if it's not a right click, then return
0351         //coz we're not interested
0352         if (! SwingUtilities.isRightMouseButton(e))
0353           return;
0354 
0355         JList theList = (JListe.getSource();
0356         //check if we have a selection and if not, try to force it
0357         if (theList.getSelectedIndex() == -1) {
0358           int index = theList.locationToIndex(e.getPoint());
0359           if (index == -1)
0360             return;
0361           else
0362             theList.setSelectedIndex(index);
0363         else
0364             //if the right click is outside the currently selected item return
0365             if theList.locationToIndex(e.getPoint())
0366                  !=  theList.getSelectedIndex())
0367               return;
0368 
0369 
0370         if (theList.equals(firstList)) {
0371           if (usersFirst)
0372             showUsersMenu(theList,
0373                           (inte.getPoint().getX(),
0374                           (inte.getPoint().getY());
0375           else
0376             showGroupsMenu(theList,
0377                           (inte.getPoint().getX(),
0378                           (inte.getPoint().getY());
0379 
0380         else {
0381           if (usersFirst)
0382             showGroupsMenu(theList,
0383                           (inte.getPoint().getX(),
0384                           (inte.getPoint().getY());
0385           else
0386             showUsersMenu(theList,
0387                           (inte.getPoint().getX(),
0388                           (inte.getPoint().getY());
0389 
0390         }
0391 
0392   }
0393 
0394   private void showUsersMenu(JList source, int x, int y) {
0395     //create the menu items first
0396     userMenu.removeAll();
0397     userMenu.add(new CreateUserAction(source));
0398     userMenu.add(new DeleteUserAction(source));
0399     userMenu.addSeparator();
0400     userMenu.add(new Add2GroupAction(source));
0401     userMenu.add(new RemoveFromGroupAction(source));
0402     userMenu.addSeparator();
0403     userMenu.add(new ChangePasswordAction(source));
0404     userMenu.add(new RenameUserAction(source));
0405 
0406     userMenu.show(source, x, y);
0407 
0408   }//create and show the menu for user manipulation
0409 
0410   private void showGroupsMenu(JList source, int x, int y) {
0411     //create the menu items first
0412     groupMenu.removeAll();
0413     groupMenu.add(new AddGroupAction(source));
0414     groupMenu.add(new DeleteGroupAction(source));
0415     groupMenu.addSeparator();
0416     groupMenu.add(new AddUserAction(source));
0417     groupMenu.add(new RemoveUserAction(source));
0418     groupMenu.addSeparator();
0419     groupMenu.add(new RenameGroupAction(source));
0420 
0421     groupMenu.show(source, x, y);
0422 
0423   }
0424 
0425   //called when the selection changes in the first list
0426   void firstListItemSelected(ListSelectionEvent e) {
0427     int i = firstList.getSelectedIndex();
0428     String name = (StringfirstList.getModel().getElementAt(i);
0429 
0430     if (usersFirst)
0431       showGroupsForUser(name);
0432     else
0433       showUsersForGroup(name);
0434   //firstListItemSelected
0435 
0436   protected void showGroupsForUser(String name) {
0437     User user = null;
0438     try {
0439       user = controller.findUser(name);
0440     catch (gate.persist.PersistenceException ex) {
0441       throw new gate.util.GateRuntimeException(
0442                   "Cannot locate the user with name: " + name
0443                 );
0444     catch (gate.security.SecurityException ex1) {
0445       throw new gate.util.GateRuntimeException(
0446                   ex1.getMessage()
0447                 );
0448     }
0449     if (user == null)
0450       return;
0451     java.util.List myGroups = user.getGroups();
0452     if (myGroups == null)
0453       return;
0454 
0455       DefaultListModel secondListData = new DefaultListModel();
0456 
0457       for (int j = 0; j< myGroups.size(); j++) {
0458         try {
0459           Group myGroup = //controller.findGroup((Long) myGroups.get(j));
0460             (Group)myGroups.get(j);
0461           secondListData.addElement(myGroup.getName());
0462         catch (Exception ex) {
0463           throw new gate.util.GateRuntimeException(
0464                   ex.getMessage()
0465                 );
0466         }//catch
0467       }//for loop
0468       secondList.setModel(secondListData);
0469 
0470   }//showGroupsForUser
0471 
0472 
0473   protected void showUsersForGroup(String name) {
0474     Group group = null;
0475     try {
0476       group = controller.findGroup(name);
0477     catch (gate.persist.PersistenceException ex) {
0478       throw new gate.util.GateRuntimeException(
0479                   "Cannot locate the group with name: " + name
0480                 );
0481     catch (gate.security.SecurityException ex1) {
0482       throw new gate.util.GateRuntimeException(
0483                   ex1.getMessage()
0484                 );
0485     }
0486     if (group == null)
0487       return;
0488     java.util.List myUsers = group.getUsers();
0489     if (myUsers == null)
0490       return;
0491 
0492       DefaultListModel secondListData = new DefaultListModel();
0493 
0494       for (int j = 0; j< myUsers.size(); j++) {
0495         try {
0496           User myUser = //controller.findUser((Long) myUsers.get(j));
0497             (User)myUsers.get(j);
0498           secondListData.addElement(myUser.getName());
0499         catch (Exception ex) {
0500           throw new gate.util.GateRuntimeException(
0501                   ex.getMessage()
0502                 );
0503         }//catch
0504       }//for loop
0505       secondList.setModel(secondListData);
0506 
0507   }//showGroupsForUser
0508 
0509 
0510   protected class CreateUserAction extends AbstractAction{
0511     private JList source;
0512 
0513     public CreateUserAction(JList source){
0514       super("Create new user");
0515       this.source = source;
0516     }
0517 
0518     public void actionPerformed(ActionEvent e){
0519       String userName= "", userPass = "";
0520 
0521       UserPasswordDialog pwdDlg = new UserPasswordDialog();
0522       boolean isOK = pwdDlg.showPasswordDialog(
0523                         "Please enter user name and password",
0524                         UserGroupEditor.this
0525                         );
0526 
0527       if (! isOK)
0528         return;
0529 
0530       try {
0531         controller.createUser(pwdDlg.getUserName(),
0532                               pwdDlg.getPassword(),
0533                               session);
0534       catch (gate.persist.PersistenceException ex) {
0535         throw new gate.util.GateRuntimeException(ex.getMessage());
0536       catch (gate.security.SecurityException ex1) {
0537         throw new gate.util.GateRuntimeException(ex1.getMessage());
0538       }
0539       DefaultListModel model = (DefaultListModelsource.getModel();
0540       model.clear();
0541       readUsers(model, source);
0542     }//public void actionPerformed(ActionEvent e)
0543   //CreateUserAction
0544 
0545   protected class DeleteUserAction extends AbstractAction{
0546     private JList source;
0547 
0548     public DeleteUserAction(JList source){
0549       super("Delete user");
0550       this.source = source;
0551     }
0552 
0553     public void actionPerformed(ActionEvent e){
0554       //first get the index of the selection
0555       int index = source.getSelectedIndex();
0556       if (index == -1//return if no selection
0557         return;
0558       DefaultListModel model = (DefaultListModelsource.getModel();
0559       try {
0560       User user = controller.findUser((Stringmodel.get(index) );
0561       controller.deleteUser(user, session);
0562       model.remove(index);
0563       catch (gate.persist.PersistenceException ex) {
0564         throw new gate.util.GateRuntimeException(ex.getMessage());
0565       catch (gate.security.SecurityException ex1) {
0566         throw new gate.util.GateRuntimeException(ex1.getMessage());
0567       }
0568     }//public void actionPerformed(ActionEvent e)
0569   //DeleteUserAction
0570 
0571   protected class Add2GroupAction extends AbstractAction{
0572     private JList source;
0573 
0574     public Add2GroupAction(JList source){
0575       super("Add to group");
0576       this.source = source;
0577     }
0578 
0579     public void actionPerformed(ActionEvent e){
0580       int index = source.getSelectedIndex();
0581       if (index == -1//return if no selection
0582         return;
0583       DefaultListModel model = (DefaultListModelsource.getModel();
0584 
0585       JList groupList = new JList();
0586       groupList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
0587 
0588       DefaultListModel grListModel = new DefaultListModel();
0589       readGroupsgrListModel, groupList);
0590       if(OkCancelDialog.showDialogUserGroupEditor.this,
0591                                     new JScrollPane(groupList),
0592                                     "Choose a new group")){
0593         String groupName = (StringgroupList.getSelectedValue();
0594 
0595         try {
0596           User user = controller.findUser((Stringmodel.get(index) );
0597           Group group = controller.findGroup(groupName);
0598           group.addUser(user, session);
0599 
0600           //finally update the original lists
0601           if (usersFirst)
0602             showGroupsForUser(user.getName());
0603         catch (gate.persist.PersistenceException ex) {
0604           throw new gate.util.GateRuntimeException(ex.getMessage());
0605         catch (gate.security.SecurityException ex1) {
0606           JOptionPane.showMessageDialog(UserGroupEditor.this,
0607                                         ex1.getMessage(),
0608                                         "Error adding user to group!",
0609                                         JOptionPane.ERROR_MESSAGE
0610                                        );
0611 
0612         }
0613 
0614       //ok selected
0615 
0616 
0617     }//public void actionPerformed(ActionEvent e)
0618   //Add2GroupAction
0619 
0620   protected class RemoveFromGroupAction extends AbstractAction{
0621     private JList source;
0622 
0623     public RemoveFromGroupAction(JList source){
0624       super("Remove from group");
0625       this.source = source;
0626     }//
0627 
0628     public void actionPerformed(ActionEvent e){
0629       int index = source.getSelectedIndex();
0630       if (index == -1//return if no selection
0631         return;
0632       DefaultListModel model = (DefaultListModelsource.getModel();
0633 
0634       JList groupList = new JList();
0635       groupList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
0636 
0637       DefaultListModel grListModel = new DefaultListModel();
0638       readGroupsgrListModel, groupList);
0639       if(OkCancelDialog.showDialog(
0640                           UserGroupEditor.this,
0641                           new JScrollPane(groupList),
0642                           "Choose the group from which to remove the user")
0643         ){
0644 
0645         String groupName = (StringgroupList.getSelectedValue();
0646 
0647         try {
0648           User user = controller.findUser((Stringmodel.get(index) );
0649           Group group = controller.findGroup(groupName);
0650           group.removeUser(user, session);
0651 
0652           //finally update the original lists
0653           if (usersFirst)
0654             showGroupsForUser(user.getName());
0655         catch (gate.persist.PersistenceException ex) {
0656           throw new gate.util.GateRuntimeException(ex.getMessage());
0657         catch (gate.security.SecurityException ex1) {
0658           JOptionPane.showMessageDialog(UserGroupEditor.this,
0659                                         ex1.getMessage(),
0660                                         "Error removing user from group!",
0661                                         JOptionPane.ERROR_MESSAGE
0662                                        );
0663 
0664         }
0665 
0666       //ok selected
0667 
0668     }//public void actionPerformed(ActionEvent e)
0669   //RemoveFromGroupAction
0670 
0671 
0672   protected class ChangePasswordAction extends AbstractAction{
0673     private JList source;
0674 
0675     public ChangePasswordAction(JList source){
0676       super("Change password");
0677       this.source = source;
0678     }//
0679 
0680     public void actionPerformed(ActionEvent e){
0681       int index = source.getSelectedIndex();
0682       if (index == -1//return if no selection
0683         return;
0684       DefaultListModel model = (DefaultListModelsource.getModel();
0685 
0686       JPanel listPanel = new JPanel();
0687       listPanel.setLayout(new BoxLayout(listPanel,BoxLayout.X_AXIS));
0688 
0689       JPanel panel1 = new JPanel();
0690       panel1.setLayout(new BoxLayout(panel1,BoxLayout.Y_AXIS));
0691       panel1.add(new JLabel("Please enter new password: "));
0692       panel1.add(new JLabel("Please re-enter new password: "));
0693 
0694 
0695       JPanel panel2 = new JPanel();
0696       panel2.setLayout(new BoxLayout(panel2,BoxLayout.Y_AXIS));
0697       JPasswordField pwd1 = new JPasswordField(30);
0698       panel2.add(pwd1);
0699       JPasswordField pwd2 = new JPasswordField(30);
0700       panel2.add(pwd2);
0701 
0702       listPanel.add(panel1);
0703       listPanel.add(Box.createHorizontalStrut(20));
0704       listPanel.add(panel2);
0705 
0706       if(OkCancelDialog.showDialogUserGroupEditor.this,
0707                                     listPanel,
0708                                     "Choose a new password")){
0709         String pass1 = new String(pwd1.getPassword());
0710         String pass2 = new String(pwd2.getPassword());
0711         if (!pass1.equals(pass2)) {
0712           JOptionPane.showMessageDialog(
0713                         UserGroupEditor.this,
0714                         "Cannot change password because you entered "
0715                           "two different values for new password",
0716                         "Error changing user password!",
0717                         JOptionPane.ERROR_MESSAGE
0718                         );
0719 
0720           return;
0721         }
0722 
0723 
0724         try {
0725           User user = controller.findUser((Stringmodel.get(index) );
0726           user.setPassword(pass1, session);
0727 
0728         catch (gate.persist.PersistenceException ex) {
0729           throw new gate.util.GateRuntimeException(ex.getMessage());
0730         catch (gate.security.SecurityException ex1) {
0731           JOptionPane.showMessageDialog(UserGroupEditor.this,
0732                                         ex1.getMessage(),
0733                                         "Error adding user to group!",
0734                                         JOptionPane.ERROR_MESSAGE
0735                                        );
0736 
0737         }
0738 
0739       //ok selected
0740 
0741     }//public void actionPerformed(ActionEvent e)
0742   //ChangePasswordAction
0743 
0744 
0745   protected class RenameUserAction extends AbstractAction{
0746     private JList source;
0747 
0748     public RenameUserAction(JList source){
0749       super("Rename user");
0750       this.source = source;
0751     }//
0752 
0753     public void actionPerformed(ActionEvent e){
0754       int index = source.getSelectedIndex();
0755       if (index == -1//return if no selection
0756         return;
0757 
0758       String newName = JOptionPane.showInputDialog(
0759                                      UserGroupEditor.this,
0760                                     "Please enter the user's new name");
0761 
0762       //don't change if nothing selected
0763       if (newName == null || newName.equals(""))
0764         return;
0765 
0766       DefaultListModel model = (DefaultListModelsource.getModel();
0767 
0768       try {
0769         User user = controller.findUser((Stringmodel.get(index) );
0770         user.setName(newName, session);
0771         model.setElementAt(newName, index);
0772 
0773         //finally update the original lists
0774         source.setSelectedIndex(index);
0775       catch (gate.persist.PersistenceException ex) {
0776         throw new gate.util.GateRuntimeException(ex.getMessage());
0777       catch (gate.security.SecurityException ex1) {
0778         JOptionPane.showMessageDialog(UserGroupEditor.this,
0779                                       ex1.getMessage(),
0780                                       "Error renaming user!",
0781                                       JOptionPane.ERROR_MESSAGE
0782                                      );
0783 
0784       }
0785 
0786     }//public void actionPerformed(ActionEvent e)
0787   //RenameUserAction
0788 
0789 
0790   protected class AddGroupAction extends AbstractAction{
0791     private JList source;
0792 
0793     public AddGroupAction(JList source){
0794       super("Create new group");
0795       this.source = source;
0796     }//
0797 
0798     public void actionPerformed(ActionEvent e){
0799       int index = source.getSelectedIndex();
0800       if (index == -1//return if no selection
0801         return;
0802 
0803       String groupName = JOptionPane.showInputDialog(
0804                                      UserGroupEditor.this,
0805                                     "Please enter the name of the new group");
0806 
0807       //don't change if nothing selected
0808       if (groupName == null || groupName.equals(""))
0809         return;
0810 
0811       try {
0812         controller.createGroup(groupName, session);
0813       catch (gate.persist.PersistenceException ex) {
0814         throw new gate.util.GateRuntimeException(ex.getMessage());
0815       catch (gate.security.SecurityException ex1) {
0816         throw new gate.util.GateRuntimeException(ex1.getMessage());
0817       }
0818       //only update if we're showing the groups first. Otherwise the groups for
0819       //this user remain the same
0820       if (!usersFirst) {
0821         DefaultListModel model = (DefaultListModelsource.getModel();
0822         model.clear();
0823         readGroups(model, source);
0824       }
0825 
0826     }//public void actionPerformed(ActionEvent e)
0827   //AddGroupAction
0828 
0829 
0830   protected class DeleteGroupAction extends AbstractAction{
0831     private JList source;
0832 
0833     public DeleteGroupAction(JList source){
0834       super("Delete group");
0835       this.source = source;
0836     }//
0837 
0838     public void actionPerformed(ActionEvent e){
0839       //first get the index of the selection
0840       int index = source.getSelectedIndex();
0841       if (index == -1//return if no selection
0842         return;
0843       DefaultListModel model = (DefaultListModelsource.getModel();
0844       try {
0845         Group group = controller.findGroup((Stringmodel.get(index) );
0846         controller.deleteGroup(group, session);
0847         model.remove(index);
0848       catch (gate.persist.PersistenceException ex) {
0849         throw new gate.util.GateRuntimeException(ex.getMessage());
0850       catch (gate.security.SecurityException ex1) {
0851         throw new gate.util.GateRuntimeException(ex1.getMessage());
0852       }
0853     }//public void actionPerformed(ActionEvent e)
0854   //DeleteGroupAction
0855 
0856 
0857   protected class AddUserAction extends AbstractAction{
0858     private JList source;
0859 
0860     public AddUserAction(JList source){
0861       super("Add user");
0862       this.source = source;
0863     }//
0864 
0865     public void actionPerformed(ActionEvent e){
0866       int index = source.getSelectedIndex();
0867       if (index == -1//return if no selection
0868         return;
0869       DefaultListModel model = (DefaultListModelsource.getModel();
0870 
0871       JList userList = new JList();
0872       userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
0873 
0874       DefaultListModel usrListModel = new DefaultListModel();
0875       readUsersusrListModel, userList);
0876       if(OkCancelDialog.showDialogUserGroupEditor.this,
0877                                     new JScrollPane(userList),
0878                                     "Choose a user to add")){
0879         String userName = (StringuserList.getSelectedValue();
0880 
0881         try {
0882           Group group = controller.findGroup((Stringmodel.get(index) );
0883           User user = controller.findUser(userName);
0884           group.addUser(user, session);
0885 
0886           //finally update the original lists
0887           if (!usersFirst)
0888             showUsersForGroup(group.getName());
0889         catch (gate.persist.PersistenceException ex) {
0890           throw new gate.util.GateRuntimeException(ex.getMessage());
0891         catch (gate.security.SecurityException ex1) {
0892           JOptionPane.showMessageDialog(UserGroupEditor.this,
0893                                         ex1.getMessage(),
0894                                         "Error adding user to group!",
0895                                         JOptionPane.ERROR_MESSAGE
0896                                        );
0897 
0898         }
0899 
0900       //ok selected
0901 
0902     }//public void actionPerformed(ActionEvent e)
0903   //AddUserAction
0904 
0905 
0906   protected class RemoveUserAction extends AbstractAction{
0907     private JList source;
0908 
0909     public RemoveUserAction(JList source){
0910       super("Remove user");
0911       this.source = source;
0912     }//
0913 
0914     public void actionPerformed(ActionEvent e){
0915       int index = source.getSelectedIndex();
0916       if (index == -1//return if no selection
0917         return;
0918       DefaultListModel model = (DefaultListModelsource.getModel();
0919       String groupName = (Stringsource.getSelectedValue();
0920 
0921       JList userList = new JList();
0922       userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
0923 
0924       DefaultListModel usrListModel = new DefaultListModel();
0925 
0926       Group group = null;
0927       try {
0928         group = controller.findGroup(groupName);
0929       catch (gate.persist.PersistenceException ex) {
0930         throw new gate.util.GateRuntimeException(
0931                     "Cannot locate group: " + groupName
0932                   );
0933       catch (gate.security.SecurityException ex1) {
0934         throw new gate.util.GateRuntimeException(
0935                     ex1.getMessage()
0936                   );
0937       }
0938       if (group == null)
0939         return;
0940       java.util.List myUsers = group.getUsers();
0941       if (myUsers == null)
0942         return;
0943 
0944       for (int j = 0; j< myUsers.size(); j++) {
0945         try {
0946           User myUser = (User)myUsers.get(j);
0947           usrListModel.addElement(myUser.getName());
0948         catch (Exception ex) {
0949           throw new gate.util.GateRuntimeException(
0950                   ex.getMessage()
0951                 );
0952         }//catch
0953       }//for loop
0954       userList.setModel(usrListModel);
0955 
0956       if(OkCancelDialog.showDialog(
0957                           UserGroupEditor.this,
0958                           new JScrollPane(userList),
0959                           "Choose the user you want removed from this group")
0960         ){
0961 
0962 
0963         try {
0964           User user = controller.findUser((StringuserList.getSelectedValue());
0965           group.removeUser(user, session);
0966 
0967           //finally update the original lists
0968           if (!usersFirst)
0969             showUsersForGroup(group.getName());
0970           else
0971             showGroupsForUser(user.getName());
0972         catch (gate.persist.PersistenceException ex) {
0973           throw new gate.util.GateRuntimeException(ex.getMessage());
0974         catch (gate.security.SecurityException ex1) {
0975           JOptionPane.showMessageDialog(UserGroupEditor.this,
0976                                         ex1.getMessage(),
0977                                         "Error removing user from group!",
0978                                         JOptionPane.ERROR_MESSAGE
0979                                        );
0980 
0981         }
0982 
0983       //ok selected
0984 
0985     }//public void actionPerformed(ActionEvent e)
0986   //RemoveUserAction
0987 
0988 
0989   protected class RenameGroupAction extends AbstractAction{
0990     private JList source;
0991 
0992     public RenameGroupAction(JList source){
0993       super("Rename group");
0994       this.source = source;
0995     }//
0996 
0997     public void actionPerformed(ActionEvent e){
0998       int index = source.getSelectedIndex();
0999       if (index == -1//return if no selection
1000         return;
1001       DefaultListModel model = (DefaultListModelsource.getModel();
1002 
1003       String newName = JOptionPane.showInputDialog(
1004                                      UserGroupEditor.this,
1005                                     "Please enter the user's new name");
1006 
1007       //don't change if nothing selected
1008       if (newName == null || newName.equals(""))
1009         return;
1010 
1011       try {
1012         Group group = controller.findGroup((Stringmodel.get(index) );
1013         group.setName(newName, session);
1014 
1015         //finally update the original lists
1016         if (!usersFirst)
1017           showGroupsFirst();
1018         else
1019           showGroupsForUser((StringfirstList.getSelectedValue());
1020       catch (gate.persist.PersistenceException ex) {
1021         throw new gate.util.GateRuntimeException(ex.getMessage());
1022       catch (gate.security.SecurityException ex1) {
1023         JOptionPane.showMessageDialog(UserGroupEditor.this,
1024                                       ex1.getMessage(),
1025                                       "Error renaming user!",
1026                                       JOptionPane.ERROR_MESSAGE
1027                                      );
1028 
1029       }
1030     }//public void actionPerformed(ActionEvent e)
1031   //RenameGroupAction
1032 
1033 //UserGroupEditor