diff --git a/source/net/sourceforge/filebot/format/ExpressionFormat.lib.groovy b/source/net/sourceforge/filebot/format/ExpressionFormat.lib.groovy index b8b3e676..4bca355c 100644 --- a/source/net/sourceforge/filebot/format/ExpressionFormat.lib.groovy +++ b/source/net/sourceforge/filebot/format/ExpressionFormat.lib.groovy @@ -1,5 +1,13 @@ -// Collection, Scanner, Random, UUID, etc. -import java.util.* +// File operations +import static net.sourceforge.tuned.FileUtilities.*; + + +/** +* Allow getAt() for File paths +* +* e.g. file[0] -> "F:" +*/ +File.metaClass.getAt = { index -> listPath(delegate).collect{ replacePathSeparators(getName(it)).trim() }.getAt(index) } /** diff --git a/source/net/sourceforge/filebot/ui/rename/RenameModel.java b/source/net/sourceforge/filebot/ui/rename/RenameModel.java index 6317b4fe..e97a4c56 100644 --- a/source/net/sourceforge/filebot/ui/rename/RenameModel.java +++ b/source/net/sourceforge/filebot/ui/rename/RenameModel.java @@ -43,43 +43,43 @@ public class RenameModel extends MatchModel { return true; } - + @Override public String preview(Match match) { return format(match); } - + @Override public String format(Match match) { // clean up path separators like / or \ - return replacePathSeparators(String.valueOf(match.getValue()).trim()); + return replacePathSeparators(String.valueOf(match.getValue())).trim(); } }; private boolean preserveExtension = true; - + public EventList names() { return names; } - + public EventList files() { return candidates(); } - + public boolean preserveExtension() { return preserveExtension; } - + public void setPreserveExtension(boolean preserveExtension) { this.preserveExtension = preserveExtension; } - + public Map getRenameMap() { Map map = new LinkedHashMap(); @@ -120,7 +120,7 @@ public class RenameModel extends MatchModel { return map; } - + public void useFormatter(Object key, MatchFormatter formatter) { if (formatter != null) { formatters.put(key, formatter); @@ -132,7 +132,7 @@ public class RenameModel extends MatchModel { names.refresh(); } - + private MatchFormatter getFormatter(Match match) { for (MatchFormatter formatter : formatters.values()) { if (formatter.canFormat(match)) { @@ -143,39 +143,39 @@ public class RenameModel extends MatchModel { return defaultFormatter; } - + private class FormattedFutureEventList extends TransformedList { private final List futures = new ArrayList(); private final Executor backgroundFormatter = new ThreadPoolExecutor(0, 1, 5L, TimeUnit.SECONDS, new LinkedBlockingQueue()); - + public FormattedFutureEventList(EventList source) { super(source); this.source.addListEventListener(this); } - + @Override public FormattedFuture get(int index) { return futures.get(index); } - + @Override protected boolean isWritable() { // can't write to source directly return false; } - + @Override public void add(int index, FormattedFuture value) { source.add(index, value.getMatch().getValue()); } - + @Override public FormattedFuture set(int index, FormattedFuture value) { FormattedFuture obsolete = get(index); @@ -185,7 +185,7 @@ public class RenameModel extends MatchModel { return obsolete; } - + @Override public FormattedFuture remove(int index) { FormattedFuture obsolete = get(index); @@ -195,7 +195,7 @@ public class RenameModel extends MatchModel { return obsolete; } - + @Override public void listChanged(ListEvent listChanges) { updates.beginEvent(true); @@ -250,7 +250,7 @@ public class RenameModel extends MatchModel { updates.commitEvent(); } - + public void refresh() { updates.beginEvent(true); @@ -270,21 +270,21 @@ public class RenameModel extends MatchModel { updates.commitEvent(); } - + private void submit(FormattedFuture future) { // observe and enqueue worker task future.addPropertyChangeListener(futureListener); backgroundFormatter.execute(future); } - + private void cancel(FormattedFuture future) { // remove listener and cancel worker task future.removePropertyChangeListener(futureListener); future.cancel(true); } - + private final PropertyChangeListener futureListener = new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { @@ -302,41 +302,41 @@ public class RenameModel extends MatchModel { }; } - + public static class FormattedFuture extends SwingWorker { private final Match match; private final MatchFormatter formatter; - + private FormattedFuture(Match match, MatchFormatter formatter) { this.match = match; this.formatter = formatter; } - + public boolean isComplexFormat() { return formatter instanceof ExpressionFormatter; } - + public Match getMatch() { return match; } - + public String preview() { return formatter.preview(match); } - + @Override protected String doInBackground() throws Exception { return formatter.format(match); } - + @Override public String toString() { if (isDone()) { diff --git a/source/net/sourceforge/tuned/FileUtilities.java b/source/net/sourceforge/tuned/FileUtilities.java index e328d2f2..300774cb 100644 --- a/source/net/sourceforge/tuned/FileUtilities.java +++ b/source/net/sourceforge/tuned/FileUtilities.java @@ -452,7 +452,12 @@ public final class FileUtilities { public static String replacePathSeparators(CharSequence path) { - return Pattern.compile("\\s*[\\\\/]+\\s*").matcher(path).replaceAll(" "); + return replacePathSeparators(path, " "); + } + + + public static String replacePathSeparators(CharSequence path, String replacement) { + return Pattern.compile("\\s*[\\\\/]+\\s*").matcher(path).replaceAll(replacement); }