diff --git a/resources/Labels.txt b/resources/Labels.txt index a2d537f7..5c19861b 100755 --- a/resources/Labels.txt +++ b/resources/Labels.txt @@ -76,6 +76,7 @@ title.openers.edit=Edit Openers title.fetchers.select=Select Fetchers title.rename=Rename title.find=Find +title.commandline=Command-Line Usage text.error=Error text.userError=Problem text.ip=IP diff --git a/src/net/azib/ipscan/Main.java b/src/net/azib/ipscan/Main.java index 62bb00cb..edb555b1 100755 --- a/src/net/azib/ipscan/Main.java +++ b/src/net/azib/ipscan/Main.java @@ -10,18 +10,21 @@ import java.util.Locale; import java.util.logging.Level; import java.util.logging.Logger; +import net.azib.ipscan.config.ComponentRegistry; +import net.azib.ipscan.config.Config; +import net.azib.ipscan.config.Labels; +import net.azib.ipscan.config.LoggerFactory; +import net.azib.ipscan.config.Platform; +import net.azib.ipscan.config.Version; +import net.azib.ipscan.core.UserErrorException; +import net.azib.ipscan.gui.InfoDialog; +import net.azib.ipscan.gui.MainWindow; + import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; -import net.azib.ipscan.config.Config; -import net.azib.ipscan.config.ComponentRegistry; -import net.azib.ipscan.config.Labels; -import net.azib.ipscan.config.LoggerFactory; -import net.azib.ipscan.core.UserErrorException; -import net.azib.ipscan.gui.MainWindow; - /** * The main executable class. * It initializes all the needed stuff and launches the user interface. @@ -78,10 +81,9 @@ public class Main { // display a nice error message String localizedMessage = getLocalizedMessage(e); Shell parent = display.getActiveShell(); - MessageBox messageBox = new MessageBox(parent != null ? parent : mainWindow.getShell(), SWT.OK | (e instanceof UserErrorException ? SWT.ICON_WARNING : SWT.ICON_ERROR)); - messageBox.setText(Labels.getLabel(e instanceof UserErrorException ? "text.userError" : "text.error")); - messageBox.setMessage(localizedMessage); - messageBox.open(); + showMessage(parent != null ? parent : mainWindow.getShell(), + e instanceof UserErrorException ? SWT.ICON_WARNING : SWT.ICON_ERROR, + Labels.getLabel(e instanceof UserErrorException ? "text.userError" : "text.error"), localizedMessage); } } @@ -92,6 +94,13 @@ public class Main { display.dispose(); } + private static void showMessage(Shell parent, int flags, String title, String localizedMessage) { + MessageBox messageBox = new MessageBox(parent, SWT.OK | flags); + messageBox.setText(title); + messageBox.setMessage(localizedMessage); + messageBox.open(); + } + private static void initSystemProperties() { // currently we support IPv4 only System.setProperty("java.net.preferIPv4Stack", "true"); @@ -103,7 +112,35 @@ public class Main { private static void processCommandLine(String[] args) { if (args.length != 0) { // TODO: implement command-line - LOG.warning("Command-line usage is not implemented yet, sorry"); + String usageText = "Command-line usage is not implemented yet, sorry"; + + showMessageToConsole(usageText); + } + } + + /** + * @param usageText + */ + private static void showMessageToConsole(String usageText) { + // use console for all platforms except Windows by default + boolean haveConsole = !Platform.WINDOWS; + + try { + // determine if we have console attached using Java 6 System.console() + haveConsole = System.class.getMethod("console").invoke(null) != null; + } + catch (Exception e) { + // Java 5 will reach here + e.printStackTrace(); + } + + if (haveConsole) { + System.err.println(usageText); + } + else { + InfoDialog dialog = new InfoDialog(Version.NAME, Labels.getLabel("title.commandline")); + dialog.setMessage(usageText); + dialog.open(); } } @@ -119,7 +156,7 @@ public class Main { localizedMessage = e.getMessage(); } else { - String exceptionClassName = getClassShortName(e.getClass()); + String exceptionClassName = e.getClass().getSimpleName(); String originalMessage = e.getMessage(); localizedMessage = Labels.getLabel("exception." + exceptionClassName + (originalMessage != null ? "." + originalMessage : "")); } @@ -136,14 +173,5 @@ public class Main { LOG.log(Level.SEVERE, "unexpected error", e); } return localizedMessage; - } - - /** - * @return the short name of a class (without package name) - */ - static String getClassShortName(Class clazz) { - String className = clazz.getName(); - return className.substring(className.lastIndexOf('.') + 1); - } - + } } diff --git a/src/net/azib/ipscan/gui/AbstractModalDialog.java b/src/net/azib/ipscan/gui/AbstractModalDialog.java index 9439cac0..bd41c36d 100755 --- a/src/net/azib/ipscan/gui/AbstractModalDialog.java +++ b/src/net/azib/ipscan/gui/AbstractModalDialog.java @@ -11,6 +11,7 @@ import net.azib.ipscan.config.Platform; import net.azib.ipscan.gui.util.LayoutHelper; import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.FormAttachment; @@ -72,9 +73,15 @@ public abstract class AbstractModalDialog { // ignore if unsuccessful } - shell = new Shell(parent, getShellStyle()); - if (parent != null) - shell.setImage(parent.getImage()); + shell = new Shell(parent, getShellStyle()); + Image icon = null; + if (parent != null) { + icon = parent.getImage(); + } + else { + icon = new Image(shell.getDisplay(), Labels.getInstance().getImageAsStream("icon")); + } + shell.setImage(icon); populateShell(); } diff --git a/src/net/azib/ipscan/gui/InfoDialog.java b/src/net/azib/ipscan/gui/InfoDialog.java new file mode 100644 index 00000000..cf74fa85 --- /dev/null +++ b/src/net/azib/ipscan/gui/InfoDialog.java @@ -0,0 +1,77 @@ +/** + * 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.gui; + +import net.azib.ipscan.gui.util.LayoutHelper; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.graphics.FontData; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.layout.FormAttachment; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Text; + +/** + * StatisticsDialog - shows statistical information about the last scan + * + * @author Anton Keks + */ +public class InfoDialog extends AbstractModalDialog { + + String title; + String title2; + String message; + + public InfoDialog(String title, String title2) { + this.title = title; + this.title2 = title2; + } + + @Override + protected void populateShell() { + shell.setText(title); + shell.setLayout(LayoutHelper.formLayout(10, 10, 15)); + + Label iconLabel = new Label(shell, SWT.ICON); + iconLabel.setLayoutData(LayoutHelper.formData(new FormAttachment(0), null, new FormAttachment(0), null)); + iconLabel.setImage(shell.getImage()); + + Label titleLabel = new Label(shell, SWT.NONE); + FontData sysFontData = shell.getDisplay().getSystemFont().getFontData()[0]; + titleLabel.setLayoutData(LayoutHelper.formData(new FormAttachment(iconLabel), null, new FormAttachment(0), null)); + titleLabel.setFont(new Font(null, sysFontData.getName(), sysFontData.getHeight()+3, sysFontData.getStyle() | SWT.BOLD)); + titleLabel.setText(title2); + + Text statsText = new Text(shell, SWT.MULTI | SWT.READ_ONLY); + statsText.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); + statsText.setLayoutData(LayoutHelper.formData(new FormAttachment(iconLabel), null, new FormAttachment(titleLabel), null)); + statsText.setText(message); + statsText.pack(); + + Button button = createCloseButton(); + Point buttonSize = button.getSize(); + button.setLayoutData(LayoutHelper.formData(buttonSize.x, buttonSize.y, null, new FormAttachment(statsText, 30, SWT.RIGHT), new FormAttachment(statsText), null)); + + shell.pack(); + } + + /** + * @return Returns the message. + */ + public String getMessage() { + return message; + } + + /** + * @param message The message to set. + */ + public void setMessage(String message) { + this.message = message; + } +} diff --git a/src/net/azib/ipscan/gui/StatisticsDialog.java b/src/net/azib/ipscan/gui/StatisticsDialog.java index 225e5d88..de1a1e91 100644 --- a/src/net/azib/ipscan/gui/StatisticsDialog.java +++ b/src/net/azib/ipscan/gui/StatisticsDialog.java @@ -13,33 +13,26 @@ import net.azib.ipscan.config.Labels; import net.azib.ipscan.core.ScanningResultList; import net.azib.ipscan.core.UserErrorException; import net.azib.ipscan.core.ScanningResultList.ScanInfo; -import net.azib.ipscan.gui.util.LayoutHelper; - -import org.eclipse.swt.SWT; -import org.eclipse.swt.graphics.Font; -import org.eclipse.swt.graphics.FontData; -import org.eclipse.swt.graphics.Point; -import org.eclipse.swt.layout.FormAttachment; -import org.eclipse.swt.widgets.Button; -import org.eclipse.swt.widgets.Label; -import org.eclipse.swt.widgets.Text; /** * StatisticsDialog - shows statistical information about the last scan * * @author Anton Keks */ -public class StatisticsDialog extends AbstractModalDialog { +public class StatisticsDialog extends InfoDialog { - private ScanningResultList scanningResults; - - public StatisticsDialog(ScanningResultList scanningResultList) { - this.scanningResults = scanningResultList; + private final ScanningResultList scanningResults; + + public StatisticsDialog(ScanningResultList scanningResults) { + super(Labels.getLabel("title.statistics"), null); + this.scanningResults = scanningResults; } @Override public void open() { if (scanningResults.isInfoAvailable()) { + setMessage(prepareText()); + if (shell != null) { // close the same window if it is already open ('scanning incomplete') shell.close(); @@ -53,35 +46,10 @@ public class StatisticsDialog extends AbstractModalDialog { } } - @Override - protected void populateShell() { - shell.setText(Labels.getLabel("title.statistics")); - shell.setLayout(LayoutHelper.formLayout(10, 10, 15)); - - Label iconLabel = new Label(shell, SWT.ICON); - iconLabel.setLayoutData(LayoutHelper.formData(new FormAttachment(0), null, new FormAttachment(0), null)); - iconLabel.setImage(shell.getImage()); - - Label titleLabel = new Label(shell, SWT.NONE); - FontData sysFontData = shell.getDisplay().getSystemFont().getFontData()[0]; - titleLabel.setLayoutData(LayoutHelper.formData(new FormAttachment(iconLabel), null, new FormAttachment(0), null)); - titleLabel.setFont(new Font(null, sysFontData.getName(), sysFontData.getHeight()+3, sysFontData.getStyle() | SWT.BOLD)); - titleLabel.setText(Labels.getLabel(scanningResults.getScanInfo().isCompletedNormally() ? "text.scan.completed" : "text.scan.incomplete")); - - Text statsText = new Text(shell, SWT.MULTI | SWT.READ_ONLY); - statsText.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); - statsText.setLayoutData(LayoutHelper.formData(new FormAttachment(iconLabel), null, new FormAttachment(titleLabel), null)); - statsText.setText(prepareText()); - statsText.pack(); - - Button button = createCloseButton(); - Point buttonSize = button.getSize(); - button.setLayoutData(LayoutHelper.formData(buttonSize.x, buttonSize.y, null, new FormAttachment(statsText, 30, SWT.RIGHT), new FormAttachment(statsText), null)); - - shell.pack(); - } - String prepareText() { + title2 = Labels.getLabel(scanningResults.getScanInfo().isCompletedNormally() ? + "text.scan.completed" : "text.scan.incomplete"); + ScanInfo scanInfo = scanningResults.getScanInfo(); String ln = System.getProperty("line.separator"); StringBuilder text = new StringBuilder(); diff --git a/test/net/azib/ipscan/MainTest.java b/test/net/azib/ipscan/MainTest.java index 0cdc1a8f..5cdfd17f 100755 --- a/test/net/azib/ipscan/MainTest.java +++ b/test/net/azib/ipscan/MainTest.java @@ -38,11 +38,4 @@ public class MainTest { assertEquals(Labels.getLabel("exception.OutOfMemoryError"), Main.getLocalizedMessage(new OutOfMemoryError())); } - - @Test - public void classShortName() { - assertEquals("String", Main.getClassShortName(String.class)); - assertEquals("MainTest", Main.getClassShortName(MainTest.class)); - } - } diff --git a/test/net/azib/ipscan/gui/StatisticsDialogTest.java b/test/net/azib/ipscan/gui/StatisticsDialogTest.java index 2c4f5218..c49c477d 100644 --- a/test/net/azib/ipscan/gui/StatisticsDialogTest.java +++ b/test/net/azib/ipscan/gui/StatisticsDialogTest.java @@ -60,7 +60,7 @@ public class StatisticsDialogTest { expect(results.getFeederInfo()).andReturn("SomeInfoHere"); replay(results); - String text = new StatisticsDialog(results).prepareText(); + String text = new StatisticsDialog(results).getMessage(); assertNotNull(text); assertTrue(text.contains(Labels.getLabel("text.scan.time.total") + "10"));