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  * OrderingModel.java
26  *
27  * The underlying model for the Preconditions Ordering Table 
28  *****************************************************************************/
29  
30  package zeus.generator.task;
31  
32  import java.util.*;
33  import javax.swing.*;
34  import javax.swing.table.*;
35  import javax.swing.event.*;
36  
37  import zeus.util.*;
38  import zeus.concepts.*;
39  import zeus.generator.*;
40  import zeus.generator.util.*;
41  import zeus.generator.util.*;
42  import zeus.generator.event.*;
43  import zeus.gui.editors.*;
44  
45  public class OrderingModel extends AbstractTableModel
46                             implements ChangeListener,
47                                        ValidatingModel,
48                                        RenameListener {
49  
50    static final int BEFORE = 0;
51    static final int AFTER  = 1;
52  
53    protected static final String[] columnNames = { "Before", "After" };  
54  
55    protected EventListenerList changeListeners = new EventListenerList();
56    protected Vector            data            = new Vector();
57    protected Vector            validityInfo    = new Vector();
58    protected BasicFactModel    preconditionsModel;
59  
60    public OrderingModel(BasicFactModel preconditionsModel,
61                         Ordering[] input) {
62  
63       this.preconditionsModel = preconditionsModel;
64       preconditionsModel.addChangeListener(this);
65       preconditionsModel.addRenameListener(this);
66       reset(input);
67    }
68  
69    public void reset(Ordering[] input) {
70       int r = data.size();
71       data.removeAllElements();
72       validityInfo.removeAllElements();
73  
74       if ( r != 0 ) fireTableRowsDeleted(0,r-1);
75  
76       Vector items = getPreconditionIds();
77       for(int i = 0; i < input.length; i++ ) {
78          data.addElement(input[i]);
79          validityInfo.addElement(isValid(input[i],items));
80       }
81       fireTableRowsInserted(0,input.length-1);
82    }
83    
84    public Ordering[] getData() {
85       // Save only valid data
86       Vector valid = new Vector();
87       for(int i = 0; i < data.size(); i++ )
88          if ( validityInfo.elementAt(i).equals(Boolean.TRUE) )
89  	   valid.addElement(data.elementAt(i));
90  
91       Ordering[] output = new Ordering[valid.size()];
92       for(int i = 0; i < valid.size(); i++ )
93          output[i] = (Ordering)valid.elementAt(i);
94       return output;
95    }
96  
97    public void removeRows(int[] rows) {
98       for(int i = 0; i < rows.length; i++ ) {
99          data.removeElementAt(rows[i]-i);
100         validityInfo.removeElementAt(rows[i]-i);
101         fireTableRowsDeleted(rows[i]-i,rows[i]-i);
102      }
103      fireTableStructureChanged(); // swing bug? force redraw of table
104      fireChanged();
105   }
106 
107   public void addNewRows(String lhs, String[] rhs)  {
108      Ordering c;
109      int count = 0;
110      int size = data.size();
111 
112      for(int i = 0; i < rhs.length; i++ ) {
113         if ( additionOK(lhs,rhs[i],-1) ) {
114            count++;
115            c = new Ordering(lhs,rhs[i]);
116            data.addElement(c);
117            Vector items = getPreconditionIds();
118            validityInfo.addElement(isValid(c,items));
119         }
120      }
121 
122      if ( count > 0 ) {
123         fireTableRowsInserted(size-1,size-1+count);
124         fireTableStructureChanged(); // swing bug? force redraw of table
125         fireChanged();
126      }
127   }
128 
129   protected Vector getPreconditionIds() {
130      Fact[] facts = preconditionsModel.getData();
131      Vector items = new Vector();
132      for(int i = 0; i < facts.length; i++ )
133         items.addElement(facts[i].getId());
134      return items;
135   }
136 
137   protected Boolean isValid(Ordering c, Vector items) {
138      boolean b = items.contains(c.getLHS()) && items.contains(c.getRHS());
139      return new Boolean(b);
140   }
141 
142   // ----------------------------------------------------------------------
143 
144   public int     getColumnCount()                 { return columnNames.length;}
145   public boolean isCellEditable(int row, int col) { return true; }
146   public int     getRowCount()                    { return data.size(); }
147   public String  getColumnName(int col)           { return columnNames[col]; }
148   public boolean isValidEntry(int row, int col)   { return validityInfo.elementAt(row).equals(Boolean.TRUE); }
149 
150   public Object getValueAt (int row, int column)  {
151      Ordering c = (Ordering)data.elementAt(row);
152      switch(column) {
153         case BEFORE:
154              return c.getLHS();
155         case AFTER:
156              return c.getRHS();
157      }
158      return null;
159   }
160 
161   public void setValueAt(Object aValue, int row, int column)  {
162     // prevents the table being accidently loaded with a null value
163     // current table implementation needs this - possibly because of a bug 
164     if (aValue.toString().equals(""))
165       return;
166 
167      Ordering c = (Ordering)data.elementAt(row);
168      String newId;
169      Vector items;
170      switch(column) {
171         case BEFORE:
172              newId = (String)aValue;          
173              if ( newId.equals(c.getLHS()) )
174                 return;
175              else if ( !additionOK(newId,c.getRHS(),row) )
176                 return;
177              else {
178                 c.setLHS(newId);
179                 items = getPreconditionIds();
180                 validityInfo.setElementAt(isValid(c,items),row);		
181                 fireTableCellUpdated(row,column);
182                 fireChanged();
183              }
184              break;
185         case AFTER:
186              newId = (String)aValue;
187              if ( newId.equals(c.getRHS()) )
188                 return;
189              else if ( !additionOK(c.getLHS(),newId,row) )
190                 return;
191              else {
192                 c.setRHS(newId);
193                 items = getPreconditionIds();
194                 validityInfo.setElementAt(isValid(c,items),row);		
195                 fireTableCellUpdated(row,column);
196                 fireChanged();
197              }
198              break;
199      }
200   }
201 
202   protected boolean additionOK(String lhs, String rhs, int row) {
203     Ordering c;
204 
205     if ( lhs.equals(rhs) ) {
206        JOptionPane.showMessageDialog(null,
207           "Attempting to add ordering " + lhs + "  --> " + rhs,
208           "Error", JOptionPane.ERROR_MESSAGE);
209        return false;
210     }
211 
212     for(int i = 0; i < data.size(); i++ ) {
213        if ( i != row ) {
214           c = (Ordering)data.elementAt(i);
215           if ( c.getLHS().equals(lhs) && c.getRHS().equals(rhs) ) {
216              JOptionPane.showMessageDialog(null,
217                 "Table already contains the ordering\n" + lhs + " < " + rhs,
218                 "Error", JOptionPane.ERROR_MESSAGE);
219              return false;
220           }
221           else if ( c.getRHS().equals(lhs) && c.getLHS().equals(rhs) ) {
222              JOptionPane.showMessageDialog(null,
223                 "Attempting to add ordering " + lhs + " < " + rhs + 
224    	        "\nwhen table already contains ordering " + rhs + " < " + lhs,
225                 "Error", JOptionPane.ERROR_MESSAGE);
226              return false;
227           }
228        }
229     }
230     return true;
231   }
232 
233   public void stateChanged(ChangeEvent e) {
234      // Preconditions have changed!
235      // NEED to verify all ordering constraints!!
236      Vector items = getPreconditionIds();
237      for(int i = 0; i < data.size(); i++ ) {
238         Ordering c = (Ordering)data.elementAt(i);
239         validityInfo.setElementAt(isValid(c,items),i);
240      }
241      fireTableDataChanged();
242   }
243   public void nameChanged(RenameEvent e) {
244      // Preconditions Ids have changed!
245      // NEED to modify relevant ordering constraints!!
246      String prev = (String)e.getOriginal();
247      String curr = (String)e.getCurrent();
248      for(int i = 0; i < data.size(); i++ ) {
249         Ordering c = (Ordering)data.elementAt(i);
250         if ( c.getLHS().equals(prev) ) c.setLHS(curr);
251         if ( c.getRHS().equals(prev) ) c.setRHS(curr);
252      }
253      fireTableDataChanged();
254   }
255 
256   public void addChangeListener(ChangeListener x) {
257      changeListeners.add(ChangeListener.class, x);
258   }
259   public void removeChangeListener(ChangeListener x) {
260      changeListeners.remove(ChangeListener.class, x);
261   }
262 
263   protected void fireChanged() {
264      ChangeEvent c = new ChangeEvent(this);
265      Object[] listeners = changeListeners.getListenerList();
266      for(int i= listeners.length-2; i >= 0; i -=2) {
267         if (listeners[i] == ChangeListener.class) {
268            ChangeListener cl = (ChangeListener)listeners[i+1];
269            cl.stateChanged(c);
270         }
271      }
272   }
273 }