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  * RestrictionTableModel.java
26  *
27  * The local model for the Restriction Table
28  *****************************************************************************/
29  
30  package zeus.ontology.restrictions;
31  
32  import java.util.*;
33  import javax.swing.event.*;
34  import javax.swing.table.*;
35  
36  import zeus.concepts.*;
37  import zeus.ontology.*;
38  import zeus.gui.editors.*;
39  
40  
41  public class RestrictionTableModel extends AbstractTableModel
42               implements ChangeListener, ValidatingModel {
43  
44    protected String[]    columnNames = { "Name", "Type", "Restrictions" };
45    protected String[][]  data = null;
46    protected boolean[][] validityInfo = null;
47    protected OntologyDb  model = null;
48  
49    static final int NAME        = 0;
50    static final int TYPE        = 1;
51    static final int RESTRICTION = 2;
52  
53    public RestrictionTableModel(OntologyDb model) {
54      this.model = model;
55      model.addChangeListener(this);
56      refresh();
57    }
58  
59    void refresh() {
60      data = model.getAllRestrictions();
61      validityInfo = model.getAllRestrictionValidityInfo();
62      fireTableStructureChanged();
63    }
64  
65    public int      getColumnCount()     { return columnNames.length; }
66    public int      getRowCount()        { return (data != null) ? data.length : 0; }
67    public String[] getRow(int row)      { return data[row]; }
68  
69    public boolean isCellEditable(int r, int c) { return true;}
70    public String  getColumnName(int c)         { return columnNames[c]; }
71    public Object  getValueAt(int r, int c)     { return data[r][c]; }
72    public boolean isValidEntry(int r, int c)   { return validityInfo[r][c]; }
73  
74    public void setValueAt(Object aValue, int row, int col) {
75      String value;
76      if ( aValue == null )
77         value = "";
78      else
79         value = ((String)aValue).trim();
80  
81      if ( data[row][col].equals(value) )
82         return;
83  
84      String result = model.setRestrictionData(data[row][NAME],col,value);
85      data[row][col] = result;
86      validityInfo[row] = model.isRestrictionValid(data[row][NAME]);
87      fireTableCellUpdated(row,col);
88    }
89  
90    public void stateChanged(ChangeEvent e) {
91      OntologyDbChangeEvent evt = (OntologyDbChangeEvent)e;
92      if ( evt.getEventType() == OntologyDb.RELOAD )
93         refresh();
94    }
95  
96    void addNewRow() {
97       model.addNewRestriction();
98       refresh();
99    }
100   void deleteRows(int[] rows) {
101      String[] names = new String[rows.length];
102      for(int i = 0; i < rows.length; i++ )
103         names[i] = data[rows[i]][NAME];
104      model.deleteRestrictions(names);
105      refresh();
106   }
107   void addRows(String[][] input) {
108      model.addRestrictions(input);
109      refresh();
110   }
111   String[][] getRows(int[] input) {
112      String[][] result = new String[input.length][columnNames.length];
113      for(int i = 0; i < result.length; i++ )
114         for(int j = 0; j < result[i].length; j++)
115            result[i][j] = data[input[i]][j];
116      return result;
117   }
118 }