Search in sources :

Example 1 with FileCollisionDialog

use of com.mucommander.ui.dialog.file.FileCollisionDialog in project mucommander by mucommander.

the class AppearancePanel method importLookAndFeelLibrary.

/**
 * Tries to import the specified library in the extensions folder.
 * <p>
 * If there is already a file with the same name in the extensions folder,
 * this method will ask the user for confirmation before overwriting it.
 * </p>
 *
 * @param library library to import in the extensions folder.
 * @return <code>true</code> if the library was imported, <code>false</code> if the user cancelled the operation.
 * @throws IOException if an I/O error occurred while importing the library
 */
private boolean importLookAndFeelLibrary(AbstractFile library) throws IOException {
    // Tries to import the file, but if a version of it is already present in the extensions folder,
    // asks the user for confirmation.
    AbstractFile destFile = ExtensionManager.getExtensionsFile(library.getName());
    int collision = FileCollisionChecker.checkForCollision(library, destFile);
    if (collision != FileCollisionChecker.NO_COLLISION) {
        // Do not offer the multiple files mode options such as 'skip' and 'apply to all'
        DialogAction action = new FileCollisionDialog(parent, parent, collision, library, destFile, false, false).getActionValue();
        // User chose to overwrite the file
        if (action == FileCollisionDialog.FileCollisionAction.OVERWRITE) {
        // Simply continue and file will be overwritten
        } else if (action == FileCollisionDialog.FileCollisionAction.OVERWRITE_IF_OLDER) {
            // Overwrite if the source is more recent than the destination
            if (library.getDate() <= destFile.getDate()) {
                return false;
            }
        // Simply continue and file will be overwritten
        } else if (action == FileCollisionDialog.FileCollisionAction.OVERWRITE_IF_SIZE_DIFFERS) {
            // Overwrite if the source and target file size differs
            if (library.getSize() == destFile.getSize()) {
                return false;
            }
        // Simply continue and file will be overwritten
        } else // User chose to cancel or closed the dialog
        {
            return false;
        }
    }
    return ExtensionManager.importLibrary(library, true);
}
Also used : AbstractFile(com.mucommander.commons.file.AbstractFile) FileCollisionDialog(com.mucommander.ui.dialog.file.FileCollisionDialog) DialogAction(com.mucommander.ui.dialog.DialogAction)

Example 2 with FileCollisionDialog

use of com.mucommander.ui.dialog.file.FileCollisionDialog in project mucommander by mucommander.

the class AppearancePanel method exportTheme.

/**
 * Exports the specified theme.
 *
 * @param theme theme to export.
 */
private void exportTheme(Theme theme) {
    JFileChooser chooser;
    AbstractFile file;
    chooser = createFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    chooser.addChoosableFileFilter(new ExtensionFileFilter("xml", Translator.get("prefs_dialog.xml_file")));
    chooser.setDialogTitle(Translator.get("prefs_dialog.export_theme", theme.getName()));
    if (chooser.showDialog(parent, Translator.get("prefs_dialog.export")) == JFileChooser.APPROVE_OPTION) {
        file = FileFactory.getFile(chooser.getSelectedFile().getAbsolutePath());
        lastSelectedFolder = file.getParent();
        // Makes sure the file's extension is .xml.
        try {
            if (// Note: getExtension() may return null if no extension
            !"xml".equalsIgnoreCase(file.getExtension()))
                file = lastSelectedFolder.getDirectChild(file.getName() + ".xml");
            int collision = FileCollisionChecker.checkForCollision(null, file);
            if (collision != FileCollisionChecker.NO_COLLISION) {
                // Do not offer the multiple files mode options such as 'skip' and 'apply to all'
                DialogAction action = new FileCollisionDialog(parent, parent, collision, null, file, false, false).getActionValue();
                // User chose to overwrite the file
                if (action == FileCollisionDialog.FileCollisionAction.OVERWRITE) {
                // Simply continue and file will be overwritten
                } else // User chose to cancel or closed the dialog
                {
                    return;
                }
            }
            // Exports the theme.
            ThemeManager.exportTheme(theme, (java.io.File) file.getUnderlyingFileObject());
            // changes.
            if (lastSelectedFolder.equals(ThemeManager.getCustomThemesFolder()))
                populateThemes(theme);
        }// Notifies users of errors.
         catch (Exception exception) {
            InformationDialog.showErrorDialog(this, Translator.get("write_error"), Translator.get("cannot_write_file", file.getName()));
        }
    }
}
Also used : AbstractFile(com.mucommander.commons.file.AbstractFile) FileCollisionDialog(com.mucommander.ui.dialog.file.FileCollisionDialog) JFileChooser(javax.swing.JFileChooser) DialogAction(com.mucommander.ui.dialog.DialogAction) IOException(java.io.IOException)

Example 3 with FileCollisionDialog

use of com.mucommander.ui.dialog.file.FileCollisionDialog in project mucommander by mucommander.

the class AbstractCopyJob method checkForCollision.

/**
 * Checks if there is a file collision (file exists in the destination).
 * If there is no collision this method returns destFile.
 * If there is a collision this method returns: <ul>
 *  <li>null if a user cancelled the transfer
 *  <li>null if a user skipped the file
 *  <li>destFile if a user resumed the transfer (and sets append flag)
 *  <li>destFile if a user has chosen to overwrite the file
 *  <li>new file if a user renamed the file
 *  </ul>
 * @param file a source file
 * @param destFolder a destination folder
 * @param destFile a destination file
 * @param allowCaseVariation if true,
 * @return destFile the new destination file
 */
protected AbstractFile checkForCollision(AbstractFile file, AbstractFile destFolder, AbstractFile destFile, boolean allowCaseVariation) {
    append = false;
    while (true) {
        // Check for file collisions (file exists in the destination, destination subfolder of source, ...)
        // if a default action hasn't been specified
        int collision = FileCollisionChecker.checkForCollision(file, destFile);
        // destination being the same.
        if (allowCaseVariation && collision == FileCollisionChecker.SAME_SOURCE_AND_DESTINATION) {
            String sourceFileName = file.getName();
            String destFileName = destFile.getName();
            if (sourceFileName.equalsIgnoreCase(destFileName) && !sourceFileName.equals(destFileName)) {
                break;
            }
        }
        // Handle collision, asking the user what to do or using a default action to resolve the collision
        if (collision != FileCollisionChecker.NO_COLLISION) {
            FileCollisionDialog.FileCollisionAction choice;
            // Use default action if one has been set, if not show up a dialog
            if (defaultFileExistsAction == FileCollisionDialog.FileCollisionAction.ASK) {
                FileCollisionDialog dialog = new FileCollisionDialog(getProgressDialog(), getMainFrame(), collision, file, destFile, true, true);
                choice = (FileCollisionDialog.FileCollisionAction) waitForUserResponse(dialog);
                // If 'apply to all' was selected, this choice will be used for any other files (user will not be asked again)
                if (dialog.applyToAllSelected()) {
                    defaultFileExistsAction = choice;
                }
            } else {
                choice = defaultFileExistsAction;
            }
            // Cancel, skip or close dialog
            if (choice == QuestionDialog.DIALOG_DISPOSED_ACTION || choice == FileCollisionDialog.FileCollisionAction.CANCEL) {
                interrupt();
                return null;
            } else // Skip file
            if (choice == FileCollisionDialog.FileCollisionAction.SKIP) {
                return null;
            } else // Append to file (resume file copy)
            if (choice == FileCollisionDialog.FileCollisionAction.RESUME) {
                append = true;
                break;
            } else // Overwrite file
            if (choice == FileCollisionDialog.FileCollisionAction.OVERWRITE) {
                // Do nothing, simply continue
                break;
            } else // Overwrite file if destination is older
            if (choice == FileCollisionDialog.FileCollisionAction.OVERWRITE_IF_OLDER) {
                // Overwrite if file is newer (strictly)
                if (file.getDate() <= destFile.getDate()) {
                    return null;
                }
                break;
            } else if (choice == FileCollisionDialog.FileCollisionAction.OVERWRITE_IF_SIZE_DIFFERS) {
                if (file.getSize() == destFile.getSize()) {
                    return null;
                }
                break;
            } else if (choice == FileCollisionDialog.FileCollisionAction.RENAME) {
                setPaused(true);
                FileCollisionRenameDialog dlg = new FileCollisionRenameDialog(getMainFrame(), destFile);
                String destFileName = (String) waitForUserResponseObject(dlg);
                setPaused(false);
                if (destFileName != null) {
                    destFile = createDestinationFile(file, destFolder, destFileName);
                } else {
                    // turn on FileCollisionDialog, so we don't loop indefinitely
                    defaultFileExistsAction = FileCollisionDialog.FileCollisionAction.ASK;
                }
                // continue with collision checking
                continue;
            }
        }
        // no collision
        break;
    }
    return destFile;
}
Also used : FileCollisionDialog(com.mucommander.ui.dialog.file.FileCollisionDialog) FileCollisionRenameDialog(com.mucommander.ui.dialog.file.FileCollisionRenameDialog)

Example 4 with FileCollisionDialog

use of com.mucommander.ui.dialog.file.FileCollisionDialog in project mucommander by mucommander.

the class MkdirJob method processFile.

// //////////////////////////
// FileJob implementation //
// //////////////////////////
/**
 * Creates the new directory in the destination folder.
 */
@Override
protected boolean processFile(AbstractFile file, Object recurseParams) {
    // Stop if interrupted (although there is no way to stop the job at this time)
    if (getState() == FileJobState.INTERRUPTED)
        return false;
    do {
        try {
            LOGGER.debug("Creating: {}", file);
            // Check for file collisions, i.e. if the file already exists in the destination
            int collision = FileCollisionChecker.checkForCollision(null, file);
            if (collision != FileCollisionChecker.NO_COLLISION) {
                // File already exists in destination, ask the user what to do (cancel, overwrite,...) but
                // do not offer the multiple files' mode options such as 'skip' and 'apply to all'.
                DialogAction choice = waitForUserResponse(new FileCollisionDialog(getMainFrame(), getMainFrame(), collision, null, file, false, false));
                // Overwrite file
                if (choice == FileCollisionDialog.FileCollisionAction.OVERWRITE) {
                    // Delete the file
                    file.delete();
                } else // Cancel or dialog close (return)
                // else if (choice==-1 || choice==FileCollisionDialog.OverwriteAction.CANCEL) {
                {
                    interrupt();
                    return false;
                }
            }
            // Create file
            if (mkfileMode) {
                // Use mkfile
                if (allocateSpace == -1) {
                    file.mkfile();
                } else // Allocate the requested number of bytes
                {
                    OutputStream mkfileOut = null;
                    try {
                        // using RandomAccessOutputStream if we can have one
                        if (file.isFileOperationSupported(FileOperation.RANDOM_WRITE_FILE)) {
                            mkfileOut = file.getRandomAccessOutputStream();
                            ((RandomAccessOutputStream) mkfileOut).setLength(allocateSpace);
                        } else // manually otherwise
                        {
                            mkfileOut = file.getOutputStream();
                            // Use BufferPool to avoid excessive memory allocation and garbage collection
                            byte[] buffer = BufferPool.getByteArray();
                            int bufferSize = buffer.length;
                            try {
                                long remaining = allocateSpace;
                                int nbWrite;
                                while (remaining > 0 && getState() != FileJobState.INTERRUPTED) {
                                    nbWrite = (int) (remaining > bufferSize ? bufferSize : remaining);
                                    mkfileOut.write(buffer, 0, nbWrite);
                                    remaining -= nbWrite;
                                }
                            } finally {
                                BufferPool.releaseByteArray(buffer);
                            }
                        }
                    } finally {
                        if (mkfileOut != null)
                            try {
                                mkfileOut.close();
                            } catch (IOException e) {
                            }
                    }
                }
            } else // Create directory
            {
                file.mkdir();
            }
            // Resolve new file instance now that it exists: remote files do not update file attributes after
            // creation, we need to get an instance that reflects the newly created file attributes
            file = FileFactory.getFile(file.getURL());
            // Select newly created file when job is finished
            selectFileWhenFinished(file);
            // Return Success
            return true;
        } catch (IOException e) {
            // thrown, this is normal behavior
            if (mkfileMode && getState() == FileJobState.INTERRUPTED)
                return false;
            LOGGER.debug("IOException caught", e);
            DialogAction action = showErrorDialog(Translator.get("error"), Translator.get(mkfileMode ? "cannot_write_file" : "cannot_create_folder", file.getAbsolutePath()), Arrays.asList(FileJobAction.RETRY, FileJobAction.CANCEL));
            // Retry (loop)
            if (action == FileJobAction.RETRY)
                continue;
            // Return Failure
            return false;
        }
    } while (true);
}
Also used : FileCollisionDialog(com.mucommander.ui.dialog.file.FileCollisionDialog) DialogAction(com.mucommander.ui.dialog.DialogAction) OutputStream(java.io.OutputStream) RandomAccessOutputStream(com.mucommander.commons.io.RandomAccessOutputStream) RandomAccessOutputStream(com.mucommander.commons.io.RandomAccessOutputStream) IOException(java.io.IOException)

Example 5 with FileCollisionDialog

use of com.mucommander.ui.dialog.file.FileCollisionDialog in project mucommander by mucommander.

the class ArchiveJob method jobStarted.

// //////////////////////
// Overridden methods //
// //////////////////////
/**
 * Overriden method to initialize the archiver and handle the case where the destination file already exists.
 */
@Override
protected void jobStarted() {
    super.jobStarted();
    // Check for file collisions, i.e. if the file already exists in the destination
    int collision = FileCollisionChecker.checkForCollision(null, destFile);
    if (collision != FileCollisionChecker.NO_COLLISION) {
        // File already exists in destination, ask the user what to do (cancel, overwrite,...) but
        // do not offer the multiple files mode options such as 'skip' and 'apply to all'.
        DialogAction choice = waitForUserResponse(new FileCollisionDialog(getProgressDialog(), getMainFrame(), collision, null, destFile, false, false));
        // Overwrite file
        if (choice == FileCollisionDialog.FileCollisionAction.OVERWRITE) {
        // Do nothing, simply continue and file will be overwritten
        } else // 'Cancel' or close dialog interrupts the job
        {
            interrupt();
            return;
        }
    }
    // Loop for retry
    do {
        try {
            // Tries to get an Archiver instance.
            this.archiver = Archiver.getArchiver(destFile, archiveFormat);
            this.archiver.setComment(archiveComment);
            break;
        } catch (Exception e) {
            DialogAction choice = showErrorDialog(Translator.get("pack_dialog.error_title"), Translator.get("cannot_write_file", destFile.getName()), Arrays.asList(FileJobAction.CANCEL, FileJobAction.RETRY));
            // Retry loops
            if (choice == FileJobAction.RETRY)
                continue;
            // 'Cancel' or close dialog interrupts the job
            interrupt();
            return;
        }
    } while (true);
}
Also used : FileCollisionDialog(com.mucommander.ui.dialog.file.FileCollisionDialog) DialogAction(com.mucommander.ui.dialog.DialogAction) IOException(java.io.IOException)

Aggregations

FileCollisionDialog (com.mucommander.ui.dialog.file.FileCollisionDialog)8 DialogAction (com.mucommander.ui.dialog.DialogAction)7 IOException (java.io.IOException)6 AbstractFile (com.mucommander.commons.file.AbstractFile)4 JFileChooser (javax.swing.JFileChooser)3 RandomAccessOutputStream (com.mucommander.commons.io.RandomAccessOutputStream)1 FileCollisionRenameDialog (com.mucommander.ui.dialog.file.FileCollisionRenameDialog)1 OutputStream (java.io.OutputStream)1