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  * AttributeTableModel.java
26  *
27  * The underlying model for the Attribute Table
28  *****************************************************************************/
29  
30  package zeus.ontology.attributes;
31  
32  import java.util.Vector;
33  import java.util.Enumeration;
34  import javax.swing.*;
35  import javax.swing.event.*;
36  import javax.swing.table.*;
37  
38  import zeus.util.*;
39  import zeus.concepts.*;
40  import zeus.ontology.*;
41  import zeus.gui.editors.*;
42  
43  
44  public class AttributeTableModel extends AbstractTableModel
45                                   implements ChangeListener, ValidatingModel {
46  
47    static final int NAME        = 0;
48    static final int TYPE        = 1;
49    static final int RESTRICTION = 2;
50    static final int DEFAULT     = 3;
51  
52    protected static final String[] columnNames = {
53       "Name", "Type", "Restriction", "Default Value" };
54  
55    protected String[][]    data = null;
56    protected boolean[][]   validityInfo = null;
57    protected String        currentName = null;
58    protected boolean       showAll = false;
59    protected OntologyDb    model;
60  
61  
62    public AttributeTableModel(OntologyDb model) {
63      this.model = model;
64    }
65  
66    void refreshAttributes(String fact) {
67      currentName = fact;
68      showAll = false;
69      data = model.getAttributeEntriesFor(fact);
70      validityInfo = model.getValidityInfoFor(fact);
71      fireTableStructureChanged();
72    }
73  
74    void refreshAllAttributes(String fact) {
75      currentName = fact;
76      showAll = true;
77      data = model.getAllAttributeEntriesFor(fact);
78      validityInfo = model.getAllValidityInfoFor(fact);
79      fireTableStructureChanged();
80    }
81  
82    void addNewRow() {
83       model.addNewAttributeRow(currentName);
84       refresh();
85    }
86    void deleteRows(int[] rows) {
87       Vector attributes = new Vector(100);
88       int limit = model.getEditableLimit();
89       for(int i = 0; i < rows.length; i++ ) {
90          if ( rows[i] < limit ) {
91             JOptionPane.showMessageDialog(null,"Attribute " +
92                data[rows[i]][NAME] + " cannot be deleted",
93                "Error", JOptionPane.ERROR_MESSAGE);
94          }
95          else
96             attributes.addElement(data[rows[i]][NAME]);
97       }
98       model.deleteAttributes(currentName,Misc.stringArray(attributes));
99       refresh();
100   }
101   void addRows(String[][] input) {
102      model.addAttributeRows(currentName,input);
103      refresh();
104   }
105   String[][] getRows(int[] input) {
106      String[][] result = new String[input.length][columnNames.length];
107      for(int i = 0; i < result.length; i++ )
108         for(int j = 0; j < result[i].length; j++)
109            result[i][j] = data[input[i]][j];
110      return result;
111   }
112 
113   public int getColumnCount() { return columnNames.length; }
114   public int getRowCount()    { return (data != null) ? data.length : 0; }
115 
116   public String   getColumnName(int col)          { return columnNames[col]; }
117   public Object   getValueAt(int row, int column) { return data[row][column]; }
118   public String[] getRow(int row)                 { return data[row]; }
119   public boolean  isValidEntry(int row, int col)  { return validityInfo[row][col]; }
120 
121 
122   public boolean isCellEditable(int row, int col) {
123      return isNodeEditable() && row >= model.getEditableLimit();
124   }
125 
126   public boolean isNodeEditable() {
127      return ( currentName != null && model.isFactEditable(currentName) );
128   }
129 
130   public void setValueAt(Object aValue, int row, int column) {
131     String value;
132     if ( aValue == null )
133        value = "";
134     else
135        value = (aValue.toString()).trim();
136 
137     if ( data[row][column].equals(value) ) return;
138 
139     data[row][column] = model.setAttribute(currentName,
140        data[row][NAME],column,value);
141     validityInfo[row] = model.isAttributeValid(currentName,data[row][NAME]);
142     fireTableCellUpdated(row,column);
143   }
144 
145   public void stateChanged(ChangeEvent e) {
146     refresh();
147   }
148 
149   protected void refresh() {
150     if ( currentName == null ) return;
151 
152     if ( !model.hasFact(currentName) )  {
153        currentName = null;
154        data = new String[0][4];
155        fireTableStructureChanged();
156        return;
157     }
158 
159     if ( showAll )
160        refreshAllAttributes(currentName);
161     else
162        refreshAttributes(currentName);
163   }
164 }