filebot/source/net/sourceforge/filebot/ui/panel/rename/RenameAction.java

151 lines
4.0 KiB
Java
Raw Normal View History

package net.sourceforge.filebot.ui.panel.rename;
2009-05-17 16:58:20 -04:00
import static java.util.Collections.*;
import static net.sourceforge.tuned.ui.TunedUtilities.*;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
2009-05-17 16:58:20 -04:00
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
2009-05-17 16:58:20 -04:00
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import net.sourceforge.filebot.ResourceManager;
class RenameAction extends AbstractAction {
private final RenameModel model;
public RenameAction(RenameModel model) {
2009-05-17 16:58:20 -04:00
this.model = model;
2009-05-17 16:58:20 -04:00
putValue(NAME, "Rename");
putValue(SMALL_ICON, ResourceManager.getIcon("action.rename"));
putValue(SHORT_DESCRIPTION, "Rename files");
}
public void actionPerformed(ActionEvent evt) {
List<Entry<File, String>> renameLog = new ArrayList<Entry<File, String>>();
try {
for (Entry<File, String> mapping : validate(model.getRenameMap(), getWindow(evt.getSource()))) {
// rename file, throw exception on failure
rename(mapping.getKey(), mapping.getValue());
// remember successfully renamed matches for history entry and possible revert
renameLog.add(mapping);
}
// renamed all matches successfully
2009-05-17 16:58:20 -04:00
if (renameLog.size() > 0) {
Logger.getLogger("ui").info(String.format("%d files renamed.", renameLog.size()));
}
} catch (Exception e) {
// could not rename one of the files, revert all changes
Logger.getLogger("ui").warning(e.getMessage());
// revert rename operations in reverse order
for (ListIterator<Entry<File, String>> iterator = renameLog.listIterator(renameLog.size()); iterator.hasPrevious();) {
Entry<File, String> mapping = iterator.previous();
2009-07-20 18:31:14 -04:00
// revert rename
File original = mapping.getKey();
File current = new File(original.getParentFile(), mapping.getValue());
if (current.renameTo(original)) {
// remove reverted rename operation from log
iterator.remove();
2009-07-20 18:31:14 -04:00
} else {
// failed to revert rename operation
Logger.getLogger("ui").severe("Failed to revert file: " + mapping.getValue());
}
}
}
2009-05-17 16:58:20 -04:00
// remove renamed matches
for (Entry<File, ?> entry : renameLog) {
2009-05-17 16:58:20 -04:00
// find index of source file
int index = model.files().indexOf(entry.getKey());
// remove complete match
model.matches().remove(index);
}
// update history
2009-05-17 16:58:20 -04:00
if (renameLog.size() > 0) {
HistorySpooler.getInstance().append(renameLog);
}
}
2009-05-17 16:58:20 -04:00
private File rename(File file, String path) throws IOException {
2009-10-28 21:22:00 -04:00
File destination = new File(path);
2009-10-28 21:22:00 -04:00
// resolve destination
if (!destination.isAbsolute()) {
// same folder, different name
destination = new File(file.getParentFile(), path);
}
// make sure we that we can create the destination folder structure
File destinationFolder = destination.getParentFile();
// create parent folder if necessary
if (!destinationFolder.isDirectory() && !destinationFolder.mkdirs()) {
throw new IOException("Failed to create folder: " + destinationFolder);
}
if (!file.renameTo(destination)) {
throw new IOException("Failed to rename file: " + file.getName());
}
return destination;
}
private Iterable<Entry<File, String>> validate(Map<File, String> renameMap, Window parent) {
final List<Entry<File, String>> source = new ArrayList<Entry<File, String>>(renameMap.entrySet());
2009-05-17 16:58:20 -04:00
List<String> destinationFileNameView = new AbstractList<String>() {
@Override
public String get(int index) {
return source.get(index).getValue();
2009-05-17 16:58:20 -04:00
}
@Override
public String set(int index, String name) {
return source.get(index).setValue(name);
2009-05-17 16:58:20 -04:00
}
@Override
public int size() {
return source.size();
}
};
if (ValidateDialog.validate(parent, destinationFileNameView)) {
// names have been validated via view
return source;
}
// return empty list if validation was cancelled
return emptyList();
}
}