KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > naming > namemanager > NSTree


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1997-2004 Gerald Brose.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */

20
21 package org.jacorb.naming.namemanager;
22
23
24 import java.awt.*;
25 import javax.swing.tree.*;
26 import javax.swing.event.*;
27 import javax.swing.*;
28 import org.omg.CosNaming.*;
29 import org.omg.CosNaming.NamingContextPackage.*;
30 import org.jacorb.naming.*;
31
32 /**
33  * @author Gerald Brose, FU Berlin/XTRADYNE Technologies AG
34  * @version $Id: NSTree.java,v 1.9 2004/05/06 12:39:59 nicolas Exp $
35  */

36
37 public class NSTree
38     extends JTree
39 {
40     public static final int MAX_BIND = 40;
41     private NamingContextExt rootContext;
42     private ContextNode rootNode;
43     private Dimension size;
44     private boolean created;
45     private org.omg.CORBA.ORB JavaDoc orb;
46
47     public static NSTable nsTable;
48
49     public NSTree(int width,
50                   int height,
51                   NSTable theTable,
52                   NamingContextExt rootCntxt,
53                   org.omg.CORBA.ORB JavaDoc orb)
54     {
55         this.orb = orb;
56     DefaultMutableTreeNode root = new DefaultMutableTreeNode("RootContext");
57     root.setAllowsChildren(true);
58     setModel(new DefaultTreeModel(root,true));
59     created = false;
60     size = new Dimension(width,height);
61     nsTable = theTable;
62     rootContext = rootCntxt;
63     ContextNode cn = new ContextNode(rootContext,(DefaultTreeModel)getModel());
64     cn.setNode(root);
65     root.setUserObject(cn);
66     }
67
68     /**
69      * Bind a new name context and insert it
70      */

71
72     public void bind(String JavaDoc name)
73     throws NotFound, CannotProceed, InvalidName, AlreadyBound
74     {
75     TreePath path = null;
76     int length = 0;
77     try
78     {
79         path = getSelectionPath();
80         length = path.getPathCount();
81     }
82     catch (Exception JavaDoc e)
83     {
84         JOptionPane.showMessageDialog(this,"Nothing selected",
85                       "Selection error",JOptionPane.ERROR_MESSAGE);
86         return;
87     }
88
89     DefaultMutableTreeNode node = (DefaultMutableTreeNode) getModel().getRoot();
90     NamingContextExt context = rootContext;
91
92     if (length > 1)
93     {
94         for (int i = 1; i < length; i++)
95         {
96         node = (DefaultMutableTreeNode)path.getPathComponent(i);
97         ContextNode bind = (ContextNode)node.getUserObject();
98         context = NamingContextExtHelper.narrow(context.resolve(bind.getName()));
99         if( context == null )
100         {
101             System.err.println("Naming context narrow failed!");
102             System.exit(1);
103         }
104         }
105     }
106     if (node.getAllowsChildren())
107     {
108         Name bindname = new Name(name);
109         if( context == null )
110         System.err.println("context null ");
111
112         if( bindname.components() == null )
113         System.err.println("name is null ");
114
115         context.bind_new_context(bindname.components());
116         update();
117     }
118     else
119     {
120         JOptionPane.showMessageDialog(this,
121                                           "Please select a naming context",
122                       "Selection error", JOptionPane.ERROR_MESSAGE);
123     }
124     }
125
126
127     public void bindObject( String JavaDoc name, String JavaDoc ior, boolean isRebind)
128     throws NotFound,CannotProceed,InvalidName, AlreadyBound
129     {
130     TreePath path = null;
131     int length = 0;
132     try
133     {
134         path = getSelectionPath();
135         length = path.getPathCount();
136     }
137     catch (Exception JavaDoc e)
138     {
139         JOptionPane.showMessageDialog(this,"Nothing selected",
140                       "Selection error",JOptionPane.ERROR_MESSAGE);
141         return;
142     }
143
144     DefaultMutableTreeNode node = (DefaultMutableTreeNode) getModel().getRoot();
145     NamingContextExt context = rootContext;
146
147     if (length>1)
148     {
149         for (int i = 1;i<length;i++)
150         {
151         node = (DefaultMutableTreeNode) path.getPathComponent(i);
152         ContextNode bind = (ContextNode) node.getUserObject();
153         context = NamingContextExtHelper.narrow( context.resolve( bind.getName()));
154         if( context == null )
155         {
156             System.err.println("Naming context narrow failed!");
157             System.exit(1);
158         }
159         }
160     }
161     if (node.getAllowsChildren())
162     {
163         Name bindname = new Name(name);
164         if( context == null )
165         System.err.println("context null ");
166
167         if( bindname.components() == null )
168         System.err.println("name is null ");
169                                                    
170             try
171             {
172                 context.bind( bindname.components(), orb.string_to_object( ior ));
173             }
174             catch( AlreadyBound ab )
175             {
176                 if (isRebind)
177                     context.rebind( bindname.components(), orb.string_to_object( ior ));
178                 else
179                     throw ab;
180             }
181         update();
182     }
183     else
184     {
185         JOptionPane.showMessageDialog(this,
186                                           "Please select a naming context",
187                       "Selection error", JOptionPane.ERROR_MESSAGE);
188     }
189     }
190
191
192
193     public Dimension getPreferredSize()
194     {
195     if (!created)
196     {
197         created = true;
198         return size;
199     }
200     else
201         return super.getPreferredSize();
202     }
203
204     /**
205      * unbind a context and remove it from this tree
206      */

207     
208     public void unbind()
209     {
210     DefaultMutableTreeNode node;
211     NamingContextExt context = rootContext;
212     TreePath path = null;
213     int length = 0;
214     try
215     {
216         path = getSelectionPath();
217         length = path.getPathCount();
218         if (length > 1)
219         {
220         for (int i = 1; i < length-1; i++)
221         {
222             node = (DefaultMutableTreeNode)path.getPathComponent(i);
223             ContextNode bind = (ContextNode)node.getUserObject();
224             context = NamingContextExtHelper.narrow(context.resolve(bind.getName()));
225         }
226         }
227
228         if (length > 0)
229         {
230         node = (DefaultMutableTreeNode)path.getPathComponent(length-1);
231         ContextNode binding = (ContextNode)node.getUserObject();
232         context.unbind(binding.getName());
233         DefaultTreeModel model = (DefaultTreeModel)getModel();
234         model.removeNodeFromParent(node);
235                 
236                 // select the parent node and display its content
237
DefaultMutableTreeNode parent = (DefaultMutableTreeNode)path.getPathComponent(length-2);
238                 setSelectionPath(new TreePath(parent.getPath()));
239         ((ContextNode)parent.getUserObject()).display();
240         }
241     }
242     catch (Exception JavaDoc e)
243     {
244             e.printStackTrace();
245
246         JOptionPane.showMessageDialog(this,
247                       "Nothing selected or invalid selection",
248                       "Selection error",
249                       JOptionPane.ERROR_MESSAGE);
250     }
251     }
252
253
254     /**
255      * update the entire tree of contexts
256      */

257      
258     public synchronized void update()
259     {
260     DefaultTreeModel model = (DefaultTreeModel)getModel();
261     ((ContextNode)((DefaultMutableTreeNode)model.getRoot()).getUserObject()).update();
262     nsTable.update();
263     }
264 }
265
266
267
Popular Tags