View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.plugin.openantivirus;
21  
22  import java.awt.GridBagLayout;
23  import java.awt.event.ActionEvent;
24  import java.io.File;
25  
26  import javax.swing.AbstractAction;
27  import javax.swing.Action;
28  import javax.swing.JPopupMenu;
29  import javax.swing.SwingUtilities;
30  
31  import org.apache.log4j.Logger;
32  import org.openantivirus.scanner.RecursiveFileFilterListener;
33  import org.openantivirus.scanner.ScanningAbortedException;
34  import org.openantivirus.scanner.VirusFoundException;
35  import org.xnap.XNap;
36  import org.xnap.gui.ActionProvider;
37  import org.xnap.gui.StatusPanel;
38  import org.xnap.gui.component.XNapMenuItem;
39  import org.xnap.gui.event.PopupListener;
40  import org.xnap.gui.util.IconHelper;
41  
42  public class OAVPanel extends StatusPanel implements ActionProvider
43  {
44  
45      // --- Data Field(s) ---
46  
47      private static Logger logger = Logger.getLogger(OAVPlugin.class);
48  
49      private StartAction acStart = new StartAction();
50      private StopAction acStop = new StopAction();
51  
52      // --- Constructor(s) ---
53  
54      public OAVPanel() 
55      {
56          setLayout(new GridBagLayout());
57  		setIcon(IconHelper.getIcon("khelpcenter.png", 16, false));
58  		setText("OpenAntiVirus");
59  
60  		// menu
61  		JPopupMenu jpm = new JPopupMenu();
62  		jpm.add(new XNapMenuItem(acStart));
63  		jpm.add(new XNapMenuItem(acStop));
64  
65  		addMouseListener(new PopupListener(jpm));
66  
67  		// scanner
68  		if (OAVPlugin.getScannerThread() != null) {
69  			OAVPlugin.getScannerThread().addRecursiveFileFilterListener
70  				(new ScannerListener());
71  		}
72  		else {
73  			acStart.setEnabled(false);
74  			setToolTipText(XNap.tr("Scanner initialization failed"));
75  		}
76      }
77  
78      // --- Method(s) ---
79  
80      public Action[] getActions()
81      {
82  		return new Action[] { acStart, acStop, };
83      }
84  
85      // --- Inner Class(es) ---
86  
87      /***
88       * Starts the scanner.
89       */
90      private class StartAction extends AbstractAction {
91  
92          public StartAction() 
93  		{
94              putValue(Action.NAME, XNap.tr("Start"));
95              putValue(Action.SHORT_DESCRIPTION,
96  					 XNap.tr("Starts the virus search."));
97  			putValue(IconHelper.XNAP_ICON, "noatunplay.png");
98          }
99  
100         public void actionPerformed(ActionEvent event) 
101 		{
102 			this.setEnabled(false);
103 			OAVPlugin.getScannerThread().startScanning();
104 		}
105 
106     } 
107 
108     /***
109      * Stops from the network.
110      */
111     private class StopAction extends AbstractAction {
112 
113         public StopAction()
114 		{
115             putValue(Action.NAME, XNap.tr("Stop"));
116             putValue(Action.SHORT_DESCRIPTION,
117 					 XNap.tr("Stops the virus search."));
118 			putValue(IconHelper.XNAP_ICON, "noatunstop.png");
119 
120 			this.setEnabled(false);
121         }
122 
123         public void actionPerformed(ActionEvent event) 
124 		{
125 			this.setEnabled(false);
126 			OAVPlugin.getScannerThread().stopScanning();
127         }
128 
129     } 
130 
131 	public class ScannerListener implements RecursiveFileFilterListener 
132 	{
133 
134 		public void startingScan() 
135 		{
136 			Runnable runner = new Runnable() 
137 				{
138 					public void run()
139 					{
140 						acStop.setEnabled(true);
141 						setText(XNap.tr("Scanning") + "...");
142 						setToolTipText(null);
143 					}
144 				};
145 			SwingUtilities.invokeLater(runner);
146         }
147         
148         public void scanning(final File file) throws ScanningAbortedException 
149 		{
150 			Runnable runner = new Runnable() 
151 				{
152 					public void run()
153 					{
154 						setToolTipText("Scanning " + file.getAbsolutePath());
155 					}
156 				};
157 			SwingUtilities.invokeLater(runner);
158         }
159         
160         public void virusFound(VirusFoundException e) 
161         {
162 			logger.warn("virus found", e);
163 			Runnable runner = new Runnable() 
164 				{
165 					public void run()
166 					{
167 						setText(XNap.tr("Virus found!"));
168 					}
169 				};
170 			SwingUtilities.invokeLater(runner);
171         }
172         
173         public void finishedScan() 
174 		{
175 			Runnable runner = new Runnable() 
176 				{
177 					public void run()
178 					{
179 						acStop.setEnabled(false);
180 						acStart.setEnabled(true);
181 						setText("OpenAntiVirus");
182 						setToolTipText("Finished scan");
183 					}
184 				};
185 			SwingUtilities.invokeLater(runner);
186         }
187         
188         public void exceptionThrown(Exception e) 
189 		{
190 			logger.warn("exception", e);
191 		}
192 
193         public void exceptionThrown(File file, Exception e) 
194 		{
195 			logger.warn("exception", e);
196 		}
197 
198 	}
199 }