From d20056380e41dc721e968de8831a8adebbda3346 Mon Sep 17 00:00:00 2001 From: Lisias Date: Mon, 8 Jan 2018 11:51:23 -0200 Subject: [PATCH] Using a new Exception for handling unknown disk formats. This will make handling errors easier. --- .../storage/DiskUnrecognizedException.java | 38 +++++++++++++++++++ .../ui/swt/SwtAppleCommander.java | 27 +++++++++---- .../applecommander/ui/UiBundle.properties | 2 +- 3 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/webcodepro/applecommander/storage/DiskUnrecognizedException.java diff --git a/src/main/java/com/webcodepro/applecommander/storage/DiskUnrecognizedException.java b/src/main/java/com/webcodepro/applecommander/storage/DiskUnrecognizedException.java new file mode 100644 index 0000000..6b92f9c --- /dev/null +++ b/src/main/java/com/webcodepro/applecommander/storage/DiskUnrecognizedException.java @@ -0,0 +1,38 @@ +/* + * AppleCommander - An Apple ][ image utility. + * Copyright (C) 2002 by Robert Greene + * robgreene at users.sourceforge.net + * + * This program 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. + * + * This program 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. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +package com.webcodepro.applecommander.storage; + +/** + * A DiskUnrecognizedException is thrown when the Disk Image being opened is not recognized by any image handler. + *
+ * Created at: Jan 8, 2018 + * @author Lisias Toledo + */ +public class DiskUnrecognizedException extends DiskException { + + private static final long serialVersionUID = 0xFFFFFFFF80000000L; + + /** + * Constructor for DiskFullException. + */ + public DiskUnrecognizedException(final String imagepath) { + super("DiskUnrecognizedException", imagepath); + } +} diff --git a/src/main/java/com/webcodepro/applecommander/ui/swt/SwtAppleCommander.java b/src/main/java/com/webcodepro/applecommander/ui/swt/SwtAppleCommander.java index 06f5c83..74b990e 100644 --- a/src/main/java/com/webcodepro/applecommander/ui/swt/SwtAppleCommander.java +++ b/src/main/java/com/webcodepro/applecommander/ui/swt/SwtAppleCommander.java @@ -39,6 +39,7 @@ import org.eclipse.swt.widgets.ToolItem; import com.webcodepro.applecommander.storage.Disk; import com.webcodepro.applecommander.storage.FormattedDisk; import com.webcodepro.applecommander.storage.Disk.FilenameFilter; +import com.webcodepro.applecommander.storage.DiskUnrecognizedException; import com.webcodepro.applecommander.ui.AppleCommander; import com.webcodepro.applecommander.ui.UiBundle; import com.webcodepro.applecommander.ui.UserPreferences; @@ -171,19 +172,31 @@ public class SwtAppleCommander implements Listener { try { Disk disk = new Disk(fullpath); FormattedDisk[] formattedDisks = disk.getFormattedDisks(); - if (formattedDisks != null) { - DiskWindow window = new DiskWindow(shell, formattedDisks, imageManager); - window.open(); - } else { - showUnrecognizedDiskFormatMessage(fullpath); - } + DiskWindow window = new DiskWindow(shell, formattedDisks, imageManager); + window.open(); + } catch (DiskUnrecognizedException e) { + showUnrecognizedDiskFormatMessage(fullpath); } catch (Exception ignored) { ignored.printStackTrace(); - showUnrecognizedDiskFormatMessage(fullpath); + showUnexpectedErrorMessage(fullpath); } } } + /** + * Displays the unrecognized disk format message. + * @param fullpath + */ + protected void showUnexpectedErrorMessage(String fullpath) { + Shell finalShell = shell; + MessageBox box = new MessageBox(finalShell, SWT.ICON_ERROR | SWT.OK); + box.setText(textBundle.get("SwtAppleCommander.UnexpectedErrorTitle")); //$NON-NLS-1$ + box.setMessage( + textBundle.format("SwtAppleCommander.UnexpectedErrorMessage", //$NON-NLS-1$ + fullpath)); + box.open(); + } + /** * Displays the unrecognized disk format message. * @param fullpath diff --git a/src/main/resources/com/webcodepro/applecommander/ui/UiBundle.properties b/src/main/resources/com/webcodepro/applecommander/ui/UiBundle.properties index 5742885..483b4ac 100644 --- a/src/main/resources/com/webcodepro/applecommander/ui/UiBundle.properties +++ b/src/main/resources/com/webcodepro/applecommander/ui/UiBundle.properties @@ -270,7 +270,7 @@ GraphicsFilterAdapter.BadImageMessage=Unexpected graphic file encountered\! # SwtAppleCommander SwtAppleCommander.AppleCommander=AppleCommander SwtAppleCommander.UnrecognizedFormatTitle=Unrecognized Disk Format -SwtAppleCommander.UnrecognizedFormatMessage=Unable to load "{0}".\n\nAppleCommander did not recognize the format\nof the disk. Either this is a new format\nor a protected disk.\n\nSorry\! +SwtAppleCommander.UnrecognizedFormatMessage=Unable to load "{0}".\n\nAppleCommander did not recognize the format of the disk. Either this is a new format or a protected disk.\n\nSorry\! SwtAppleCommander.OpenDiskImageTooltip=Open a disk image (Ctrl+O) SwtAppleCommander.CreateDiskImageTooltip=Create a disk image (Ctrl+C) SwtAppleCommander.CompareDiskImageTooltip=Compare two disk images (Ctrl+E)