1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 /******************************************************************************
25 * AcquaintanceModel.java
26 *
27 * The underlying model for the Acquaintance Table
28 *****************************************************************************/
29
30 package zeus.generator.agent;
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
41 public class AcquaintanceModel extends AbstractTableModel
42 implements ChangeListener {
43 static final int ALL = 0;
44 static final int PEERS = 1;
45 static final int OTHERS = 2;
46
47 static final int AGENT = 0;
48 static final int RELATION = 1;
49 static final int ID = 2;
50
51 public static Vector RELATIONS_LIST = null;
52 public static final String PEER_RELATION =
53 SystemProps.getProperty("system.organisation.relations.default");
54
55 static {
56 String system = SystemProps.getProperty("system.organisation.relations");
57 String user = SystemProps.getProperty("user.organisation.relations");
58 String sep = SystemProps.getProperty("file.separator");
59
60 StringTokenizer st = new StringTokenizer(system,sep);
61 RELATIONS_LIST = new Vector(100);
62
63 String token;
64 while( st.hasMoreTokens() ) {
65 token = st.nextToken();
66 if ( !RELATIONS_LIST.contains(token) )
67 RELATIONS_LIST.addElement(token);
68 }
69
70 if ( user != null ) {
71 st = new StringTokenizer(user,sep);
72 while( st.hasMoreTokens() ) {
73 token = st.nextToken();
74 if ( !RELATIONS_LIST.contains(token) )
75 RELATIONS_LIST.addElement(token);
76 }
77 }
78 if ( !RELATIONS_LIST.contains(PEER_RELATION) )
79 RELATIONS_LIST.addElement(PEER_RELATION);
80 };
81
82
83
84 protected String[] columnNames = { "Agent", "Relation" };
85 protected Vector data = new Vector(100);
86 protected int filter = ALL;
87 protected int selectedRow = -1;
88 protected AbilityModel abilityModel;
89 protected GeneratorModel genmodel;
90 protected OntologyDb ontologyDb;
91
92 public AcquaintanceModel(GeneratorModel genmodel,
93 OntologyDb ontologyDb,
94 AbilityModel abilityModel,
95 Acquaintance[] input) {
96
97 this.abilityModel = abilityModel;
98 this.genmodel = genmodel;
99 reset(input);
100 ontologyDb.addChangeListener(this);
101 genmodel.addChangeListener(this);
102 }
103
104 public void reset(Acquaintance[] input) {
105 int size = data.size();
106 data.removeAllElements();
107 if ( size != 0 )
108 fireTableRowsDeleted(0,size-1);
109 filter = ALL;
110 selectRow(-1);
111 if ( input == null || input.length == 0 ) return;
112 for(int i = 0; i < input.length; i++ )
113 data.addElement(input[i]);
114 fireTableRowsInserted(0,input.length-1);
115 }
116
117
118 public void setFilter(int f) {
119 if ( filter == f ) return;
120 filter = f;
121 selectRow(-1);
122 fireTableDataChanged();
123 }
124
125 public void selectRow(int row) {
126 selectedRow = row;
127 if ( abilityModel != null ) {
128 if ( selectedRow != -1 )
129 abilityModel.reset(getAcquaintanceAt(row));
130 else
131 abilityModel.reset(null);
132 }
133 }
134
135 public void addNewRow() {
136 String agentId = genmodel.createNewAgentId();
137 genmodel.createNewAgent(agentId);
138 Acquaintance a = new Acquaintance(agentId,PEER_RELATION);
139 data.addElement(a);
140 selectRow(-1);
141 int size = data.size();
142 fireTableRowsInserted(size-2,size-1);
143 fireChanged();
144 }
145
146 public void addNewRow(String agent) {
147 if ( contains(agent) ) return;
148
149 String agentId = genmodel.reverseAgentNameLookup(agent);
150 Acquaintance a = new Acquaintance(agentId,PEER_RELATION);
151 data.addElement(a);
152 selectRow(-1);
153 int size = data.size();
154 fireTableRowsInserted(size-2,size-1);
155 fireChanged();
156 }
157
158 public void removeRow(int row) {
159 Acquaintance a = getAcquaintanceAt(row);
160 data.removeElement(a);
161 selectRow(-1);
162 fireTableRowsDeleted(row,row);
163 fireChanged();
164 }
165
166 public Acquaintance[] getData() {
167 selectRow(selectedRow);
168 Acquaintance[] out = new Acquaintance[data.size()];
169 for(int i = 0; i < out.length; i++ )
170 out[i] = (Acquaintance)data.elementAt(i);
171 return out;
172 }
173
174
175
176 public int getColumnCount() { return columnNames.length; }
177 public boolean isCellEditable(int r, int c) { return true; }
178 public String getColumnName(int column) { return columnNames[column]; }
179
180 public int getRowCount() {
181 int count = 0;
182 Acquaintance a;
183
184 switch(filter) {
185 case ALL:
186 return data.size();
187
188 case PEERS:
189 for(int i = 0; i < data.size(); i++ ) {
190 a = (Acquaintance)data.elementAt(i);
191 if ( a.getRelation().equals(PEER_RELATION) )
192 count++;
193 }
194 return count;
195
196 case OTHERS:
197 for(int i = 0; i < data.size(); i++ ) {
198 a = (Acquaintance)data.elementAt(i);
199 if ( !(a.getRelation().equals(PEER_RELATION)) )
200 count++;
201 }
202 return count;
203 }
204 return 0;
205 }
206
207 public void setValueAt(Object aValue, int row, int column) {
208
209
210 if (aValue.toString().equals(""))
211 return;
212
213 Acquaintance a = getAcquaintanceAt(row);
214 String id = a.getName();
215 String name = genmodel.getAgentName(id);
216 String relation = a.getRelation();
217
218 switch(column) {
219 case AGENT:
220 String agent = (String)aValue;
221 if ( name.equals(agent) ) return;
222 if ( contains(agent) ) return;
223 String newId = genmodel.reverseAgentNameLookup(agent);
224 if ( newId != null )
225 a.setName(newId);
226 else
227 genmodel.renameAgent(id,agent);
228 fireTableCellUpdated(row,column);
229 fireChanged();
230 break;
231 case RELATION:
232 if ( relation.equals(aValue) ) return;
233 a.setRelation((String)aValue);
234 fireTableCellUpdated(row,column);
235 fireChanged();
236 break;
237 }
238 }
239
240 public Object getValueAt(int row, int column) {
241 Acquaintance a = getAcquaintanceAt(row);
242 switch(column) {
243 case AGENT:
244 return genmodel.getAgentName(a.getName());
245 case ID:
246 return a.getName();
247 case RELATION:
248 return a.getRelation();
249 }
250 return null;
251 }
252
253 protected Acquaintance getAcquaintanceAt(int row) {
254 Acquaintance a = null;
255 int position;
256
257 switch(filter) {
258 case ALL:
259 return (Acquaintance)data.elementAt(row);
260
261 case PEERS:
262 position = -1;
263 for(int i = 0; i < data.size(); i++ ) {
264 a = (Acquaintance)data.elementAt(i);
265 if ( a.getRelation().equals(PEER_RELATION) )
266 position++;
267 if ( position == row )
268 return a;
269 }
270 break;
271
272 case OTHERS:
273 position = -1;
274 for(int i = 0; i < data.size(); i++ ) {
275 a = (Acquaintance)data.elementAt(i);
276 if ( !(a.getRelation().equals(PEER_RELATION)) )
277 position++;
278 if ( position == row )
279 return a;
280 }
281 break;
282 }
283 Core.ERROR(false,1,this);
284 return null;
285 }
286
287
288 protected boolean contains(String agent) {
289
290 Acquaintance a;
291 for(int i = 0; i < data.size(); i++ ) {
292 a = (Acquaintance)data.elementAt(i);
293 if ( agent.equals(genmodel.getAgentName(a.getName())) ) {
294 JOptionPane.showMessageDialog(null,
295 "Attempting to add an already\nexisting agent",
296 "Error", JOptionPane.ERROR_MESSAGE);
297 return true;
298 }
299 }
300 return false;
301 }
302
303 public void stateChanged(ChangeEvent e) {
304
305
306
307 Object src = e.getSource();
308 if ( src == ontologyDb ) {
309
310 }
311 else if ( src == genmodel ) {
312 fireTableDataChanged();
313 }
314 }
315
316 protected EventListenerList changeListeners = new EventListenerList();
317 public void addChangeListener(ChangeListener x) {
318 changeListeners.add(ChangeListener.class, x);
319 }
320 public void removeChangeListener(ChangeListener x) {
321 changeListeners.remove(ChangeListener.class, x);
322 }
323
324 protected void fireChanged() {
325 ChangeEvent c = new ChangeEvent(this);
326 Object[] listeners = changeListeners.getListenerList();
327 for(int i= listeners.length-2; i >= 0; i -=2) {
328 if (listeners[i] == ChangeListener.class) {
329 ChangeListener cl = (ChangeListener)listeners[i+1];
330 cl.stateChanged(c);
331 }
332 }
333 }
334
335 }