KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ubermq > jms > ui > admin > ServerTreeNode


1 package com.ubermq.jms.ui.admin;
2
3 import com.ubermq.jms.server.admin.*;
4 import java.rmi.*;
5 import java.util.*;
6 import javax.swing.*;
7 import javax.swing.tree.*;
8
9 class ServerTreeNode
10     extends DefaultMutableTreeNode
11     implements AdminTreeNode
12 {
13     private MessageServerAdmin admin;
14
15     private MyStatusNode serverStatusNode;
16     private MySubscriptionsNode subscriptionsNode;
17     private MyConnectionsNode connectionsNode;
18     private MyPersistentConsumersNode consumersNode;
19
20     private static final String JavaDoc SERVER_STATUS = "Server Status",
21         SUBSCRIPTION_TABLE = "Subscription Table",
22         CONNECTIONS = "Connections",
23         CONSUMERS = "Persistent Subscribers";
24
25     ServerTreeNode(MessageServerAdmin admin)
26         throws RemoteException
27     {
28         super(admin.getServerName(), true);
29         this.admin = admin;
30
31         // setup default subnodes.
32
this.serverStatusNode = new MyStatusNode();
33         this.subscriptionsNode = new MySubscriptionsNode();
34         this.connectionsNode = new MyConnectionsNode();
35         this.consumersNode = new MyPersistentConsumersNode();
36
37         // add em
38
add(serverStatusNode);
39         add(subscriptionsNode);
40         add(connectionsNode);
41         add(consumersNode);
42     }
43
44     MessageServerAdmin getAdmin()
45     {
46         return admin;
47     }
48
49     /**
50      * Refreshes the connection subnode from the admin.
51      */

52     public void refresh(DefaultTreeModel treeModel)
53         throws RemoteException
54     {
55         connectionsNode.refresh(treeModel);
56         serverStatusNode.refresh(treeModel);
57         consumersNode.refresh(treeModel);
58     }
59
60     public String JavaDoc getDescription()
61     {
62         try
63         {
64             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
65             sb.append("<h2>");
66             sb.append(admin.getServerName());
67             sb.append("</h2>");
68             return sb.toString();
69         }
70         catch (RemoteException e) {
71             return "Description unavailable.";
72         }
73     }
74
75     public JPopupMenu getContextMenu()
76     {
77         return null;
78     }
79
80     public ImageIcon getImageIcon()
81     {
82         return AdminViewer.serverIcon;
83     }
84
85     private class MyStatusNode
86         extends DefaultMutableTreeNode
87         implements AdminTreeNode
88     {
89         public MyStatusNode()
90         {
91             super(SERVER_STATUS, false);
92         }
93
94         public void refresh(DefaultTreeModel tm)
95         {
96         }
97
98         public String JavaDoc getDescription()
99         {
100             try
101             {
102                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
103                 sb.append("<i>Statistics</i>");
104                 sb.append("<pre>");
105                 sb.append(admin.getRouterStatistics().getStatisticsAsText());
106                 sb.append("</pre>");
107
108                 return sb.toString();
109             }
110             catch (RemoteException e) {
111                 return "Statistics Unavailable.";
112             }
113         }
114
115         public JPopupMenu getContextMenu()
116         {
117             return null;
118         }
119
120         public ImageIcon getImageIcon()
121         {
122             return AdminViewer.scriptIcon;
123         }
124     }
125
126     private class MyConnectionsNode
127         extends DefaultMutableTreeNode
128         implements AdminTreeNode
129     {
130         public MyConnectionsNode()
131         {
132             super(CONNECTIONS, true);
133         }
134
135         public void refresh(DefaultTreeModel treeModel)
136             throws RemoteException
137         {
138             // get a new set of connections
139
Collection c = admin.getConnections();
140
141             // build a map of the new tree by name.
142
Map newTree = new HashMap();
143             Iterator iter = c.iterator();
144             while (iter.hasNext())
145             {
146                 ConnectionAdmin ca = (ConnectionAdmin)iter.next();
147                 newTree.put(ca.getName(), ca);
148             }
149
150             // build a map of what we already have, indexed by name.
151
Map alreadyInTree = new HashMap();
152             iter = ((children != null) ? children : Collections.EMPTY_LIST).iterator();
153             while (iter.hasNext())
154             {
155                 ConnectionTreeNode ctn = (ConnectionTreeNode)iter.next();
156                 alreadyInTree.put(ctn.getAdmin().getName(), ctn);
157             }
158
159             // cross reference to get a set of deletions and additions
160
Set insertions = new HashSet(), deletions = new HashSet();
161             insertions.addAll(newTree.keySet());
162             insertions.removeAll(alreadyInTree.keySet());
163             deletions.addAll(alreadyInTree.keySet());
164             deletions.removeAll(newTree.keySet());
165
166             // do deletions...
167
iter = deletions.iterator();
168             while (iter.hasNext())
169             {
170                 ConnectionTreeNode ctn = (ConnectionTreeNode)alreadyInTree.get(iter.next());
171                 treeModel.removeNodeFromParent(ctn);
172             }
173
174             // do insertions
175
iter = insertions.iterator();
176             while (iter.hasNext())
177             {
178                 ConnectionAdmin ca = (ConnectionAdmin)newTree.get(iter.next());
179                 treeModel.insertNodeInto(new ConnectionTreeNode(ca), this, 0);
180             }
181         }
182
183         public String JavaDoc getDescription()
184         {
185             return "";
186         }
187
188         public JPopupMenu getContextMenu()
189         {
190             return null;
191         }
192
193         public ImageIcon getImageIcon()
194         {
195             return AdminViewer.serverIcon;
196         }
197     }
198
199     private class MySubscriptionsNode
200         extends DefaultMutableTreeNode
201         implements AdminTreeNode
202     {
203         MySubscriptionsNode()
204         {
205             super(SUBSCRIPTION_TABLE, false);
206         }
207
208         public String JavaDoc getDescription()
209         {
210             // build a topic -> conn list map.
211
try
212             {
213                 Map topicToConns = new HashMap();
214                 Iterator iter = admin.getConnections().iterator();
215                 while (iter.hasNext())
216                 {
217                     ConnectionAdmin admin = (ConnectionAdmin)iter.next();
218
219                     Iterator i2 = admin.getSubscriptions().iterator();
220                     while(i2.hasNext())
221                     {
222                         Object JavaDoc topic = (Object JavaDoc)i2.next();
223                         List l = (List)topicToConns.get(topic);
224                         if (l == null)
225                         {
226                             l = new ArrayList();
227                             topicToConns.put(topic, l);
228                         }
229                         l.add(admin.getName());
230                     }
231                 }
232
233                 // output it as html
234
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
235                 sb.append("<table border=0 width=\"80%\">");
236
237                 iter = topicToConns.keySet().iterator();
238                 if (!iter.hasNext())
239                     sb.append("<i>No Subscriptions</i>");
240                 while (iter.hasNext())
241                 {
242                     Object JavaDoc topic = iter.next();
243
244                     sb.append("<tr><td>");
245                     sb.append(topic);
246                     sb.append("</td><td>");
247
248                     List l = (List)topicToConns.get(topic);
249                     Iterator i2 = l.iterator();
250                     int i=0;
251                     while(i2.hasNext())
252                     {
253                         if (i++ > 0)
254                             sb.append("<br>");
255                         sb.append(i2.next());
256                     }
257                     sb.append("</td></tr>");
258                 }
259                 sb.append("</table>");
260
261                 return sb.toString();
262             }
263             catch (RemoteException e) {
264                 return "Unavailable.";
265             }
266         }
267
268         public void refresh(DefaultTreeModel treeModel)
269         {
270         }
271
272         public JPopupMenu getContextMenu()
273         {
274             return null;
275         }
276
277         public ImageIcon getImageIcon()
278         {
279             return AdminViewer.scriptIcon;
280         }
281     }
282
283     private class MyPersistentConsumersNode
284         extends DefaultMutableTreeNode
285         implements AdminTreeNode
286     {
287         MyPersistentConsumersNode()
288         {
289             super(CONSUMERS, true);
290         }
291
292         /**
293          * Gets the image icon for this node, or null to use
294          * the default.
295          */

296         public ImageIcon getImageIcon()
297         {
298             return AdminViewer.envelopeIcon;
299         }
300
301         /**
302          * Refreshes the node.
303          * @param treeModel the model that the node is contained in.
304          */

305         public void refresh(DefaultTreeModel treeModel) throws RemoteException
306         {
307             // get a new set of connections
308
Collection c = admin.getPersistentConsumers();
309
310             // build a map of the new tree by name.
311
Map newTree = new HashMap();
312             Iterator iter = c.iterator();
313             while (iter.hasNext())
314             {
315                 PersistentConsumerAdmin ca = (PersistentConsumerAdmin)iter.next();
316                 newTree.put(ca.getName(), ca);
317             }
318
319             // build a map of what we already have, indexed by name.
320
Map alreadyInTree = new HashMap();
321             iter = ((children != null) ? children : Collections.EMPTY_LIST).iterator();
322             while (iter.hasNext())
323             {
324                 MyPersistentConsumerNode ctn = (MyPersistentConsumerNode)iter.next();
325                 alreadyInTree.put(ctn.getAdmin().getName(), ctn);
326             }
327
328             // cross reference to get a set of deletions and additions
329
Set insertions = new HashSet(), deletions = new HashSet();
330             insertions.addAll(newTree.keySet());
331             insertions.removeAll(alreadyInTree.keySet());
332             deletions.addAll(alreadyInTree.keySet());
333             deletions.removeAll(newTree.keySet());
334
335             // do deletions...
336
iter = deletions.iterator();
337             while (iter.hasNext())
338             {
339                 MyPersistentConsumerNode ctn = (MyPersistentConsumerNode)alreadyInTree.get(iter.next());
340                 treeModel.removeNodeFromParent(ctn);
341             }
342
343             // do insertions
344
iter = insertions.iterator();
345             while (iter.hasNext())
346             {
347                 PersistentConsumerAdmin ca = (PersistentConsumerAdmin)newTree.get(iter.next());
348                 treeModel.insertNodeInto(new MyPersistentConsumerNode(ca), this, 0);
349             }
350         }
351
352         /**
353          * Returns a context menu for the node, or null if
354          * no context menu should be created.
355          */

356         public JPopupMenu getContextMenu()
357         {
358             return null;
359         }
360
361         /**
362          * Returns descriptive text about the node.
363          */

364         public String JavaDoc getDescription()
365         {
366             return "";
367         }
368     }
369
370     private class MyPersistentConsumerNode
371         extends DefaultMutableTreeNode
372         implements AdminTreeNode
373     {
374         private PersistentConsumerAdmin pc;
375         private String JavaDoc description;
376         private boolean goingAway;
377
378         private ImageIcon openIcon, closedIcon;
379
380         MyPersistentConsumerNode(PersistentConsumerAdmin pc)
381             throws RemoteException
382         {
383             super(pc.getName(), false);
384             this.pc = pc;
385             this.goingAway = false;
386
387             if (pc.isQueue())
388             {
389                 this.openIcon = AdminViewer.queueIcon;
390                 this.closedIcon = AdminViewer.queueIcon;
391             }
392             else
393             {
394                 this.openIcon = AdminViewer.scriptIcon;
395                 this.closedIcon = AdminViewer.scriptDeletedIcon;
396             }
397
398             // describe
399
if (pc.isQueue())
400                 this.description = "<b>Queue</b> " + pc.getName() + "<P>";
401             else
402                 this.description = "<b>Subscribed to: </b>" + pc.getSubscription() + "<P>";
403         }
404
405         public PersistentConsumerAdmin getAdmin()
406         {
407             return pc;
408         }
409
410         /**
411          * Gets the image icon for this node, or null to use
412          * the default.
413          */

414         public ImageIcon getImageIcon()
415         {
416             if (goingAway)
417                 return AdminViewer.trashIcon;
418
419             try
420             {
421                 return (pc.isActive() ? openIcon : closedIcon);
422             }
423             catch (RemoteException e) {
424                 return AdminViewer.deleteIcon;
425             }
426         }
427
428         /**
429          * Refreshes the node.
430          * @param treeModel the model that the node is contained in.
431          */

432         public void refresh(DefaultTreeModel treeModel) throws RemoteException
433         {
434         }
435
436         /**
437          * Returns a context menu for the node, or null if
438          * no context menu should be created.
439          */

440         public JPopupMenu getContextMenu()
441         {
442             JPopupMenu pm = new JPopupMenu();
443             pm.add(new CloseAction());
444             return pm;
445         }
446
447         /**
448          * Returns descriptive text about the node.
449          */

450         public String JavaDoc getDescription()
451         {
452             try
453             {
454                 return description + pc.getDescription();
455             }
456             catch (RemoteException e) {
457                 return "Unavailable.";
458             }
459         }
460
461
462         private class CloseAction
463             extends AbstractAction
464         {
465             public CloseAction() {super("Delete", AdminViewer.trashIcon);}
466             public boolean isEnabled()
467             {
468                 try
469                 {
470                     return !pc.isActive();
471                 }
472                 catch (RemoteException e) {
473                     return false;
474                 }
475             }
476             public void actionPerformed(java.awt.event.ActionEvent JavaDoc e)
477             {
478                 try
479                 {
480                     if (JOptionPane.showConfirmDialog(null,
481                                                       "Deleting this consumer will permanently delete all stored messages. Are you sure you want to continue?",
482                                                       "Delete",
483                                                       JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
484                     {
485                         pc.close();
486                         goingAway = true;
487                     }
488                 }
489                 catch (Exception JavaDoc x) {
490                     JOptionPane.showMessageDialog(null, x.getMessage());
491                 }
492             }
493         }
494
495     }
496
497 }
498
Popular Tags