1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-08-13 17:03:45 -04: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.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.text.Format;
import java.util.LinkedHashSet;
@ -229,17 +227,13 @@ public class FormatDialog extends JDialog {
pane.add(header, "h 60px, growx, dock north");
pane.add(content, "grow");
addPropertyChangeListener("sample", new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (isMacSandbox()) {
if (sample != null && sample.getFileObject() != null && sample.getFileObject().exists()) {
MacAppUtilities.askUnlockFolders(getWindow(evt.getSource()), singleton(sample.getFileObject()));
}
addPropertyChangeListener("sample", evt -> {
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
@ -375,7 +369,7 @@ public class FormatDialog extends JDialog {
panel.setBorder(createLineBorder(new Color(0xACA899)));
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)));
formatLink.setFont(new Font(MONOSPACED, PLAIN, 11));
@ -383,27 +377,14 @@ public class FormatDialog extends JDialog {
JLabel formatExample = new JLabel("[evaluate]");
// bind text to preview
addPropertyChangeListener("sample", new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
new SwingWorker<String, Void>() {
@Override
protected String doInBackground() throws Exception {
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();
}
addPropertyChangeListener("sample", evt -> {
newSwingWorker(() -> {
return new ExpressionFormat(format).format(sample);
}, s -> {
formatExample.setText(s);
}, e -> {
debug.log(Level.SEVERE, e.getMessage(), e);
}).execute();
});
panel.add(formatLink);

View File

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