KickJava   Java API By Example, From Geeks To Geeks.

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


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  * NmapView displays output text produced by Nmap.
48  * @author Alexandre Fenyo
49  * @version $Id: NmapView.java,v 1.2 2007/03/12 05:04:15 fenyo Exp $
50  */

51
52 public class NmapView 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 Browser browser = null;
59
60   /**
61    * Constructor.
62    * @param gui current GUI instance.
63    * @param target ingress target interface.
64    */

65   // GUI thread
66
public NmapView(final GUI gui, final Target target) {
67     this.target = target;
68     setType("view");
69     setItem("nmap signature");
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) setImageMultiRow();
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 "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 JavaDoc htmlFace(final String JavaDoc 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 JavaDoc getBrowserContent() {
127     final StringBuffer JavaDoc content = new StringBuffer JavaDoc();
128
129    content.append("Nmap output:<BR/><TABLE BORDER='0' BGCOLOR='black' cellspacing='1' cellpadding='1'><TR><TD bgcolor='lightyellow'>" +
130        htmlFace("<PRE>" + ((EventNmap) getTarget().getLastEvent(EventNmap.class)).getOutput() + "</PRE>") +
131        "</TD></TR></TABLE");
132
133     return new StringBuffer JavaDoc(htmlFace(content.toString()));
134   }
135
136   /**
137    * Computes a new version of the report.
138    * @param none.
139    * @return void.
140    */

141   private void updateBrowserContent() {
142     browser.setText("<html><body bgcolor='#" +
143         String.format("%2x%2x%2x",
144             getGUI().getBackgroundColor().getRed(),
145             getGUI().getBackgroundColor().getGreen(),
146             getGUI().getBackgroundColor().getBlue()) +
147             "'><small>" +
148             getBrowserContent() +
149             "</small></body></html>");
150   }
151
152   /**
153    * Called when the user wants this NmapView instance to create a CTabFolder instance
154    * containing a report.
155    * @param none.
156    * @return void.
157    */

158   // this method must only be called from the SWT thread
159
// GUI thread
160
final public void informSelected() {
161     final CTabFolder folder = getGUI().getTabFolder();
162
163     // ce synchr n'est pas forcément utile
164
synchronized (folder) {
165       // create a tab item
166

167       boolean tab_item_found = false;
168       for (final CTabItem tab_item : folder.getItems())
169         if (tab_item == this.tab_item) {
170           folder.setSelection(tab_item);
171           tab_item_found = true;
172           updateBrowserContent();
173         }
174       if (tab_item_found == false) {
175         // create a new tab item
176
tab_item = new CTabItem(folder, SWT.CLOSE);
177         tab_item.setText(getTitle());
178         folder.setSelection(tab_item);
179
180         // create a control inside the tab item
181
browser = new Browser(getGUI().getTabFolder(), SWT.BORDER | SWT.FILL);
182         tab_item.setControl(browser);
183
184         updateBrowserContent();
185       }
186
187     }
188   }
189
190   /**
191    * Removes objects associated with this DataView instance.
192    * @param none.
193    * @return void.
194    */

195   protected void disposed() {
196     super.disposed();
197     if (tab_item != null) tab_item.dispose();
198     getTarget().removeEvents(EventNmap.class);
199   }
200 }
201
Popular Tags