001: /*
002: * GNetWatch
003: * Copyright 2006, 2007 Alexandre Fenyo
004: * gnetwatch@fenyo.net
005: *
006: * This file is part of GNetWatch.
007: *
008: * GNetWatch is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License as published by
010: * the Free Software Foundation; either version 2 of the License, or
011: * (at your option) any later version.
012: *
013: * GNetWatch is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with GNetWatch; if not, write to the Free Software
020: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
021: */
022:
023: package net.fenyo.gnetwatch.data;
024:
025: import net.fenyo.gnetwatch.*;
026: import net.fenyo.gnetwatch.GUI.*;
027: import net.fenyo.gnetwatch.targets.*;
028:
029: import java.lang.reflect.InvocationTargetException;
030: import java.util.*;
031: import java.util.List;
032: import java.awt.Component;
033:
034: import javax.swing.JFrame;
035:
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038: import org.eclipse.swt.*;
039: import org.eclipse.swt.events.*;
040: import org.eclipse.swt.graphics.*;
041: import org.eclipse.swt.custom.*;
042: import org.eclipse.swt.widgets.*;
043: import org.eclipse.swt.browser.*;
044:
045: /**
046: * DataView is the base class to display time series.
047: * @author Alexandre Fenyo
048: * @version $Id: DataView.java,v 1.25 2007/03/12 05:04:15 fenyo Exp $
049: */
050:
051: public abstract class DataView extends VisualElement {
052: private static Log log = LogFactory.getLog(DataView.class);
053:
054: final private Target target;
055: private CTabItem tab_item = null;
056:
057: private JFrame frame = null;
058: private Browser browser = null;
059:
060: /**
061: * Constructor.
062: * @param gui current GUI instance.
063: * @param target ingress target interface.
064: */
065: // GUI thread
066: public DataView(final GUI gui, final Target target) {
067: this .target = target;
068: setType("view");
069: setParent(gui, target);
070: }
071:
072: /**
073: * Sets the current GUI instance.
074: * @param gui current GUI instance.
075: * @return void.
076: */
077: protected void initialize(final GUI gui) {
078: super .initialize(gui);
079: if (gui != null)
080: setImageOscillo();
081: }
082:
083: /**
084: * Returns the SWT browser.
085: * @param none.
086: * @return Browser SWT browser.
087: */
088: protected Browser getBrowser() {
089: return browser;
090: }
091:
092: /**
093: * Returns the target this view works on.
094: * @param none.
095: * @return Target target this view works on.
096: */
097: // GUI thread
098: public Target getTarget() {
099: 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 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 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 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 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 htmlFace(final String 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 getBrowserContent() {
148: final StringBuffer content = new StringBuffer();
149:
150: content.append("<B>"
151: + getGUI().getConfig().getPattern("report_browser",
152: browserName()) + "</B><HR/>");
153: content.append(getGUI().getConfig().getPattern("target_name",
154: getTarget().getName())
155: + "<BR/>");
156: content.append(getGUI().getConfig().getPattern("target_type",
157: getTarget().getClass().toString())
158: + "<BR/>");
159:
160: if (TargetIPv4.class.isInstance(getTarget())) {
161: final TargetIPv4 _target = (TargetIPv4) getTarget();
162: content.append(getGUI().getConfig().getPattern(
163: "address_name",
164: _target.getAddress().toString().substring(1))
165: + "<BR/>");
166: }
167:
168: if (TargetIPv6.class.isInstance(getTarget())) {
169: final TargetIPv6 _target = (TargetIPv6) getTarget();
170: content.append(getGUI().getConfig().getPattern(
171: "address_name",
172: _target.getAddress().toString().substring(1))
173: + "<BR/>");
174: }
175:
176: Date begin, end;
177:
178: content.append("<HR/><B>"
179: + getGUI().getConfig().getString("every_event")
180: + "</B><BR/>");
181: // should use <? extends EventGeneric>
182: end = new Date();
183: begin = new Date(0);
184: content.append(analyzeEvents(getTarget().getEvents(begin, end,
185: browserEventClass()), begin, end));
186:
187: content.append("<HR/><B>"
188: + getGUI().getConfig().getString("last_30_sec")
189: + "</B><BR/>");
190: end = new Date();
191: begin = new Date(end.getTime() - 30000);
192: content.append(analyzeEvents(getTarget().getEvents(begin, end,
193: browserEventClass()), begin, end));
194:
195: content.append("<HR/><B>"
196: + getGUI().getConfig().getString("last_5_min")
197: + "</B><BR/>");
198: end = new Date();
199: begin = new Date(new Date().getTime() - 300000);
200: content.append(analyzeEvents(getTarget().getEvents(begin, end,
201: browserEventClass()), begin, end));
202:
203: content.append("<HR/><B>"
204: + getGUI().getConfig().getString("last_1_hour")
205: + "</B><BR/>");
206: end = new Date();
207: begin = new Date(new Date().getTime() - 3600000);
208: content.append(analyzeEvents(getTarget().getEvents(begin, end,
209: browserEventClass()), begin, end));
210:
211: return new StringBuffer(htmlFace(content.toString()));
212: }
213:
214: /**
215: * Returns a report about a specific period of time.
216: * @param events every event managed by this view and concerning the associated target.
217: * @param begin start time.
218: * @param end end time.
219: * @return String HTML report.
220: */
221: private StringBuffer analyzeEvents(List<EventGeneric> events,
222: final Date begin, final Date end) {
223: final StringBuffer content = new StringBuffer();
224:
225: if (events.size() > 0
226: && events.get(events.size() - 1).getDate().after(end))
227: events.remove(events.size() - 1);
228:
229: if (events.size() > 0 && events.get(0).getDate().before(begin))
230: events.remove(0);
231:
232: if (events.size() > 0)
233: content
234: .append("<B>"
235: + events.size()
236: + " "
237: + getGUI().getConfig().getString("event")
238: + (events.size() > 1 ? "s" : "")
239: + "</B><BR/><TABLE BORDER='0' BGCOLOR='black' cellspacing='1' cellpadding='1'><TR><TD bgcolor='lightyellow'>"
240: + htmlFace(getGUI().getConfig().getString(
241: "first_event"))
242: + "</TD><TD bgcolor='lightyellow' ALIGN='right'>"
243: + htmlFace(events.get(0).getDate()
244: .toString())
245: + "</TD></TR><TR><TD bgcolor='lightyellow'>"
246: + htmlFace(getGUI().getConfig().getString(
247: "last_event"))
248: + "</TD><TD bgcolor='lightyellow' ALIGN='right'>"
249: + htmlFace(events.get(events.size() - 1)
250: .getDate().toString())
251: + "</TD></TR>");
252: else {
253: content.append(getGUI().getConfig().getString("no_event")
254: + "<BR/>");
255: return content;
256: }
257:
258: // if the first event has a null value, we do not take it into account
259:
260: int min_value, max_value = 0;
261: double average = 0;
262: int i = 0;
263:
264: if (events.size() == 1)
265: min_value = events.get(0).getIntValue(events, 0);
266: else {
267: if (events.get(0).getIntValue(events, 0) == 0)
268: min_value = events.get(1).getIntValue(events, 1);
269: else
270: min_value = events.get(0).getIntValue(events, 0);
271: }
272:
273: for (final EventGeneric event : events) {
274: final int value = event.getIntValue(events, i);
275: if (value > max_value)
276: max_value = value;
277: if ((i == 0 && value != 0 || i != 0) && value < min_value)
278: min_value = value;
279: average += value;
280: i++;
281: }
282:
283: content.append("<TR><TD bgcolor='lightyellow'>"
284: + htmlFace(getGUI().getConfig().getString("min_value"))
285: + "</TD><TD bgcolor='lightyellow' ALIGN='right'>"
286: + htmlFace(min_value + " " + browserUnit())
287: + "</TD></TR>");
288: content.append("<TR><TD bgcolor='lightyellow'>"
289: + htmlFace(getGUI().getConfig().getString("max_value"))
290: + "</TD><TD bgcolor='lightyellow' ALIGN='right'>"
291: + htmlFace(max_value + " " + browserUnit())
292: + "</TD></TR>");
293:
294: if (events.size() != 0) {
295: average = average
296: / (events.size() - (events.get(0).getIntValue(
297: events, 0) != 0 ? 0 : 1));
298: content.append("<TR><TD bgcolor='lightyellow'>"
299: + htmlFace(getGUI().getConfig()
300: .getString("average"))
301: + "</TD><TD bgcolor='lightyellow' ALIGN='right'>"
302: + htmlFace((int) average + " " + browserUnit())
303: + "</TD></TR>");
304: }
305:
306: content.append("</TABLE><BR/>");
307:
308: return content;
309: }
310:
311: /**
312: * Creates a new display component.
313: * @param none.
314: * @return BasicComponent new display component.
315: */
316: protected abstract BasicComponent createComponent();
317:
318: /**
319: * Computes a new version of the report.
320: * @param none.
321: * @return void.
322: */
323: private void updateBrowserContent() {
324: browser.setText("<html><body bgcolor='#"
325: + String.format("%2x%2x%2x", getGUI()
326: .getBackgroundColor().getRed(), getGUI()
327: .getBackgroundColor().getGreen(), getGUI()
328: .getBackgroundColor().getBlue()) + "'><small>"
329: + getBrowserContent() + "</small></body></html>");
330: }
331:
332: /**
333: * Called when the user wants this DataView instance to create a CTabFolder instance
334: * containing a report.
335: * @param none.
336: * @return void.
337: */
338: // this method must only be called from the SWT thread
339: // GUI thread
340: final public void informSelected() {
341: final CTabFolder folder = getGUI().getTabFolder();
342:
343: // ce synchr n'est pas forcément utile
344: synchronized (folder) {
345: // create a tab item
346:
347: boolean tab_item_found = false;
348: for (final CTabItem tab_item : folder.getItems())
349: if (tab_item == this .tab_item) {
350: folder.setSelection(tab_item);
351: tab_item_found = true;
352: updateBrowserContent();
353: }
354: if (tab_item_found == false) {
355: // create a new tab item
356: tab_item = new CTabItem(folder, SWT.CLOSE);
357: tab_item.setText(getTitle());
358: folder.setSelection(tab_item);
359:
360: // create a control inside the tab item
361: browser = new Browser(getGUI().getTabFolder(),
362: SWT.BORDER | SWT.FILL);
363: tab_item.setControl(browser);
364:
365: updateBrowserContent();
366: }
367:
368: // create a drawing window
369:
370: if (frame != null && frame.isDisplayable()) {
371: frame.setVisible(true);
372: frame.toFront();
373: } else {
374: final BasicComponent component = createComponent();
375: try {
376: component.init();
377: frame = getGUI().getAwtGUI().createFrame(
378: getTarget().toString(), component);
379: } catch (final InterruptedException ex) {
380: log.info("Exception", ex);
381: } catch (final InvocationTargetException ex) {
382: log.warn("Exception", ex);
383: }
384: }
385: }
386: }
387:
388: /**
389: * Removes objects associated with this DataView instance.
390: * @param none.
391: * @return void.
392: */
393: protected void disposed() {
394: super.disposed();
395: if (frame != null)
396: getGUI().getAwtGUI().dropFrame(frame);
397: if (tab_item != null)
398: tab_item.dispose();
399: }
400: }
|