mirror of
https://github.com/mitb-archive/filebot
synced 2024-08-13 17:03:45 -04:00
Refactor FormatDialog
This commit is contained in:
parent
f9fdf3e375
commit
3f56e58b8b
@ -15,7 +15,6 @@ import java.awt.Dimension;
|
|||||||
import java.awt.Font;
|
import java.awt.Font;
|
||||||
import java.awt.Rectangle;
|
import java.awt.Rectangle;
|
||||||
import java.awt.Window;
|
import java.awt.Window;
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
import java.awt.event.WindowAdapter;
|
import java.awt.event.WindowAdapter;
|
||||||
import java.awt.event.WindowEvent;
|
import java.awt.event.WindowEvent;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -37,7 +36,6 @@ import java.util.logging.Level;
|
|||||||
import java.util.prefs.Preferences;
|
import java.util.prefs.Preferences;
|
||||||
|
|
||||||
import javax.script.ScriptException;
|
import javax.script.ScriptException;
|
||||||
import javax.swing.AbstractAction;
|
|
||||||
import javax.swing.Action;
|
import javax.swing.Action;
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
import javax.swing.JComponent;
|
import javax.swing.JComponent;
|
||||||
@ -68,7 +66,6 @@ import net.filebot.format.SuppressedThrowables;
|
|||||||
import net.filebot.mac.MacAppUtilities;
|
import net.filebot.mac.MacAppUtilities;
|
||||||
import net.filebot.media.MetaAttributes;
|
import net.filebot.media.MetaAttributes;
|
||||||
import net.filebot.util.DefaultThreadFactory;
|
import net.filebot.util.DefaultThreadFactory;
|
||||||
import net.filebot.util.ExceptionUtilities;
|
|
||||||
import net.filebot.util.PreferencesList;
|
import net.filebot.util.PreferencesList;
|
||||||
import net.filebot.util.PreferencesMap.PreferencesEntry;
|
import net.filebot.util.PreferencesMap.PreferencesEntry;
|
||||||
import net.filebot.util.ui.GradientStyle;
|
import net.filebot.util.ui.GradientStyle;
|
||||||
@ -111,22 +108,23 @@ public class FormatDialog extends JDialog {
|
|||||||
private static final PreferencesEntry<String> persistentSampleFile = Settings.forPackage(FormatDialog.class).entry("format.sample.file");
|
private static final PreferencesEntry<String> persistentSampleFile = Settings.forPackage(FormatDialog.class).entry("format.sample.file");
|
||||||
|
|
||||||
public enum Mode {
|
public enum Mode {
|
||||||
|
|
||||||
Episode, Movie, Music, File;
|
Episode, Movie, Music, File;
|
||||||
|
|
||||||
public Mode next() {
|
public Mode next() {
|
||||||
// cycle through Episode -> Movie -> Music (but ignore generic File mode)
|
// cycle through Episode -> Movie -> Music (but ignore generic File mode)
|
||||||
return values()[(this.ordinal() + 1) % File.ordinal()];
|
return values()[(ordinal() + 1) % File.ordinal()];
|
||||||
}
|
}
|
||||||
|
|
||||||
public String key() {
|
public String key() {
|
||||||
return this.name().toLowerCase();
|
return name().toLowerCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Format getFormat() {
|
public Format getFormat() {
|
||||||
switch (this) {
|
switch (this) {
|
||||||
case Episode:
|
case Episode:
|
||||||
return new EpisodeFormat();
|
return new EpisodeFormat();
|
||||||
case Movie: // case Movie
|
case Movie:
|
||||||
return new MovieFormat(true, true, false);
|
return new MovieFormat(true, true, false);
|
||||||
case Music:
|
case Music:
|
||||||
return new AudioTrackFormat();
|
return new AudioTrackFormat();
|
||||||
@ -157,6 +155,7 @@ public class FormatDialog extends JDialog {
|
|||||||
if (key.startsWith(prefix))
|
if (key.startsWith(prefix))
|
||||||
examples.put(key, bundle.getString(key));
|
examples.put(key, bundle.getString(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
return examples.values();
|
return examples.values();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -526,7 +525,7 @@ public class FormatDialog extends JDialog {
|
|||||||
executor.execute(currentPreviewFuture);
|
executor.execute(currentPreviewFuture);
|
||||||
} catch (ScriptException e) {
|
} catch (ScriptException e) {
|
||||||
// incorrect syntax
|
// incorrect syntax
|
||||||
status.setText(ExceptionUtilities.getRootCauseMessage(e));
|
status.setText(getRootCauseMessage(e));
|
||||||
status.setIcon(ResourceManager.getIcon("status.error"));
|
status.setIcon(ResourceManager.getIcon("status.error"));
|
||||||
status.setVisible(true);
|
status.setVisible(true);
|
||||||
|
|
||||||
@ -587,136 +586,111 @@ public class FormatDialog extends JDialog {
|
|||||||
return popup;
|
return popup;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected final Action changeSampleAction = new AbstractAction("Change Sample", ResourceManager.getIcon("action.variables")) {
|
protected final Action changeSampleAction = newAction("Change Sample", ResourceManager.getIcon("action.variables"), evt -> {
|
||||||
|
BindingDialog dialog = new BindingDialog(getWindow(evt.getSource()), String.format("%s Bindings", mode), mode.getFormat(), !locked);
|
||||||
|
dialog.setSample(sample);
|
||||||
|
|
||||||
@Override
|
// open dialog
|
||||||
public void actionPerformed(ActionEvent evt) {
|
dialog.setLocationRelativeTo((Component) evt.getSource());
|
||||||
BindingDialog dialog = new BindingDialog(getWindow(evt.getSource()), String.format("%s Bindings", mode), mode.getFormat(), !locked);
|
dialog.setVisible(true);
|
||||||
dialog.setSample(sample);
|
|
||||||
|
|
||||||
// open dialog
|
if (dialog.submit()) {
|
||||||
dialog.setLocationRelativeTo((Component) evt.getSource());
|
Object info = dialog.getInfoObject();
|
||||||
dialog.setVisible(true);
|
File file = dialog.getMediaFile();
|
||||||
|
|
||||||
if (dialog.submit()) {
|
// change sample
|
||||||
Object info = dialog.getInfoObject();
|
sample = new MediaBindingBean(info, file);
|
||||||
File file = dialog.getMediaFile();
|
|
||||||
|
|
||||||
// change sample
|
// remember sample
|
||||||
sample = new MediaBindingBean(info, file);
|
|
||||||
|
|
||||||
// remember sample
|
|
||||||
try {
|
|
||||||
mode.persistentSample().setValue(info == null ? "" : MetaAttributes.toJson(info));
|
|
||||||
persistentSampleFile.setValue(file == null ? "" : sample.getFileObject().getAbsolutePath());
|
|
||||||
} catch (Exception e) {
|
|
||||||
debug.log(Level.WARNING, e.getMessage(), e);
|
|
||||||
}
|
|
||||||
|
|
||||||
// reevaluate everything
|
|
||||||
fireSampleChanged();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
protected final Action selectFolderAction = new AbstractAction("Change Folder", ResourceManager.getIcon("action.load")) {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent evt) {
|
|
||||||
String relativeFormat = editor.getText().trim();
|
|
||||||
File absoluteFolder = null;
|
|
||||||
|
|
||||||
if (relativeFormat.length() > 0) {
|
|
||||||
File templatePath = new File(relativeFormat);
|
|
||||||
if (templatePath.isAbsolute()) {
|
|
||||||
File existingPath = null;
|
|
||||||
for (File next : listPath(templatePath)) {
|
|
||||||
if (existingPath != null && !next.exists()) {
|
|
||||||
absoluteFolder = existingPath;
|
|
||||||
relativeFormat = relativeFormat.substring(existingPath.getPath().length() + 1); // account for file separator
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
existingPath = next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
File selectedFolder = UserFiles.showOpenDialogSelectFolder(absoluteFolder, "Select Folder", evt);
|
|
||||||
if (selectedFolder != null) {
|
|
||||||
editor.setText(normalizePathSeparators(selectedFolder.getAbsolutePath()) + "/" + relativeFormat);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
protected final Action showRecentAction = new AbstractAction("Change Format", ResourceManager.getIcon("action.menu")) {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent evt) {
|
|
||||||
// display popup below format editor
|
|
||||||
JComponent c = (JComponent) evt.getSource();
|
|
||||||
editor.getComponentPopupMenu().show(c, 0, c.getHeight() + 3);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
protected final Action cancelAction = new AbstractAction("Cancel", ResourceManager.getIcon("dialog.cancel")) {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
finish(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
protected final Action switchEditModeAction = new AbstractAction("Switch Mode", ResourceManager.getIcon("dialog.switch")) {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
Mode next = mode.next();
|
|
||||||
setState(next, restoreSample(next), false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
protected final Action approveFormatAction = new AbstractAction("Use Format", ResourceManager.getIcon("dialog.continue")) {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent evt) {
|
|
||||||
try {
|
try {
|
||||||
// check syntax
|
mode.persistentSample().setValue(info == null ? "" : MetaAttributes.toJson(info));
|
||||||
format = new ExpressionFormat(editor.getText().trim());
|
persistentSampleFile.setValue(file == null ? "" : sample.getFileObject().getAbsolutePath());
|
||||||
|
} catch (Exception e) {
|
||||||
|
debug.log(Level.WARNING, e.getMessage(), e);
|
||||||
|
}
|
||||||
|
|
||||||
if (format.getExpression().isEmpty()) {
|
// reevaluate everything
|
||||||
throw new ScriptException("Expression is empty");
|
fireSampleChanged();
|
||||||
}
|
}
|
||||||
if (format.getExpression().length() > Preferences.MAX_VALUE_LENGTH) {
|
});
|
||||||
throw new ScriptException("Expression is limited to " + Preferences.MAX_VALUE_LENGTH + " characters");
|
|
||||||
}
|
|
||||||
|
|
||||||
// create new recent history and ignore duplicates
|
protected final Action selectFolderAction = newAction("Change Folder", ResourceManager.getIcon("action.load"), evt -> {
|
||||||
Set<String> recent = new LinkedHashSet<String>();
|
String relativeFormat = editor.getText().trim();
|
||||||
|
File absoluteFolder = null;
|
||||||
|
|
||||||
// add new format first
|
if (relativeFormat.length() > 0) {
|
||||||
recent.add(format.getExpression());
|
File templatePath = new File(relativeFormat);
|
||||||
|
if (templatePath.isAbsolute()) {
|
||||||
// save the 8 most recent formats
|
File existingPath = null;
|
||||||
for (String expression : mode.persistentFormatHistory()) {
|
for (File next : listPath(templatePath)) {
|
||||||
recent.add(expression);
|
if (existingPath != null && !next.exists()) {
|
||||||
|
absoluteFolder = existingPath;
|
||||||
if (recent.size() >= 8) {
|
relativeFormat = relativeFormat.substring(existingPath.getPath().length() + 1); // account for file separator
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
existingPath = next;
|
||||||
}
|
}
|
||||||
|
|
||||||
// update persistent history
|
|
||||||
mode.persistentFormatHistory().set(recent);
|
|
||||||
|
|
||||||
finish(true);
|
|
||||||
} catch (ScriptException e) {
|
|
||||||
log.log(Level.WARNING, ExceptionUtilities.getRootCauseMessage(e));
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.log(Level.WARNING, e.getMessage(), e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
File selectedFolder = UserFiles.showOpenDialogSelectFolder(absoluteFolder, "Select Folder", evt);
|
||||||
|
if (selectedFolder != null) {
|
||||||
|
editor.setText(normalizePathSeparators(selectedFolder.getAbsolutePath()) + "/" + relativeFormat);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
protected final Action showRecentAction = newAction("Change Format", ResourceManager.getIcon("action.menu"), evt -> {
|
||||||
|
// display popup below format editor
|
||||||
|
JComponent c = (JComponent) evt.getSource();
|
||||||
|
editor.getComponentPopupMenu().show(c, 0, c.getHeight() + 3);
|
||||||
|
});
|
||||||
|
|
||||||
|
protected final Action cancelAction = newAction("Cancel", ResourceManager.getIcon("dialog.cancel"), evt -> {
|
||||||
|
finish(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
protected final Action switchEditModeAction = newAction("Switch Mode", ResourceManager.getIcon("dialog.switch"), evt -> {
|
||||||
|
Mode next = mode.next();
|
||||||
|
setState(next, restoreSample(next), false);
|
||||||
|
});
|
||||||
|
|
||||||
|
protected final Action approveFormatAction = newAction("Use Format", ResourceManager.getIcon("dialog.continue"), evt -> {
|
||||||
|
try {
|
||||||
|
// check syntax
|
||||||
|
format = new ExpressionFormat(editor.getText().trim());
|
||||||
|
|
||||||
|
if (format.getExpression().isEmpty()) {
|
||||||
|
throw new ScriptException("Expression is empty");
|
||||||
|
}
|
||||||
|
if (format.getExpression().length() > Preferences.MAX_VALUE_LENGTH) {
|
||||||
|
throw new ScriptException("Expression is limited to " + Preferences.MAX_VALUE_LENGTH + " characters");
|
||||||
|
}
|
||||||
|
|
||||||
|
// create new recent history and ignore duplicates
|
||||||
|
Set<String> recent = new LinkedHashSet<String>();
|
||||||
|
|
||||||
|
// add new format first
|
||||||
|
recent.add(format.getExpression());
|
||||||
|
|
||||||
|
// save the 8 most recent formats
|
||||||
|
for (String expression : mode.persistentFormatHistory()) {
|
||||||
|
recent.add(expression);
|
||||||
|
|
||||||
|
if (recent.size() >= 8) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// update persistent history
|
||||||
|
mode.persistentFormatHistory().set(recent);
|
||||||
|
|
||||||
|
finish(true);
|
||||||
|
} catch (ScriptException e) {
|
||||||
|
log.log(Level.WARNING, getRootCauseMessage(e));
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.log(Level.WARNING, e.getMessage(), e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
protected void fireSampleChanged() {
|
protected void fireSampleChanged() {
|
||||||
firePropertyChange("sample", null, sample);
|
firePropertyChange("sample", null, sample);
|
||||||
|
Loading…
Reference in New Issue
Block a user