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 com.mindtree.techworks.insight;
25
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Map;
31
32 import com.mindtree.techworks.insight.gui.Insight;
33 import com.mindtree.techworks.insight.gui.Presentation;
34 import com.mindtree.techworks.insight.gui.action.IAction;
35 import com.mindtree.techworks.insight.gui.widgets.StatusBar;
36 import com.mindtree.techworks.insight.model.ILogEventModelMutator;
37 import com.mindtree.techworks.insight.model.IMutatorListener;
38 import com.mindtree.techworks.insight.model.LogEventModel;
39 import com.mindtree.techworks.insight.pagination.IPage;
40 import com.mindtree.techworks.insight.spi.LogEvent;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 public class Controller implements IMutatorListener {
57
58
59
60
61 private Insight insight;
62
63
64
65
66
67 private Map presentationMap = new HashMap();
68
69
70
71
72 private List presentationList = new ArrayList();
73
74
75
76
77 private List actionsList = new ArrayList();
78
79
80
81
82 private LogEventModel rootLogEventModel;
83
84
85
86
87 private LogEventModel currentLogEventModel;
88
89
90
91
92 private int mutatorType;
93
94
95
96
97 private boolean scrollLock = false;
98
99
100
101
102
103 public Controller(Insight insight) {
104 this.insight = insight;
105 }
106
107
108
109
110
111 public void registerPresentation(Presentation presentation) {
112 presentationList.add(presentation);
113 }
114
115
116
117
118
119 public void registerAction(IAction action) {
120 actionsList.add(action);
121 action.setEnabled(evaluateActionEnable(IAction.STARTUP, action));
122 }
123
124
125
126
127
128
129
130 public Presentation getPresentation(String className) {
131 Iterator iterator = presentationList.iterator();
132 while(iterator.hasNext()) {
133 Presentation presentation = (Presentation)iterator.next();
134 if (presentation.getClass().getName().equals(className)) {
135 return presentation;
136 }
137 }
138 throw new RuntimeException("No registered Presentation with name : " + className);
139 }
140
141
142
143
144
145
146
147 public void registerWidgetChangeListener(Presentation sourcePresentation, Presentation listener) {
148 List listenerList = (List)presentationMap.get(sourcePresentation.getUID());
149 if (listenerList == null) {
150 listenerList = new ArrayList();
151 presentationMap.put(sourcePresentation.getUID(),listenerList);
152 }
153 listenerList.add(listener);
154 }
155
156
157
158
159
160
161
162 public void fireWidgetChanged(Presentation sourcePresentation, int widgetId, Object data) {
163 List listenerList = (List)presentationMap.get(sourcePresentation.getUID());
164 Iterator iterator = listenerList.iterator();
165 while(iterator.hasNext()) {
166 ((Presentation)iterator.next()).notifyWidgetStateChange(sourcePresentation, widgetId, data);
167 }
168 }
169
170
171
172
173
174
175
176 public void notifyLogEventOccurred(LogEvent event) {
177 Iterator iterator = presentationList.iterator();
178 while(iterator.hasNext()) {
179 Presentation p = (Presentation)iterator.next();
180 if (p.doesProcessRealTimeUpdates()) {
181 p.processRealTimeUpdate(event);
182 }
183 }
184 setStatus();
185 }
186
187
188
189
190
191 public void resetPresentations() {
192 Iterator iterator = presentationList.iterator();
193 while(iterator.hasNext()) {
194 Presentation p = (Presentation)iterator.next();
195 p.resetWidgets();
196 }
197 }
198
199
200
201 public Insight getInsight() {
202 return insight;
203 }
204
205
206
207
208 public LogEventModel getCurrentLogEventModel() {
209 return this.currentLogEventModel;
210 }
211
212
213
214
215
216
217
218 public void setCurrentLogEventModel(LogEventModel logEventModel, long pageNumber, long selectedEventIndex) {
219 if(!logEventModel.equals(rootLogEventModel)){
220 logEventModel.setPreviousLogEventModel(this.currentLogEventModel);
221 }
222 logEventModel.clearSearchResults();
223 this.currentLogEventModel = logEventModel;
224 setSelectedEvent(pageNumber, selectedEventIndex);
225 }
226
227
228
229
230
231
232 public void setSelectedEvent(long pageNumber, long selectedEventIndex) {
233 this.currentLogEventModel.getPageSet().loadPageByNumber(pageNumber);
234 firePageChanged(selectedEventIndex);
235 }
236
237
238
239
240
241
242 public void setCurrentLogEventModel(LogEventModel logEventModel) {
243
244
245 setCurrentLogEventModel(logEventModel, 1, -1);
246 }
247
248
249
250
251
252 public void clearCurrentModel(){
253 resetPresentations();
254 getCurrentLogEventModel().clear();
255 if(getCurrentLogEventModel().getPreviousLogEventModel() != null){
256 this.currentLogEventModel = getCurrentLogEventModel().getPreviousLogEventModel();
257 this.currentLogEventModel.clearSearchResults();
258
259 this.currentLogEventModel.getPageSet().loadPageByNumber(1);
260 firePageChanged();
261 } else {
262
263 actionPerformed(IAction.STARTUP);
264 }
265 }
266
267
268
269
270 public LogEventModel getRootLogEventModel() {
271 return rootLogEventModel;
272 }
273
274
275
276
277 public void setRootLogEventModel(LogEventModel rootLogEventModel) {
278 this.rootLogEventModel = rootLogEventModel;
279
280 this.currentLogEventModel = rootLogEventModel;
281 this.currentLogEventModel.clearSearchResults();
282 }
283
284
285
286
287
288 public void firePageChanged() {
289 IPage page = this.currentLogEventModel.getPageSet().getCurrentPage();
290 Iterator iterator = presentationList.iterator();
291 while(iterator.hasNext()) {
292 Presentation p = (Presentation)iterator.next();
293 p.displayPage(page, -1);
294 }
295 setStatus();
296 }
297
298
299
300
301
302
303
304 public void firePageChanged(long selectedEventIndex) {
305 IPage page = this.currentLogEventModel.getPageSet().getCurrentPage();
306 Iterator iterator = presentationList.iterator();
307 while(iterator.hasNext()) {
308 Presentation p = (Presentation)iterator.next();
309 p.displayPage(page, selectedEventIndex);
310 }
311 setStatus();
312 }
313
314
315
316
317 public void setStatus(){
318 StatusBar.getInstance().setStatisticsDisplayText(this.currentLogEventModel.getStatus());
319 }
320
321
322
323
324
325
326 public void startMutating (int mutatorType) {
327 this.mutatorType = mutatorType;
328 actionPerformed(IAction.MUTATION);
329 }
330
331
332
333
334
335
336 public void endMutating (int infoFlag) {
337
338 if (infoFlag == ILogEventModelMutator.SUCCESS) {
339 actionPerformed(IAction.NO_CONSTRAINTS);
340 } else {
341
342 if (this.currentLogEventModel.getPreviousLogEventModel() != null) {
343 setCurrentLogEventModel(this.currentLogEventModel.getPreviousLogEventModel());
344 actionPerformed(IAction.NO_CONSTRAINTS);
345 } else {
346 if (this.currentLogEventModel.getPageSet().getLastPage().getLogEventList().size() > 0) {
347
348 actionPerformed(IAction.NO_CONSTRAINTS);
349 } else {
350
351 actionPerformed(IAction.STARTUP);
352 }
353 }
354 }
355 }
356
357
358
359
360
361
362
363
364 public void actionPerformed(int actionType) {
365 Iterator iterator = actionsList.iterator();
366 while(iterator.hasNext()) {
367 IAction action = (IAction)iterator.next();
368 action.setEnabled(evaluateActionEnable(actionType, action));
369 }
370 }
371
372
373
374
375
376
377
378
379
380 private boolean evaluateActionEnable(int actionType, IAction action) {
381 boolean enable = false;
382 int mutualExclusionType = IAction.NONE;
383 for (int i = 0; i < IAction.MUTUAL_EXCLUSION_TYPES.length; i++) {
384 if (IAction.MUTUAL_EXCLUSION_TYPES[i][0] == action.getType()) {
385 mutualExclusionType = IAction.MUTUAL_EXCLUSION_TYPES[i][1];
386 break;
387 }
388 }
389 if (((action.getType() & actionType) == actionType) &&
390 ((action.getType() & actionType) != mutualExclusionType)){
391 enable = true;
392 }
393 return enable;
394 }
395
396
397
398
399
400 public int getMutatorType() {
401 return mutatorType;
402 }
403
404
405
406
407
408 public void setScrollLock(boolean lock) {
409 this.scrollLock = lock;
410 this.currentLogEventModel.getPageSet().setMutatorType(this.scrollLock ?
411 ILogEventModelMutator.NON_TAILING_MUTATOR : ILogEventModelMutator.TAILING_MUTATOR);
412 Iterator iterator = presentationList.iterator();
413 while(iterator.hasNext()) {
414 Presentation p = (Presentation)iterator.next();
415 p.setScrollLock(this.scrollLock);
416 }
417 }
418
419
420
421
422
423 public boolean isScrollLock() {
424 return scrollLock;
425 }
426 }