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 package zeus.agentviewer.resources;
25
26 import javax.swing.*;
27 import javax.swing.table.*;
28 import javax.swing.event.*;
29 import java.util.*;
30 import zeus.concepts.*;
31 import zeus.actors.event.*;
32 import zeus.concepts.*;
33 import zeus.actors.*;
34
35
36 public class ResourceTableModel extends AbstractTableModel
37 implements FactMonitor {
38
39 static final int TYPE = 0;
40 static final int ID = 1;
41 static final int STATUS = 2;
42
43 private String[] header = { "Type", "Id", "Status" };
44 private Vector data ;
45 public ResourceDb resDB;
46 public OntologyDb ontologyDb;
47 protected EventListenerList changeListeners = new EventListenerList();
48
49
50 //---------------------------------------------------------------------------
51 public ResourceTableModel(AgentContext context){
52 data = new Vector();
53 resDB = context.ResourceDb();
54 ontologyDb = context.OntologyDb();
55 resDB.addFactMonitor(this, FactEvent.ADD_MASK | FactEvent.DELETE_MASK
56 | FactEvent.MODIFY_MASK,true);
57 }
58
59
60 //---------------------------------------------------------------------------
61 public int getRowCount() { return data.size(); }
62 //---------------------------------------------------------------------------
63 public int getColumnCount() { return header.length; }
64 //---------------------------------------------------------------------------
65 public String getColumnName(int col) { return header[col]; }
66 //---------------------------------------------------------------------------
67 public Object getValueAt(int row, int col) {
68 ResourceItem item = (ResourceItem)data.elementAt(row);
69 switch(col) {
70 case TYPE:
71 return item.getFact().getType();
72 case ID:
73 return item.getFact().getId();
74 case STATUS:
75 return (item.isReserved() ? "RESERVED" : "UNRESERVED");
76 }
77 return null;
78 }
79 //---------------------------------------------------------------------------
80 public boolean isCellEditable(int row, int col) {
81 return false;
82 }
83 //---------------------------------------------------------------------------
84 public Fact getAttributesOf(int row){
85 return ((ResourceItem)data.elementAt(row)).getFact();
86 }
87 //---------------------------------------------------------------------------
88 public void factAddedEvent(FactEvent event) {
89 if ( !data.contains(event.getObject()) )
90 data.addElement(event.getObject());
91 fireTableDataChanged();
92 fireChanged();
93 }
94 //---------------------------------------------------------------------------
95 public void factModifiedEvent(FactEvent event) {
96 fireTableDataChanged();
97 fireChanged();
98 }
99 //---------------------------------------------------------------------------
100 public void factDeletedEvent(FactEvent event) {
101 data.removeElement(event.getObject());
102 fireTableDataChanged();
103 fireChanged();
104 }
105 //---------------------------------------------------------------------------
106 public void factAccessedEvent(FactEvent event) {}
107 //---------------------------------------------------------------------------
108
109 // -- EVENTLISTENER METHODS ---------------------------------------
110
111 public void addChangeListener(ChangeListener x) {
112 changeListeners.add(ChangeListener.class, x);
113 }
114 //---------------------------------------------------------------------------
115 public void removeChangeListener(ChangeListener x) {
116 changeListeners.remove(ChangeListener.class, x);
117 }
118 //---------------------------------------------------------------------------
119 protected void fireChanged() {
120 ChangeEvent c = new ChangeEvent(this);
121 Object[] listeners = changeListeners.getListenerList();
122 for (int i= listeners.length-2; i >= 0; i -=2) {
123 if (listeners[i] == ChangeListener.class) {
124 ChangeListener cl = (ChangeListener)listeners[i+1];
125 cl.stateChanged(c);
126 }
127 }
128 }
129
130 //---------------------------------------------------------------------------
131 public void removeZeusEventMonitors(){
132 resDB.removeFactMonitor(this, FactEvent.ADD_MASK | FactEvent.DELETE_MASK
133 | FactEvent.MODIFY_MASK);
134
135 }
136 }