Skip to content

Commit

Permalink
avoiding to display an error dialog when computer is offline and augm…
Browse files Browse the repository at this point in the history
  • Loading branch information
cpesch committed Sep 10, 2024
1 parent d22774e commit 046dd64
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@

package slash.common.helpers;

import javax.net.ssl.SSLException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

/**
Expand All @@ -31,6 +34,11 @@
*/

public class ExceptionHelper {
public static boolean isComputerOffline(Throwable throwable) {
return throwable instanceof ConnectException || throwable instanceof UnknownHostException ||
throwable instanceof SSLException || throwable instanceof SocketTimeoutException;
}

public static String getLocalizedMessage(Throwable throwable) {
if (throwable instanceof UnknownHostException)
return "Your computer is not connected to the Internet and\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@

import static java.lang.String.format;
import static java.util.logging.Logger.getLogger;
import static slash.common.helpers.ExceptionHelper.getLocalizedMessage;
import static slash.common.helpers.ExceptionHelper.printStackTrace;
import static slash.common.helpers.ExceptionHelper.*;
import static slash.navigation.download.State.*;

/**
Expand Down Expand Up @@ -82,8 +81,7 @@ public void run() {
performer.run();
} catch (Exception e) {
log.severe(format("Failed to download content from %s: %s %s", download.getUrl(), getLocalizedMessage(e),
e instanceof ConnectException || e instanceof UnknownHostException ||
e instanceof SSLException || e instanceof SocketTimeoutException ? "" : printStackTrace(e)));
isComputerOffline(e) ? "" : printStackTrace(e)));
downloadFailed();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@
import static javax.swing.JOptionPane.showMessageDialog;
import static javax.swing.SwingUtilities.invokeLater;
import static javax.swing.event.TableModelEvent.ALL_COLUMNS;
import static slash.common.helpers.ExceptionHelper.getLocalizedMessage;
import static slash.common.helpers.ExceptionHelper.printStackTrace;
import static slash.common.helpers.ExceptionHelper.*;
import static slash.common.helpers.ThreadHelper.createSingleThreadExecutor;
import static slash.common.io.Transfer.widthInDigits;
import static slash.common.type.CompactCalendar.fromMillis;
Expand Down Expand Up @@ -158,20 +157,17 @@ private void executeOperation(final JTable positionsTable,
public void run() {
final int[] count = new int[1];
count[0] = 0;
final Exception[] lastException = new Exception[1];
lastException[0] = null;

try {
invokeLater(new Runnable() {
public void run() {
if (positionsTable != null && rows.length > 0)
scrollToPosition(positionsTable, rows[0]);
}
invokeLater(() -> {
if (positionsTable != null && rows.length > 0)
scrollToPosition(positionsTable, rows[0]);
});
operation.performOnStart();

final Exception[] lastException = new Exception[1];
lastException[0] = null;
final int maximumRangeLength = rows.length > 99 ? rows.length / (slowOperation ? 100 : 10) : rows.length;

new ContinousRange(rows, new RangeOperation() {
public void performOnIndex(final int index) {
NavigationPosition position = positionsModel.getPosition(index);
Expand All @@ -190,12 +186,10 @@ public void performOnIndex(final int index) {
}

public void performOnRange(final int firstIndex, final int lastIndex) {
invokeLater(new Runnable() {
public void run() {
positionsModel.fireTableRowsUpdated(firstIndex, lastIndex, operation.getColumnIndex());
if (positionsTable != null) {
scrollToPosition(positionsTable, min(lastIndex + maximumRangeLength, positionsModel.getRowCount() - 1));
}
invokeLater(() -> {
positionsModel.fireTableRowsUpdated(firstIndex, lastIndex, operation.getColumnIndex());
if (positionsTable != null) {
scrollToPosition(positionsTable, min(lastIndex + maximumRangeLength, positionsModel.getRowCount() - 1));
}
});
}
Expand All @@ -207,18 +201,15 @@ public boolean isInterrupted() {
}
}).performMonotonicallyIncreasing(maximumRangeLength);

if (lastException[0] != null) {
if (lastException[0] != null && !isComputerOffline(lastException[0])) {
String errorMessage = RouteConverter.getBundle().getString(operation.getMessagePrefix() + "error");
showMessageDialog(frame,
MessageFormat.format(errorMessage, getLocalizedMessage(lastException[0])), frame.getTitle(), ERROR_MESSAGE);
}
} finally {
invokeLater(new Runnable() {
public void run() {
getNotificationManager().showNotification(MessageFormat.format(
RouteConverter.getBundle().getString("augmenting-finished"), count[0]), null);
}
});
if (lastException[0] == null || !isComputerOffline(lastException[0]))
invokeLater(() -> getNotificationManager().showNotification(MessageFormat.format(
RouteConverter.getBundle().getString("augmenting-finished"), count[0]), null));
}
}
});
Expand Down

0 comments on commit 046dd64

Please sign in to comment.