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: * NmapView displays output text produced by Nmap.
047: * @author Alexandre Fenyo
048: * @version $Id: NmapView.java,v 1.2 2007/03/12 05:04:15 fenyo Exp $
049: */
050:
051: public class NmapView 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 Browser browser = null;
058:
059: /**
060: * Constructor.
061: * @param gui current GUI instance.
062: * @param target ingress target interface.
063: */
064: // GUI thread
065: public NmapView(final GUI gui, final Target target) {
066: this .target = target;
067: setType("view");
068: setItem("nmap signature");
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: setImageMultiRow();
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 "NmapView";
110: }
111:
112: /**
113: * Embed face informations in an HTML part.
114: * @param html source part.
115: * @return String embedded html part.
116: */
117: private String htmlFace(final String html) {
118: return getGUI().htmlFace(html);
119: }
120:
121: /**
122: * Generates the Nmap report.
123: * @param none.
124: * @return StringBuffer Nmap report.
125: */
126: protected StringBuffer getBrowserContent() {
127: final StringBuffer content = new StringBuffer();
128:
129: content
130: .append("Nmap output:<BR/><TABLE BORDER='0' BGCOLOR='black' cellspacing='1' cellpadding='1'><TR><TD bgcolor='lightyellow'>"
131: + htmlFace("<PRE>"
132: + ((EventNmap) getTarget()
133: .getLastEvent(EventNmap.class))
134: .getOutput() + "</PRE>")
135: + "</TD></TR></TABLE");
136:
137: return new StringBuffer(htmlFace(content.toString()));
138: }
139:
140: /**
141: * Computes a new version of the report.
142: * @param none.
143: * @return void.
144: */
145: private void updateBrowserContent() {
146: browser.setText("<html><body bgcolor='#"
147: + String.format("%2x%2x%2x", getGUI()
148: .getBackgroundColor().getRed(), getGUI()
149: .getBackgroundColor().getGreen(), getGUI()
150: .getBackgroundColor().getBlue()) + "'><small>"
151: + getBrowserContent() + "</small></body></html>");
152: }
153:
154: /**
155: * Called when the user wants this NmapView instance to create a CTabFolder instance
156: * containing a report.
157: * @param none.
158: * @return void.
159: */
160: // this method must only be called from the SWT thread
161: // GUI thread
162: final public void informSelected() {
163: final CTabFolder folder = getGUI().getTabFolder();
164:
165: // ce synchr n'est pas forcément utile
166: synchronized (folder) {
167: // create a tab item
168:
169: boolean tab_item_found = false;
170: for (final CTabItem tab_item : folder.getItems())
171: if (tab_item == this .tab_item) {
172: folder.setSelection(tab_item);
173: tab_item_found = true;
174: updateBrowserContent();
175: }
176: if (tab_item_found == false) {
177: // create a new tab item
178: tab_item = new CTabItem(folder, SWT.CLOSE);
179: tab_item.setText(getTitle());
180: folder.setSelection(tab_item);
181:
182: // create a control inside the tab item
183: browser = new Browser(getGUI().getTabFolder(),
184: SWT.BORDER | SWT.FILL);
185: tab_item.setControl(browser);
186:
187: updateBrowserContent();
188: }
189:
190: }
191: }
192:
193: /**
194: * Removes objects associated with this DataView instance.
195: * @param none.
196: * @return void.
197: */
198: protected void disposed() {
199: super .disposed();
200: if (tab_item != null)
201: tab_item.dispose();
202: getTarget().removeEvents(EventNmap.class);
203: }
204: }
|