From 6ab01e19fda4c5e7f5d74ee63e65586e7ea96ebd Mon Sep 17 00:00:00 2001 From: angryziber Date: Tue, 16 Oct 2007 21:13:56 +0000 Subject: [PATCH] Comment fetcher implemented (with the exception of a TODO) git-svn-id: https://ipscan.svn.sourceforge.net/svnroot/ipscan/trunk@221 375186e5-ef17-0410-b0b6-91563547dcda --- resources/Labels.txt | 3 ++ .../azib/ipscan/config/CommentsConfig.java | 31 ++++++++++++++ .../azib/ipscan/config/ComponentRegistry.java | 3 ++ .../azib/ipscan/fetchers/CommentFetcher.java | 11 ++++- src/net/azib/ipscan/gui/InputDialog.java | 6 ++- src/net/azib/ipscan/gui/MainMenu.java | 2 + .../ipscan/gui/actions/CommandsActions.java | 40 +++++++++++++++++++ .../fetchers/FetcherRegistryImplTest.java | 2 +- 8 files changed, 93 insertions(+), 5 deletions(-) create mode 100644 src/net/azib/ipscan/config/CommentsConfig.java diff --git a/resources/Labels.txt b/resources/Labels.txt index 245ff9cf..ac95f624 100755 --- a/resources/Labels.txt +++ b/resources/Labels.txt @@ -19,6 +19,7 @@ menu.commands.delete=&Delete IP(s) menu.commands.copy=&Copy IP menu.commands.copyDetails=Co&py details menu.commands.show=Show +menu.commands.editComment=S&et IP comment... menu.commands.open=Open menu.commands.open.edit=Edit openers... menu.favorites=Fa&vorites @@ -66,6 +67,7 @@ title.favorite.edit=Edit favorites title.openers.edit=Edit Openers title.fetchers.select=Select Fetchers title.find=Find +title.editComment=Set comment text.error=Error text.userError=Problem text.ip=IP @@ -78,6 +80,7 @@ text.favorite.edit=Below you can rearrange or delete favorites text.find=Enter the text to search for text.find.notFound=Nothing was found. text.find.restart=Would you like to start from the beginning? +text.editComment=Specify comment for the IP address text.configDetect=We will try to detect the number of threads that works reliably on this machine by connecting to a known host many times simultaneously using the configured port timeout.\n\nPlease provide host and port that is 100% open and works, e.g. a proxy or web server on your network. text.configDetect.host=Host: text.configDetect.port=Port: diff --git a/src/net/azib/ipscan/config/CommentsConfig.java b/src/net/azib/ipscan/config/CommentsConfig.java new file mode 100644 index 00000000..57976493 --- /dev/null +++ b/src/net/azib/ipscan/config/CommentsConfig.java @@ -0,0 +1,31 @@ +/** + * This file is a part of Angry IP Scanner source code, + * see http://www.azib.net/ for more information. + * Licensed under GPLv2. + */ + +package net.azib.ipscan.config; + +import java.net.InetAddress; +import java.util.prefs.Preferences; + +/** + * CommentsConfig - a class for encapsulating of loading/storing of comments. + * + * @author Anton Keks + */ +public class CommentsConfig { + private Preferences preferences; + + public CommentsConfig(Preferences preferences) { + this.preferences = preferences; + } + + public String getComment(InetAddress address) { + return preferences.get(address.getHostAddress(), null); + } + + public void setComment(InetAddress address, String comment) { + preferences.put(address.getHostAddress(), comment); + } +} diff --git a/src/net/azib/ipscan/config/ComponentRegistry.java b/src/net/azib/ipscan/config/ComponentRegistry.java index bdbd610c..6cdb8973 100755 --- a/src/net/azib/ipscan/config/ComponentRegistry.java +++ b/src/net/azib/ipscan/config/ComponentRegistry.java @@ -16,6 +16,7 @@ import net.azib.ipscan.exporters.ExporterRegistry; import net.azib.ipscan.exporters.IPListExporter; import net.azib.ipscan.exporters.TXTExporter; import net.azib.ipscan.exporters.XMLExporter; +import net.azib.ipscan.fetchers.CommentFetcher; import net.azib.ipscan.fetchers.FetcherRegistry; import net.azib.ipscan.fetchers.FetcherRegistryImpl; import net.azib.ipscan.fetchers.FilteredPortsFetcher; @@ -78,6 +79,7 @@ public class ComponentRegistry { container.registerComponentInstance(Config.getOpenersConfig()); container.registerComponentInstance(Config.getFavoritesConfig()); container.registerComponentInstance(Labels.getInstance()); + container.registerComponentImplementation(CommentsConfig.class); container.registerComponentImplementation(ConfigDetector.class); container.registerComponentImplementation(ExporterRegistry.class); @@ -94,6 +96,7 @@ public class ComponentRegistry { container.registerComponentImplementation(PortsFetcher.class); container.registerComponentImplementation(FilteredPortsFetcher.class); container.registerComponentImplementation(WebDetectFetcher.class); + container.registerComponentImplementation(CommentFetcher.class); container.registerComponentImplementation(PingerRegistry.class, PingerRegistryImpl.class); container.registerComponentImplementation(ScanningResultList.class); diff --git a/src/net/azib/ipscan/fetchers/CommentFetcher.java b/src/net/azib/ipscan/fetchers/CommentFetcher.java index ea766c94..ea35a9d7 100755 --- a/src/net/azib/ipscan/fetchers/CommentFetcher.java +++ b/src/net/azib/ipscan/fetchers/CommentFetcher.java @@ -4,6 +4,7 @@ */ package net.azib.ipscan.fetchers; +import net.azib.ipscan.config.CommentsConfig; import net.azib.ipscan.core.ScanningSubject; /** @@ -16,7 +17,13 @@ import net.azib.ipscan.core.ScanningSubject; */ public class CommentFetcher implements Fetcher { - static final String LABEL = "fetcher.comment"; + public static final String LABEL = "fetcher.comment"; + + private CommentsConfig commentsConfig; + + public CommentFetcher(CommentsConfig commentsConfig) { + this.commentsConfig = commentsConfig; + } /** * @see net.azib.ipscan.fetchers.Fetcher#getLabel() @@ -29,7 +36,7 @@ public class CommentFetcher implements Fetcher { * @see net.azib.ipscan.fetchers.Fetcher#scan(net.azib.ipscan.core.ScanningSubject) */ public Object scan(ScanningSubject subject) { - return "a dummy comment!!!"; + return commentsConfig.getComment(subject.getAddress()); } public void init() { diff --git a/src/net/azib/ipscan/gui/InputDialog.java b/src/net/azib/ipscan/gui/InputDialog.java index 6cdf57dc..5c7bcbc0 100755 --- a/src/net/azib/ipscan/gui/InputDialog.java +++ b/src/net/azib/ipscan/gui/InputDialog.java @@ -78,8 +78,10 @@ public class InputDialog extends AbstractModalDialog { } private void setText(String text) { - this.text.setText(text); - this.text.setSelection(0, -1); + if (text != null) { + this.text.setText(text); + this.text.setSelection(0, -1); + } } /** diff --git a/src/net/azib/ipscan/gui/MainMenu.java b/src/net/azib/ipscan/gui/MainMenu.java index 6ae720cd..368266aa 100755 --- a/src/net/azib/ipscan/gui/MainMenu.java +++ b/src/net/azib/ipscan/gui/MainMenu.java @@ -116,6 +116,8 @@ public class MainMenu { initMenuItem(menu, null, null, null, null); initMenuItem(menu, "menu.commands.copy", "Ctrl+C", /* this is not a global key binding */ null, initListener(CommandsActions.CopyIP.class)); initMenuItem(menu, "menu.commands.copyDetails", null, null, initListener(CommandsActions.CopyIPDetails.class)); + initMenuItem(menu, null, null, null, null); + initMenuItem(menu, "menu.commands.editComment", "Ctrl+E", new Integer(SWT.MOD1 | 'E'), initListener(CommandsActions.EditComment.class)); initMenuItem(menu, null, null, null, null); createOpenersMenu(menu); // initMenuItem(subMenu, "menu.commands.show", null, initListener()); diff --git a/src/net/azib/ipscan/gui/actions/CommandsActions.java b/src/net/azib/ipscan/gui/actions/CommandsActions.java index 3bb20920..e90b5bb4 100755 --- a/src/net/azib/ipscan/gui/actions/CommandsActions.java +++ b/src/net/azib/ipscan/gui/actions/CommandsActions.java @@ -5,16 +5,21 @@ */ package net.azib.ipscan.gui.actions; +import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.Iterator; +import net.azib.ipscan.config.CommentsConfig; import net.azib.ipscan.config.Config; import net.azib.ipscan.config.Labels; import net.azib.ipscan.config.OpenersConfig.Opener; import net.azib.ipscan.core.UserErrorException; import net.azib.ipscan.core.state.StateMachine; +import net.azib.ipscan.fetchers.CommentFetcher; import net.azib.ipscan.fetchers.FetcherRegistry; import net.azib.ipscan.gui.DetailsDialog; import net.azib.ipscan.gui.EditOpenersDialog; +import net.azib.ipscan.gui.InputDialog; import net.azib.ipscan.gui.ResultTable; import net.azib.ipscan.gui.StatusBar; @@ -173,6 +178,41 @@ public class CommandsActions { } } + public static class EditComment implements Listener { + private ResultTable resultTable; + private CommentsConfig commentsConfig; + private FetcherRegistry fetcherRegistry; + + public EditComment(ResultTable resultTable, CommentsConfig commentsConfig, FetcherRegistry fetcherRegistry) { + this.resultTable = resultTable; + this.commentsConfig = commentsConfig; + this.fetcherRegistry = fetcherRegistry; + } + + public void handleEvent(Event event) { + checkSelection(resultTable); + try { + int index = resultTable.getSelectionIndex(); + InetAddress address = InetAddress.getByName(resultTable.getItem(index).getText()); + String comment = commentsConfig.getComment(address); + String newComment = new InputDialog(Labels.getLabel("title.editComment"), Labels.getLabel("text.editComment")).open(comment); + if (newComment != null) { + commentsConfig.setComment(address, newComment); + // now update the result table for user to immediately see the change + int fetcherIndex = fetcherRegistry.getSelectedFetcherIndex(CommentFetcher.LABEL); + if (fetcherIndex >= 0) { + // we have comment fetcher in the table + // TODO: update the value in the results list, otherwise old values will be exported + resultTable.getItem(index).setText(fetcherIndex, newComment); + } + } + } + catch (UnknownHostException e) { + // should not happen + } + } + } + public static class EditOpeners implements Listener { FetcherRegistry fetcherRegistry; diff --git a/test/net/azib/ipscan/fetchers/FetcherRegistryImplTest.java b/test/net/azib/ipscan/fetchers/FetcherRegistryImplTest.java index d32b3dba..2616e0c5 100755 --- a/test/net/azib/ipscan/fetchers/FetcherRegistryImplTest.java +++ b/test/net/azib/ipscan/fetchers/FetcherRegistryImplTest.java @@ -34,7 +34,7 @@ public class FetcherRegistryImplTest { ipFetcher = new IPFetcher(); pingFetcher = new PingFetcher(null, null); hostnameFetcher = new HostnameFetcher(); - commentFetcher = new CommentFetcher(); + commentFetcher = new CommentFetcher(null); portsFetcher = new PortsFetcher(null); fetcherRegistry = new FetcherRegistryImpl(new Fetcher[] {ipFetcher, pingFetcher, hostnameFetcher, commentFetcher, portsFetcher}, preferences); }