Nicer message if command-line is given, InfoDialog refactored out of StatisticsDialog

git-svn-id: https://ipscan.svn.sourceforge.net/svnroot/ipscan/trunk@428 375186e5-ef17-0410-b0b6-91563547dcda
This commit is contained in:
angryziber 2009-02-24 19:44:32 +00:00
parent 56e5bf1824
commit 463ad1f0db
7 changed files with 151 additions and 77 deletions

View File

@ -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

View File

@ -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);
}
}
}

View File

@ -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();
}

View File

@ -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;
}
}

View File

@ -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();

View File

@ -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));
}
}

View File

@ -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"));