* lazy-load rhino script engine in RenamePanel (startup will be 0.5s faster)

This commit is contained in:
Reinhard Pointner 2009-08-25 15:41:08 +00:00
parent d71a70316a
commit cd1ea72c88
2 changed files with 16 additions and 14 deletions

View File

@ -15,16 +15,16 @@ import net.sourceforge.filebot.web.EpisodeFormat;
class EpisodeExpressionFormatter implements MatchFormatter { class EpisodeExpressionFormatter implements MatchFormatter {
private final ExpressionFormat format; private final String expression;
private ExpressionFormat format;
public EpisodeExpressionFormatter(ExpressionFormat format) { public EpisodeExpressionFormatter(String expression) {
this.format = format; if (expression == null || expression.isEmpty())
} throw new IllegalArgumentException("Expression must not be null or empty");
this.expression = expression;
public ExpressionFormat getFormat() {
return format;
} }
@ -46,6 +46,11 @@ class EpisodeExpressionFormatter implements MatchFormatter {
Episode episode = (Episode) match.getValue(); Episode episode = (Episode) match.getValue();
File mediaFile = (File) match.getCandidate(); File mediaFile = (File) match.getCandidate();
// lazy initialize script engine
if (format == null) {
format = new ExpressionFormat(expression);
}
String result = format.format(new EpisodeBindingBean(episode, mediaFile)).trim(); String result = format.format(new EpisodeBindingBean(episode, mediaFile)).trim();
// if result is empty, check for script exceptions // if result is empty, check for script exceptions

View File

@ -31,7 +31,6 @@ import ca.odell.glazedlists.swing.EventSelectionModel;
import net.miginfocom.swing.MigLayout; import net.miginfocom.swing.MigLayout;
import net.sourceforge.filebot.ResourceManager; import net.sourceforge.filebot.ResourceManager;
import net.sourceforge.filebot.Settings; import net.sourceforge.filebot.Settings;
import net.sourceforge.filebot.format.ExpressionFormat;
import net.sourceforge.filebot.similarity.Match; import net.sourceforge.filebot.similarity.Match;
import net.sourceforge.filebot.ui.panel.rename.RenameModel.FormattedFuture; import net.sourceforge.filebot.ui.panel.rename.RenameModel.FormattedFuture;
import net.sourceforge.filebot.web.AnidbClient; import net.sourceforge.filebot.web.AnidbClient;
@ -78,8 +77,7 @@ public class RenamePanel extends JComponent {
try { try {
// restore custom episode formatter // restore custom episode formatter
ExpressionFormat format = new ExpressionFormat(persistentFormatExpression.getValue()); renameModel.useFormatter(Episode.class, new EpisodeExpressionFormatter(persistentFormatExpression.getValue()));
renameModel.useFormatter(Episode.class, new EpisodeExpressionFormatter(format));
} catch (Exception e) { } catch (Exception e) {
// illegal format, ignore // illegal format, ignore
} }
@ -157,9 +155,8 @@ public class RenamePanel extends JComponent {
switch (dialog.getSelectedOption()) { switch (dialog.getSelectedOption()) {
case APPROVE: case APPROVE:
EpisodeExpressionFormatter formatter = new EpisodeExpressionFormatter(dialog.getSelectedFormat()); renameModel.useFormatter(Episode.class, new EpisodeExpressionFormatter(dialog.getSelectedFormat().getExpression()));
renameModel.useFormatter(Episode.class, formatter); persistentFormatExpression.setValue(dialog.getSelectedFormat().getExpression());
persistentFormatExpression.setValue(formatter.getFormat().getExpression());
break; break;
case USE_DEFAULT: case USE_DEFAULT:
renameModel.useFormatter(Episode.class, null); renameModel.useFormatter(Episode.class, null);