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  * FactModel.java
26  *
27  * The underlying model for the Fact Table
28  *****************************************************************************/
29  
30  package zeus.generator.util;
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.event.*;
40  
41  public class FactModel extends AbstractTableModel
42                         implements BasicFactModel, ChangeListener {
43  
44    public static final int TYPE      = 0;
45    public static final int INSTANCE  = 1;
46    public static final int MODIFIERS = 2;
47    public static final int FACT      = 3;
48  
49  
50    protected static int count  = 0;
51    protected static final boolean ERROR = true;
52    protected static final boolean NO_ERROR = false;
53    protected static final String[] columnNames     = {
54       "Fact Type", "Instance", "Modifiers"
55    };
56  
57    protected EventListenerList listeners = new EventListenerList();
58    protected Vector            data            = new Vector();
59    protected int               selectedRow     = -1;
60    protected AttributeModel    attributeModel;
61    protected OntologyDb        ontologyDb;
62    protected boolean           isVariable;
63    protected boolean           isEditable = true;
64    protected int               type;
65    protected Vector            relatedModels = new Vector();
66  
67    public FactModel(OntologyDb ontologyDb, AttributeModel attributeModel,
68                     boolean isVariable, int type, Fact[] input) {
69  
70       this.ontologyDb = ontologyDb;
71       this.attributeModel = attributeModel;
72       this.isVariable = isVariable;
73       this.type = type;
74       ontologyDb.addChangeListener(this);
75       reset(input);
76    }
77  
78    public OntologyDb     getOntologyDb()     { return ontologyDb; }
79    public AttributeModel getAttributeModel() { return attributeModel; }
80  
81    public void reset(Fact[] input) {
82       int r = data.size();
83       selectRow(-1);
84       data.removeAllElements();
85       if ( r != 0 ) fireTableRowsDeleted(0,r-1);
86  
87       for(int i = 0; i < input.length; i++ )
88          data.addElement(input[i]);
89       fireTableRowsInserted(0,input.length-1);
90       fireTableStructureChanged(); // bug in swing?
91    }
92  
93    public Fact[] getData() {
94       Fact[] output = new Fact[data.size()];
95       for(int i = 0; i < data.size(); i++ )
96          output[i] = (Fact)data.elementAt(i);
97       return output;
98    }
99  
100   public void addRelatedModel(FactModel model) {
101      if ( !relatedModels.contains(model) )
102         relatedModels.addElement(model);
103   }
104   public void removeRelatedModel(FactModel model) {
105      relatedModels.removeElement(model);
106   }
107 
108   public void removeRows(int[] rows) {
109      Fact f;
110      for(int i = 0; i < rows.length; i++ ) {
111         f = (Fact)data.elementAt(rows[i]-i);
112         data.removeElementAt(rows[i]-i);
113         fireTableRowsDeleted(rows[i]-i,rows[i]-i);
114         fireFactEvent(f,FactModelEvent.FACT_REMOVED);
115      }
116      selectRow(-1);
117      fireChanged();
118   }
119 
120   public void selectRow(int row) {
121      selectedRow = row;
122      if ( attributeModel != null ) {
123         if ( selectedRow >= 0 )
124            attributeModel.reset((Fact)data.elementAt(selectedRow));
125         else
126            attributeModel.reset(null);
127      }
128   }
129 
130   public void addNewRows(String[] names)  {
131      if ( names == null || names.length == 0 ) return;
132      Fact[] input = new Fact[names.length];
133      for(int i = 0; i < names.length; i++ )
134         input[i] = ontologyDb.getFact(isVariable,names[i]);
135      addRows(input);
136   }
137 
138   public void addRows(Fact[] input) {
139      if ( input == null || input.length == 0 ) return;
140 
141      Fact f1;
142      String id;
143      int size = data.size();
144      for(int i = 0; i < input.length; i++ ) {
145         f1 = new Fact(input[i]);
146         id = f1.ID();
147         while( contains(id,NO_ERROR) )
148            id += (count++);
149         f1.setId(id);
150         data.addElement(f1);
151         fireFactEvent(f1,FactModelEvent.FACT_ADDED);
152      }
153      fireTableRowsInserted(size,size+input.length-1);
154      selectRow(-1);
155      fireChanged();
156   }
157   public void removeRows(Fact[] input) {
158      if ( input == null || input.length == 0 ) return;
159 
160      Fact f1;
161      String id;
162      for(int i = 0; i < input.length; i++ ) {
163         id = input[i].ID();
164         for(int j = 0; j < data.size(); j++ ) {
165            f1 = (Fact)data.elementAt(j);
166            if ( f1.ID().equals(id) ) {
167               data.removeElementAt(j);
168               fireTableRowsDeleted(j,j);
169               fireFactEvent(f1,FactModelEvent.FACT_REMOVED);
170               j--;
171            }
172         }
173      }
174      selectRow(-1);
175      fireChanged();
176   }
177 
178   // ----------------------------------------------------------------------
179 
180   public int getColumnCount() {
181      if ( type == FactPanel.NONE )
182         return columnNames.length - 1;
183      else
184         return columnNames.length;
185   }
186   public int     getRowCount()                    { return data.size(); }
187   public String  getColumnName(int col)           { return columnNames[col]; }
188   public boolean isCellEditable(int row, int col) {
189      return isEditable && col != TYPE;
190   }
191 
192   public void setEditable(boolean isEditable) {
193      this.isEditable = isEditable;
194   }
195 
196   public Object getValueAt (int row, int column)  {
197      Fact f = (Fact)data.elementAt(row);
198      switch(column) {
199         case TYPE:
200              return f.getType();
201         case INSTANCE:
202              return f.getId();
203         case MODIFIERS:
204              return new Integer(f.getModifiers());
205         case FACT:
206              return f;
207      }
208      return null;
209   }
210 
211   public void setValueAt(Object aValue, int row, int column)  {
212     // prevents the table being accidently loaded with a null value
213     // current table implementation needs this - possibly because of a bug
214     if (aValue.toString().equals(""))
215       return;
216 
217      Fact f = (Fact)data.elementAt(row);
218      switch(column) {
219         case TYPE:
220              Core.ERROR(null,1,this);
221              break;
222         case INSTANCE:
223              String newId = (String)aValue;
224              String id = f.ID();
225              if ( id.equals(newId) )
226                 return;
227              else if ( contains(newId,ERROR) )
228                 return;
229              else {
230                 String fid0 = f.getId();
231                 f.setId(newId);
232                 String fid1 = f.getId();
233                 fireTableCellUpdated(row,column);
234                 fireNameChanged(f,fid0,fid1);
235                 fireChanged();
236              }
237              break;
238         case MODIFIERS:
239              int modifiers = ((Integer)aValue).intValue();
240              if ( modifiers == f.getModifiers()) return;
241              f.setModifiers(modifiers);
242              fireTableCellUpdated(row,column);
243              fireChanged();
244              break;
245         case FACT:
246              Core.ERROR(null,2,this);
247              break;
248      }
249   }
250 
251   protected boolean contains(String id, boolean error) {
252      return contains(null,id,error);
253   }
254   protected boolean contains(FactModel origin, String id, boolean error) {
255     Fact f;
256     for(int i = 0; i < data.size(); i++ ) {
257        f = (Fact)data.elementAt(i);
258        if ( id.equals(f.ID()) ) {
259           if ( error )
260              JOptionPane.showMessageDialog(null,
261                 "Attempting to rename fact to an already\nexisting name",
262                 "Error", JOptionPane.ERROR_MESSAGE);
263           return true;
264        }
265     }
266 
267     FactModel model;
268     for(int i = 0; i < relatedModels.size(); i++ ) {
269        model = (FactModel)relatedModels.elementAt(i);
270        if ( model != origin )
271           if ( model.contains(this,id,error) ) return true;
272     }
273     return false;
274   }
275 
276   public void stateChanged(ChangeEvent e) {
277      // Underlying ontology has changed!!
278      // NEED to verify all facts!!
279   }
280   public void addFactModelListener(FactModelListener x) {
281      listeners.add(FactModelListener.class, x);
282   }
283   public void removeFactModelListener(FactModelListener x) {
284      listeners.remove(FactModelListener.class, x);
285   }
286   public void addChangeListener(ChangeListener x) {
287      listeners.add(ChangeListener.class, x);
288   }
289   public void removeChangeListener(ChangeListener x) {
290      listeners.remove(ChangeListener.class, x);
291   }
292   public void addRenameListener(RenameListener x) {
293      listeners.add(RenameListener.class, x);
294   }
295   public void removeRenameListener(RenameListener x) {
296      listeners.remove(RenameListener.class, x);
297   }
298 
299   protected void fireChanged() {
300      ChangeEvent c = new ChangeEvent(this);
301      Object[] list = listeners.getListenerList();
302      for(int i= list.length-2; i >= 0; i -=2) {
303         if (list[i] == ChangeListener.class) {
304            ChangeListener cl = (ChangeListener)list[i+1];
305            cl.stateChanged(c);
306         }
307      }
308   }
309 
310   protected void fireNameChanged(Object object, Object previous,
311                                  Object current) {
312      RenameEvent c = new RenameEvent(this,object,previous,current);
313      FactModelEvent e = new FactModelEvent(this,(Fact)object,
314         FactModelEvent.FACT_ID_CHANGED,(String)previous,(String)current);
315      Object[] list = listeners.getListenerList();
316      for(int i= list.length-2; i >= 0; i -=2) {
317         if (list[i] == FactModelListener.class) {
318            FactModelListener l = (FactModelListener)list[i+1];
319            l.factModelChanged(e);
320         }
321         else if (list[i] == RenameListener.class) {
322            RenameListener cl = (RenameListener)list[i+1];
323            cl.nameChanged(c);
324         }
325      }
326   }
327 
328   protected void fireFactEvent(Fact f, int type) {
329      FactModelEvent e = new FactModelEvent(this,f,type);
330      Object[] list = listeners.getListenerList();
331      for(int i= list.length-2; i >= 0; i -=2) {
332         if (list[i] == FactModelListener.class) {
333            FactModelListener l = (FactModelListener)list[i+1];
334            l.factModelChanged(e);
335         }
336      }
337   }
338 
339 }