Source code: com/webobjects/woextensions/WOEventDisplayPage.java
1 /*
2 * WOEventDisplayPage.java
3 * © Copyright 2001 Apple Computer, Inc. All rights reserved.
4 * This a modified version.
5 * Original license: http://www.opensource.apple.com/apsl/
6 */
7
8 package com.webobjects.woextensions;
9
10 import com.webobjects.appserver.*;
11 import com.webobjects.foundation.*;
12 import com.webobjects.eocontrol.*;
13 import java.util.Enumeration;
14
15 public class WOEventDisplayPage extends WOEventPage {
16 public EOEvent currentEvent;
17 public NSArray selectionPath;
18 public int _displayMode;
19 public NSArray events;
20 public NSMutableDictionary cache;
21 public NSMutableArray webEvents, eofEvents;
22 public WOEventDisplayPage _self = this;
23 protected _EventComparator _eventAscendingComparator;
24
25 public WOEventDisplayPage(WOContext aContext) {
26 super(aContext);
27 selectionPath = new NSMutableArray();
28 _displayMode = 1;
29 cache = new NSMutableDictionary();
30 // we sort these things descending, i.e. the opposite of normal sorting order
31 _eventAscendingComparator = new _EventComparator(EOSortOrdering.CompareDescending, this);
32 }
33
34 public int displayMode() {
35 return _displayMode;
36 }
37
38 public void setDisplayMode(Object ick) {
39 if (ick == null) {
40 _displayMode = 0;
41 } else if (ick instanceof Number) {
42 _displayMode = ((Number)ick).intValue();
43 } else {
44 try {
45 _displayMode = Integer.parseInt(ick.toString());
46 } catch (NumberFormatException e) {
47 _displayMode = 0;
48 }
49 }
50 }
51
52 public int displayLevelForEvent(EOEvent e)
53 {
54 int index, i, n;
55 NSArray children;
56
57 index = selectionPath.indexOfObject(e);
58 if (index != NSArray.NotFound)
59 return index;
60
61 children = rootEventList();
62 if (children.containsObject(e))
63 return 0;
64
65 int count = selectionPath.count();
66 for (i = 0, n = count; i < n; i++) {
67 children = (NSArray)cache.objectForKey(selectionPath.objectAtIndex(i));
68 if (null==children)
69 break;
70
71 if (children.containsObject(e))
72 return i+1;
73 }
74
75 return -1;
76 }
77
78 public NSArray filterEvents(NSArray evs, int level)
79 {
80 int i, n;
81 NSArray filtered;
82 if (evs == null) {
83 return NSArray.EmptyArray;
84 }
85 // in the general case, it is sufficient to sort the events
86 // by their plain duration, which is what the default implementation does.
87 try {
88 if (_displayMode != 4 || level != 0) {
89 try {
90 filtered = evs.sortedArrayUsingComparator(_eventAscendingComparator);
91 } catch (IllegalStateException ex) {
92 filtered = evs;
93 }
94 } else {
95
96 // For association mode, we need to filter out unwanted events,
97 // i.e. those which are not related to associations.
98 int count = evs.count();
99 NSMutableArray mutableFiltered = new NSMutableArray(count);
100
101 for (i = 0, n = count; i < n; i++) {
102 if (childrenForEvent((EOEvent)evs.objectAtIndex(i)).count()!=0)
103 mutableFiltered.addObject(evs.objectAtIndex(i));
104 }
105
106 mutableFiltered.sortUsingComparator(_eventAscendingComparator);
107
108 filtered = mutableFiltered;
109 }
110
111 } catch (NSComparator.ComparisonException e) {
112 throw NSForwardException._runtimeExceptionForThrowable(e);
113 }
114
115 return filtered;
116 }
117
118 public int groupTagForDisplayLevel(int level)
119 {
120 switch (_displayMode) {
121 case 0:
122 return -1;
123
124 case 1:
125 return -1;
126
127 case 2:
128 switch (level) {
129 case 0:
130 return 2;
131
132 case 1:
133 return 1;
134
135 default:
136 return -1;
137 }
138
139 case 3:
140 if (level == 0)
141 return 2;
142 else
143 return -1;
144
145 case 4:
146 if (level == 0)
147 return 2;
148 else
149 return -1;
150
151 default: // bogus
152 return -1;
153 }
154 }
155
156 public int aggregateTagForDisplayLevel(int level)
157 {
158 switch (_displayMode) {
159 case 0:
160 return -1;
161
162 case 1:
163 return 0;
164
165 case 2:
166 if (level <= 1)
167 return -1;
168 else
169 return 0;
170
171 case 3:
172 if (level == 0)
173 return -1;
174 else
175 return 0;
176
177 case 4:
178 if (level == 0)
179 return -1;
180 else
181 return 3;
182
183 default: // bogus
184 return -1;
185 }
186 }
187
188 public NSArray rootEventList()
189 {
190 if (null == events) {
191 switch (_displayMode) {
192 case 0:
193 case 1:
194 events = EOEventCenter.rootEventsForAllCenters();
195 break;
196
197 default:
198 events = EOEventCenter.allEventsForAllCenters();
199 break;
200 }
201
202 events = EOEvent.groupEvents(events, groupTagForDisplayLevel(0));
203
204 events = EOEvent.aggregateEvents(events, aggregateTagForDisplayLevel(0));
205
206 events = filterEvents(events, 0);
207 }
208 return events;
209 }
210
211 public EOEvent object()
212 {
213 return currentEvent;
214 }
215
216 public NSArray childrenForEvent(EOEvent event)
217 {
218 NSArray anArray;
219 int level, tag;
220
221 anArray = (NSArray)cache.objectForKey(event);
222 if (null!=anArray) {
223 if (anArray.count()==0)
224 return null;
225 else
226 return anArray;
227 }
228
229 anArray = event.subevents();
230
231 if (anArray == null || (anArray.count()==0)) {
232 cache.setObjectForKey(NSArray.EmptyArray, event);
233 return null;
234 }
235
236 level = displayLevelForEvent(event) + 1;
237 if (level == -1)
238 level = selectionPath.count() + 1;
239
240 tag = groupTagForDisplayLevel(level);
241 if (tag >= 0)
242 anArray = EOEvent.groupEvents(anArray, tag);
243
244 tag = aggregateTagForDisplayLevel(level);
245 if (tag >= 0)
246 anArray = EOEvent.aggregateEvents(anArray, tag);
247
248 anArray = filterEvents(anArray, level);
249
250 cache.setObjectForKey(anArray, event);
251
252 return anArray;
253 }
254
255 public NSArray currentEventChildren()
256 {
257 NSArray result = childrenForEvent(currentEvent);
258 return (result != null) ? result : NSArray.EmptyArray;
259 }
260
261 public boolean isDirectory()
262 {
263 return currentEventChildren().count() > 0;
264 }
265
266 public WOComponent resetLoggingClicked()
267 {
268 EOEventCenter.resetLoggingForAllCenters();
269 return refreshLoggingClicked();
270 }
271
272 public WOComponent refreshLoggingClicked()
273 {
274 cache.removeAllObjects();
275 selectionPath = new NSMutableArray();
276 events = null;
277 webEvents = null;
278 eofEvents = null;
279
280 return null;
281 }
282
283 public String displayComponentName()
284 {
285 if (currentEvent == null) {
286 return "";
287 }
288 return currentEvent.displayComponentName();
289 }
290
291 public int eventCount()
292 {
293 return EOEventCenter.allEventsForAllCenters().count();
294 }
295
296 public long topmostDurationValue()
297 {
298 NSArray roots;
299
300 roots = rootEventList();
301 if (roots == null || (roots.count()==0))
302 return 0;
303 else
304 return durationOfEvent((EOEvent)roots.objectAtIndex(0));
305 }
306
307 public long durationOfEvent(EOEvent e)
308 {
309 int i, n;
310 long sum;
311 NSArray kids;
312
313 // mode 4 is in so far special as we need to filter out events which will not
314 // be displayed, even if they may have a duration.
315 if (_displayMode != 4) {
316 sum = e.duration();
317 return sum;
318 }
319
320 // if an event has no kids, but we are still here, it means that it is
321 // an association event (because otherwise it would be filtered out).
322 kids = childrenForEvent(e);
323 if (kids == null) {
324 kids = NSArray.EmptyArray;
325 }
326 n = kids.count();
327 if (n!=0) {
328 for (i = 0, sum = 0; i < n; i++) {
329 sum += ((EOEvent)kids.objectAtIndex(i)).duration();
330 }
331 } else {
332 sum = e.duration();
333 }
334
335 return sum;
336 }
337
338 public void _cacheWebEofEvents()
339 {
340 if (webEvents != null)
341 return;
342
343 NSArray allCenters = EOEventCenter.allEventsForAllCenters();
344 int halfCount = allCenters.count() / 2;
345 webEvents = new NSMutableArray(halfCount);
346 eofEvents = new NSMutableArray(halfCount);
347
348 Enumeration anEnumerator = allCenters.objectEnumerator();
349 while (anEnumerator.hasMoreElements()) {
350 EOEvent e = (EOEvent) anEnumerator.nextElement();
351 if (e instanceof WOEvent)
352 webEvents.addObject(e);
353 else eofEvents.addObject(e);
354 }
355 }
356
357 public int webEventDuration()
358 {
359 int i, n, time;
360
361 _cacheWebEofEvents();
362
363 n = (webEvents != null) ? webEvents.count() : 0;
364
365 for (i = 0, time = 0; i < n; i++) {
366 EOEvent e = (EOEvent) webEvents.objectAtIndex(i);
367 if (e instanceof WOEvent)
368 time = time + (int) e.durationWithoutSubevents();
369 }
370
371 return time;
372 }
373
374 public int eofEventDuration()
375 {
376 int i, n, time;
377
378 _cacheWebEofEvents();
379
380 n = (eofEvents != null) ? eofEvents.count() : 0;
381 for (i = 0, time = 0; i < n; i++) {
382 EOEvent e = (EOEvent) eofEvents.objectAtIndex(i);
383 if (!(e instanceof WOEvent))
384 time = time + (int) e.durationWithoutSubevents();
385 }
386
387 return time;
388 }
389
390 public int webEventCount() {
391 _cacheWebEofEvents();
392 return (webEvents != null) ? webEvents.count() : 0;
393 }
394
395 public int eofEventCount() {
396 _cacheWebEofEvents();
397 return (eofEvents != null) ? eofEvents.count() : 0;
398 }
399
400 public boolean isEmpty() {
401 return eventCount() == 0;
402 }
403 }