1 2 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 ; 31 import java.util.*; 32 import java.util.List ; 33 import java.awt.Component ; 34 35 import javax.swing.JFrame ; 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 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 frame = null; 59 private Browser browser = null; 60 61 66 public DataView(final GUI gui, final Target target) { 68 this.target = target; 69 setType("view"); 70 setParent(gui, target); 71 } 72 73 78 protected void initialize(final GUI gui) { 79 super.initialize(gui); 80 if (gui != null) setImageOscillo(); 81 } 82 83 88 protected Browser getBrowser() { 89 return browser; 90 } 91 92 97 public Target getTarget() { 99 return target; 100 } 101 102 107 public String getTitle() { 109 return "DataView"; 110 } 111 112 117 protected abstract String browserName(); 118 119 124 protected abstract String browserUnit(); 125 126 131 protected abstract Class browserEventClass(); 132 133 138 private String htmlFace(final String html) { 139 return getGUI().htmlFace(html); 140 } 141 142 147 protected StringBuffer getBrowserContent() { 148 final StringBuffer content = new StringBuffer (); 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 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 (htmlFace(content.toString())); 188 } 189 190 197 private StringBuffer analyzeEvents(List <EventGeneric> events, final Date begin, final Date end) { 198 final StringBuffer content = new StringBuffer (); 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 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 260 protected abstract BasicComponent createComponent(); 261 262 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 284 final public void informSelected() { 287 final CTabFolder folder = getGUI().getTabFolder(); 288 289 synchronized (folder) { 291 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 tab_item = new CTabItem(folder, SWT.CLOSE); 303 tab_item.setText(getTitle()); 304 folder.setSelection(tab_item); 305 306 browser = new Browser(getGUI().getTabFolder(), SWT.BORDER | SWT.FILL); 308 tab_item.setControl(browser); 309 310 updateBrowserContent(); 311 } 312 313 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 ex) { 324 log.info("Exception", ex); 325 } catch (final InvocationTargetException ex) { 326 log.warn("Exception", ex); 327 } 328 } 329 } 330 } 331 332 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 |