KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > client > tools > admin > UserTreeNode


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA.
18  *
19  * Initial developer(s): Alexander Fedorowicz
20  * Contributor(s):
21  */

22 package org.objectweb.joram.client.tools.admin;
23
24 import javax.swing.*;
25 import javax.swing.tree.*;
26
27 import org.objectweb.joram.client.jms.admin.User;
28
29
30 class UserTreeNode extends DefaultMutableTreeNode
31     implements AdminTreeNode
32 {
33   private AdminController c;
34   private User user;
35
36   private SubscriptionRootTreeNode subscriptionRoot;
37   
38   public UserTreeNode(AdminController c, User user) {
39     super(user.getName() + ':' +
40           user.getProxyId());
41     this.c = c;
42     this.user = user;
43     
44     subscriptionRoot = new SubscriptionRootTreeNode();
45     add(subscriptionRoot);
46   }
47
48   public void refresh(DefaultTreeModel treeModel)
49     {
50     }
51
52   public String JavaDoc getDescription()
53     {
54       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
55       sb.append("<font face=Arial><b>User: </b>");
56       sb.append(user);
57       sb.append("<br><br>");
58
59       sb.append("</font>");
60       return sb.toString();
61     }
62
63   public JPopupMenu getContextMenu()
64     {
65       JPopupMenu popup = new JPopupMenu("User");
66
67       ChangePasswordAction cpa = new ChangePasswordAction();
68       if (!c.isAdminConnected())
69         cpa.setEnabled(false);
70       popup.add(new JMenuItem(cpa));
71
72       DeleteUserAction dua = new DeleteUserAction();
73       if (!c.isAdminConnected())
74         dua.setEnabled(false);
75       popup.add(new JMenuItem(dua));
76
77       return popup;
78     }
79
80   public ImageIcon getImageIcon()
81     {
82       return AdminToolConstants.userIcon;
83     }
84
85   public User getUser() { return user; }
86   
87   // TODO: This is a hack because the User object doesn't provide the user name
88
public String JavaDoc getUserName() {
89     String JavaDoc name = null;
90
91     try {
92       name = (String JavaDoc) user.code().get("name");
93     }
94     catch (Exception JavaDoc exc) {}
95     
96     return name;
97   }
98
99   public ServerTreeNode getParentServerTreeNode() {
100     return (ServerTreeNode) getParent().getParent();
101   }
102
103   public boolean getAllowsChildren() {
104     return true;
105   }
106
107   private class DeleteUserAction extends AbstractAction
108   {
109     public DeleteUserAction()
110       {
111         super("Delete", AdminToolConstants.trashIcon);
112       }
113
114     public void actionPerformed(java.awt.event.ActionEvent JavaDoc e)
115       {
116         try
117           {
118             Object JavaDoc[] options = { "OK", "CANCEL" };
119             int conf = JOptionPane.showOptionDialog(AdminTool.getInstance(), "You are about to permanently delete this user. Please click OK to proceed.",
120                                                     "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
121
122             if (conf == 0)
123               c.deleteUser(UserTreeNode.this);
124           }
125         catch (Exception JavaDoc x) {
126           x.printStackTrace();
127           JOptionPane.showMessageDialog(null, x.getMessage());
128         }
129       }
130   }
131
132   private class ChangePasswordAction extends AbstractAction
133   {
134     public ChangePasswordAction()
135       {
136         super("Change Password...");
137       }
138
139     public void actionPerformed(java.awt.event.ActionEvent JavaDoc e)
140       {
141         try
142           {
143             while (true) {
144               CreateUserDialog cud = CreateUserDialog.showDialog(UserTreeNode.this.getUserName(), true);
145
146               if (cud.getActionCancelled()) {
147                 break;
148               }
149               else {
150                 String JavaDoc msg = "";
151
152                 if (cud.getPassword().length() == 0)
153                   msg = "You have to enter a new password for the user. Please click OK to make corrections.";
154                 else if (!cud.getPassword().equals(cud.getConfirmationPassword()))
155                   msg = "The two passwords that you have entered do not match. Please click OK to make corrections.";
156                 else {
157                   c.updateUser(UserTreeNode.this, cud.getUserName(), cud.getPassword());
158                   break;
159                 }
160
161                 Object JavaDoc[] options = { "OK" };
162                 JOptionPane.showOptionDialog(AdminTool.getInstance(), msg,
163                                              "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE, null, options, options[0]);
164               }
165             }
166           }
167         catch (Exception JavaDoc x) {
168           x.printStackTrace();
169           JOptionPane.showMessageDialog(null, x.getMessage());
170         }
171       }
172   }
173 }
174
Popular Tags