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

129 lines
3.4 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, File>> renameLog = new ArrayList<Entry<File, File>>();
try {
2009-05-17 16:58:20 -04:00
for (Entry<File, File> mapping : validate(model.getRenameMap(), getWindow(evt.getSource()))) {
// rename file
if (!mapping.getKey().renameTo(mapping.getValue()))
throw new IOException(String.format("Failed to rename file: \"%s\".", mapping.getKey().getName()));
// 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, File>> iterator = renameLog.listIterator(renameLog.size()); iterator.hasPrevious();) {
Entry<File, File> mapping = iterator.previous();
if (mapping.getValue().renameTo(mapping.getKey())) {
// remove reverted rename operation from log
iterator.remove();
} else {
// failed to revert rename operation
Logger.getLogger("ui").severe(String.format("Failed to revert file: \"%s\".", mapping.getValue().getName()));
}
}
}
2009-05-17 16:58:20 -04:00
// remove renamed matches
for (Entry<File, File> entry : renameLog) {
// 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 Iterable<Entry<File, File>> validate(Map<File, File> renameMap, Window parent) {
final List<Entry<File, File>> source = new ArrayList<Entry<File, File>>(renameMap.entrySet());
List<String> destinationFileNameView = new AbstractList<String>() {
@Override
public String get(int index) {
return source.get(index).getValue().getName();
}
@Override
public String set(int index, String name) {
Entry<File, File> entry = source.get(index);
File old = entry.getValue();
// update name
entry.setValue(new File(old.getParent(), name));
return old.getName();
}
@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();
}
}