Cleaner error handling

This commit is contained in:
xfarrow 2023-08-09 13:00:40 +02:00
parent abeae2f4a4
commit 7be8f4a2e1
1 changed files with 79 additions and 32 deletions

View File

@ -193,28 +193,40 @@ public class Desktop extends JFrame implements IDesktopFrame {
* @param directory * @param directory
*/ */
public void drawComponentsForDirectory(String directory) { public void drawComponentsForDirectory(String directory) {
// Clear any selection
unselectAllNodes(); unselectAllNodes();
// Set the current working directory
controller.setCurrentWorkingDirectory(directory); controller.setCurrentWorkingDirectory(directory);
// Only loadDesktop() can tell whether we have the permission to view
// the content of the directory
try { try {
// Load the desktop content
loadDesktop(); loadDesktop();
} catch (Exception ex) { } catch (Exception ex) {
// Handle lack of permissions or other errors
if (ex.getMessage().contains("Permission denied")) { if (ex.getMessage().contains("Permission denied")) {
JOptionPane.showMessageDialog(new JFrame(), "Permission denied", showErrorDialog("Permission denied", "Permission denied");
"Permission denied", JOptionPane.ERROR_MESSAGE);
} else { } else {
JOptionPane.showMessageDialog(new JFrame(), showErrorDialog("Error",
"An unknown error has occurred: " + ex.getMessage(), "An unknown error has occurred. Details: "
"Error", JOptionPane.ERROR_MESSAGE); + ex.getMessage());
} }
controller.setCurrentWorkingDirectory( controller.setCurrentWorkingDirectory(
controller.getLastSafeDirectory()); controller.getLastSafeDirectory());
return; return;
} }
// Load the tree structure
loadTree(); loadTree();
// Update the path text box
pathTextBox.setText(controller.getCurrentWorkingDirectory()); pathTextBox.setText(controller.getCurrentWorkingDirectory());
// Store the current directory as the last safe directory
controller.setLastSafeDirectory(directory); controller.setLastSafeDirectory(directory);
// Refresh the display
repaint(); repaint();
revalidate(); revalidate();
} }
@ -319,7 +331,6 @@ public class Desktop extends JFrame implements IDesktopFrame {
// Download // Download
case 0 : case 0 :
JFileChooser fileChooser = new JFileChooser(); JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode( fileChooser.setFileSelectionMode(
JFileChooser.DIRECTORIES_ONLY); JFileChooser.DIRECTORIES_ONLY);
@ -656,13 +667,16 @@ public class Desktop extends JFrame implements IDesktopFrame {
controller.getCurrentWorkingDirectory(), newName); controller.getCurrentWorkingDirectory(), newName);
try { try {
controller.rename(oldPath, newPath); controller.rename(oldPath, newPath);
} catch (SftpException e1) { } catch (Exception e1) {
if (e1.getMessage().contains("Permission denied")) { if (e1.getMessage().contains("Permission denied")) {
JOptionPane.showMessageDialog(new JFrame(), showErrorDialog("Not enough permissions",
"Not enough permissions to rename this element", "Not enough permissions to rename this element");
"Permission denied", JOptionPane.ERROR_MESSAGE); } else {
return; showErrorDialog("Error",
"An unknown error has occurred: "
+ e1.getMessage());
} }
return;
} }
drawComponentsForDirectory( drawComponentsForDirectory(
controller.getCurrentWorkingDirectory()); // TODO controller.getCurrentWorkingDirectory()); // TODO
@ -726,14 +740,16 @@ public class Desktop extends JFrame implements IDesktopFrame {
newFileName); newFileName);
try { try {
controller.touch(newFilePath); controller.touch(newFilePath);
} catch (SftpException e1) { } catch (Exception e1) {
if (e1.getMessage().contains("Permission denied")) { if (e1.getMessage().contains("Permission denied")) {
JOptionPane.showMessageDialog(new JFrame(), showErrorDialog("Permission denied",
"Not enough permissions to create a file here", "Not enough permissions to create a file here");
"Permission denied", } else {
JOptionPane.ERROR_MESSAGE); showErrorDialog("Error",
return; "An unknown error has occurred: "
+ e1.getMessage());
} }
return;
} }
drawComponentsForDirectory( drawComponentsForDirectory(
controller.getCurrentWorkingDirectory()); controller.getCurrentWorkingDirectory());
@ -768,14 +784,16 @@ public class Desktop extends JFrame implements IDesktopFrame {
newFolderName); newFolderName);
try { try {
controller.mkdir(newFolderPath); controller.mkdir(newFolderPath);
} catch (SftpException e1) { } catch (Exception e1) {
if (e1.getMessage().contains("Permission denied")) { if (e1.getMessage().contains("Permission denied")) {
JOptionPane.showMessageDialog(new JFrame(), showErrorDialog("Permission denied",
"Not enough permissions to create a folder here", "Not enough permissions to create a folder here");
"Permission denied", } else {
JOptionPane.ERROR_MESSAGE); showErrorDialog("Error",
return; "An unknown error has occurred: "
+ e1.getMessage());
} }
return;
} }
drawComponentsForDirectory( drawComponentsForDirectory(
controller.getCurrentWorkingDirectory()); // TODO: controller.getCurrentWorkingDirectory()); // TODO:
@ -841,12 +859,15 @@ public class Desktop extends JFrame implements IDesktopFrame {
if (choice == 0) { // yes if (choice == 0) { // yes
try { try {
controller.deleteSelectedNodes(); controller.deleteSelectedNodes();
} catch (SftpException e1) { } catch (Exception e1) {
if (e1.getMessage().contains("Permission denied")) { if (e1.getMessage().contains("Permission denied")) {
JOptionPane.showMessageDialog(new JFrame(), showErrorDialog("Permission denied",
"Deletion process has encountered an item which cannot be deleted", "The deletion process has encountered an item which cannot be deleted. Details: "
"Permission denied", + e1.getMessage());
JOptionPane.ERROR_MESSAGE); } else {
showErrorDialog("Error",
"An unknown error has occurred. Details: "
+ e1.getMessage());
} }
} }
drawComponentsForDirectory( drawComponentsForDirectory(
@ -1084,8 +1105,11 @@ public class Desktop extends JFrame implements IDesktopFrame {
* Enables or disables tool bar buttons according to these propositions: * Enables or disables tool bar buttons according to these propositions:
* *
* 1. At least a selected node is selected <--> cut, copy, delete, download * 1. At least a selected node is selected <--> cut, copy, delete, download
* ENABLED 2. Only one selected node is selected <--> rename ENABLED 3. * ENABLED
* selectedToolBarOperation is not none <--> paste ENABLED *
* 2. Only one selected node is selected <--> rename ENABLED
*
* 3. selectedToolBarOperation is not none <--> paste ENABLED
*/ */
private void updateToolBarItems() { private void updateToolBarItems() {
@ -1152,4 +1176,27 @@ public class Desktop extends JFrame implements IDesktopFrame {
* ========== END Node selection/deselection ========== * ========== END Node selection/deselection ==========
*/ */
/*
* ========== BEGIN Other ==========
*
*/
/**
* Displays an error message dialog with the given title and message.
*
* @param title
* The title of the dialog.
* @param message
* The message to display.
*/
private void showErrorDialog(String title, String message) {
JOptionPane.showMessageDialog(new JFrame(), message, title,
JOptionPane.ERROR_MESSAGE);
}
/*
* ========== END Other ==========
*
*/
} }