Refactor LazyDocumentListener

This commit is contained in:
Reinhard Pointner 2016-08-10 18:43:26 +08:00
parent 7ac41d2c36
commit 4b25cd6983
6 changed files with 59 additions and 100 deletions

View File

@ -29,7 +29,6 @@ import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel; import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import javax.swing.TransferHandler; import javax.swing.TransferHandler;
import javax.swing.event.DocumentEvent;
import javax.swing.text.AttributeSet; import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException; import javax.swing.text.BadLocationException;
@ -192,22 +191,15 @@ public class ListPanel extends JComponent {
editor.setFont(new Font(MONOSPACED, PLAIN, 14)); editor.setFont(new Font(MONOSPACED, PLAIN, 14));
// update format on change // update format on change
editor.getDocument().addDocumentListener(new LazyDocumentListener(20) { editor.getDocument().addDocumentListener(new LazyDocumentListener(20, evt -> {
try {
private Color valid = editor.getForeground(); String expression = editor.getText().trim();
private Color invalid = Color.red; setFormat(expression.isEmpty() ? null : new ExpressionFormat(expression));
editor.setForeground(editor.getForeground());
@Override } catch (ScriptException e) {
public void update(DocumentEvent evt) { editor.setForeground(Color.RED);
try {
String expression = editor.getText().trim();
setFormat(expression.isEmpty() ? null : new ExpressionFormat(expression));
editor.setForeground(valid);
} catch (ScriptException e) {
editor.setForeground(invalid);
}
} }
}); }));
return editor; return editor;
} }

View File

@ -106,17 +106,13 @@ class BindingDialog extends JDialog {
} }
// update preview on change // update preview on change
DocumentListener changeListener = new LazyDocumentListener(1000) { DocumentListener changeListener = new LazyDocumentListener(1000, evt -> {
// ignore lazy events that come in after the window has been closed
if (bindingModel.executor.isShutdown())
return;
@Override bindingModel.setModel(getSampleExpressions(), new MediaBindingBean(getInfoObject(), getMediaFile()));
public void update(DocumentEvent evt) { });
// ignore lazy events that come in after the window has been closed
if (bindingModel.executor.isShutdown())
return;
bindingModel.setModel(getSampleExpressions(), new MediaBindingBean(getInfoObject(), getMediaFile()));
}
};
// update example bindings on change // update example bindings on change
infoTextField.getDocument().addDocumentListener(changeListener); infoTextField.getDocument().addDocumentListener(changeListener);

View File

@ -50,7 +50,6 @@ import javax.swing.JTextField;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import javax.swing.SwingWorker; import javax.swing.SwingWorker;
import javax.swing.Timer; import javax.swing.Timer;
import javax.swing.event.DocumentEvent;
import javax.swing.event.PopupMenuEvent; import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener; import javax.swing.event.PopupMenuListener;
@ -332,13 +331,9 @@ public class FormatDialog extends JDialog {
editor.setFont(new Font(MONOSPACED, PLAIN, 14)); editor.setFont(new Font(MONOSPACED, PLAIN, 14));
// update format on change // update format on change
editor.getDocument().addDocumentListener(new LazyDocumentListener() { editor.getDocument().addDocumentListener(new LazyDocumentListener(evt -> {
checkFormatInBackground();
@Override }));
public void update(DocumentEvent e) {
checkFormatInBackground();
}
});
return editor; return editor;
} }

View File

@ -60,7 +60,6 @@ import javax.swing.RowSorter.SortKey;
import javax.swing.SortOrder; import javax.swing.SortOrder;
import javax.swing.border.CompoundBorder; import javax.swing.border.CompoundBorder;
import javax.swing.border.TitledBorder; import javax.swing.border.TitledBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener; import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel; import javax.swing.table.AbstractTableModel;
@ -219,29 +218,25 @@ class HistoryDialog extends JDialog {
}); });
// update sequence and element filter on change // update sequence and element filter on change
filterEditor.getDocument().addDocumentListener(new LazyDocumentListener() { filterEditor.getDocument().addDocumentListener(new LazyDocumentListener(evt -> {
List<HistoryFilter> filterList = new ArrayList<HistoryFilter>();
@Override // filter by all words
public void update(DocumentEvent e) { for (String word : SPACE.split(filterEditor.getText())) {
List<HistoryFilter> filterList = new ArrayList<HistoryFilter>(); filterList.add(new HistoryFilter(word));
// filter by all words
for (String word : SPACE.split(filterEditor.getText())) {
filterList.add(new HistoryFilter(word));
}
// use filter on both tables
for (JTable table : Arrays.asList(sequenceTable, elementTable)) {
TableRowSorter sorter = (TableRowSorter) table.getRowSorter();
sorter.setRowFilter(RowFilter.andFilter(filterList));
}
if (sequenceTable.getSelectedRow() < 0 && sequenceTable.getRowCount() > 0) {
// selection lost, maybe due to filtering, auto-select next row
sequenceTable.getSelectionModel().addSelectionInterval(0, 0);
}
} }
});
// use filter on both tables
for (JTable table : Arrays.asList(sequenceTable, elementTable)) {
TableRowSorter sorter = (TableRowSorter) table.getRowSorter();
sorter.setRowFilter(RowFilter.andFilter(filterList));
}
if (sequenceTable.getSelectedRow() < 0 && sequenceTable.getRowCount() > 0) {
// selection lost, maybe due to filtering, auto-select next row
sequenceTable.getSelectionModel().addSelectionInterval(0, 0);
}
}));
// install context menu // install context menu
sequenceTable.addMouseListener(contextMenuProvider); sequenceTable.addMouseListener(contextMenuProvider);

View File

@ -35,7 +35,6 @@ import javax.swing.KeyStroke;
import javax.swing.ListSelectionModel; import javax.swing.ListSelectionModel;
import javax.swing.RowFilter; import javax.swing.RowFilter;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.table.AbstractTableModel; import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableColumnModel; import javax.swing.table.DefaultTableColumnModel;
@ -180,13 +179,9 @@ public class SubtitleViewer extends JFrame {
}; };
// update sequence and element filter on change // update sequence and element filter on change
editor.getDocument().addDocumentListener(new LazyDocumentListener(0) { editor.getDocument().addDocumentListener(new LazyDocumentListener(0, evt -> {
setTableFilter(editor.getText());
@Override }));
public void update(DocumentEvent e) {
setTableFilter(editor.getText());
}
});
return editor; return editor;
} }

View File

@ -1,60 +1,46 @@
package net.filebot.util.ui; package net.filebot.util.ui;
import java.util.function.Consumer;
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 class LazyDocumentListener implements DocumentListener {
private DocumentEvent lastEvent;
private Timer timer; private Timer timer;
public LazyDocumentListener() { private DocumentEvent lastEvent = null;
this(200);
public LazyDocumentListener(Consumer<DocumentEvent> handler) {
this(200, handler);
} }
public LazyDocumentListener(int delay) { public LazyDocumentListener(int delay, Consumer<DocumentEvent> handler) {
if (delay >= 0) { timer = new Timer(delay, evt -> {
timer = new Timer(delay, evt -> { handler.accept(lastEvent);
update(lastEvent); lastEvent = null;
});
// we don't need it anymore timer.setRepeats(false);
lastEvent = null;
});
timer.setRepeats(false);
}
}
public void defer(DocumentEvent e) {
lastEvent = e;
if (timer != null) {
// defer update
timer.restart();
} else {
// update immediately
update(lastEvent);
}
} }
@Override @Override
public void changedUpdate(DocumentEvent e) { public void changedUpdate(DocumentEvent evt) {
defer(e); lastEvent = evt;
timer.restart();
} }
@Override @Override
public void insertUpdate(DocumentEvent e) { public void insertUpdate(DocumentEvent evt) {
defer(e); lastEvent = evt;
timer.restart();
} }
@Override @Override
public void removeUpdate(DocumentEvent e) { public void removeUpdate(DocumentEvent evt) {
defer(e); lastEvent = evt;
timer.restart();
} }
public abstract void update(DocumentEvent e);
} }