1   /*  UserGroupEditor.java
2    *
3    *  Copyright (c) 1998-2001, The University of Sheffield.
4    *
5    *  This file is part of GATE (see http://gate.ac.uk/), and is free
6    *  software, licenced under the GNU Library General Public License,
7    *  Version 2, June 1991 (in the distribution as file licence.html,
8    *  and also available at http://gate.ac.uk/gate/licence.html).
9    *
10   *  Kalina Bontcheva,  03/October/2001
11   *
12   *  $Id: UserGroupEditor.java,v 1.13 2001/11/24 17:29:38 kalina Exp $
13   *
14   */
15  
16  
17  package gate.gui;
18  
19  import javax.swing.*;
20  import javax.swing.event.*;
21  import java.awt.*;
22  import java.awt.event.*;
23  import java.io.*;
24  import java.util.*;
25  
26  import junit.framework.*;
27  
28  import gate.security.*;
29  import gate.*;
30  import gate.util.*;
31  
32  
33  public class UserGroupEditor extends JComponent {
34    protected JPanel jPanel1 = new JPanel();
35    protected JPanel jPanel2 = new JPanel();
36    protected JList firstList = new JList();
37    protected JList secondList = new JList();
38    protected CardLayout cardLayout1 = new CardLayout();
39    protected JRadioButton displayUsersFirst = new JRadioButton();
40    protected JRadioButton displayGroupsFirst = new JRadioButton();
41  
42    protected Session session;
43    protected AccessController controller;
44  
45    protected boolean usersFirst = true;
46    protected JButton exitButton = new JButton();
47    protected JPopupMenu userMenu = new JPopupMenu();
48    protected JPopupMenu groupMenu = new JPopupMenu();
49  
50    public UserGroupEditor(AccessController ac, Session theSession) {
51      try {
52        jbInit();
53      }
54      catch(Exception e) {
55        e.printStackTrace();
56      }
57  
58      this.session = theSession;
59      this.controller = ac;
60  
61      showUsersFirst();
62  
63    }
64  
65    public static void main(String[] args) throws Exception {
66      Gate.init();
67  
68      JFrame frame = new JFrame();
69  
70      java.util.List dbPaths = new ArrayList();
71      DataStoreRegister reg = Gate.getDataStoreRegister();
72      Iterator keyIter = reg.getConfigData().keySet().iterator();
73      while (keyIter.hasNext()) {
74        String keyName = (String) keyIter.next();
75        if (keyName.startsWith("url"))
76          dbPaths.add(reg.getConfigData().get(keyName));
77      }
78      if (dbPaths.isEmpty())
79        throw new
80          GateRuntimeException("Oracle URL not configured in gate.xml");
81      //by default make it the first
82      String storageURL = (String)dbPaths.get(0);
83      if (dbPaths.size() > 1) {
84        Object[] paths = dbPaths.toArray();
85        Object answer = JOptionPane.showInputDialog(
86                            frame,
87                            "Select a database",
88                            "Gate", JOptionPane.QUESTION_MESSAGE,
89                            null, paths,
90                            paths[0]);
91        if (answer != null)
92          storageURL = (String) answer;
93        else
94          return;
95      }
96  
97  //    AccessController ac = new AccessControllerImpl(urlString);
98      AccessController ac = Factory.createAccessController(storageURL);
99      Assert.assertNotNull(ac);
100     ac.open();
101 
102     Session mySession = null;
103 
104     try {
105       mySession = login(ac, frame.getContentPane());
106     } catch (gate.security.SecurityException ex) {
107         JOptionPane.showMessageDialog(
108           frame,
109           "To use this tool you must login as a user "
110             + "with administrative rights!",
111           "Login error",
112           JOptionPane.ERROR_MESSAGE
113           );
114       ac.close();
115       System.exit(-1);
116     }
117 
118     if (! ac.isValidSession(mySession)){
119       JOptionPane.showMessageDialog(
120         frame,
121         "Incorrect session obtained. "
122           + "Probably there is a problem with the database!",
123         "Login error",
124         JOptionPane.ERROR_MESSAGE
125         );
126       ac.close();
127       System.exit(-1);
128     }
129 
130     if (!mySession.isPrivilegedSession()) {
131         JOptionPane.showMessageDialog(
132           frame,
133           "Insufficient priviliges to edit/view groups and users!",
134           "Login error",
135           JOptionPane.ERROR_MESSAGE
136           );
137       ac.close();
138       System.exit(-1);
139     }
140 
141     //INITIALISE THE FRAME, ETC.
142     frame.setEnabled(true);
143     frame.setTitle("GATE User/Group Administration Tool");
144     frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
145 
146 
147     UserGroupEditor userGroupEditor1 = new UserGroupEditor(ac, mySession);
148 
149     //Put the bean in a scroll pane.
150     frame.getContentPane().add(userGroupEditor1, BorderLayout.CENTER);
151 
152     //DISPLAY FRAME
153     frame.pack();
154     frame.setSize(800, 600);
155     frame.show();
156 
157   }
158 
159   public static Session login(AccessController ac, Component parent)
160                           throws  gate.persist.PersistenceException,
161                                   gate.security.SecurityException {
162     String userName = "";
163     String userPass = "";
164     String group = "";
165 
166     JPanel listPanel = new JPanel();
167     listPanel.setLayout(new BoxLayout(listPanel,BoxLayout.X_AXIS));
168 
169     JPanel panel1 = new JPanel();
170     panel1.setLayout(new BoxLayout(panel1,BoxLayout.Y_AXIS));
171     panel1.add(new JLabel("User name: "));
172     panel1.add(new JLabel("Password: "));
173     panel1.add(new JLabel("Group: "));
174 
175     JPanel panel2 = new JPanel();
176     panel2.setLayout(new BoxLayout(panel2,BoxLayout.Y_AXIS));
177     JTextField usrField = new JTextField(30);
178     panel2.add(usrField);
179     JPasswordField pwdField = new JPasswordField(30);
180     panel2.add(pwdField);
181     JTextField grpField = new JTextField(30);
182     panel2.add(grpField);
183 
184     listPanel.add(panel1);
185     listPanel.add(Box.createHorizontalStrut(20));
186     listPanel.add(panel2);
187 
188     if(OkCancelDialog.showDialog( parent,
189                                   listPanel,
190                                   "Please enter login details")){
191       userName = usrField.getText();
192       userPass = new String(pwdField.getPassword());
193       group = grpField.getText();
194       if (userName.equals("") || userPass.equals("") || group.equals("")) {
195         JOptionPane.showMessageDialog(
196           parent,
197           "You must provide non-empty user name, password and group!",
198           "Login error",
199           JOptionPane.ERROR_MESSAGE
200           );
201         System.exit(-1);
202       }
203     }
204 
205     return ac.login(userName, userPass, ac.findGroup(group).getID());
206   }
207 
208 
209   private void jbInit() throws Exception {
210     this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
211 //    jPanel2.setLayout(new BorderLayout(40, 40));
212     jPanel2.setLayout(new BoxLayout(jPanel2,BoxLayout.X_AXIS));
213 
214 
215 //    jPanel1.setSize(800, 100);
216 //    jPanel2.setSize(800, 500);
217 
218     displayUsersFirst.setText("Show all users");
219     displayUsersFirst.setToolTipText("");
220     displayUsersFirst.setActionCommand("usersFirst");
221     displayUsersFirst.setSelected(true);
222     displayUsersFirst.addItemListener(new java.awt.event.ItemListener() {
223       public void itemStateChanged(ItemEvent e) {
224         displayUsersFirst_itemStateChanged(e);
225       }
226     });
227     displayGroupsFirst.setText("Show all groups");
228     displayGroupsFirst.setActionCommand("groupsFirst");
229 
230     this.add(jPanel1, null);
231     ButtonGroup group = new ButtonGroup();
232     group.add(displayUsersFirst);
233     group.add(displayGroupsFirst);
234     this.add(jPanel1);
235     jPanel1.add(displayUsersFirst);
236     jPanel1.add(Box.createHorizontalStrut(50));
237     jPanel1.add(displayGroupsFirst);
238 
239     this.add(jPanel2, null);
240     jPanel2.add(new JScrollPane(firstList), BorderLayout.WEST);
241     jPanel2.add(Box.createHorizontalStrut(50));
242     jPanel2.add(new JScrollPane(secondList), BorderLayout.EAST);
243     firstList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
244     firstList.setModel(new DefaultListModel());
245     firstList.addMouseListener(new MouseAdapter() {
246       public void mouseClicked(MouseEvent e) {
247         listRightMouseClick(e);
248       }//mouse clicked
249     });
250     firstList.getSelectionModel().addListSelectionListener(
251       new ListSelectionListener() {
252         public void valueChanged(ListSelectionEvent e) {
253           firstListItemSelected(e);
254         }//
255       }//the selection listener
256     );
257     secondList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
258     secondList.setModel(new DefaultListModel());
259     secondList.addMouseListener(new MouseAdapter() {
260       public void mouseClicked(MouseEvent e) {
261         listRightMouseClick(e);
262       }//mouse clicked
263     });
264 
265     this.add(Box.createVerticalGlue());
266 
267     this.add(exitButton);
268     exitButton.setText("Exit");
269     exitButton.addActionListener(new ActionListener() {
270       public void actionPerformed(ActionEvent e) {
271         try {
272           controller.close();
273         } catch (gate.persist.PersistenceException ex) {
274           Out.prln(ex.getMessage());
275         }
276         System.exit(0);
277       } //actionPerformed
278     });
279     this.add(Box.createVerticalStrut(50));
280 
281   }
282 
283   private void showUsersFirst() {
284     DefaultListModel firstListData = (DefaultListModel) firstList.getModel();
285     DefaultListModel secondListData = (DefaultListModel) secondList.getModel();
286     firstListData.clear();
287     secondListData.clear();
288 
289     readUsers(firstListData, firstList);
290   }
291 
292   private void readUsers(DefaultListModel listModel, JList list) {
293     //get the names of all users
294     try {
295       java.util.List users = controller.listUsers();
296       for (int i = 0; i < users.size(); i++)
297         listModel.addElement(users.get(i));
298       list.setModel(listModel);
299     } catch (gate.persist.PersistenceException ex) {
300       throw new gate.util.GateRuntimeException("Cannot read users!");
301     }
302 
303   }//readUsers
304 
305   private void showGroupsFirst() {
306     DefaultListModel firstListData = (DefaultListModel) firstList.getModel();
307     DefaultListModel secondListData = (DefaultListModel) secondList.getModel();
308     firstListData.clear();
309     secondListData.clear();
310 
311     readGroups(firstListData, firstList);
312   }
313 
314   private void readGroups(DefaultListModel listModel, JList list) {
315     //get the names of all groups
316     try {
317       java.util.List groups = controller.listGroups();
318       for (int i = 0; i < groups.size(); i++)
319         listModel.addElement(groups.get(i));
320       list.setModel(listModel);
321     } catch (gate.persist.PersistenceException ex) {
322       throw new gate.util.GateRuntimeException("Cannot read groups!");
323     }
324 
325   }//readGroups
326 
327   void displayUsersFirst_itemStateChanged(ItemEvent e) {
328     if (e.getStateChange() == e.DESELECTED) {
329       if (!usersFirst)
330         return;
331       displayGroupsFirst.setSelected(true);
332       if (usersFirst)  //if it used to be users first, we need to change
333         showGroupsFirst();
334       usersFirst = false;
335     } else {
336       if (usersFirst)
337         return;
338       displayGroupsFirst.setSelected(false);
339       if (! usersFirst)
340         showUsersFirst();
341       usersFirst = true;
342     }
343   } //display users first (de-)selected
344 
345   void listRightMouseClick(MouseEvent e) {
346         //if it's not a right click, then return
347         //coz we're not interested
348         if (! SwingUtilities.isRightMouseButton(e))
349           return;
350 
351         JList theList = (JList) e.getSource();
352         //check if we have a selection and if not, try to force it
353         if (theList.getSelectedIndex() == -1) {
354           int index = theList.locationToIndex(e.getPoint());
355           if (index == -1)
356             return;
357           else
358             theList.setSelectedIndex(index);
359         } else
360             //if the right click is outside the currently selected item return
361             if ( theList.locationToIndex(e.getPoint())
362                  !=  theList.getSelectedIndex())
363               return;
364 
365 
366         if (theList.equals(firstList)) {
367           if (usersFirst)
368             showUsersMenu(theList,
369                           (int) e.getPoint().getX(),
370                           (int) e.getPoint().getY());
371           else
372             showGroupsMenu(theList,
373                           (int) e.getPoint().getX(),
374                           (int) e.getPoint().getY());
375 
376         } else {
377           if (usersFirst)
378             showGroupsMenu(theList,
379                           (int) e.getPoint().getX(),
380                           (int) e.getPoint().getY());
381           else
382             showUsersMenu(theList,
383                           (int) e.getPoint().getX(),
384                           (int) e.getPoint().getY());
385 
386         }
387 
388   }
389 
390   private void showUsersMenu(JList source, int x, int y) {
391     //create the menu items first
392     userMenu.removeAll();
393     userMenu.add(new CreateUserAction(source));
394     userMenu.add(new DeleteUserAction(source));
395     userMenu.addSeparator();
396     userMenu.add(new Add2GroupAction(source));
397     userMenu.add(new RemoveFromGroupAction(source));
398     userMenu.addSeparator();
399     userMenu.add(new ChangePasswordAction(source));
400     userMenu.add(new RenameUserAction(source));
401 
402     userMenu.show(source, x, y);
403 
404   }//create and show the menu for user manipulation
405 
406   private void showGroupsMenu(JList source, int x, int y) {
407     //create the menu items first
408     groupMenu.removeAll();
409     groupMenu.add(new AddGroupAction(source));
410     groupMenu.add(new DeleteGroupAction(source));
411     groupMenu.addSeparator();
412     groupMenu.add(new AddUserAction(source));
413     groupMenu.add(new RemoveUserAction(source));
414     groupMenu.addSeparator();
415     groupMenu.add(new RenameGroupAction(source));
416 
417     groupMenu.show(source, x, y);
418 
419   }
420 
421   //called when the selection changes in the first list
422   void firstListItemSelected(ListSelectionEvent e) {
423     int i = firstList.getSelectedIndex();
424     String name = (String) firstList.getModel().getElementAt(i);
425 
426     if (usersFirst)
427       showGroupsForUser(name);
428     else
429       showUsersForGroup(name);
430   } //firstListItemSelected
431 
432   protected void showGroupsForUser(String name) {
433     User user = null;
434     try {
435       user = controller.findUser(name);
436     } catch (gate.persist.PersistenceException ex) {
437       throw new gate.util.GateRuntimeException(
438                   "Cannot locate the user with name: " + name
439                 );
440     } catch (gate.security.SecurityException ex1) {
441       throw new gate.util.GateRuntimeException(
442                   ex1.getMessage()
443                 );
444     }
445     if (user == null)
446       return;
447     java.util.List myGroups = user.getGroups();
448     if (myGroups == null)
449       return;
450 
451       DefaultListModel secondListData = new DefaultListModel();
452 
453       for (int j = 0; j< myGroups.size(); j++) {
454         try {
455           Group myGroup = //controller.findGroup((Long) myGroups.get(j));
456             (Group)myGroups.get(j);
457           secondListData.addElement(myGroup.getName());
458         } catch (Exception ex) {
459           throw new gate.util.GateRuntimeException(
460                   ex.getMessage()
461                 );
462         }//catch
463       }//for loop
464       secondList.setModel(secondListData);
465 
466   }//showGroupsForUser
467 
468 
469   protected void showUsersForGroup(String name) {
470     Group group = null;
471     try {
472       group = controller.findGroup(name);
473     } catch (gate.persist.PersistenceException ex) {
474       throw new gate.util.GateRuntimeException(
475                   "Cannot locate the group with name: " + name
476                 );
477     } catch (gate.security.SecurityException ex1) {
478       throw new gate.util.GateRuntimeException(
479                   ex1.getMessage()
480                 );
481     }
482     if (group == null)
483       return;
484     java.util.List myUsers = group.getUsers();
485     if (myUsers == null)
486       return;
487 
488       DefaultListModel secondListData = new DefaultListModel();
489 
490       for (int j = 0; j< myUsers.size(); j++) {
491         try {
492           User myUser = //controller.findUser((Long) myUsers.get(j));
493             (User)myUsers.get(j);
494           secondListData.addElement(myUser.getName());
495         } catch (Exception ex) {
496           throw new gate.util.GateRuntimeException(
497                   ex.getMessage()
498                 );
499         }//catch
500       }//for loop
501       secondList.setModel(secondListData);
502 
503   }//showGroupsForUser
504 
505 
506   protected class CreateUserAction extends AbstractAction{
507     private JList source;
508 
509     public CreateUserAction(JList source){
510       super("Create new user");
511       this.source = source;
512     }
513 
514     public void actionPerformed(ActionEvent e){
515       String userName= "", userPass = "";
516 
517       UserPasswordDialog pwdDlg = new UserPasswordDialog();
518       boolean isOK = pwdDlg.showPasswordDialog(
519                         "Please enter user name and password",
520                         UserGroupEditor.this
521                         );
522 
523       if (! isOK)
524         return;
525 
526       try {
527         controller.createUser(pwdDlg.getUserName(),
528                               pwdDlg.getPassword(),
529                               session);
530       } catch (gate.persist.PersistenceException ex) {
531         throw new gate.util.GateRuntimeException(ex.getMessage());
532       } catch (gate.security.SecurityException ex1) {
533         throw new gate.util.GateRuntimeException(ex1.getMessage());
534       }
535       DefaultListModel model = (DefaultListModel) source.getModel();
536       model.clear();
537       readUsers(model, source);
538     }//public void actionPerformed(ActionEvent e)
539   } //CreateUserAction
540 
541   protected class DeleteUserAction extends AbstractAction{
542     private JList source;
543 
544     public DeleteUserAction(JList source){
545       super("Delete user");
546       this.source = source;
547     }
548 
549     public void actionPerformed(ActionEvent e){
550       //first get the index of the selection
551       int index = source.getSelectedIndex();
552       if (index == -1) //return if no selection
553         return;
554       DefaultListModel model = (DefaultListModel) source.getModel();
555       try {
556       User user = controller.findUser((String) model.get(index) );
557       controller.deleteUser(user, session);
558       model.remove(index);
559       } catch (gate.persist.PersistenceException ex) {
560         throw new gate.util.GateRuntimeException(ex.getMessage());
561       } catch (gate.security.SecurityException ex1) {
562         throw new gate.util.GateRuntimeException(ex1.getMessage());
563       }
564     }//public void actionPerformed(ActionEvent e)
565   } //DeleteUserAction
566 
567   protected class Add2GroupAction extends AbstractAction{
568     private JList source;
569 
570     public Add2GroupAction(JList source){
571       super("Add to group");
572       this.source = source;
573     }
574 
575     public void actionPerformed(ActionEvent e){
576       int index = source.getSelectedIndex();
577       if (index == -1) //return if no selection
578         return;
579       DefaultListModel model = (DefaultListModel) source.getModel();
580 
581       JList groupList = new JList();
582       groupList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
583 
584       DefaultListModel grListModel = new DefaultListModel();
585       readGroups( grListModel, groupList);
586       if(OkCancelDialog.showDialog( UserGroupEditor.this,
587                                     new JScrollPane(groupList),
588                                     "Choose a new group")){
589         String groupName = (String) groupList.getSelectedValue();
590 
591         try {
592           User user = controller.findUser((String) model.get(index) );
593           Group group = controller.findGroup(groupName);
594           group.addUser(user, session);
595 
596           //finally update the original lists
597           if (usersFirst)
598             showGroupsForUser(user.getName());
599         } catch (gate.persist.PersistenceException ex) {
600           throw new gate.util.GateRuntimeException(ex.getMessage());
601         } catch (gate.security.SecurityException ex1) {
602           JOptionPane.showMessageDialog(UserGroupEditor.this,
603                                         ex1.getMessage(),
604                                         "Error adding user to group!",
605                                         JOptionPane.ERROR_MESSAGE
606                                        );
607 
608         }
609 
610       } //ok selected
611 
612 
613     }//public void actionPerformed(ActionEvent e)
614   } //Add2GroupAction
615 
616   protected class RemoveFromGroupAction extends AbstractAction{
617     private JList source;
618 
619     public RemoveFromGroupAction(JList source){
620       super("Remove from group");
621       this.source = source;
622     }//
623 
624     public void actionPerformed(ActionEvent e){
625       int index = source.getSelectedIndex();
626       if (index == -1) //return if no selection
627         return;
628       DefaultListModel model = (DefaultListModel) source.getModel();
629 
630       JList groupList = new JList();
631       groupList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
632 
633       DefaultListModel grListModel = new DefaultListModel();
634       readGroups( grListModel, groupList);
635       if(OkCancelDialog.showDialog(
636                           UserGroupEditor.this,
637                           new JScrollPane(groupList),
638                           "Choose the group from which to remove the user")
639         ){
640 
641         String groupName = (String) groupList.getSelectedValue();
642 
643         try {
644           User user = controller.findUser((String) model.get(index) );
645           Group group = controller.findGroup(groupName);
646           group.removeUser(user, session);
647 
648           //finally update the original lists
649           if (usersFirst)
650             showGroupsForUser(user.getName());
651         } catch (gate.persist.PersistenceException ex) {
652           throw new gate.util.GateRuntimeException(ex.getMessage());
653         } catch (gate.security.SecurityException ex1) {
654           JOptionPane.showMessageDialog(UserGroupEditor.this,
655                                         ex1.getMessage(),
656                                         "Error removing user from group!",
657                                         JOptionPane.ERROR_MESSAGE
658                                        );
659 
660         }
661 
662       } //ok selected
663 
664     }//public void actionPerformed(ActionEvent e)
665   } //RemoveFromGroupAction
666 
667 
668   protected class ChangePasswordAction extends AbstractAction{
669     private JList source;
670 
671     public ChangePasswordAction(JList source){
672       super("Change password");
673       this.source = source;
674     }//
675 
676     public void actionPerformed(ActionEvent e){
677       int index = source.getSelectedIndex();
678       if (index == -1) //return if no selection
679         return;
680       DefaultListModel model = (DefaultListModel) source.getModel();
681 
682       JPanel listPanel = new JPanel();
683       listPanel.setLayout(new BoxLayout(listPanel,BoxLayout.X_AXIS));
684 
685       JPanel panel1 = new JPanel();
686       panel1.setLayout(new BoxLayout(panel1,BoxLayout.Y_AXIS));
687       panel1.add(new JLabel("Please enter new password: "));
688       panel1.add(new JLabel("Please re-enter new password: "));
689 
690 
691       JPanel panel2 = new JPanel();
692       panel2.setLayout(new BoxLayout(panel2,BoxLayout.Y_AXIS));
693       JPasswordField pwd1 = new JPasswordField(30);
694       panel2.add(pwd1);
695       JPasswordField pwd2 = new JPasswordField(30);
696       panel2.add(pwd2);
697 
698       listPanel.add(panel1);
699       listPanel.add(Box.createHorizontalStrut(20));
700       listPanel.add(panel2);
701 
702       if(OkCancelDialog.showDialog( UserGroupEditor.this,
703                                     listPanel,
704                                     "Choose a new password")){
705         String pass1 = new String(pwd1.getPassword());
706         String pass2 = new String(pwd2.getPassword());
707         if (!pass1.equals(pass2)) {
708           JOptionPane.showMessageDialog(
709                         UserGroupEditor.this,
710                         "Cannot change password because you entered "
711                           + "two different values for new password",
712                         "Error changing user password!",
713                         JOptionPane.ERROR_MESSAGE
714                         );
715 
716           return;
717         }
718 
719 
720         try {
721           User user = controller.findUser((String) model.get(index) );
722           user.setPassword(pass1, session);
723 
724         } catch (gate.persist.PersistenceException ex) {
725           throw new gate.util.GateRuntimeException(ex.getMessage());
726         } catch (gate.security.SecurityException ex1) {
727           JOptionPane.showMessageDialog(UserGroupEditor.this,
728                                         ex1.getMessage(),
729                                         "Error adding user to group!",
730                                         JOptionPane.ERROR_MESSAGE
731                                        );
732 
733         }
734 
735       } //ok selected
736 
737     }//public void actionPerformed(ActionEvent e)
738   } //ChangePasswordAction
739 
740 
741   protected class RenameUserAction extends AbstractAction{
742     private JList source;
743 
744     public RenameUserAction(JList source){
745       super("Rename user");
746       this.source = source;
747     }//
748 
749     public void actionPerformed(ActionEvent e){
750       int index = source.getSelectedIndex();
751       if (index == -1) //return if no selection
752         return;
753 
754       String newName = JOptionPane.showInputDialog(
755                                      UserGroupEditor.this,
756                                     "Please enter the user's new name");
757 
758       //don't change if nothing selected
759       if (newName == null || newName.equals(""))
760         return;
761 
762       DefaultListModel model = (DefaultListModel) source.getModel();
763 
764       try {
765         User user = controller.findUser((String) model.get(index) );
766         user.setName(newName, session);
767         model.setElementAt(newName, index);
768 
769         //finally update the original lists
770         source.setSelectedIndex(index);
771       } catch (gate.persist.PersistenceException ex) {
772         throw new gate.util.GateRuntimeException(ex.getMessage());
773       } catch (gate.security.SecurityException ex1) {
774         JOptionPane.showMessageDialog(UserGroupEditor.this,
775                                       ex1.getMessage(),
776                                       "Error renaming user!",
777                                       JOptionPane.ERROR_MESSAGE
778                                      );
779 
780       }
781 
782     }//public void actionPerformed(ActionEvent e)
783   } //RenameUserAction
784 
785 
786   protected class AddGroupAction extends AbstractAction{
787     private JList source;
788 
789     public AddGroupAction(JList source){
790       super("Create new group");
791       this.source = source;
792     }//
793 
794     public void actionPerformed(ActionEvent e){
795       int index = source.getSelectedIndex();
796       if (index == -1) //return if no selection
797         return;
798 
799       String groupName = JOptionPane.showInputDialog(
800                                      UserGroupEditor.this,
801                                     "Please enter the name of the new group");
802 
803       //don't change if nothing selected
804       if (groupName == null || groupName.equals(""))
805         return;
806 
807       try {
808         controller.createGroup(groupName, session);
809       } catch (gate.persist.PersistenceException ex) {
810         throw new gate.util.GateRuntimeException(ex.getMessage());
811       } catch (gate.security.SecurityException ex1) {
812         throw new gate.util.GateRuntimeException(ex1.getMessage());
813       }
814       //only update if we're showing the groups first. Otherwise the groups for
815       //this user remain the same
816       if (!usersFirst) {
817         DefaultListModel model = (DefaultListModel) source.getModel();
818         model.clear();
819         readGroups(model, source);
820       }
821 
822     }//public void actionPerformed(ActionEvent e)
823   } //AddGroupAction
824 
825 
826   protected class DeleteGroupAction extends AbstractAction{
827     private JList source;
828 
829     public DeleteGroupAction(JList source){
830       super("Delete group");
831       this.source = source;
832     }//
833 
834     public void actionPerformed(ActionEvent e){
835       //first get the index of the selection
836       int index = source.getSelectedIndex();
837       if (index == -1) //return if no selection
838         return;
839       DefaultListModel model = (DefaultListModel) source.getModel();
840       try {
841         Group group = controller.findGroup((String) model.get(index) );
842         controller.deleteGroup(group, session);
843         model.remove(index);
844       } catch (gate.persist.PersistenceException ex) {
845         throw new gate.util.GateRuntimeException(ex.getMessage());
846       } catch (gate.security.SecurityException ex1) {
847         throw new gate.util.GateRuntimeException(ex1.getMessage());
848       }
849     }//public void actionPerformed(ActionEvent e)
850   } //DeleteGroupAction
851 
852 
853   protected class AddUserAction extends AbstractAction{
854     private JList source;
855 
856     public AddUserAction(JList source){
857       super("Add user");
858       this.source = source;
859     }//
860 
861     public void actionPerformed(ActionEvent e){
862       int index = source.getSelectedIndex();
863       if (index == -1) //return if no selection
864         return;
865       DefaultListModel model = (DefaultListModel) source.getModel();
866 
867       JList userList = new JList();
868       userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
869 
870       DefaultListModel usrListModel = new DefaultListModel();
871       readUsers( usrListModel, userList);
872       if(OkCancelDialog.showDialog( UserGroupEditor.this,
873                                     new JScrollPane(userList),
874                                     "Choose a user to add")){
875         String userName = (String) userList.getSelectedValue();
876 
877         try {
878           Group group = controller.findGroup((String) model.get(index) );
879           User user = controller.findUser(userName);
880           group.addUser(user, session);
881 
882           //finally update the original lists
883           if (!usersFirst)
884             showUsersForGroup(group.getName());
885         } catch (gate.persist.PersistenceException ex) {
886           throw new gate.util.GateRuntimeException(ex.getMessage());
887         } catch (gate.security.SecurityException ex1) {
888           JOptionPane.showMessageDialog(UserGroupEditor.this,
889                                         ex1.getMessage(),
890                                         "Error adding user to group!",
891                                         JOptionPane.ERROR_MESSAGE
892                                        );
893 
894         }
895 
896       } //ok selected
897 
898     }//public void actionPerformed(ActionEvent e)
899   } //AddUserAction
900 
901 
902   protected class RemoveUserAction extends AbstractAction{
903     private JList source;
904 
905     public RemoveUserAction(JList source){
906       super("Remove user");
907       this.source = source;
908     }//
909 
910     public void actionPerformed(ActionEvent e){
911       int index = source.getSelectedIndex();
912       if (index == -1) //return if no selection
913         return;
914       DefaultListModel model = (DefaultListModel) source.getModel();
915       String groupName = (String) source.getSelectedValue();
916 
917       JList userList = new JList();
918       userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
919 
920       DefaultListModel usrListModel = new DefaultListModel();
921 
922       Group group = null;
923       try {
924         group = controller.findGroup(groupName);
925       } catch (gate.persist.PersistenceException ex) {
926         throw new gate.util.GateRuntimeException(
927                     "Cannot locate group: " + groupName
928                   );
929       } catch (gate.security.SecurityException ex1) {
930         throw new gate.util.GateRuntimeException(
931                     ex1.getMessage()
932                   );
933       }
934       if (group == null)
935         return;
936       java.util.List myUsers = group.getUsers();
937       if (myUsers == null)
938         return;
939 
940       for (int j = 0; j< myUsers.size(); j++) {
941         try {
942           User myUser = (User)myUsers.get(j);
943           usrListModel.addElement(myUser.getName());
944         } catch (Exception ex) {
945           throw new gate.util.GateRuntimeException(
946                   ex.getMessage()
947                 );
948         }//catch
949       }//for loop
950       userList.setModel(usrListModel);
951 
952       if(OkCancelDialog.showDialog(
953                           UserGroupEditor.this,
954                           new JScrollPane(userList),
955                           "Choose the user you want removed from this group")
956         ){
957 
958 
959         try {
960           User user = controller.findUser((String) userList.getSelectedValue());
961           group.removeUser(user, session);
962 
963           //finally update the original lists
964           if (!usersFirst)
965             showUsersForGroup(group.getName());
966           else
967             showGroupsForUser(user.getName());
968         } catch (gate.persist.PersistenceException ex) {
969           throw new gate.util.GateRuntimeException(ex.getMessage());
970         } catch (gate.security.SecurityException ex1) {
971           JOptionPane.showMessageDialog(UserGroupEditor.this,
972                                         ex1.getMessage(),
973                                         "Error removing user from group!",
974                                         JOptionPane.ERROR_MESSAGE
975                                        );
976 
977         }
978 
979       } //ok selected
980 
981     }//public void actionPerformed(ActionEvent e)
982   } //RemoveUserAction
983 
984 
985   protected class RenameGroupAction extends AbstractAction{
986     private JList source;
987 
988     public RenameGroupAction(JList source){
989       super("Rename group");
990       this.source = source;
991     }//
992 
993     public void actionPerformed(ActionEvent e){
994       int index = source.getSelectedIndex();
995       if (index == -1) //return if no selection
996         return;
997       DefaultListModel model = (DefaultListModel) source.getModel();
998 
999       String newName = JOptionPane.showInputDialog(
1000                                     UserGroupEditor.this,
1001                                    "Please enter the user's new name");
1002
1003      //don't change if nothing selected
1004      if (newName == null || newName.equals(""))
1005        return;
1006
1007      try {
1008        Group group = controller.findGroup((String) model.get(index) );
1009        group.setName(newName, session);
1010
1011        //finally update the original lists
1012        if (!usersFirst)
1013          showGroupsFirst();
1014        else
1015          showGroupsForUser((String) firstList.getSelectedValue());
1016      } catch (gate.persist.PersistenceException ex) {
1017        throw new gate.util.GateRuntimeException(ex.getMessage());
1018      } catch (gate.security.SecurityException ex1) {
1019        JOptionPane.showMessageDialog(UserGroupEditor.this,
1020                                      ex1.getMessage(),
1021                                      "Error renaming user!",
1022                                      JOptionPane.ERROR_MESSAGE
1023                                     );
1024
1025      }
1026    }//public void actionPerformed(ActionEvent e)
1027  } //RenameGroupAction
1028
1029} //UserGroupEditor
1030
1031