View Javadoc

1   /*
2    * Copyright (c) 2005 The University of Reading
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    * 1. Redistributions of source code must retain the above copyright
9    *    notice, this list of conditions and the following disclaimer.
10   * 2. Redistributions in binary form must reproduce the above copyright
11   *    notice, this list of conditions and the following disclaimer in the
12   *    documentation and/or other materials provided with the distribution.
13   * 3. Neither the name of the University of Reading, nor the names of the
14   *    authors or contributors may be used to endorse or promote products
15   *    derived from this software without specific prior written permission.
16   * 
17   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20   * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27   */
28  
29  package uk.ac.rdg.resc.jstyx.gridservice.client;
30  
31  import java.util.Vector;
32  
33  import javax.swing.tree.DefaultTreeModel;
34  import javax.swing.tree.DefaultMutableTreeNode;
35  
36  import javax.swing.event.TreeExpansionEvent;
37  import javax.swing.event.TreeExpansionListener;
38  
39  import uk.ac.rdg.resc.jstyx.client.StyxConnection;
40  import uk.ac.rdg.resc.jstyx.client.CStyxFileChangeListener;
41  import uk.ac.rdg.resc.jstyx.client.CStyxFile;
42  
43  
44  /***
45   * Data model for tree view of SGS servers and services in the SGSExplorer.
46   * Only allows drilling down to the level of service instances.  Hierarchy is
47   * server -> service -> instance.
48   *
49   * @author Jon Blower
50   * $Revision: 310 $
51   * $Date: 2005-07-06 18:53:44 +0100 (Wed, 06 Jul 2005) $
52   * $Log$
53   * Revision 1.6  2005/07/06 17:53:44  jonblower
54   * Implementing automatic update of SGS instances in SGS Explorer
55   *
56   * Revision 1.5  2005/05/17 18:20:50  jonblower
57   * Separated CStyxFileNode from SGSExplorerTreeModel
58   *
59   * Revision 1.4  2005/05/17 15:51:43  jonblower
60   * Correct operation of display of tree of SGS servers, services and instances
61   *
62   * Revision 1.3  2005/05/17 07:52:23  jonblower
63   * Further developments
64   *
65   * Revision 1.2  2005/05/16 16:49:22  jonblower
66   * Updated to use CStyxFileNode as a general node in the model
67   *
68   * Revision 1.1  2005/05/16 16:17:47  jonblower
69   * Initial import
70   *
71   */
72  public class SGSExplorerTreeModel extends DefaultTreeModel implements TreeExpansionListener
73  {
74      
75      /*** Creates a new instance of SGSExplorerTreeModel */
76      public SGSExplorerTreeModel()
77      {
78          super(new DefaultMutableTreeNode("/", true), true);
79      }
80      
81      /***
82       * Adds a new connection to a server to this tree model
83       */
84      public void addNewConnection(StyxConnection conn)
85      {
86          // Generate a String describing this connection briefly
87          String str = conn.getRemoteHost() + ":" + conn.getRemotePort();
88          String str2 = str;
89          
90          // Check that a connection with this name doesn't already exist
91          int j = 1;
92          boolean exists;
93          synchronized (this.root)
94          {
95              do
96              {
97                  exists = false;
98                  for (int i = 0; i < this.root.getChildCount(); i++)
99                  {
100                     CStyxFileNode node =
101                         (CStyxFileNode)this.root.getChildAt(i);
102                     if (str2.equals(node.toString()))
103                     {
104                         exists = true;
105                         break; // Stop searching through nodes
106                     }
107                 }
108                 if (exists)
109                 {
110                     // If a node with this name already exists, we must create
111                     // a new string to identify this node
112                     str2 = str + " [" + j + "]";
113                     j++;
114                 }
115             } while (exists);
116             
117             // Add a node representing the root of this connection
118             int insertLocation = this.root.getChildCount();
119             DefaultMutableTreeNode newNode =
120                 new CStyxFileNode(this, conn.getRootDirectory(), str2);
121             // The insertNodeInto() method fires the necessary event
122             this.insertNodeInto(newNode, (DefaultMutableTreeNode)this.root,
123                 insertLocation);
124         }
125     }
126     
127     /***
128      * Required by TreeExpansionListener. Causes the node's children to be
129      * loaded.
130      */
131     public void treeExpanded(TreeExpansionEvent event)
132     {
133         Object source = event.getPath().getLastPathComponent();
134         if (source instanceof CStyxFileNode)
135         {
136             CStyxFileNode node = (CStyxFileNode)source;
137             // Send message to find the children of this node.  When the reply
138             // arrives, the GUI will be updated
139             node.findChildren();
140         }
141     }
142     
143     /***
144      * Required by TreeExpansionListener. Does nothing here.
145      */
146     public void treeCollapsed(TreeExpansionEvent event) {}
147     
148 }