diff --git a/source/net/filebot/ui/list/ListPanel.java b/source/net/filebot/ui/list/ListPanel.java index da0df61a..339f8183 100644 --- a/source/net/filebot/ui/list/ListPanel.java +++ b/source/net/filebot/ui/list/ListPanel.java @@ -1,6 +1,5 @@ package net.filebot.ui.list; -import static java.awt.Font.*; import static java.util.stream.Collectors.*; import static javax.swing.BorderFactory.*; import static net.filebot.Settings.*; @@ -8,7 +7,6 @@ import static net.filebot.util.ui.SwingUI.*; import java.awt.BorderLayout; import java.awt.Color; -import java.awt.Font; import java.awt.Toolkit; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; @@ -29,9 +27,9 @@ import javax.swing.JSpinner.NumberEditor; import javax.swing.JTextField; import javax.swing.SpinnerNumberModel; import javax.swing.TransferHandler; +import javax.swing.border.Border; import org.fife.ui.rsyntaxtextarea.RSyntaxDocument; -import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea; import org.fife.ui.rsyntaxtextarea.SyntaxConstants; import org.fife.ui.rtextarea.RTextScrollPane; @@ -42,12 +40,12 @@ import net.filebot.format.ExpressionFormat; import net.filebot.ui.FileBotList; import net.filebot.ui.FileBotListExportHandler; import net.filebot.ui.PanelBuilder; +import net.filebot.ui.rename.FormatExpressionTextArea; import net.filebot.ui.transfer.LoadAction; import net.filebot.ui.transfer.SaveAction; import net.filebot.ui.transfer.TransferablePolicy; import net.filebot.ui.transfer.TransferablePolicy.TransferAction; import net.filebot.util.ui.DefaultFancyListCellRenderer; -import net.filebot.util.ui.LazyDocumentListener; import net.filebot.util.ui.PrototypeCellValueUpdater; import net.filebot.util.ui.SwingEventBus; import net.miginfocom.swing.MigLayout; @@ -58,7 +56,7 @@ public class ListPanel extends JComponent { public static final String DEFAULT_FILE_FORMAT = "{fn}"; public static final String DEFAULT_EPISODE_FORMAT = "{n} - {s00e00} - [{absolute}] - [{airdate}] - {t}"; - private RSyntaxTextArea editor = createEditor(); + private FormatExpressionTextArea editor = new FormatExpressionTextArea(new RSyntaxDocument(SyntaxConstants.SYNTAX_STYLE_GROOVY)); private SpinnerNumberModel fromSpinnerModel = new SpinnerNumberModel(1, 0, Integer.MAX_VALUE, 1); private SpinnerNumberModel toSpinnerModel = new SpinnerNumberModel(20, 0, Integer.MAX_VALUE, 1); @@ -134,14 +132,28 @@ public class ListPanel extends JComponent { editorScrollPane.setVerticalScrollBarPolicy(RTextScrollPane.VERTICAL_SCROLLBAR_NEVER); editorScrollPane.setHorizontalScrollBarPolicy(RTextScrollPane.HORIZONTAL_SCROLLBAR_NEVER); editorScrollPane.setBackground(editor.getBackground()); - editorScrollPane.setViewportBorder(createEmptyBorder(2, 2, 2, 2)); editorScrollPane.setOpaque(true); - editorScrollPane.setBorder(new JTextField().getBorder()); + + Border defaultBorder = new JTextField().getBorder(); + Border okBorder = createCompoundBorder(defaultBorder, createEmptyBorder(2, 2, 2, 2)); + Border errorBorder = createCompoundBorder(createLineBorder(Color.RED, 1), createCompoundBorder(defaultBorder, createEmptyBorder(1, 1, 1, 1))); + + editorScrollPane.setBorder(okBorder); + + // update format on change + editor.onChange(20, evt -> { + try { + String expression = editor.getText().trim(); + setFormat(expression.isEmpty() ? null : new ExpressionFormat(expression)); + editorScrollPane.setBorder(okBorder); + } catch (ScriptException e) { + editorScrollPane.setBorder(errorBorder); + } + }); setLayout(new MigLayout("nogrid, fill, insets dialog", "align center", "[pref!, center][fill]")); - JLabel patternLabel = new JLabel("Pattern:"); - add(patternLabel, "gapbefore indent"); + add(new JLabel("Pattern:"), "gapbefore indent"); add(editorScrollPane, "gap related, growx, wmin 2cm, h pref!, sizegroupy editor"); add(new JLabel("From:"), "gap 5mm"); add(fromSpinner, "gap related, wmax 15mm, sizegroup spinner, sizegroupy editor"); @@ -162,41 +174,6 @@ public class ListPanel extends JComponent { createItemSequence(); } - private RSyntaxTextArea createEditor() { - RSyntaxTextArea editor = new RSyntaxTextArea(new RSyntaxDocument(SyntaxConstants.SYNTAX_STYLE_GROOVY), "", 1, 80); - - editor.setAntiAliasingEnabled(true); - editor.setAnimateBracketMatching(false); - editor.setAutoIndentEnabled(false); - editor.setClearWhitespaceLinesEnabled(false); - editor.setBracketMatchingEnabled(true); - editor.setCloseCurlyBraces(false); - editor.setCodeFoldingEnabled(false); - editor.setHyperlinksEnabled(false); - editor.setUseFocusableTips(false); - editor.setHighlightCurrentLine(false); - editor.setLineWrap(false); - editor.setPaintMarkOccurrencesBorder(false); - editor.setPaintTabLines(false); - editor.setMarkOccurrences(false); - editor.setFont(new Font(MONOSPACED, PLAIN, 14)); - - Color defaultForeground = editor.getForeground(); - - // update format on change - editor.getDocument().addDocumentListener(new LazyDocumentListener(20, evt -> { - try { - String expression = editor.getText().trim(); - setFormat(expression.isEmpty() ? null : new ExpressionFormat(expression)); - editor.setForeground(defaultForeground); - } catch (ScriptException e) { - editor.setForeground(Color.RED); - } - })); - - return editor; - } - private ExpressionFormat format; private String template; diff --git a/source/net/filebot/ui/rename/FormatDialog.java b/source/net/filebot/ui/rename/FormatDialog.java index ff769b29..8ba72154 100644 --- a/source/net/filebot/ui/rename/FormatDialog.java +++ b/source/net/filebot/ui/rename/FormatDialog.java @@ -55,9 +55,6 @@ import javax.swing.Timer; import javax.swing.event.PopupMenuEvent; import javax.swing.event.PopupMenuListener; -import org.fife.ui.rsyntaxtextarea.RSyntaxDocument; -import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea; -import org.fife.ui.rsyntaxtextarea.Theme; import org.fife.ui.rtextarea.RTextScrollPane; import net.filebot.ResourceManager; @@ -74,7 +71,6 @@ import net.filebot.util.DefaultThreadFactory; import net.filebot.util.PreferencesList; import net.filebot.util.PreferencesMap.PreferencesEntry; import net.filebot.util.ui.GradientStyle; -import net.filebot.util.ui.LazyDocumentListener; import net.filebot.util.ui.LinkButton; import net.filebot.util.ui.ProgressIndicator; import net.filebot.util.ui.notification.SeparatorBorder; @@ -103,7 +99,7 @@ public class FormatDialog extends JDialog { private JLabel preview = new JLabel(); private JLabel status = new JLabel(); - private RSyntaxTextArea editor = createEditor(); + private FormatExpressionTextArea editor = new FormatExpressionTextArea(); private ProgressIndicator progressIndicator = new ProgressIndicator(); private JLabel title = new JLabel(); @@ -255,6 +251,11 @@ public class FormatDialog extends JDialog { log.info("Error message has been copied to clipboard"); })); + // update format on change + editor.onChange(evt -> { + checkFormatInBackground(); + }); + // update preview if sample has changed addPropertyChangeListener("sample", evt -> { if (isMacSandbox()) { @@ -358,51 +359,6 @@ public class FormatDialog extends JDialog { } } - private RSyntaxTextArea createEditor() { - RSyntaxDocument document = new RSyntaxDocument(new FormatExpressionTokenMakerFactory(), FormatExpressionTokenMakerFactory.SYNTAX_STYLE_GROOVY_FORMAT_EXPRESSION); - RSyntaxTextArea editor = new RSyntaxTextArea(document, "", 1, 80); - - try { - Theme.load(FormatDialog.class.getResourceAsStream("FormatExpression.RSyntaxTheme.xml")).apply(editor); - } catch (Exception e) { - debug.log(Level.WARNING, e, e::toString); - } - - editor.setAntiAliasingEnabled(true); - editor.setAnimateBracketMatching(true); - editor.setAutoIndentEnabled(true); - editor.setBracketMatchingEnabled(true); - editor.setCloseCurlyBraces(true); - editor.setCodeFoldingEnabled(false); - editor.setHyperlinksEnabled(false); - editor.setUseFocusableTips(false); - editor.setClearWhitespaceLinesEnabled(false); - editor.setHighlightCurrentLine(false); - editor.setHighlightSecondaryLanguages(false); - editor.setLineWrap(false); - editor.setMarkOccurrences(false); - editor.setPaintMarkOccurrencesBorder(false); - editor.setPaintTabLines(false); - editor.setFont(new Font(MONOSPACED, PLAIN, 14)); - - // update format on change - editor.getDocument().addDocumentListener(new LazyDocumentListener(evt -> { - checkFormatInBackground(); - })); - - // dynamically resize the code editor depending on how many lines the current format expression has - editor.getDocument().addDocumentListener(new LazyDocumentListener(0, evt -> { - int r1 = editor.getRows(); - int r2 = (int) editor.getText().chars().filter(c -> c == '\n').count() + 1; - if (r1 != r2) { - editor.setRows(r2); - getWindow(editor).revalidate(); - } - })); - - return editor; - } - private JComponent createSyntaxPanel(Mode mode) { JPanel panel = new JPanel(new MigLayout("fill, nogrid, novisualpadding", "[pref]", "[fill, min]")); panel.setBorder(createLineBorder(new Color(0xACA899))); diff --git a/source/net/filebot/ui/rename/FormatExpression.RSyntaxTheme.xml b/source/net/filebot/ui/rename/FormatExpressionTextArea.Theme.xml similarity index 100% rename from source/net/filebot/ui/rename/FormatExpression.RSyntaxTheme.xml rename to source/net/filebot/ui/rename/FormatExpressionTextArea.Theme.xml diff --git a/source/net/filebot/ui/rename/FormatExpressionTextArea.java b/source/net/filebot/ui/rename/FormatExpressionTextArea.java new file mode 100644 index 00000000..4d97056e --- /dev/null +++ b/source/net/filebot/ui/rename/FormatExpressionTextArea.java @@ -0,0 +1,70 @@ +package net.filebot.ui.rename; + +import static java.awt.Font.*; +import static net.filebot.Logging.*; +import static net.filebot.util.ui.SwingUI.*; + +import java.awt.Font; +import java.util.function.Consumer; +import java.util.logging.Level; + +import javax.swing.event.DocumentEvent; + +import org.fife.ui.rsyntaxtextarea.RSyntaxDocument; +import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea; +import org.fife.ui.rsyntaxtextarea.Theme; + +import net.filebot.util.ui.LazyDocumentListener; + +public class FormatExpressionTextArea extends RSyntaxTextArea { + + public FormatExpressionTextArea() { + this(new RSyntaxDocument(new FormatExpressionTokenMakerFactory(), FormatExpressionTokenMakerFactory.SYNTAX_STYLE_GROOVY_FORMAT_EXPRESSION)); + } + + public FormatExpressionTextArea(RSyntaxDocument syntaxDocument) { + super(syntaxDocument, "", 1, 80); + + try { + Theme.load(FormatExpressionTextArea.class.getResourceAsStream("FormatExpressionTextArea.Theme.xml")).apply(this); + } catch (Exception e) { + debug.log(Level.WARNING, e, e::toString); + } + + setAntiAliasingEnabled(true); + setAnimateBracketMatching(true); + setAutoIndentEnabled(true); + setBracketMatchingEnabled(true); + setCloseCurlyBraces(true); + setCodeFoldingEnabled(false); + setHyperlinksEnabled(false); + setUseFocusableTips(false); + setClearWhitespaceLinesEnabled(false); + setHighlightCurrentLine(false); + setHighlightSecondaryLanguages(false); + setLineWrap(false); + setMarkOccurrences(false); + setPaintMarkOccurrencesBorder(false); + setPaintTabLines(false); + setFont(new Font(MONOSPACED, PLAIN, 14)); + + // dynamically resize the code editor depending on how many lines the current format expression has + getDocument().addDocumentListener(new LazyDocumentListener(0, evt -> { + int r1 = getRows(); + int r2 = (int) getText().chars().filter(c -> c == '\n').count() + 1; + if (r1 != r2) { + setRows(r2); + getWindow(FormatExpressionTextArea.this).revalidate(); + } + })); + } + + public void onChange(Consumer handler) { + getDocument().addDocumentListener(new LazyDocumentListener(handler)); + } + + public void onChange(int delay, Consumer handler) { + getDocument().addDocumentListener(new LazyDocumentListener(delay, handler)); + } + +} diff --git a/source/net/filebot/ui/rename/PresetEditor.java b/source/net/filebot/ui/rename/PresetEditor.java index 001111b5..37e0d194 100644 --- a/source/net/filebot/ui/rename/PresetEditor.java +++ b/source/net/filebot/ui/rename/PresetEditor.java @@ -1,13 +1,11 @@ package net.filebot.ui.rename; -import static java.awt.Font.*; import static java.util.Collections.*; import static javax.swing.BorderFactory.*; import static net.filebot.Logging.*; import static net.filebot.Settings.*; import static net.filebot.util.ui.SwingUI.*; -import java.awt.Font; import java.awt.Window; import java.io.File; import java.util.List; @@ -28,7 +26,6 @@ import javax.swing.ListCellRenderer; import org.fife.ui.rsyntaxtextarea.RSyntaxDocument; import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea; -import org.fife.ui.rsyntaxtextarea.SyntaxConstants; import org.fife.ui.rtextarea.RTextScrollPane; import net.filebot.Language; @@ -83,8 +80,8 @@ public class PresetEditor extends JDialog { selectRadio = new JRadioButton("Do Select files"); pathInput = new JTextField(40); - filterEditor = createEditor(); - formatEditor = createEditor(); + filterEditor = new FormatExpressionTextArea(new RSyntaxDocument(RSyntaxDocument.SYNTAX_STYLE_GROOVY)); + formatEditor = new FormatExpressionTextArea(); actionCombo = createRenameActionCombo(); providerCombo = createDataProviderCombo(); @@ -199,28 +196,6 @@ public class PresetEditor extends JDialog { return inputGroup; } - private RSyntaxTextArea createEditor() { - RSyntaxTextArea editor = new RSyntaxTextArea(new RSyntaxDocument(SyntaxConstants.SYNTAX_STYLE_GROOVY), "", 1, 80); - - editor.setAntiAliasingEnabled(true); - editor.setAnimateBracketMatching(false); - editor.setAutoIndentEnabled(false); - editor.setClearWhitespaceLinesEnabled(false); - editor.setBracketMatchingEnabled(true); - editor.setCloseCurlyBraces(false); - editor.setCodeFoldingEnabled(false); - editor.setHyperlinksEnabled(false); - editor.setUseFocusableTips(false); - editor.setHighlightCurrentLine(false); - editor.setLineWrap(false); - editor.setPaintMarkOccurrencesBorder(false); - editor.setPaintTabLines(false); - editor.setMarkOccurrences(false); - editor.setFont(new Font(MONOSPACED, PLAIN, 14)); - - return editor; - } - private RTextScrollPane wrapEditor(RSyntaxTextArea editor) { RTextScrollPane scroll = new RTextScrollPane(editor, false); scroll.setLineNumbersEnabled(false);