KickJava   Java API By Example, From Geeks To Geeks.

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


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 import javax.naming.*;
27
28
29 class JndiTreeNode extends DefaultMutableTreeNode
30     implements AdminTreeNode
31 {
32   private AdminController c;
33
34   private Context ctx = null;
35   
36   private String JavaDoc name = null;
37   private Object JavaDoc obj = null;
38   
39   public JndiTreeNode(AdminController c, Context ctx, String JavaDoc name) throws NamingException
40   {
41     super(name);
42     
43     this.c = c;
44     this.ctx = ctx;
45     this.name = name;
46     this.obj = ctx.lookup(name);
47   }
48
49   public JndiTreeNode(AdminController c, Context ctx, Binding binding)
50   {
51     super(binding.getName());
52     
53     this.c = c;
54     this.ctx = ctx;
55     name = binding.getName();
56     obj = binding.getObject();
57   }
58
59   public void refresh(DefaultTreeModel treeModel)
60   {
61   }
62
63   public String JavaDoc getDescription()
64   {
65     if (obj == null) {
66       try
67       {
68         obj = ctx.lookup(name);
69       }
70       catch (NamingException exc) {
71         obj = null;
72       }
73     }
74     
75     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
76     if (obj != null) {
77       sb.append("<font face=Arial><b>");
78       sb.append(getType());
79       sb.append("</b><br></font><hr><font face=Arial><br><b>Name:</b> ");
80       sb.append(name);
81       sb.append("<br><b>Class:</b> ");
82       sb.append(obj.getClass().getName());
83       sb.append("<br><br><b>Info:</b> ");
84       sb.append(getInfo());
85       sb.append("<br></font>");
86     }
87     else {
88       sb.append("<font face=Arial><b>Object lookup failed</b></font>");
89     }
90     return sb.toString();
91   }
92
93   public JPopupMenu getContextMenu()
94   {
95     JPopupMenu popup = new JPopupMenu("Binding");
96
97     DeleteObjectAction doa = new DeleteObjectAction();
98     if (!c.isJndiConnected() || !c.isAdminConnected())
99       doa.setEnabled(false);
100     popup.add(new JMenuItem(doa));
101     return popup;
102   }
103
104   public ImageIcon getImageIcon()
105   {
106     return AdminToolConstants.jndiIcon;
107   }
108   
109   public boolean getAllowsChildren() { return false; }
110   
111   public String JavaDoc getType() {
112     if (obj == null)
113       return null;
114     
115     String JavaDoc str = obj.toString();
116     int col = str.indexOf(':');
117     if (col == -1)
118       return "Unknown";
119
120     String JavaDoc t = str.substring(0, col);
121     
122     if (t.equals("CF"))
123       return "ConnectionFactory";
124     else if (t.equals("QCF"))
125       return "QueueConnectionFactory";
126     else if (t.equals("TCF"))
127       return "TopicConnectionFactory";
128       
129     return t;
130   }
131
132   public String JavaDoc getInfo() {
133     if (obj == null)
134       return null;
135     
136     String JavaDoc str = obj.toString();
137     int col = str.indexOf(':');
138     if (col == -1 || col == (str.length() - 1))
139       return str;
140
141     return str.substring(col + 1);
142   }
143
144   public String JavaDoc getName() { return name; }
145
146     public Object JavaDoc getObject() { return obj; }
147
148   private class DeleteObjectAction extends AbstractAction
149   {
150     public DeleteObjectAction()
151     {
152       super("Delete", AdminToolConstants.trashIcon);
153     }
154
155     public void actionPerformed(java.awt.event.ActionEvent JavaDoc e)
156     {
157       try
158       {
159         Object JavaDoc[] options = { "OK", "CANCEL" };
160         int conf = JOptionPane.showOptionDialog(AdminTool.getInstance(), "You are about to permanently delete this object. Please click OK to proceed.",
161           "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
162
163         if (conf == 0)
164           c.deleteObject(JndiTreeNode.this);
165       }
166       catch (Exception JavaDoc x) {
167         x.printStackTrace();
168         JOptionPane.showMessageDialog(null, x.getMessage());
169       }
170     }
171   }
172 }
173
Popular Tags