2007-12-23 14:28:04 -05:00
|
|
|
|
|
|
|
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;
|
2007-12-23 14:28:04 -05:00
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
import java.io.File;
|
2009-05-17 17:57:13 -04:00
|
|
|
import java.io.IOException;
|
2009-05-17 16:58:20 -04:00
|
|
|
import java.util.AbstractList;
|
2009-05-02 19:34:04 -04:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2009-05-11 20:17:53 -04:00
|
|
|
import java.util.ListIterator;
|
2009-05-17 16:58:20 -04:00
|
|
|
import java.util.Map;
|
2009-05-02 19:34:04 -04:00
|
|
|
import java.util.Map.Entry;
|
2008-12-28 15:04:13 -05:00
|
|
|
import java.util.logging.Logger;
|
2007-12-23 14:28:04 -05:00
|
|
|
|
|
|
|
import javax.swing.AbstractAction;
|
|
|
|
|
2008-10-10 14:59:01 -04:00
|
|
|
import net.sourceforge.filebot.ResourceManager;
|
2007-12-23 14:28:04 -05:00
|
|
|
|
|
|
|
|
2009-01-17 06:03:09 -05:00
|
|
|
class RenameAction extends AbstractAction {
|
2007-12-23 14:28:04 -05:00
|
|
|
|
2009-04-26 09:34:22 -04:00
|
|
|
private final RenameModel model;
|
2007-12-23 14:28:04 -05:00
|
|
|
|
2009-07-20 07:03:24 -04:00
|
|
|
|
2009-04-26 09:34:22 -04:00
|
|
|
public RenameAction(RenameModel model) {
|
2009-05-17 16:58:20 -04:00
|
|
|
this.model = model;
|
2007-12-23 14:28:04 -05:00
|
|
|
|
2009-05-17 16:58:20 -04:00
|
|
|
putValue(NAME, "Rename");
|
|
|
|
putValue(SMALL_ICON, ResourceManager.getIcon("action.rename"));
|
2009-01-11 16:23:03 -05:00
|
|
|
putValue(SHORT_DESCRIPTION, "Rename files");
|
2007-12-23 14:28:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-11 16:23:03 -05:00
|
|
|
public void actionPerformed(ActionEvent evt) {
|
2009-07-20 07:03:24 -04:00
|
|
|
List<Entry<File, String>> renameLog = new ArrayList<Entry<File, String>>();
|
2007-12-23 14:28:04 -05:00
|
|
|
|
2009-01-11 16:23:03 -05:00
|
|
|
try {
|
2009-07-20 07:03:24 -04:00
|
|
|
for (Entry<File, String> mapping : validate(model.getRenameMap(), getWindow(evt.getSource()))) {
|
|
|
|
// rename file, throw exception on failure
|
|
|
|
rename(mapping.getKey(), mapping.getValue());
|
2009-01-11 16:23:03 -05:00
|
|
|
|
2009-05-17 17:57:13 -04:00
|
|
|
// remember successfully renamed matches for history entry and possible revert
|
2009-05-02 19:34:04 -04:00
|
|
|
renameLog.add(mapping);
|
2009-01-11 16:23:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()));
|
|
|
|
}
|
2009-05-02 19:34:04 -04:00
|
|
|
} catch (Exception e) {
|
|
|
|
// could not rename one of the files, revert all changes
|
|
|
|
Logger.getLogger("ui").warning(e.getMessage());
|
2007-12-23 14:28:04 -05:00
|
|
|
|
2009-05-11 20:17:53 -04:00
|
|
|
// revert rename operations in reverse order
|
2009-07-20 07:03:24 -04:00
|
|
|
for (ListIterator<Entry<File, String>> iterator = renameLog.listIterator(renameLog.size()); iterator.hasPrevious();) {
|
|
|
|
Entry<File, String> mapping = iterator.previous();
|
2009-05-11 20:17:53 -04:00
|
|
|
|
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)) {
|
2009-05-11 20:17:53 -04:00
|
|
|
// remove reverted rename operation from log
|
|
|
|
iterator.remove();
|
2009-07-20 18:31:14 -04:00
|
|
|
} else {
|
2009-05-11 20:17:53 -04:00
|
|
|
// failed to revert rename operation
|
2009-07-23 10:25:43 -04:00
|
|
|
Logger.getLogger("ui").severe("Failed to revert file: " + mapping.getValue());
|
2009-05-02 19:34:04 -04:00
|
|
|
}
|
2007-12-23 14:28:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-17 16:58:20 -04:00
|
|
|
// remove renamed matches
|
2009-07-20 07:03:24 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2009-05-11 20:17:53 -04:00
|
|
|
// update history
|
2009-05-17 16:58:20 -04:00
|
|
|
if (renameLog.size() > 0) {
|
|
|
|
HistorySpooler.getInstance().append(renameLog);
|
|
|
|
}
|
2007-12-23 14:28:04 -05:00
|
|
|
}
|
2009-05-17 16:58:20 -04:00
|
|
|
|
|
|
|
|
2009-07-23 10:25:43 -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-07-23 10:25:43 -04:00
|
|
|
|
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
|
2009-07-20 07:03:24 -04:00
|
|
|
File destinationFolder = destination.getParentFile();
|
|
|
|
|
|
|
|
// create parent folder if necessary
|
2009-07-23 10:25:43 -04:00
|
|
|
if (!destinationFolder.isDirectory() && !destinationFolder.mkdirs()) {
|
|
|
|
throw new IOException("Failed to create folder: " + destinationFolder);
|
2009-07-20 07:03:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2009-07-20 07:03:24 -04:00
|
|
|
return source.get(index).getValue();
|
2009-05-17 16:58:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String set(int index, String name) {
|
2009-07-20 07:03:24 -04:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2007-12-23 14:28:04 -05:00
|
|
|
}
|