1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package zeus.agentviewer.plansch;
25
26 import javax.swing.JOptionPane;
27 import javax.swing.event.*;
28 import javax.swing.table.DefaultTableModel;
29 import java.util.*;
30 import zeus.actors.*;
31 import zeus.actors.event.*;
32 import javax.swing.JTable;
33 import javax.swing.table.JTableHeader;
34
35 public class PlanSchModel extends DefaultTableModel implements PlanStepMonitor,
36 ClockMonitor{
37 static final int DEFAULT_SIZE = 10;
38 private PlanRecord[][] data;
39 private PlanRecord [][] newData = null;
40 private int length;
41 private int width;
42 private int from;
43 private int now;
44 private ExecutionMonitor em;
45 private Planner planner;
46 protected EventListenerList changeListeners = new EventListenerList();
47 private JTable table;
48
49
50 public PlanSchModel(AgentContext context){
51 em = context.ExecutionMonitor();
52 planner = context.Planner();
53
54 length = planner.getPlannerLength();
55 width = planner.getPlannerWidth();
56
57 now = (int)context.now();
58 data = new PlanRecord[width][length];
59 for(int i = 0; i < width; i++ )
60 for(int j = 0; j < length; j++ )
61 data[i][j] = null;
62
63 planner.addPlanStepMonitor(this,PlanStepEvent.CREATE_MASK
64 |PlanStepEvent.DISPOSE_MASK
65 | PlanStepEvent.STATE_CHANGE_MASK);
66
67 em.addClockMonitor(this, ClockEvent.TICK_MASK);
68
69 from = now;
70 fireTableStructureChanged();
71 }
72
73
74 public int getRowCount() { return width; }
75
76
77 public int getColumnCount() { return DEFAULT_SIZE;}
78
79
80 public Object getValueAt(int row, int col) {
81 if (row > width) return null;
82 if (col + from - now > length) return null;
83 return data[row][col+from-now];
84 }
85
86
87 /***
88 this is altered to return column names that make sense
89 when the table structure is not changing
90 */
91 public String getColumnName(int col) {
92 int aCol = col + from;
93 return ("+" + Integer.toString(col));
94 }
95
96
97 public synchronized void createPlanRecord(PlanRecord pr){
98 int st = pr.getStartTime() - now;
99 int et = pr.getEndTime() - now;
100 int proc = pr.getProc();
101 if ( st < 0 || et < 0 )
102 return;
103 for(int i = st; i < et; i++)
104 data[proc][i] = pr;
105 fireTableDataChanged();
106 }
107
108
109 /***
110 */
111 public synchronized void stateChangedPlanRecord(PlanRecord rec){
112
113 int st = rec.getStartTime() - now;
114 int et = rec.getEndTime() - now;
115 int proc = rec.getProc();
116 if ( st < 0 )
117 return;
118
119 for(int i = 0; i < width; i++ )
120 for(int j = 0; j < length; j++ )
121 if ( data[i][j] == rec ) data[i][j] = null;
122
123 for(int i = st; i < et; i++)
124 data[proc][i] = rec;
125 fireTableDataChanged();
126
127 }
128
129
130 protected void setTable(JTable table) {
131 this.table = table;
132 }
133
134
135 public synchronized void removePlanRecord(PlanRecord pr){
136 int proc = pr.getProc();
137 int st = pr.getStartTime() - now;
138 int et = pr.getEndTime() - now;
139 if ( st < 0 )
140 return;
141 for(int i = st; i < et; i++)
142 data[proc][i] = null;
143 fireTableDataChanged();
144 }
145
146 /***
147 this is changed for 1.3 to get rid of the annoying
148 swing synchronisation exception. This was caused by the
149 swing threads attempting to access the table data structures
150 while redrawing.
151 To retunr to the old version fire a tableStructureChanged event
152
153 */
154 public synchronized void clockTicked(int now){
155 if ( newData == null) {
156 newData = new PlanRecord [width][length];}
157 for(int i = 0; i < width; i++ ) {
158 for(int j = 0; j < length - 1; j++ )
159 newData[i][j] = data[i][j+1];
160
161 newData[i][length-1] = null;
162 }
163 data = newData;
164 this.now = now;
165 if ( now > from ) from = now;
166
167 fireTableDataChanged();
168 }
169
170
171 public void setFrom(int from){
172 if ( from < now ) {
173 reSetFrom();
174 }
175 else {
176 this.from = from;
177 }
178 fireTableStructureChanged();
179 }
180
181
182 public int getFrom() {
183 return from;
184 }
185
186
187 public void reSetFrom() {
188 from = now;
189 fireTableStructureChanged();
190 }
191
192
193 public Object[] getListeners(){
194 Object[] listeners = listenerList.getListenerList();
195 return listeners;
196 }
197
198
199 public void clockTickEvent(ClockEvent event) {
200 clockTicked(event.getValue());
201 fireChanged();
202 }
203
204
205 public void planStepCreatedEvent(PlanStepEvent event) {
206 createPlanRecord(event.getPlanRecord());
207 fireChanged();
208 }
209
210
211 public void planStepDisposedEvent(PlanStepEvent event) {
212 removePlanRecord( event.getPlanRecord());
213 fireChanged();
214 }
215
216
217 public void planStepStateChangedEvent(PlanStepEvent event) {
218 stateChangedPlanRecord( event.getPlanRecord());
219 fireChanged();
220 }
221
222
223 public void addChangeListener(ChangeListener x) {
224 changeListeners.add(ChangeListener.class, x);
225 }
226
227
228 public void removeChangeListener(ChangeListener x) {
229 changeListeners.remove(ChangeListener.class, x);
230 }
231
232
233 protected void fireChanged() {
234 ChangeEvent c = new ChangeEvent(this);
235 Object[] listeners = changeListeners.getListenerList();
236 for (int i= listeners.length-2; i >= 0; i -=2) {
237 if (listeners[i] == ChangeListener.class) {
238 ChangeListener cl = (ChangeListener)listeners[i+1];
239 cl.stateChanged(c);
240 }
241 }
242 }
243
244
245 public void removeZeusEventMonitors(){
246 planner.removePlanStepMonitor(this,PlanStepEvent.CREATE_MASK
247 |PlanStepEvent.DISPOSE_MASK
248 | PlanStepEvent.STATE_CHANGE_MASK);
249
250 em.removeClockMonitor(this, ClockEvent.TICK_MASK);
251 }
252
253
254 public int getProcessors(){
255 return width;
256 }
257 }