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