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  /******************************************************************************
25  * ProtocolModel.java
26  *
27  * The underlying model for the Coordination Protocol Table
28  *****************************************************************************/
29  
30  package zeus.generator.agent;
31  
32  import java.util.*;
33  import java.awt.event.*;
34  import javax.swing.table.*;
35  import javax.swing.event.*;
36  
37  import zeus.util.*;
38  import zeus.concepts.*;
39  
40  public class ProtocolModel extends AbstractTableModel {
41  
42    static final int TYPE     = 0;
43    static final int PROTOCOL = 1;
44    static final int STATE    = 2;
45  
46    protected String[]          columnNames = { "Type",  "Protocol", "State" };
47    protected Object[][]        data;  
48    protected EventListenerList changeListeners = new EventListenerList();
49    protected Hashtable         store = new Hashtable();
50    protected StrategyModel     strategyModel = null;
51    protected OntologyDb        ontologyDb;
52    protected int               selectedRow = -1;
53  
54    public ProtocolModel(OntologyDb ontologyDb,
55                         StrategyModel strategyModel,
56                         ProtocolInfo[] protocols) {
57  
58       this.strategyModel = strategyModel;
59       this.ontologyDb = ontologyDb;
60  
61       String sep = SystemProps.getProperty("file.separator");
62       String system1 = SystemProps.getProperty("system.protocols.initiator");
63       String system2 = SystemProps.getProperty("system.protocols.respondent");
64       String user1 = SystemProps.getProperty("user.protocols.initiator");
65       String user2 = SystemProps.getProperty("user.protocols.respondent");
66  
67       StringTokenizer s1 = null, s2 = null, u1 = null, u2 = null;
68       int count = 0;
69  
70       if ( system1 != null ) {
71          s1 = new StringTokenizer(system1,sep);
72          count += s1.countTokens();
73       }
74       if ( system2 != null ) {
75          s2 = new StringTokenizer(system2,sep);
76          count += s2.countTokens();
77       }
78       if ( user1 != null ) {
79          u1 = new StringTokenizer(user1,sep);
80          count += u1.countTokens();
81       }
82       if ( user2 != null ) {
83          u2 = new StringTokenizer(user2,sep);
84          count += u2.countTokens();
85       }
86  
87       data = new Object[count][3];
88       int i = 0;
89       if ( system1 != null ) {
90          while( s1.hasMoreTokens() ) {
91             data[i][TYPE] = ProtocolInfo.INITIATOR;
92             data[i][PROTOCOL] = s1.nextToken();
93             data[i][STATE] = Boolean.FALSE;           
94             i++;
95          }        
96       }
97       if ( system2 != null ) {
98          while( s2.hasMoreTokens() ) {
99             data[i][TYPE] = ProtocolInfo.RESPONDENT;
100            data[i][PROTOCOL] = s2.nextToken();
101            data[i][STATE] = Boolean.FALSE;
102            i++;
103         }        
104      }
105      if ( user1 != null ) {
106         while( u1.hasMoreTokens() ) {
107            data[i][TYPE] = ProtocolInfo.INITIATOR;
108            data[i][PROTOCOL] = u1.nextToken();
109            data[i][STATE] = Boolean.FALSE;
110            i++;
111         }        
112      }
113      if ( user2 != null ) {
114         while( u2.hasMoreTokens() ) {
115            data[i][TYPE] = ProtocolInfo.RESPONDENT;
116            data[i][PROTOCOL] = u2.nextToken();
117            data[i][STATE] = Boolean.FALSE;
118            i++;
119         }        
120      }
121 
122      for(int j = 0; j < data.length; j++ ) {
123         for(int k = 0; k < protocols.length; k++ ) {
124            if ( data[j][PROTOCOL].equals(protocols[k].getName()) ) {
125               data[j][STATE] = Boolean.TRUE;
126               store.put(protocols[k].getName(),protocols[k]);
127               break;
128            }
129         }
130      }
131   }                     
132   
133   public int     getColumnCount()                 { return columnNames.length; }
134   public int     getRowCount()                    { return data.length; }
135   public boolean isCellEditable(int row, int col) { return col == STATE; }
136   public String  getColumnName(int col)           { return columnNames[col]; }
137   public Object  getValueAt(int row, int col)     { return data[row][col]; }
138 
139   public void setValueAt(Object aValue, int row, int column) {
140     // prevents the table being accidently loaded with a null value
141     // current table implementation needs this - possibly because of a bug
142     if (aValue == null || aValue.toString().equals(""))
143        return;
144 
145     switch(column) {
146        case STATE:
147             if ( data[row][column].equals(aValue) ) return;
148             data[row][column] = (Boolean)aValue;
149             if ( aValue.equals(Boolean.TRUE) )
150                store.put(data[row][PROTOCOL],
151                   new ProtocolInfo((String)data[row][PROTOCOL],
152                      (String)data[row][TYPE],
153                      ontologyDb.getFact(Fact.VARIABLE,OntologyDb.ROOT)));
154             else
155                store.remove(data[row][PROTOCOL]);
156             selectRow(-1); selectRow(row); // force row reselection
157             fireTableCellUpdated(row,column);
158             fireChanged();
159             break;
160     }
161   }
162 
163   public ProtocolInfo[] getData() {
164      int row = selectedRow;
165      selectRow(-1); selectRow(row); // side effect save previous changes
166 
167      ProtocolInfo[] output = new ProtocolInfo[store.size()];
168      Enumeration enum = store.elements();
169      for(int i = 0; enum.hasMoreElements(); i++ )
170            output[i] = (ProtocolInfo)enum.nextElement();
171      return output;
172   }
173 
174   public void selectRow(int row) {
175      if ( selectedRow == row ) return;
176      selectedRow = row;
177      if ( strategyModel != null ) {
178         if ( selectedRow != -1 && data[row][STATE].equals(Boolean.TRUE) )
179            strategyModel.reset((ProtocolInfo)store.get(data[row][PROTOCOL]));
180         else
181            strategyModel.reset(null);
182      }
183   }
184 
185 
186   //------------------------------------------------------------------------
187   // Event Methods
188   //------------------------------------------------------------------------
189 
190   public void addChangeListener(ChangeListener x) {
191     changeListeners.add(ChangeListener.class, x);
192   }  
193 
194   public void removeChangeListener(ChangeListener x) {
195     changeListeners.remove(ChangeListener.class, x);
196   }
197     
198   protected void fireChanged() {
199      ChangeEvent c = new ChangeEvent(this);
200      Object[] listeners = changeListeners.getListenerList();
201      for(int i= listeners.length-2; i >= 0; i -=2) {
202         if (listeners[i] == ChangeListener.class) {
203            ChangeListener cl = (ChangeListener)listeners[i+1];
204            cl.stateChanged(c);
205         }
206      }
207   }
208 }