KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > InOut > Impl > SOFANode


1 /**
2  * SOFANode.java is a part of the SOFA project.
3  * This file was created by pepan on 18.4.2003.
4  */

5 package SOFA.SOFAnode.InOut.Impl;
6
7 import javax.swing.JTextArea JavaDoc;
8 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
9
10 import SOFA.Connector.ConnectorException;
11 import SOFA.Connector.ConnectorTransportException;
12 import SOFA.Connector.Reference;
13 import SOFA.SOFAnode.InOut.Bundle;
14 import SOFA.SOFAnode.InOut.InOut2Client;
15 import SOFA.SOFAnode.InOut.Connector.InOut2ClientConnector;
16 import SOFA.SOFAnode.TR.ComponentInfo;
17
18 /**
19  * Contains necessary information about one node in the SOFA network. Each SOFA node
20  * creates a set of such objects for every other SOFA node (known to the local node) in the network.
21  * @author Petr Panuska
22  */

23 class SOFANode {
24   /**
25    * Name of the SOFA node as it appears in a client.
26    */

27   private String JavaDoc name = null;
28
29   /**
30    * The SOFA reference to the SOFA node.
31    */

32   private Reference reference = null;
33
34   /**
35    * The SOFA connector to the SOFA node. If <code>null</code>, there is no connection made
36    * from the local SOFA node to this SOFA node.
37    */

38   private InOut2Client inOut = null;
39
40   /**
41    * An array of components stored on this SOFA node.
42    */

43   private ComponentInfo[] components = null;
44
45   /**
46    * A text area where messages about connecting, etc. are written to.
47    */

48   private JTextArea JavaDoc messages = null;
49
50   /**
51    * Creates an instance of this SOFA node.
52    * @param name The name of this SOFA node.
53    * @param ref The string representation of the {@link #reference} to this SOFA node.
54    * @param messages The text area where messages are written to.
55    */

56   SOFANode (String JavaDoc name, String JavaDoc ref, JTextArea JavaDoc messages) {
57     this.name = name;
58     reference = Reference.fromString(ref);
59     this.messages = messages;
60   }
61
62   /**
63    * Tries to connect the local SOFA node to this SOFA node. The {@link #inOut} field is
64    * set according to the result. If the connecting fails, the field is set to <code>null</code>.
65    * It also writes a message about the result to the {@link #messages} field.
66    */

67   private void _connect () {
68     try {
69       inOut = (InOut2Client) InOut2ClientConnector.createClt(reference);
70       messages.append("A connection to '" + name + "' has been made successfully.\n");
71     } catch (ConnectorException e) {
72       messages.append("When connecting to '" + name + "', the following exception was thrown:\n" + e + "\n");
73       inOut = null;
74       components = null;
75     }
76   }
77
78   /**
79    * Connects the local SOFA node to this one. It also adds an instance of a
80    * <code>TreeNode</code> to the tree.
81    * @param root The parent node the new tree node is added to.
82    * @return The newly created tree node.
83    */

84   DefaultMutableTreeNode JavaDoc connect (DefaultMutableTreeNode JavaDoc root) {
85     _connect();
86     DefaultMutableTreeNode JavaDoc tNode = new DefaultMutableTreeNode JavaDoc(this, inOut != null); // allows children if connected
87
root.add(tNode);
88     return tNode;
89   }
90
91   /**
92    * Reconnects the local SOFA node to this one. It also changes an instance of a
93    * <code>TreeNode</code> according to the result of connecting.
94    * @param tNode The tree node which is being changed.
95    * @return The changed tree node.
96    */

97   DefaultMutableTreeNode JavaDoc reconnect (DefaultMutableTreeNode JavaDoc tNode) {
98     _connect();
99     tNode = new DefaultMutableTreeNode JavaDoc(this, inOut != null); // allows children if connected
100
// tNode = new DefaultMutableTreeNode(this, true); // allows always children (even if not connected now)
101
return tNode;
102   }
103
104   /**
105    * Lists components stored on this SOFA node. It also removes all previous child nodes
106    * (= installed components) and adds the new ones to the specified parent tree node.
107    * <br>If the local SOFA node is not connected to this one, it just removes the child tree nodes
108    * (but it doesn't list anything).
109    * @param tNode The parent tree node where the listed components are stored to.
110    */

111   void list (DefaultMutableTreeNode JavaDoc tNode) {
112     tNode.removeAllChildren();
113     if (isConnected()) {
114       Bundle bundle;
115       try {
116         bundle = inOut.list();
117       } catch (ConnectorTransportException e) {
118         messages.append("When listing components on the SOFA node '" + name + "', the following exception was thrown:\n" + e + "\n");
119         inOut = null;
120         return;
121       }
122       components = bundle.getComponents();
123       if (components.length > 0) {
124         tNode.setAllowsChildren(true);
125         for (int i = 0; i < components.length; i++) {
126           ComponentInfo info = components[i];
127           tNode.add(new DefaultMutableTreeNode JavaDoc(info));
128         }
129       }
130     }
131   }
132
133   /**
134    * Returns <code>true</code> when the local SOFA node is connected to this one.
135    * @return <code>false</code> when the local node is not connected to this one.
136    */

137   boolean isConnected () {
138     return inOut != null;
139   }
140
141   /**
142    * Returns the name of this SOFA node. In case that the local node is not connected to
143    * this one, a string ' - not accessible' is added to the name.
144    * @return the name of this SOFA node as it was stated in the configuration file.
145    */

146   public String JavaDoc toString () {
147     return name + ((isConnected()) ? "" : " - not accessible");
148   }
149
150   /**
151    * Gets the name of this SOFA node.
152    * @return the SOFA node name.
153    */

154   String JavaDoc getName () {
155     return name;
156   }
157
158   /**
159    * Gets the reference to this SOFA node.
160    * @return the SOFA node reference.
161    */

162   Reference getReference () {
163     return reference;
164   }
165
166   /**
167    * Gets the connector from the local SOFA node to this one.
168    * @return the SOFA connector.
169    */

170   InOut2Client getInOut () {
171     return inOut;
172   }
173
174   /**
175    * Gets an array of components stored on this SOFA node.
176    * @return an array of stored components.
177    */

178   ComponentInfo[] getComponents () {
179     return components;
180   }
181
182 }
183
Popular Tags