KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > fenyo > gnetwatch > data > DataView


1
2 /*
3  * GNetWatch
4  * Copyright 2006, 2007 Alexandre Fenyo
5  * gnetwatch@fenyo.net
6  *
7  * This file is part of GNetWatch.
8  *
9  * GNetWatch is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * GNetWatch is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with GNetWatch; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22  */

23
24 package net.fenyo.gnetwatch.data;
25
26 import net.fenyo.gnetwatch.*;
27 import net.fenyo.gnetwatch.GUI.*;
28 import net.fenyo.gnetwatch.targets.*;
29
30 import java.lang.reflect.InvocationTargetException JavaDoc;
31 import java.util.*;
32 import java.util.List JavaDoc;
33 import java.awt.Component JavaDoc;
34
35 import javax.swing.JFrame JavaDoc;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.eclipse.swt.*;
40 import org.eclipse.swt.events.*;
41 import org.eclipse.swt.graphics.*;
42 import org.eclipse.swt.custom.*;
43 import org.eclipse.swt.widgets.*;
44 import org.eclipse.swt.browser.*;
45
46 /**
47  * DataView is the base class to display time series.
48  * @author Alexandre Fenyo
49  * @version $Id: DataView.java,v 1.25 2007/03/12 05:04:15 fenyo Exp $
50  */

51
52 public abstract class DataView extends VisualElement {
53   private static Log log = LogFactory.getLog(DataView.class);
54
55   final private Target target;
56   private CTabItem tab_item = null;
57
58   private JFrame JavaDoc frame = null;
59   private Browser browser = null;
60
61   /**
62    * Constructor.
63    * @param gui current GUI instance.
64    * @param target ingress target interface.
65    */

66   // GUI thread
67
public DataView(final GUI gui, final Target target) {
68     this.target = target;
69     setType("view");
70     setParent(gui, target);
71   }
72
73   /**
74    * Sets the current GUI instance.
75    * @param gui current GUI instance.
76    * @return void.
77    */

78   protected void initialize(final GUI gui) {
79     super.initialize(gui);
80     if (gui != null) setImageOscillo();
81   }
82
83   /**
84    * Returns the SWT browser.
85    * @param none.
86    * @return Browser SWT browser.
87    */

88   protected Browser getBrowser() {
89     return browser;
90   }
91
92   /**
93    * Returns the target this view works on.
94    * @param none.
95    * @return Target target this view works on.
96    */

97   // GUI thread
98
public Target getTarget() {
99     return target;
100   }
101
102   /**
103    * Returns the title of the view.
104    * @param none.
105    * @return String title view.
106    */

107   // GUI thread
108
public String JavaDoc getTitle() {
109     return "DataView";
110   }
111
112   /**
113    * Returns the name of report this view can generate.
114    * @param none.
115    * @return String report name.
116    */

117   protected abstract String JavaDoc browserName();
118
119   /**
120    * Returns the data unit for values this view can generate.
121    * @param none.
122    * @return String data unit.
123    */

124   protected abstract String JavaDoc browserUnit();
125
126   /**
127    * Returns the event class this view can manage.
128    * @param none.
129    * @return Class event class.
130    */

131   protected abstract Class JavaDoc browserEventClass();
132
133   /**
134    * Embed face informations in an HTML part.
135    * @param html source part.
136    * @return String embedded html part.
137    */

138   private String JavaDoc htmlFace(final String JavaDoc html) {
139     return getGUI().htmlFace(html);
140   }
141
142   /**
143    * Generates an HTML report.
144    * @param none.
145    * @return StringBuffer HTML report.
146    */

147   protected StringBuffer JavaDoc getBrowserContent() {
148     final StringBuffer JavaDoc content = new StringBuffer JavaDoc();
149
150     content.append("<B>" + getGUI().getConfig().getPattern("report_browser", browserName()) + "</B><HR/>");
151     content.append(getGUI().getConfig().getPattern("target_name", getTarget().getName()) + "<BR/>");
152     content.append(getGUI().getConfig().getPattern("target_type", getTarget().getClass().toString()) + "<BR/>");
153
154     if (TargetIPv4.class.isInstance(getTarget())) {
155       final TargetIPv4 _target = (TargetIPv4) getTarget();
156       content.append(getGUI().getConfig().getPattern("address_name", _target.getAddress().toString().substring(1)) + "<BR/>");
157     }
158
159     if (TargetIPv6.class.isInstance(getTarget())) {
160       final TargetIPv6 _target = (TargetIPv6) getTarget();
161       content.append(getGUI().getConfig().getPattern("address_name", _target.getAddress().toString().substring(1)) + "<BR/>");
162     }
163
164     Date begin, end;
165
166     content.append("<HR/><B>" + getGUI().getConfig().getString("every_event") + "</B><BR/>");
167     // should use <? extends EventGeneric>
168
end = new Date();
169     begin = new Date(0);
170     content.append(analyzeEvents(getTarget().getEvents(begin, end, browserEventClass()), begin, end));
171
172     content.append("<HR/><B>" + getGUI().getConfig().getString("last_30_sec") + "</B><BR/>");
173     end = new Date();
174     begin = new Date(end.getTime() - 30000);
175     content.append(analyzeEvents(getTarget().getEvents(begin, end, browserEventClass()), begin, end));
176
177     content.append("<HR/><B>" + getGUI().getConfig().getString("last_5_min") + "</B><BR/>");
178     end = new Date();
179     begin = new Date(new Date().getTime() - 300000);
180     content.append(analyzeEvents(getTarget().getEvents(begin, end, browserEventClass()), begin, end));
181
182     content.append("<HR/><B>" + getGUI().getConfig().getString("last_1_hour") + "</B><BR/>");
183     end = new Date();
184     begin = new Date(new Date().getTime() - 3600000);
185     content.append(analyzeEvents(getTarget().getEvents(begin, end, browserEventClass()), begin, end));
186
187     return new StringBuffer JavaDoc(htmlFace(content.toString()));
188   }
189
190   /**
191    * Returns a report about a specific period of time.
192    * @param events every event managed by this view and concerning the associated target.
193    * @param begin start time.
194    * @param end end time.
195    * @return String HTML report.
196    */

197   private StringBuffer JavaDoc analyzeEvents(List JavaDoc<EventGeneric> events, final Date begin, final Date end) {
198     final StringBuffer JavaDoc content = new StringBuffer JavaDoc();
199
200     if (events.size() > 0 && events.get(events.size() - 1).getDate().after(end))
201       events.remove(events.size() - 1);
202
203     if (events.size() > 0 && events.get(0).getDate().before(begin))
204       events.remove(0);
205
206     if (events.size() > 0)
207       content.append("<B>" + events.size() + " " + getGUI().getConfig().getString("event") +
208           (events.size() > 1 ? "s" : "") +
209           "</B><BR/><TABLE BORDER='0' BGCOLOR='black' cellspacing='1' cellpadding='1'><TR><TD bgcolor='lightyellow'>" +
210           htmlFace(getGUI().getConfig().getString("first_event")) +
211           "</TD><TD bgcolor='lightyellow' ALIGN='right'>" +
212           htmlFace(events.get(0).getDate().toString()) +
213           "</TD></TR><TR><TD bgcolor='lightyellow'>" +
214           htmlFace(getGUI().getConfig().getString("last_event")) +
215           "</TD><TD bgcolor='lightyellow' ALIGN='right'>" +
216           htmlFace(events.get(events.size() - 1).getDate().toString()) + "</TD></TR>");
217     else {
218       content.append(getGUI().getConfig().getString("no_event") + "<BR/>");
219       return content;
220     }
221
222     // if the first event has a null value, we do not take it into account
223

224     int min_value, max_value = 0;
225     double average = 0;
226     int i = 0;
227
228     if (events.size() == 1) min_value = events.get(0).getIntValue(events, 0);
229     else {
230       if (events.get(0).getIntValue(events, 0) == 0) min_value = events.get(1).getIntValue(events, 1);
231       else min_value = events.get(0).getIntValue(events, 0);
232     }
233
234     for (final EventGeneric event : events) {
235       final int value = event.getIntValue(events, i);
236       if (value > max_value) max_value = value;
237       if ((i == 0 && value != 0 || i != 0) && value < min_value) min_value = value;
238       average += value;
239       i++;
240     }
241
242     content.append("<TR><TD bgcolor='lightyellow'>" + htmlFace(getGUI().getConfig().getString("min_value")) + "</TD><TD bgcolor='lightyellow' ALIGN='right'>" + htmlFace(min_value + " " + browserUnit()) + "</TD></TR>");
243     content.append("<TR><TD bgcolor='lightyellow'>" + htmlFace(getGUI().getConfig().getString("max_value")) + "</TD><TD bgcolor='lightyellow' ALIGN='right'>" + htmlFace(max_value + " " + browserUnit()) + "</TD></TR>");
244
245     if (events.size() != 0) {
246       average = average / (events.size() - (events.get(0).getIntValue(events, 0) != 0 ? 0 : 1));
247       content.append("<TR><TD bgcolor='lightyellow'>" + htmlFace(getGUI().getConfig().getString("average")) + "</TD><TD bgcolor='lightyellow' ALIGN='right'>" + htmlFace((int) average + " " + browserUnit()) + "</TD></TR>");
248     }
249
250     content.append("</TABLE><BR/>");
251
252     return content;
253   }
254
255   /**
256    * Creates a new display component.
257    * @param none.
258    * @return BasicComponent new display component.
259    */

260   protected abstract BasicComponent createComponent();
261
262   /**
263    * Computes a new version of the report.
264    * @param none.
265    * @return void.
266    */

267   private void updateBrowserContent() {
268     browser.setText("<html><body bgcolor='#" +
269         String.format("%2x%2x%2x",
270             getGUI().getBackgroundColor().getRed(),
271             getGUI().getBackgroundColor().getGreen(),
272             getGUI().getBackgroundColor().getBlue()) +
273             "'><small>" +
274             getBrowserContent() +
275             "</small></body></html>");
276   }
277
278   /**
279    * Called when the user wants this DataView instance to create a CTabFolder instance
280    * containing a report.
281    * @param none.
282    * @return void.
283    */

284   // this method must only be called from the SWT thread
285
// GUI thread
286
final public void informSelected() {
287     final CTabFolder folder = getGUI().getTabFolder();
288
289     // ce synchr n'est pas forcément utile
290
synchronized (folder) {
291       // create a tab item
292

293       boolean tab_item_found = false;
294       for (final CTabItem tab_item : folder.getItems())
295         if (tab_item == this.tab_item) {
296           folder.setSelection(tab_item);
297           tab_item_found = true;
298           updateBrowserContent();
299         }
300       if (tab_item_found == false) {
301         // create a new tab item
302
tab_item = new CTabItem(folder, SWT.CLOSE);
303         tab_item.setText(getTitle());
304         folder.setSelection(tab_item);
305
306         // create a control inside the tab item
307
browser = new Browser(getGUI().getTabFolder(), SWT.BORDER | SWT.FILL);
308         tab_item.setControl(browser);
309
310         updateBrowserContent();
311       }
312
313       // create a drawing window
314

315       if (frame != null && frame.isDisplayable()) {
316         frame.setVisible(true);
317         frame.toFront();
318       } else {
319         final BasicComponent component = createComponent();
320         try {
321           component.init();
322           frame = getGUI().getAwtGUI().createFrame(getTarget().toString(), component);
323         } catch (final InterruptedException JavaDoc ex) {
324           log.info("Exception", ex);
325         } catch (final InvocationTargetException JavaDoc ex) {
326           log.warn("Exception", ex);
327         }
328       }
329     }
330   }
331
332   /**
333    * Removes objects associated with this DataView instance.
334    * @param none.
335    * @return void.
336    */

337   protected void disposed() {
338     super.disposed();
339     if (frame != null) getGUI().getAwtGUI().dropFrame(frame);
340     if (tab_item != null) tab_item.dispose();
341   }
342 }
343
Popular Tags