001: /*
002: * JacORB - a free Java ORB
003: *
004: * Copyright (C) 1997-2004 Gerald Brose.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Library General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Library General Public License for more details.
015: *
016: * You should have received a copy of the GNU Library General Public
017: * License along with this library; if not, write to the Free
018: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
019: */
020:
021: package org.jacorb.naming.namemanager;
022:
023: import java.awt.*;
024: import javax.swing.tree.*;
025: import javax.swing.event.*;
026: import javax.swing.*;
027: import org.omg.CosNaming.*;
028: import org.omg.CosNaming.NamingContextPackage.*;
029: import org.jacorb.naming.*;
030:
031: /**
032: * @author Gerald Brose, FU Berlin/XTRADYNE Technologies AG
033: * @version $Id: NSTree.java,v 1.9 2004/05/06 12:39:59 nicolas Exp $
034: */
035:
036: public class NSTree extends JTree {
037: public static final int MAX_BIND = 40;
038: private NamingContextExt rootContext;
039: private ContextNode rootNode;
040: private Dimension size;
041: private boolean created;
042: private org.omg.CORBA.ORB orb;
043:
044: public static NSTable nsTable;
045:
046: public NSTree(int width, int height, NSTable theTable,
047: NamingContextExt rootCntxt, org.omg.CORBA.ORB orb) {
048: this .orb = orb;
049: DefaultMutableTreeNode root = new DefaultMutableTreeNode(
050: "RootContext");
051: root.setAllowsChildren(true);
052: setModel(new DefaultTreeModel(root, true));
053: created = false;
054: size = new Dimension(width, height);
055: nsTable = theTable;
056: rootContext = rootCntxt;
057: ContextNode cn = new ContextNode(rootContext,
058: (DefaultTreeModel) getModel());
059: cn.setNode(root);
060: root.setUserObject(cn);
061: }
062:
063: /**
064: * Bind a new name context and insert it
065: */
066:
067: public void bind(String name) throws NotFound, CannotProceed,
068: InvalidName, AlreadyBound {
069: TreePath path = null;
070: int length = 0;
071: try {
072: path = getSelectionPath();
073: length = path.getPathCount();
074: } catch (Exception e) {
075: JOptionPane.showMessageDialog(this , "Nothing selected",
076: "Selection error", JOptionPane.ERROR_MESSAGE);
077: return;
078: }
079:
080: DefaultMutableTreeNode node = (DefaultMutableTreeNode) getModel()
081: .getRoot();
082: NamingContextExt context = rootContext;
083:
084: if (length > 1) {
085: for (int i = 1; i < length; i++) {
086: node = (DefaultMutableTreeNode) path
087: .getPathComponent(i);
088: ContextNode bind = (ContextNode) node.getUserObject();
089: context = NamingContextExtHelper.narrow(context
090: .resolve(bind.getName()));
091: if (context == null) {
092: System.err.println("Naming context narrow failed!");
093: System.exit(1);
094: }
095: }
096: }
097: if (node.getAllowsChildren()) {
098: Name bindname = new Name(name);
099: if (context == null)
100: System.err.println("context null ");
101:
102: if (bindname.components() == null)
103: System.err.println("name is null ");
104:
105: context.bind_new_context(bindname.components());
106: update();
107: } else {
108: JOptionPane.showMessageDialog(this ,
109: "Please select a naming context",
110: "Selection error", JOptionPane.ERROR_MESSAGE);
111: }
112: }
113:
114: public void bindObject(String name, String ior, boolean isRebind)
115: throws NotFound, CannotProceed, InvalidName, AlreadyBound {
116: TreePath path = null;
117: int length = 0;
118: try {
119: path = getSelectionPath();
120: length = path.getPathCount();
121: } catch (Exception e) {
122: JOptionPane.showMessageDialog(this , "Nothing selected",
123: "Selection error", JOptionPane.ERROR_MESSAGE);
124: return;
125: }
126:
127: DefaultMutableTreeNode node = (DefaultMutableTreeNode) getModel()
128: .getRoot();
129: NamingContextExt context = rootContext;
130:
131: if (length > 1) {
132: for (int i = 1; i < length; i++) {
133: node = (DefaultMutableTreeNode) path
134: .getPathComponent(i);
135: ContextNode bind = (ContextNode) node.getUserObject();
136: context = NamingContextExtHelper.narrow(context
137: .resolve(bind.getName()));
138: if (context == null) {
139: System.err.println("Naming context narrow failed!");
140: System.exit(1);
141: }
142: }
143: }
144: if (node.getAllowsChildren()) {
145: Name bindname = new Name(name);
146: if (context == null)
147: System.err.println("context null ");
148:
149: if (bindname.components() == null)
150: System.err.println("name is null ");
151:
152: try {
153: context.bind(bindname.components(), orb
154: .string_to_object(ior));
155: } catch (AlreadyBound ab) {
156: if (isRebind)
157: context.rebind(bindname.components(), orb
158: .string_to_object(ior));
159: else
160: throw ab;
161: }
162: update();
163: } else {
164: JOptionPane.showMessageDialog(this ,
165: "Please select a naming context",
166: "Selection error", JOptionPane.ERROR_MESSAGE);
167: }
168: }
169:
170: public Dimension getPreferredSize() {
171: if (!created) {
172: created = true;
173: return size;
174: } else
175: return super .getPreferredSize();
176: }
177:
178: /**
179: * unbind a context and remove it from this tree
180: */
181:
182: public void unbind() {
183: DefaultMutableTreeNode node;
184: NamingContextExt context = rootContext;
185: TreePath path = null;
186: int length = 0;
187: try {
188: path = getSelectionPath();
189: length = path.getPathCount();
190: if (length > 1) {
191: for (int i = 1; i < length - 1; i++) {
192: node = (DefaultMutableTreeNode) path
193: .getPathComponent(i);
194: ContextNode bind = (ContextNode) node
195: .getUserObject();
196: context = NamingContextExtHelper.narrow(context
197: .resolve(bind.getName()));
198: }
199: }
200:
201: if (length > 0) {
202: node = (DefaultMutableTreeNode) path
203: .getPathComponent(length - 1);
204: ContextNode binding = (ContextNode) node
205: .getUserObject();
206: context.unbind(binding.getName());
207: DefaultTreeModel model = (DefaultTreeModel) getModel();
208: model.removeNodeFromParent(node);
209:
210: // select the parent node and display its content
211: DefaultMutableTreeNode parent = (DefaultMutableTreeNode) path
212: .getPathComponent(length - 2);
213: setSelectionPath(new TreePath(parent.getPath()));
214: ((ContextNode) parent.getUserObject()).display();
215: }
216: } catch (Exception e) {
217: e.printStackTrace();
218:
219: JOptionPane.showMessageDialog(this ,
220: "Nothing selected or invalid selection",
221: "Selection error", JOptionPane.ERROR_MESSAGE);
222: }
223: }
224:
225: /**
226: * update the entire tree of contexts
227: */
228:
229: public synchronized void update() {
230: DefaultTreeModel model = (DefaultTreeModel) getModel();
231: ((ContextNode) ((DefaultMutableTreeNode) model.getRoot())
232: .getUserObject()).update();
233: nsTable.update();
234: }
235: }
|