use safer close()

just shell.close() sometimes produces NPE in GA when closing the PreferencesDialog
This commit is contained in:
Anton Keks 2021-02-15 21:45:25 +02:00
parent b0c8136f65
commit 7fd0d1c5ca
10 changed files with 36 additions and 78 deletions

View File

@ -50,6 +50,13 @@ public abstract class AbstractModalDialog {
// forget the reference to the shell (this class is reused in the container)
shell = null;
}
protected void close() {
if (shell != null && !shell.isDisposed()) {
shell.close();
shell.dispose();
}
}
/**
* Populates the newly created shell with controls
@ -141,12 +148,7 @@ public abstract class AbstractModalDialog {
Button button = new Button(shell, SWT.NONE);
button.setText(Labels.getLabel("button.close"));
positionButtons(button, null);
button.addListener(SWT.Selection, event -> {
shell.close();
shell.dispose();
});
button.addListener(SWT.Selection, event -> close());
button.setFocus();
return button;
}
@ -154,7 +156,6 @@ public abstract class AbstractModalDialog {
// common listeners follow
protected static class UpButtonListener implements Listener {
private List list;
public UpButtonListener(List list) {

View File

@ -109,10 +109,7 @@ public class DetailsWindow extends AbstractModalDialog {
class TraverseListener implements Listener {
public void handleEvent(Event e) {
if (e.detail == SWT.TRAVERSE_RETURN) {
shell.close();
shell.dispose();
}
if (e.detail == SWT.TRAVERSE_RETURN) close();
}
}
}

View File

@ -3,7 +3,6 @@ package net.azib.ipscan.gui;
import net.azib.ipscan.config.FavoritesConfig;
import net.azib.ipscan.config.Labels;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.*;
@ -70,17 +69,11 @@ public class EditFavoritesDialog extends AbstractModalDialog {
shell.pack();
okButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
saveFavorites();
shell.close();
}
});
cancelButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
shell.close();
}
okButton.addListener(SWT.Selection, e -> {
saveFavorites();
close();
});
cancelButton.addListener(SWT.Selection, e -> close());
}
private void saveFavorites() {

View File

@ -7,7 +7,6 @@ import net.azib.ipscan.fetchers.Fetcher;
import net.azib.ipscan.fetchers.FetcherRegistry;
import net.azib.ipscan.gui.util.LayoutHelper;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.RowLayout;
@ -141,17 +140,11 @@ public class EditOpenersDialog extends AbstractModalDialog {
shell.pack();
okButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
saveOpeners();
shell.close();
}
});
cancelButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
shell.close();
}
okButton.addListener(SWT.Selection, e -> {
saveOpeners();
close();
});
cancelButton.addListener(SWT.Selection, e -> close());
openersList.select(0);
loadFieldsForSelection();

View File

@ -78,11 +78,8 @@ public class GettingStartedDialog extends AbstractModalDialog {
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());
closeButton.addListener(SWT.Selection, e -> close());
nextButton.addListener(SWT.Selection, e -> displayActivePage());
displayActivePage();
}

View File

@ -114,13 +114,15 @@ public class PreferencesDialog extends AbstractModalDialog {
okButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
savePreferences();
shell.close();
if (shell != null && !shell.isDisposed()) {
savePreferences();
close();
}
}
});
cancelButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
shell.close();
close();
}
});
}

View File

@ -136,14 +136,10 @@ public class SelectFetchersDialog extends AbstractModalDialog {
shell.pack();
cancelButton.addListener(SWT.Selection, e -> {
shell.close();
shell.dispose();
});
cancelButton.addListener(SWT.Selection, e -> close());
okButton.addListener(SWT.Selection, event -> {
saveFetchersToRegistry(selectedFetchersList.getItems());
shell.close();
shell.dispose();
close();
});
}

View File

@ -32,13 +32,8 @@ public class StatisticsDialog extends InfoDialog {
public void open() {
if (scanningResults.isInfoAvailable()) {
setMessage(prepareText());
if (shell != null) {
// close the same window if it is already open ('scanning incomplete')
shell.close();
shell.dispose();
}
// close the same window if it is already open ('scanning incomplete')
if (shell != null) close();
super.open();
}
else {

View File

@ -12,8 +12,6 @@ import net.azib.ipscan.fetchers.MACFetcher;
import net.azib.ipscan.gui.AbstractModalDialog;
import net.azib.ipscan.gui.util.LayoutHelper;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.*;
@ -46,17 +44,11 @@ public class MACFetcherPrefs extends AbstractModalDialog implements FetcherPrefs
positionButtonsInFormLayout(okButton, cancelButton, separator);
okButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
savePreferences();
shell.close();
}
});
cancelButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
shell.close();
}
okButton.addListener(SWT.Selection, e -> {
savePreferences();
close();
});
cancelButton.addListener(SWT.Selection, e -> close());
shell.pack();
}

View File

@ -12,8 +12,6 @@ import net.azib.ipscan.fetchers.PortTextFetcher;
import net.azib.ipscan.gui.AbstractModalDialog;
import net.azib.ipscan.gui.util.LayoutHelper;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.*;
@ -77,17 +75,11 @@ public class PortTextFetcherPrefs extends AbstractModalDialog implements Fetcher
positionButtonsInFormLayout(okButton, cancelButton, extractGroup);
okButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
savePreferences();
shell.close();
}
});
cancelButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
shell.close();
}
okButton.addListener(SWT.Selection, e -> {
savePreferences();
close();
});
cancelButton.addListener(SWT.Selection, e -> close());
shell.pack();
}