View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  
23  
24  package zeus.agentviewer.engine;
25  
26  import javax.swing.table.*;
27  import javax.swing.*;
28  import java.util.*;
29  import zeus.util.OrderedHashtable;
30  import zeus.actors.rtn.Node;
31  import zeus.actors.event.*;
32  import zeus.actors.*;
33  import zeus.actors.rtn.Engine;
34  import zeus.gui.graph.*;
35  import zeus.util.Core;
36  import zeus.util.Misc;
37  
38  
39  public class EngineTableModel  extends AbstractTableModel implements NodeMonitor {
40        private String[] header = { "Graphs" };
41        private OrderedHashtable   data ;
42        private GraphsModel graphsModel;
43        private Engine engine;
44        
45        private int      BUFFER_CAPACITY = 1000;
46        private int      REMOVE_INDEX = 0;
47  //--------------------------------------------------------------------------
48        public EngineTableModel(AgentContext context){
49            data = new OrderedHashtable();
50            graphsModel = new GraphsModel();
51            engine = context.Engine();
52            engine.addNodeMonitor(this, NodeEvent.CREATE_MASK |
53  	     NodeEvent.DISPOSE_MASK | NodeEvent.STATE_CHANGE_MASK);
54        }
55  //--------------------------------------------------------------------------
56         public int getRowCount() {
57             return data.size();
58         }
59  //--------------------------------------------------------------------------
60         public int getColumnCount() {
61             return 1;
62         }
63  //--------------------------------------------------------------------------
64         public Object getValueAt(int row, int col) {
65          try { 
66              return (String) data.getKeyAt(row);
67          } catch (Exception e) { 
68              return ("removed");  // this occurs when a graph has been sweeped off the 
69              // table and the table length has not been updated yet - catch it and 
70              // put it away
71          }
72          }
73  //--------------------------------------------------------------------------
74         public String getColumnName(int col) {
75              return  header[col];
76         }
77  //--------------------------------------------------------------------------
78  
79         EngineGraphModel markedForDeletion = null;
80         
81         protected void markForDeletion(EngineGraphModel eRoot) {
82            if ( markedForDeletion != null ) {
83               Node root_node = (Node)(((markedForDeletion).getRoot()).getUserObject());
84               String name = root_node.getDescription();
85               data.remove(name);
86               fireTableDataChanged();
87               markedForDeletion.deleteTree(graphsModel);
88            }
89            markedForDeletion = eRoot;
90         }
91  
92  
93         public synchronized void nodeCreatedEvent(NodeEvent event){
94             Core.DEBUG(1,"EngineTableModel addNode(): " + event.getNode().getDescription());
95            if (event.getParents() == null) {
96                Core.DEBUG(1,"\tNode has no parents");
97                EngineGraphModel newRoot = new EngineGraphModel(new GraphNode(event.getNode()));
98                try {
99                      while (data.size() > BUFFER_CAPACITY ) {
100                         EngineGraphModel eRoot = (EngineGraphModel)data.get(data.getKeyAt(REMOVE_INDEX)); 
101                         markForDeletion(eRoot);
102                        Thread.yield(); 
103 	            }
104 	          } catch (Exception e) { 
105 	                e.printStackTrace(); 
106 	          }
107 
108                // data.removeElementAt(REMOVE_INDEX); 
109 	          data.put(event.getNodeName(),newRoot);
110               graphsModel.addRoot(event.getNodeName(),newRoot.getRoot());
111               fireTableDataChanged();
112               Thread.yield(); 
113           }
114           else {
115              Core.DEBUG(1,"\tNode has parents: " + Misc.concat(event.getParentNames()));
116              GraphNode gNode = graphsModel.addToGraph(event);
117              EngineGraphModel eRoot = updateGraph(event.getNodeName());
118              if (eRoot != null && gNode != null) {
119                 eRoot.refresh(EngineGraphModel.ADD,gNode);
120              }
121              else
122              {
123                 debug ("eRoot or gNode null"); 
124                 Core.DEBUG(1,"eRoot or gNode is null");}
125           }
126            Thread.yield();
127        }
128 //--------------------------------------------------------------------------
129        private EngineGraphModel updateGraph(String name){
130         try {
131           GraphNode aRoot = graphsModel.getRoot(name);
132           Node node = (Node)aRoot.getUserObject();
133           EngineGraphModel eRoot = (EngineGraphModel)data.get(node.getDescription());
134           return eRoot;}
135           catch (Exception e) { // added 30/9/01
136             return (null); 
137           }
138 
139        }
140 //--------------------------------------------------------------------------
141        public  void nodeStateChangedEvent(NodeEvent event){
142            GraphNode gNode = graphsModel.changeNode(event);
143 //           fireTableDataChanged();
144            EngineGraphModel eRoot = updateGraph(event.getNodeName());
145 
146           if (eRoot != null && gNode != null) {
147              eRoot.refresh(EngineGraphModel.CHANGE,gNode);
148 /*
149              Node last_node = (Node)gNode.getUserObject();
150              if ( last_node.getState() == Node.DONE ||
151 	          last_node.getState() == Node.FAILED ) {
152                 Node root_node = (Node)(((eRoot).getRoot()).getUserObject());
153                 if ( root_node.getGraph() == last_node.getGraph() )
154                    markForDeletion(eRoot);
155              }
156 */
157           }
158        }
159 //--------------------------------------------------------------------------
160        public void removeAll(){
161          data.clear();
162          fireTableDataChanged();
163        }
164 //--------------------------------------------------------------------------
165        public void removeGraph(EngineGraphModel root){
166           data.remove(root);
167           fireTableDataChanged();
168        }
169 //--------------------------------------------------------------------------
170        public EngineGraphModel getGraph(int row){
171           String key = (String) data.getKeyAt(row);
172           return  (EngineGraphModel) data.get(key);
173        }
174 
175 //--------------------------------------------------------------------------
176        public  void nodeDisposedEvent(NodeEvent event){
177        }
178 //--------------------------------------------------------------------------
179        public void removeZeusEventMonitors(){
180          engine.removeNodeMonitor(this, NodeEvent.CREATE_MASK |
181 	    NodeEvent.DISPOSE_MASK | NodeEvent.STATE_CHANGE_MASK);
182        }
183        
184        private void debug (String str) { 
185             System.out.println("EngineTableModel>>" + str); 
186        }
187 }