1
0
mirror of https://github.com/mitb-archive/filebot synced 2025-01-11 13:58:16 -05:00

Refactor FormatDialog

This commit is contained in:
Reinhard Pointner 2016-08-10 18:10:05 +08:00
parent 3ca390678d
commit 135c28d456
2 changed files with 18 additions and 53 deletions

View File

@ -18,8 +18,6 @@ import java.awt.Window;
import java.awt.event.ActionEvent; 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.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File; import java.io.File;
import java.text.Format; import java.text.Format;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
@ -229,17 +227,13 @@ public class FormatDialog extends JDialog {
pane.add(header, "h 60px, growx, dock north"); pane.add(header, "h 60px, growx, dock north");
pane.add(content, "grow"); pane.add(content, "grow");
addPropertyChangeListener("sample", new PropertyChangeListener() { addPropertyChangeListener("sample", evt -> {
if (isMacSandbox()) {
@Override if (sample != null && sample.getFileObject() != null && sample.getFileObject().exists()) {
public void propertyChange(PropertyChangeEvent evt) { MacAppUtilities.askUnlockFolders(getWindow(evt.getSource()), singleton(sample.getFileObject()));
if (isMacSandbox()) {
if (sample != null && sample.getFileObject() != null && sample.getFileObject().exists()) {
MacAppUtilities.askUnlockFolders(getWindow(evt.getSource()), singleton(sample.getFileObject()));
}
} }
checkFormatInBackground();
} }
checkFormatInBackground();
}); });
// focus editor by default and finish dialog and close window manually // focus editor by default and finish dialog and close window manually
@ -375,7 +369,7 @@ public class FormatDialog extends JDialog {
panel.setBorder(createLineBorder(new Color(0xACA899))); panel.setBorder(createLineBorder(new Color(0xACA899)));
panel.setBackground(new Color(0xFFFFE1)); panel.setBackground(new Color(0xFFFFE1));
for (final String format : mode.getSampleExpressions()) { for (String format : mode.getSampleExpressions()) {
LinkButton formatLink = new LinkButton(newAction(format, e -> setFormatCode(format))); LinkButton formatLink = new LinkButton(newAction(format, e -> setFormatCode(format)));
formatLink.setFont(new Font(MONOSPACED, PLAIN, 11)); formatLink.setFont(new Font(MONOSPACED, PLAIN, 11));
@ -383,27 +377,14 @@ public class FormatDialog extends JDialog {
JLabel formatExample = new JLabel("[evaluate]"); JLabel formatExample = new JLabel("[evaluate]");
// bind text to preview // bind text to preview
addPropertyChangeListener("sample", new PropertyChangeListener() { addPropertyChangeListener("sample", evt -> {
newSwingWorker(() -> {
@Override return new ExpressionFormat(format).format(sample);
public void propertyChange(PropertyChangeEvent evt) { }, s -> {
new SwingWorker<String, Void>() { formatExample.setText(s);
}, e -> {
@Override debug.log(Level.SEVERE, e.getMessage(), e);
protected String doInBackground() throws Exception { }).execute();
return new ExpressionFormat(format).format(sample);
}
@Override
protected void done() {
try {
formatExample.setText(get());
} catch (Exception e) {
debug.log(Level.SEVERE, e.getMessage(), e);
}
}
}.execute();
}
}); });
panel.add(formatLink); panel.add(formatLink);

View File

@ -1,45 +1,33 @@
package net.filebot.util.ui; package net.filebot.util.ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer; import javax.swing.Timer;
import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener; import javax.swing.event.DocumentListener;
public abstract class LazyDocumentListener implements DocumentListener { public abstract class LazyDocumentListener implements DocumentListener {
private DocumentEvent lastEvent; private DocumentEvent lastEvent;
private Timer timer; private Timer timer;
public LazyDocumentListener() { public LazyDocumentListener() {
this(200); this(200);
} }
public LazyDocumentListener(int delay) { public LazyDocumentListener(int delay) {
if (delay >= 0) { if (delay >= 0) {
timer = new Timer(delay, new ActionListener() { timer = new Timer(delay, evt -> {
update(lastEvent);
@Override // we don't need it anymore
public void actionPerformed(ActionEvent e) { lastEvent = null;
update(lastEvent);
// we don't need it anymore
lastEvent = null;
}
}); });
timer.setRepeats(false); timer.setRepeats(false);
} }
} }
public void defer(DocumentEvent e) { public void defer(DocumentEvent e) {
lastEvent = e; lastEvent = e;
@ -52,25 +40,21 @@ public abstract class LazyDocumentListener implements DocumentListener {
} }
} }
@Override @Override
public void changedUpdate(DocumentEvent e) { public void changedUpdate(DocumentEvent e) {
defer(e); defer(e);
} }
@Override @Override
public void insertUpdate(DocumentEvent e) { public void insertUpdate(DocumentEvent e) {
defer(e); defer(e);
} }
@Override @Override
public void removeUpdate(DocumentEvent e) { public void removeUpdate(DocumentEvent e) {
defer(e); defer(e);
} }
public abstract void update(DocumentEvent e); public abstract void update(DocumentEvent e);
} }