use lambdas

This commit is contained in:
Anton Keks 2019-07-15 23:57:05 +03:00
parent 189ac1783b
commit 0c55cd0995
20 changed files with 522 additions and 582 deletions

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id="ipscan" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="" external.system.module.version="3.5.5" type="JAVA_MODULE" version="4">
<module external.linked.project.id="ipscan" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="" external.system.module.version="3.6.0" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager">
<output url="file://$MODULE_DIR$/out/production/classes" />
<output-test url="file://$MODULE_DIR$/out/test/classes" />

View File

@ -6,7 +6,6 @@ import net.azib.ipscan.config.LoggerFactory;
import javax.inject.Singleton;
import java.io.File;
import java.io.FilenameFilter;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
@ -74,12 +73,7 @@ public class PluginLoader {
File parentDir = ownFile.getParentFile();
if (parentDir == null || !parentDir.exists()) return;
File[] jars = parentDir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".jar") && !name.equals(ownFile.getName());
}
});
File[] jars = parentDir.listFiles((dir, name) -> name.endsWith(".jar") && !name.equals(ownFile.getName()));
if (jars == null) return;
PluginClassLoader loader = new PluginClassLoader();

View File

@ -128,6 +128,4 @@ public class ScanningResult {
}
return details.toString();
}
}

View File

@ -1,104 +1,104 @@
package net.azib.ipscan.gui;
import net.azib.ipscan.config.Labels;
import net.azib.ipscan.config.Version;
import net.azib.ipscan.gui.actions.BrowserLauncher;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.*;
import javax.inject.Inject;
import static net.azib.ipscan.config.Version.*;
/**
* About Dialog
*
* @author Anton Keks
*/
public class AboutDialog extends AbstractModalDialog {
@Inject
public AboutDialog() {
}
@Override
protected void populateShell() {
shell.setText(Labels.getLabel("title.about"));
shell.setSize(new Point(400, 393));
Label iconLabel = new Label(shell, SWT.ICON);
iconLabel.setLocation(10, 10);
if (shell.getImage() != null) {
iconLabel.setImage(shell.getImage());
}
iconLabel.pack();
int leftBound = iconLabel.getBounds().width + 20;
String aboutText = Labels.getLabel("text.about");
aboutText = aboutText.replace("%NAME", NAME);
aboutText = aboutText.replace("%VERSION", getVersion());
aboutText = aboutText.replace("%DATE", getBuildDate());
aboutText = aboutText.replace("%COPYLEFT", COPYLEFT);
Label aboutLabel = new Label(shell, SWT.NONE);
aboutLabel.setText(aboutText);
aboutLabel.setLocation(leftBound, 10);
aboutLabel.pack();
Label websiteLabel = createLinkLabel(WEBSITE, WEBSITE);
websiteLabel.setLocation(leftBound, 10 + aboutLabel.getBounds().height);
String systemText = Labels.getLabel("text.about.system");
systemText = systemText.replace("%JAVA", System.getProperty("java.vm.vendor") + " " + System.getProperty("java.runtime.version"));
systemText = systemText.replace("%OS", System.getProperty("os.name") + " " + System.getProperty("os.version") + " (" + System.getProperty("os.arch") + ")");
Label systemLabel = new Label(shell, SWT.NONE);
systemLabel.setText(systemText);
systemLabel.setLocation(leftBound, 20 + aboutLabel.getBounds().height + websiteLabel.getBounds().height);
systemLabel.pack();
Button button = createCloseButton();
Text licenseText = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.READ_ONLY | SWT.V_SCROLL | SWT.WRAP);
licenseText.setBounds(leftBound, systemLabel.getBounds().y + systemLabel.getBounds().height + 10,
shell.getClientArea().width - leftBound - 10,
button.getLocation().y - systemLabel.getBounds().y - systemLabel.getBounds().height - 20);
licenseText.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
licenseText.setText("Licensed under the GNU General Public License Version 2\n\n" +
NAME + " is free software; you can redistribute it and/or " +
"modify it under the terms of the GNU General Public License " +
"as published by the Free Software Foundation; either version 2 " +
"of the License, or (at your option) any later version.\n\n" +
NAME + " is distributed in the hope that it will be useful, " +
"but WITHOUT ANY WARRANTY; without even the implied warranty of " +
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the " +
"GNU General Public License for more details.");
Label fullLicenseLabel = createLinkLabel("Full license", Version.FULL_LICENSE_URL);
fullLicenseLabel.setLocation(leftBound, licenseText.getBounds().y + licenseText.getBounds().height + 10);
Label privacyLabel = createLinkLabel("Privacy", Version.PRIVACY_URL);
privacyLabel.setLocation(leftBound + privacyLabel.getBounds().width + 40, fullLicenseLabel.getBounds().y);
}
@Override
protected int getShellStyle() {
return super.getShellStyle() | SWT.SHEET;
}
private Label createLinkLabel(final String text, final String url) {
final Label link = new Label(shell, SWT.NONE);
link.setForeground(new Color(null, 0, 0, 0xCC));
link.setCursor(shell.getDisplay().getSystemCursor(SWT.CURSOR_HAND));
link.setText(text);
link.addListener(SWT.MouseUp, new Listener() {
public void handleEvent(Event event) {
BrowserLauncher.openURL(url);
link.setForeground(new Color(null, 0x88, 0, 0xAA));
}
});
link.pack();
return link;
}
}
package net.azib.ipscan.gui;
import net.azib.ipscan.config.Labels;
import net.azib.ipscan.config.Version;
import net.azib.ipscan.gui.actions.BrowserLauncher;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import javax.inject.Inject;
import static net.azib.ipscan.config.Version.*;
/**
* About Dialog
*
* @author Anton Keks
*/
public class AboutDialog extends AbstractModalDialog {
@Inject
public AboutDialog() {
}
@Override
protected void populateShell() {
shell.setText(Labels.getLabel("title.about"));
shell.setSize(new Point(400, 393));
Label iconLabel = new Label(shell, SWT.ICON);
iconLabel.setLocation(10, 10);
if (shell.getImage() != null) {
iconLabel.setImage(shell.getImage());
}
iconLabel.pack();
int leftBound = iconLabel.getBounds().width + 20;
String aboutText = Labels.getLabel("text.about");
aboutText = aboutText.replace("%NAME", NAME);
aboutText = aboutText.replace("%VERSION", getVersion());
aboutText = aboutText.replace("%DATE", getBuildDate());
aboutText = aboutText.replace("%COPYLEFT", COPYLEFT);
Label aboutLabel = new Label(shell, SWT.NONE);
aboutLabel.setText(aboutText);
aboutLabel.setLocation(leftBound, 10);
aboutLabel.pack();
Label websiteLabel = createLinkLabel(WEBSITE, WEBSITE);
websiteLabel.setLocation(leftBound, 10 + aboutLabel.getBounds().height);
String systemText = Labels.getLabel("text.about.system");
systemText = systemText.replace("%JAVA", System.getProperty("java.vm.vendor") + " " + System.getProperty("java.runtime.version"));
systemText = systemText.replace("%OS", System.getProperty("os.name") + " " + System.getProperty("os.version") + " (" + System.getProperty("os.arch") + ")");
Label systemLabel = new Label(shell, SWT.NONE);
systemLabel.setText(systemText);
systemLabel.setLocation(leftBound, 20 + aboutLabel.getBounds().height + websiteLabel.getBounds().height);
systemLabel.pack();
Button button = createCloseButton();
Text licenseText = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.READ_ONLY | SWT.V_SCROLL | SWT.WRAP);
licenseText.setBounds(leftBound, systemLabel.getBounds().y + systemLabel.getBounds().height + 10,
shell.getClientArea().width - leftBound - 10,
button.getLocation().y - systemLabel.getBounds().y - systemLabel.getBounds().height - 20);
licenseText.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
licenseText.setText("Licensed under the GNU General Public License Version 2\n\n" +
NAME + " is free software; you can redistribute it and/or " +
"modify it under the terms of the GNU General Public License " +
"as published by the Free Software Foundation; either version 2 " +
"of the License, or (at your option) any later version.\n\n" +
NAME + " is distributed in the hope that it will be useful, " +
"but WITHOUT ANY WARRANTY; without even the implied warranty of " +
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the " +
"GNU General Public License for more details.");
Label fullLicenseLabel = createLinkLabel("Full license", Version.FULL_LICENSE_URL);
fullLicenseLabel.setLocation(leftBound, licenseText.getBounds().y + licenseText.getBounds().height + 10);
Label privacyLabel = createLinkLabel("Privacy", Version.PRIVACY_URL);
privacyLabel.setLocation(leftBound + privacyLabel.getBounds().width + 40, fullLicenseLabel.getBounds().y);
}
@Override
protected int getShellStyle() {
return super.getShellStyle() | SWT.SHEET;
}
private Label createLinkLabel(final String text, final String url) {
final Label link = new Label(shell, SWT.NONE);
link.setForeground(new Color(null, 0, 0, 0xCC));
link.setCursor(shell.getDisplay().getSystemCursor(SWT.CURSOR_HAND));
link.setText(text);
link.addListener(SWT.MouseUp, event -> {
BrowserLauncher.openURL(url);
link.setForeground(new Color(null, 0x88, 0, 0xAA));
});
link.pack();
return link;
}
}

View File

@ -150,11 +150,9 @@ public abstract class AbstractModalDialog {
button.setText(Labels.getLabel("button.close"));
positionButtons(button, null);
button.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
shell.close();
shell.dispose();
}
button.addListener(SWT.Selection, event -> {
shell.close();
shell.dispose();
});
button.setFocus();

View File

@ -1,124 +1,120 @@
package net.azib.ipscan.gui;
import net.azib.ipscan.config.CommentsConfig;
import net.azib.ipscan.config.GUIConfig;
import net.azib.ipscan.config.Labels;
import net.azib.ipscan.core.ScanningResult;
import net.azib.ipscan.core.ScanningResultList;
import net.azib.ipscan.fetchers.CommentFetcher;
import net.azib.ipscan.gui.util.LayoutHelper;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import javax.inject.Inject;
/**
* The "Show IP Details" Window
*
* @author Anton Keks
*/
public class DetailsWindow extends AbstractModalDialog {
@Inject CommentsConfig commentsConfig;
private GUIConfig guiConfig;
private ResultTable resultTable;
private ScanningResultList scanningResults;
int resultIndex;
private Text commentsText;
@Inject public DetailsWindow(GUIConfig guiConfig, ResultTable resultTable, ScanningResultList scanningResults) {
this.guiConfig = guiConfig;
this.resultTable = resultTable;
this.scanningResults = scanningResults;
}
@Override
protected int getShellStyle() {
return SWT.TOOL | SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.RESIZE;
}
@Override
protected void populateShell() {
shell.setText(Labels.getLabel("title.details"));
shell.setLayout(LayoutHelper.formLayout(3, 3, 3));
shell.setSize(guiConfig.detailsWindowSize);
resultIndex = resultTable.getSelectionIndex();
ScanningResult result = resultTable.getSelectedResult();
commentsText = new Text(shell, SWT.BORDER); // TODO: change to SWT.SEARCH in SWT 3.5
commentsText.pack();
commentsText.setLayoutData(LayoutHelper.formData(new FormAttachment(0), new FormAttachment(100), null, new FormAttachment(100)));
CommentsTextListener commentsTextListener = new CommentsTextListener();
commentsText.addFocusListener(commentsTextListener);
commentsText.addModifyListener(commentsTextListener);
String comment = commentsConfig.getComment(scanningResults, resultIndex);
if (comment != null) commentsText.setText(comment);
else commentsTextListener.focusLost(null);
Text detailsText = new Text(shell, SWT.BORDER | SWT.READ_ONLY | SWT.MULTI | SWT.V_SCROLL | SWT.WRAP);
detailsText.setText(result.toString());
detailsText.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
detailsText.setTabs(32);
detailsText.setLayoutData(LayoutHelper.formData(new FormAttachment(0), new FormAttachment(100), new FormAttachment(0), new FormAttachment(commentsText)));
Listener traverseListener = new TraverseListener();
detailsText.addListener(SWT.Traverse, traverseListener);
commentsText.addListener(SWT.Traverse, traverseListener);
shell.layout();
detailsText.forceFocus();
shell.addListener(SWT.Close, new Listener() {
@Override public void handleEvent(Event event) {
guiConfig.detailsWindowSize = shell.getSize();
}
});
}
class CommentsTextListener implements FocusListener, ModifyListener {
String defaultText = Labels.getLabel("text.comment.edit");
public void focusGained(FocusEvent e) {
if (commentsText.getText().equals(defaultText)) {
commentsText.setText("");
commentsText.setForeground(commentsText.getDisplay().getSystemColor(SWT.COLOR_WIDGET_FOREGROUND));
}
}
public void focusLost(FocusEvent e) {
if (commentsText.getText().length() == 0) {
commentsText.setText(defaultText);
commentsText.setForeground(commentsText.getDisplay().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
}
}
public void modifyText(ModifyEvent e) {
String newComment = commentsText.getText();
if (!defaultText.equals(newComment)) {
// store the new comment
commentsConfig.setComment(scanningResults, resultIndex, newComment);
// now update the result table for user to immediately see the change
resultTable.updateResult(resultIndex, CommentFetcher.ID, newComment);
}
}
}
class TraverseListener implements Listener {
public void handleEvent(Event e) {
if (e.detail == SWT.TRAVERSE_RETURN) {
shell.close();
shell.dispose();
}
}
}
}
package net.azib.ipscan.gui;
import net.azib.ipscan.config.CommentsConfig;
import net.azib.ipscan.config.GUIConfig;
import net.azib.ipscan.config.Labels;
import net.azib.ipscan.core.ScanningResult;
import net.azib.ipscan.core.ScanningResultList;
import net.azib.ipscan.fetchers.CommentFetcher;
import net.azib.ipscan.gui.util.LayoutHelper;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import javax.inject.Inject;
/**
* The "Show IP Details" Window
*
* @author Anton Keks
*/
public class DetailsWindow extends AbstractModalDialog {
@Inject CommentsConfig commentsConfig;
private GUIConfig guiConfig;
private ResultTable resultTable;
private ScanningResultList scanningResults;
int resultIndex;
private Text commentsText;
@Inject public DetailsWindow(GUIConfig guiConfig, ResultTable resultTable, ScanningResultList scanningResults) {
this.guiConfig = guiConfig;
this.resultTable = resultTable;
this.scanningResults = scanningResults;
}
@Override
protected int getShellStyle() {
return SWT.TOOL | SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.RESIZE;
}
@Override
protected void populateShell() {
shell.setText(Labels.getLabel("title.details"));
shell.setLayout(LayoutHelper.formLayout(3, 3, 3));
shell.setSize(guiConfig.detailsWindowSize);
resultIndex = resultTable.getSelectionIndex();
ScanningResult result = resultTable.getSelectedResult();
commentsText = new Text(shell, SWT.BORDER); // TODO: change to SWT.SEARCH in SWT 3.5
commentsText.pack();
commentsText.setLayoutData(LayoutHelper.formData(new FormAttachment(0), new FormAttachment(100), null, new FormAttachment(100)));
CommentsTextListener commentsTextListener = new CommentsTextListener();
commentsText.addFocusListener(commentsTextListener);
commentsText.addModifyListener(commentsTextListener);
String comment = commentsConfig.getComment(scanningResults, resultIndex);
if (comment != null) commentsText.setText(comment);
else commentsTextListener.focusLost(null);
Text detailsText = new Text(shell, SWT.BORDER | SWT.READ_ONLY | SWT.MULTI | SWT.V_SCROLL | SWT.WRAP);
detailsText.setText(result.toString());
detailsText.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
detailsText.setTabs(32);
detailsText.setLayoutData(LayoutHelper.formData(new FormAttachment(0), new FormAttachment(100), new FormAttachment(0), new FormAttachment(commentsText)));
Listener traverseListener = new TraverseListener();
detailsText.addListener(SWT.Traverse, traverseListener);
commentsText.addListener(SWT.Traverse, traverseListener);
shell.layout();
detailsText.forceFocus();
shell.addListener(SWT.Close, event -> guiConfig.detailsWindowSize = shell.getSize());
}
class CommentsTextListener implements FocusListener, ModifyListener {
String defaultText = Labels.getLabel("text.comment.edit");
public void focusGained(FocusEvent e) {
if (commentsText.getText().equals(defaultText)) {
commentsText.setText("");
commentsText.setForeground(commentsText.getDisplay().getSystemColor(SWT.COLOR_WIDGET_FOREGROUND));
}
}
public void focusLost(FocusEvent e) {
if (commentsText.getText().length() == 0) {
commentsText.setText(defaultText);
commentsText.setForeground(commentsText.getDisplay().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
}
}
public void modifyText(ModifyEvent e) {
String newComment = commentsText.getText();
if (!defaultText.equals(newComment)) {
// store the new comment
commentsConfig.setComment(scanningResults, resultIndex, newComment);
// now update the result table for user to immediately see the change
resultTable.updateResult(resultIndex, CommentFetcher.ID, newComment);
}
}
}
class TraverseListener implements Listener {
public void handleEvent(Event e) {
if (e.detail == SWT.TRAVERSE_RETURN) {
shell.close();
shell.dispose();
}
}
}
}

View File

@ -1,95 +1,89 @@
package net.azib.ipscan.gui;
import net.azib.ipscan.config.Labels;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.*;
import java.util.ArrayList;
import java.util.List;
public class GettingStartedDialog extends AbstractModalDialog {
private int activePage;
private List<String> texts = new ArrayList<String>();
private Text gettingStartedText;
private Button closeButton;
private Button nextButton;
public GettingStartedDialog() {
int num = 1;
try {
while (true) {
texts.add(Labels.getLabel("text.gettingStarted" + num++));
}
}
catch (Exception noMoreTexts) {}
}
GettingStartedDialog prependText(String text) {
texts.add(0, text);
return this;
}
@Override
protected void populateShell() {
Display currentDisplay = Display.getCurrent();
Shell parent = currentDisplay != null ? currentDisplay.getActiveShell() : null;
shell = new Shell(parent, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
shell.setText(Labels.getLabel("title.gettingStarted"));
shell.setSize(new Point(500, 300));
Label iconLabel = new Label(shell, SWT.ICON);
iconLabel.setLocation(10, 10);
if (parent != null) {
iconLabel.setImage(parent.getImage());
shell.setImage(parent.getImage());
}
iconLabel.pack();
int leftBound = iconLabel.getBounds().width + 20;
closeButton = new Button(shell, SWT.NONE);
closeButton.setText(Labels.getLabel("button.close"));
nextButton = new Button(shell, SWT.NONE);
nextButton.setText(Labels.getLabel("button.next"));
nextButton.setFocus();
positionButtons(nextButton, closeButton);
gettingStartedText = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.READ_ONLY | SWT.V_SCROLL | SWT.WRAP);
gettingStartedText.setBounds(leftBound, 10, shell.getClientArea().width - leftBound - 10, closeButton.getLocation().y - 20);
gettingStartedText.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
closeButton.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
shell.close();
shell.dispose();
}
});
nextButton.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
displayActivePage();
}
});
displayActivePage();
}
@Override
protected int getShellStyle() {
return super.getShellStyle() | SWT.SHEET;
}
private void displayActivePage() {
gettingStartedText.setText(texts.get(activePage++));
if (activePage >= texts.size()) {
nextButton.setEnabled(false);
shell.setDefaultButton(closeButton);
closeButton.setFocus();
}
}
}
package net.azib.ipscan.gui;
import net.azib.ipscan.config.Labels;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.*;
import java.util.ArrayList;
import java.util.List;
public class GettingStartedDialog extends AbstractModalDialog {
private int activePage;
private List<String> texts = new ArrayList<String>();
private Text gettingStartedText;
private Button closeButton;
private Button nextButton;
public GettingStartedDialog() {
int num = 1;
try {
while (true) {
texts.add(Labels.getLabel("text.gettingStarted" + num++));
}
}
catch (Exception noMoreTexts) {}
}
GettingStartedDialog prependText(String text) {
texts.add(0, text);
return this;
}
@Override
protected void populateShell() {
Display currentDisplay = Display.getCurrent();
Shell parent = currentDisplay != null ? currentDisplay.getActiveShell() : null;
shell = new Shell(parent, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
shell.setText(Labels.getLabel("title.gettingStarted"));
shell.setSize(new Point(500, 300));
Label iconLabel = new Label(shell, SWT.ICON);
iconLabel.setLocation(10, 10);
if (parent != null) {
iconLabel.setImage(parent.getImage());
shell.setImage(parent.getImage());
}
iconLabel.pack();
int leftBound = iconLabel.getBounds().width + 20;
closeButton = new Button(shell, SWT.NONE);
closeButton.setText(Labels.getLabel("button.close"));
nextButton = new Button(shell, SWT.NONE);
nextButton.setText(Labels.getLabel("button.next"));
nextButton.setFocus();
positionButtons(nextButton, closeButton);
gettingStartedText = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.READ_ONLY | SWT.V_SCROLL | SWT.WRAP);
gettingStartedText.setBounds(leftBound, 10, shell.getClientArea().width - leftBound - 10, closeButton.getLocation().y - 20);
gettingStartedText.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
closeButton.addListener(SWT.Selection, event -> {
shell.close();
shell.dispose();
});
nextButton.addListener(SWT.Selection, event -> displayActivePage());
displayActivePage();
}
@Override
protected int getShellStyle() {
return super.getShellStyle() | SWT.SHEET;
}
private void displayActivePage() {
gettingStartedText.setText(texts.get(activePage++));
if (activePage >= texts.size()) {
nextButton.setEnabled(false);
shell.setDefaultButton(closeButton);
closeButton.setFocus();
}
}
}

View File

@ -22,11 +22,7 @@ public class MacApplicationMenu {
@Inject CheckVersion checkVersionListener;
@Inject public MacApplicationMenu(final Display display) {
display.syncExec(new Runnable() {
public void run() {
initApplicationMenu(display);
}
});
display.syncExec(() -> initApplicationMenu(display));
}
private void initApplicationMenu(Display display) {

View File

@ -86,11 +86,9 @@ public class MainWindow {
startup.onStart();
stateMachine.addTransitionListener(new EnablerDisabler());
Display.getCurrent().asyncExec(new Runnable() {
public void run() {
// asynchronously run init handlers outside of the constructor
stateMachine.init();
}
Display.getCurrent().asyncExec(() -> {
// asynchronously run init handlers outside of the constructor
stateMachine.init();
});
}
@ -104,11 +102,9 @@ public class MainWindow {
Image image = new Image(shell.getDisplay(), getClass().getResourceAsStream("/images/icon.png"));
shell.setImage(image);
shell.addListener(SWT.Close, new Listener() {
public void handleEvent(Event event) {
// save dimensions!
guiConfig.setMainWindowSize(shell.getSize(), shell.getMaximized());
}
shell.addListener(SWT.Close, event -> {
// save dimensions!
guiConfig.setMainWindowSize(shell.getSize(), shell.getMaximized());
});
}

View File

@ -325,11 +325,7 @@ public class PreferencesDialog extends AbstractModalDialog {
adaptTimeoutCheckbox = new Button(timingGroup, SWT.CHECK);
adaptTimeoutCheckbox.setText(Labels.getLabel("preferences.ports.timing.adaptTimeout"));
adaptTimeoutCheckbox.setLayoutData(gridData1);
adaptTimeoutCheckbox.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
minPortTimeoutText.setEnabled(adaptTimeoutCheckbox.getSelection());
}
});
adaptTimeoutCheckbox.addListener(SWT.Selection, event -> minPortTimeoutText.setEnabled(adaptTimeoutCheckbox.getSelection()));
label = new Label(timingGroup, SWT.NONE);
label.setText(Labels.getLabel("preferences.ports.timing.minTimeout"));

View File

@ -130,23 +130,21 @@ public class ResultTable extends Table implements FetcherRegistryUpdateListener,
public void addOrUpdateResultRow(final ScanningResult result) {
if (isDisposed())
return;
getDisplay().asyncExec(new Runnable() {
public void run() {
if (isDisposed())
return;
if (scanningResults.isRegistered(result)) {
// just redraw the item
int index = scanningResults.update(result);
clear(index);
}
else {
// first register, then add - otherwise first redraw may fail (the table is virtual)
int index = getItemCount();
scanningResults.registerAtIndex(index, result);
// setItemCount(index+1) - this seems to rebuild TableItems inside, so is slower
new TableItem(ResultTable.this, SWT.NONE);
}
getDisplay().asyncExec(() -> {
if (isDisposed())
return;
if (scanningResults.isRegistered(result)) {
// just redraw the item
int index = scanningResults.update(result);
clear(index);
}
else {
// first register, then add - otherwise first redraw may fail (the table is virtual)
int index = getItemCount();
scanningResults.registerAtIndex(index, result);
// setItemCount(index+1) - this seems to rebuild TableItems inside, so is slower
new TableItem(ResultTable.this, SWT.NONE);
}
});
}

View File

@ -34,11 +34,7 @@ public class SWTAwareStateMachine extends StateMachine {
return;
// call super asynchronously in the correct thread
display.asyncExec(new Runnable() {
public void run() {
SWTAwareStateMachine.super.notifyAboutTransition(transition);
}
});
display.asyncExec(() -> SWTAwareStateMachine.super.notifyAboutTransition(transition));
}
}

View File

@ -144,18 +144,14 @@ public class SelectFetchersDialog extends AbstractModalDialog {
shell.pack();
cancelButton.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
shell.close();
shell.dispose();
}
cancelButton.addListener(SWT.Selection, e -> {
shell.close();
shell.dispose();
});
okButton.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
saveFetchersToRegistry(selectedFetchersList.getItems());
shell.close();
shell.dispose();
}
okButton.addListener(SWT.Selection, event -> {
saveFetchersToRegistry(selectedFetchersList.getItems());
shell.close();
shell.dispose();
});
}

View File

@ -21,18 +21,16 @@ public class Startup {
public void onStart() {
if (guiConfig.isFirstRun) {
new GoogleAnalytics().asyncReport("First run");
Display.getCurrent().asyncExec(new Runnable() {
public void run() {
GettingStartedDialog dialog = new GettingStartedDialog();
if (Platform.CRIPPLED_WINDOWS)
dialog.prependText(Labels.getLabel("text.crippledWindowsInfo"));
if (Platform.GNU_JAVA)
dialog.prependText(Labels.getLabel("text.gnuJavaInfo"));
Display.getCurrent().asyncExec(() -> {
GettingStartedDialog dialog = new GettingStartedDialog();
if (Platform.CRIPPLED_WINDOWS)
dialog.prependText(Labels.getLabel("text.crippledWindowsInfo"));
if (Platform.GNU_JAVA)
dialog.prependText(Labels.getLabel("text.gnuJavaInfo"));
shell.forceActive();
dialog.open();
guiConfig.isFirstRun = false;
}
shell.forceActive();
dialog.open();
guiConfig.isFirstRun = false;
});
}
else if (!Version.getVersion().equals(guiConfig.lastRunVersion)) {

View File

@ -1,127 +1,125 @@
package net.azib.ipscan.gui.actions;
import net.azib.ipscan.config.LoggerFactory;
import net.azib.ipscan.feeders.FeederException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.widgets.*;
import java.net.*;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import static java.net.NetworkInterface.getNetworkInterfaces;
/**
* FeederActions
* TODO: tests
*
* @author Anton Keks
*/
public class FeederActions {
static final Logger LOG = LoggerFactory.getLogger();
public static class HostnameButton extends SelectionAdapter implements TraverseListener {
private Text hostnameText;
private Text ipText;
private Combo netmaskCombo;
public HostnameButton(Text hostnameText, Text ipText, Combo netmaskCombo) {
this.hostnameText = hostnameText;
this.ipText = ipText;
this.netmaskCombo = netmaskCombo;
}
public void widgetSelected(SelectionEvent event) {
String hostname = hostnameText.getText();
try {
if (hostname.equals(InetAddress.getLocalHost().getHostName())) {
askLocalIPAddress();
}
else {
// resolve remote address
InetAddress address = InetAddress.getByName(hostname);
ipText.setText(address.getHostAddress());
// now update the hostname itself using a reverse lookup
String realHostname = address.getCanonicalHostName();
if (!address.getHostAddress().equals(realHostname)) {
// if a hostname was returned, not the same IP address
hostnameText.setText(realHostname);
hostnameText.setSelection(realHostname.length());
}
}
}
catch (UnknownHostException e) {
throw new FeederException("invalidHostname");
}
}
public void keyTraversed(TraverseEvent e) {
if (e.detail == SWT.TRAVERSE_RETURN) {
widgetSelected(null);
e.doit = false;
}
}
/**
* Asks user which local IP address they want to use
*/
private void askLocalIPAddress() {
try {
Menu popupMenu = new Menu(Display.getCurrent().getActiveShell(), SWT.POP_UP);
Listener menuItemListener = new Listener() {
public void handleEvent(Event event) {
MenuItem menuItem = (MenuItem) event.widget;
String address = (String) menuItem.getData();
ipText.setText(address.substring(0, address.lastIndexOf('/')));
netmaskCombo.setText(address.substring(address.lastIndexOf('/')));
// netmaskCombo.traverse(SWT.TRAVERSE_RETURN);
menuItem.getParent().dispose();
}
};
for (Enumeration<NetworkInterface> i = getNetworkInterfaces(); i.hasMoreElements(); ) {
NetworkInterface networkInterface = i.nextElement();
for (InterfaceAddress ifaddr : networkInterface.getInterfaceAddresses()) {
if (ifaddr == null) continue;
InetAddress address = ifaddr.getAddress();
if (address instanceof Inet4Address && !address.isLoopbackAddress()) {
MenuItem menuItem = new MenuItem(popupMenu, 0);
menuItem.setText(networkInterface.getDisplayName() + ": " + address.getHostAddress());
menuItem.setData(address.getHostAddress() + "/" + ifaddr.getNetworkPrefixLength());
menuItem.addListener(SWT.Selection, menuItemListener);
}
}
}
if (popupMenu.getItemCount() > 1) {
popupMenu.setLocation(Display.getCurrent().getCursorLocation());
popupMenu.setVisible(true);
}
else {
// emulate click on the single menu item
if (popupMenu.getItemCount() == 1) {
Event event = new Event();
event.widget = popupMenu.getItem(0);
menuItemListener.handleEvent(event);
popupMenu.dispose();
}
// otherwise, unable to retrieve any sane local addresses,
// leave the field as-is, which probably shows the loopback address already
}
}
catch (SocketException e) {
LOG.log(Level.FINE, "Cannot enumerate network interfaces", e);
}
}
}
}
package net.azib.ipscan.gui.actions;
import net.azib.ipscan.config.LoggerFactory;
import net.azib.ipscan.feeders.FeederException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.widgets.*;
import java.net.*;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import static java.net.NetworkInterface.getNetworkInterfaces;
/**
* FeederActions
* TODO: tests
*
* @author Anton Keks
*/
public class FeederActions {
static final Logger LOG = LoggerFactory.getLogger();
public static class HostnameButton extends SelectionAdapter implements TraverseListener {
private Text hostnameText;
private Text ipText;
private Combo netmaskCombo;
public HostnameButton(Text hostnameText, Text ipText, Combo netmaskCombo) {
this.hostnameText = hostnameText;
this.ipText = ipText;
this.netmaskCombo = netmaskCombo;
}
public void widgetSelected(SelectionEvent event) {
String hostname = hostnameText.getText();
try {
if (hostname.equals(InetAddress.getLocalHost().getHostName())) {
askLocalIPAddress();
}
else {
// resolve remote address
InetAddress address = InetAddress.getByName(hostname);
ipText.setText(address.getHostAddress());
// now update the hostname itself using a reverse lookup
String realHostname = address.getCanonicalHostName();
if (!address.getHostAddress().equals(realHostname)) {
// if a hostname was returned, not the same IP address
hostnameText.setText(realHostname);
hostnameText.setSelection(realHostname.length());
}
}
}
catch (UnknownHostException e) {
throw new FeederException("invalidHostname");
}
}
public void keyTraversed(TraverseEvent e) {
if (e.detail == SWT.TRAVERSE_RETURN) {
widgetSelected(null);
e.doit = false;
}
}
/**
* Asks user which local IP address they want to use
*/
private void askLocalIPAddress() {
try {
Menu popupMenu = new Menu(Display.getCurrent().getActiveShell(), SWT.POP_UP);
Listener menuItemListener = event -> {
MenuItem menuItem = (MenuItem) event.widget;
String address = (String) menuItem.getData();
ipText.setText(address.substring(0, address.lastIndexOf('/')));
netmaskCombo.setText(address.substring(address.lastIndexOf('/')));
// netmaskCombo.traverse(SWT.TRAVERSE_RETURN);
menuItem.getParent().dispose();
};
for (Enumeration<NetworkInterface> i = getNetworkInterfaces(); i.hasMoreElements(); ) {
NetworkInterface networkInterface = i.nextElement();
for (InterfaceAddress ifaddr : networkInterface.getInterfaceAddresses()) {
if (ifaddr == null) continue;
InetAddress address = ifaddr.getAddress();
if (address instanceof Inet4Address && !address.isLoopbackAddress()) {
MenuItem menuItem = new MenuItem(popupMenu, 0);
menuItem.setText(networkInterface.getDisplayName() + ": " + address.getHostAddress());
menuItem.setData(address.getHostAddress() + "/" + ifaddr.getNetworkPrefixLength());
menuItem.addListener(SWT.Selection, menuItemListener);
}
}
}
if (popupMenu.getItemCount() > 1) {
popupMenu.setLocation(Display.getCurrent().getCursorLocation());
popupMenu.setVisible(true);
}
else {
// emulate click on the single menu item
if (popupMenu.getItemCount() == 1) {
Event event = new Event();
event.widget = popupMenu.getItem(0);
menuItemListener.handleEvent(event);
popupMenu.dispose();
}
// otherwise, unable to retrieve any sane local addresses,
// leave the field as-is, which probably shows the loopback address already
}
}
catch (SocketException e) {
LOG.log(Level.FINE, "Cannot enumerate network interfaces", e);
}
}
}
}

View File

@ -147,16 +147,14 @@ public class HelpMenuActions {
// show the box in the SWT thread
final String messageToShow = message;
final int messageStyleToShow = messageStyle;
Display.getDefault().asyncExec(new Runnable() {
public void run() {
statusBar.setStatusText(null);
if (messageToShow == null) return;
MessageBox messageBox = new MessageBox(statusBar.getShell(), messageStyleToShow | SWT.SHEET);
messageBox.setText(Version.getFullName());
messageBox.setMessage(messageToShow);
if (messageBox.open() == SWT.YES) {
BrowserLauncher.openURL(Version.DOWNLOAD_URL);
}
Display.getDefault().asyncExec(() -> {
statusBar.setStatusText(null);
if (messageToShow == null) return;
MessageBox messageBox = new MessageBox(statusBar.getShell(), messageStyleToShow | SWT.SHEET);
messageBox.setText(Version.getFullName());
messageBox.setMessage(messageToShow);
if (messageBox.open() == SWT.YES) {
BrowserLauncher.openURL(Version.DOWNLOAD_URL);
}
});
}

View File

@ -181,11 +181,7 @@ public class ScanMenuActions {
// in case of isSelection we need to create our filter
ScanningResultFilter filter = null;
if (isSelection) {
filter = new ScanningResultFilter() {
public boolean apply(int index, ScanningResult result) {
return resultTable.isSelected(index);
}
};
filter = (index, result) -> resultTable.isSelected(index);
}
exportProcessor.process(resultTable.getScanningResults(), filter);

View File

@ -250,27 +250,25 @@ public class StartStopScanningAction implements SelectionListener, ScanningProgr
public void updateProgress(final InetAddress currentAddress, final int runningThreads, final int percentageComplete) {
if (display.isDisposed()) return;
display.asyncExec(new Runnable() {
public void run() {
if (statusBar.isDisposed()) return;
// update status bar
if (currentAddress != null) {
statusBar.setStatusText(Labels.getLabel("state.scanning") + currentAddress.getHostAddress());
}
statusBar.setRunningThreads(runningThreads);
statusBar.setProgress(percentageComplete);
if (taskBarItem != null) taskBarItem.setProgress(percentageComplete);
// show percentage in main window title
if (!stateMachine.inState(IDLE))
statusBar.getShell().setText(percentageComplete + "% - " + mainWindowTitle);
else
statusBar.getShell().setText(mainWindowTitle);
display.asyncExec(() -> {
if (statusBar.isDisposed()) return;
// change button image according to the current state
button.setImage(buttonImages[stateMachine.getState().ordinal()]);
// update status bar
if (currentAddress != null) {
statusBar.setStatusText(Labels.getLabel("state.scanning") + currentAddress.getHostAddress());
}
statusBar.setRunningThreads(runningThreads);
statusBar.setProgress(percentageComplete);
if (taskBarItem != null) taskBarItem.setProgress(percentageComplete);
// show percentage in main window title
if (!stateMachine.inState(IDLE))
statusBar.getShell().setText(percentageComplete + "% - " + mainWindowTitle);
else
statusBar.getShell().setText(mainWindowTitle);
// change button image according to the current state
button.setImage(buttonImages[stateMachine.getState().ordinal()]);
});
}

View File

@ -82,15 +82,13 @@ public abstract class AbstractFeederGUI extends Composite implements FeederCreat
localName = localInterface.getAddress().getHostName();
}
}
Display.getDefault().asyncExec(new Runnable() {
public void run() {
// fill the IP and hostname fields with local hostname and IP addresses
if ("".equals(hostnameText.getText()))
hostnameText.setText(localName);
if ("".equals(ipText.getText())) {
ipText.setText(localInterface.getAddress().getHostAddress());
afterLocalHostInfoFilled(localInterface);
}
Display.getDefault().asyncExec(() -> {
// fill the IP and hostname fields with local hostname and IP addresses
if ("".equals(hostnameText.getText()))
hostnameText.setText(localName);
if ("".equals(ipText.getText())) {
ipText.setText(localInterface.getAddress().getHostAddress());
afterLocalHostInfoFilled(localInterface);
}
});
}

View File

@ -9,8 +9,6 @@ import net.azib.ipscan.feeders.Feeder;
import net.azib.ipscan.feeders.RandomFeeder;
import net.azib.ipscan.gui.actions.FeederActions;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
@ -91,14 +89,12 @@ public class RandomFeederGUI extends AbstractFeederGUI {
countSpinner.setMaximum(100000000);
countSpinner.setMinimum(1);
countSpinner.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
countSpinner.addTraverseListener(new TraverseListener() {
public void keyTraversed(TraverseEvent e) {
// this due to a bug either in SWT or GTK:
// spinner getText() returns the new value only if
// it has lost the focus first
ipPrototypeText.forceFocus();
countSpinner.forceFocus();
}
countSpinner.addTraverseListener(e -> {
// this due to a bug either in SWT or GTK:
// spinner getText() returns the new value only if
// it has lost the focus first
ipPrototypeText.forceFocus();
countSpinner.forceFocus();
});
pack();