From 13e6360c4da978992ea8e8e12b619a55eb29ec1e Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Wed, 17 Aug 2016 05:37:59 +0800 Subject: [PATCH] Java 8 Refactoring --- source/net/filebot/Main.java | 2 +- source/net/filebot/cli/GroovyPad.java | 22 ++++------ .../net/filebot/ui/rename/ConflictDialog.java | 2 +- .../net/filebot/ui/rename/ValidateDialog.java | 9 ++-- .../filebot/ui/sfv/TotalProgressPanel.java | 41 +++++++------------ .../ui/subtitle/SubtitleAutoMatchDialog.java | 2 +- .../BackgroundFileTransferablePolicy.java | 10 +---- 7 files changed, 31 insertions(+), 57 deletions(-) diff --git a/source/net/filebot/Main.java b/source/net/filebot/Main.java index 716afd65..15e6c8da 100644 --- a/source/net/filebot/Main.java +++ b/source/net/filebot/Main.java @@ -313,7 +313,7 @@ public class Main { started.flush(); // open Getting Started - SwingUtilities.invokeLater(() -> GettingStartedStage.start()); + SwingUtilities.invokeLater(GettingStartedStage::start); } } diff --git a/source/net/filebot/cli/GroovyPad.java b/source/net/filebot/cli/GroovyPad.java index 049e00cf..3706f96a 100644 --- a/source/net/filebot/cli/GroovyPad.java +++ b/source/net/filebot/cli/GroovyPad.java @@ -27,7 +27,6 @@ import javax.swing.JToolBar; import javax.swing.KeyStroke; import javax.swing.SwingUtilities; import javax.swing.SwingWorker; -import javax.swing.text.BadLocationException; import javax.swing.text.JTextComponent; import org.fife.ui.rsyntaxtextarea.FileLocation; @@ -298,25 +297,20 @@ public class GroovyPad extends JFrame { try { String message = this.toString("UTF-8"); reset(); - commit(message); - } catch (UnsupportedEncodingException e) { + } catch (Exception e) { // can't happen } } private void commit(final String line) { - SwingUtilities.invokeLater(new Runnable() { - - @Override - public void run() { - try { - int offset = textComponent.getDocument().getLength(); - textComponent.getDocument().insertString(offset, line, null); - textComponent.setCaretPosition(textComponent.getDocument().getLength()); - } catch (BadLocationException e) { - // ignore - } + SwingUtilities.invokeLater(() -> { + try { + int offset = textComponent.getDocument().getLength(); + textComponent.getDocument().insertString(offset, line, null); + textComponent.setCaretPosition(textComponent.getDocument().getLength()); + } catch (Exception e) { + // ignore } }); } diff --git a/source/net/filebot/ui/rename/ConflictDialog.java b/source/net/filebot/ui/rename/ConflictDialog.java index 7999cca9..92ac8b8b 100644 --- a/source/net/filebot/ui/rename/ConflictDialog.java +++ b/source/net/filebot/ui/rename/ConflictDialog.java @@ -81,7 +81,7 @@ class ConflictDialog extends JDialog { c.add(b, "tag next"); // focus "Continue" button - SwingUtilities.invokeLater(() -> c.getComponent(2).requestFocusInWindow()); + SwingUtilities.invokeLater(c.getComponent(2)::requestFocusInWindow); installAction(c, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), newAction("Cancel", this::cancel)); diff --git a/source/net/filebot/ui/rename/ValidateDialog.java b/source/net/filebot/ui/rename/ValidateDialog.java index 4d89c123..ad91d22f 100644 --- a/source/net/filebot/ui/rename/ValidateDialog.java +++ b/source/net/filebot/ui/rename/ValidateDialog.java @@ -37,7 +37,7 @@ import net.miginfocom.swing.MigLayout; class ValidateDialog extends JDialog { - private final JList list; + private JList list; private File[] model; @@ -61,7 +61,7 @@ class ValidateDialog extends JDialog { c.add(new JButton(continueAction), "tag ok"); // focus "Validate" button - SwingUtilities.invokeLater(() -> c.getComponent(2).requestFocusInWindow()); + SwingUtilities.invokeLater(c.getComponent(2)::requestFocusInWindow); installAction(c, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelAction); @@ -140,9 +140,8 @@ class ValidateDialog extends JDialog { private static class IndexView extends AbstractList { - private final List mapping = new ArrayList(); - - private final List source; + private List mapping = new ArrayList(); + private List source; public IndexView(List source) { this.source = source; diff --git a/source/net/filebot/ui/sfv/TotalProgressPanel.java b/source/net/filebot/ui/sfv/TotalProgressPanel.java index 43f1c6ae..f613635f 100644 --- a/source/net/filebot/ui/sfv/TotalProgressPanel.java +++ b/source/net/filebot/ui/sfv/TotalProgressPanel.java @@ -1,7 +1,6 @@ package net.filebot.ui.sfv; - import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.PropertyChangeEvent; @@ -15,14 +14,12 @@ import javax.swing.border.TitledBorder; import net.miginfocom.swing.MigLayout; - class TotalProgressPanel extends JComponent { private final JProgressBar progressBar = new JProgressBar(0, 0); private final int millisToSetVisible = 200; - public TotalProgressPanel(ChecksumComputationService computationService) { setLayout(new MigLayout("insets 1px")); @@ -45,36 +42,28 @@ class TotalProgressPanel extends JComponent { private final DelayedToggle delayed = new DelayedToggle(); - @Override public void propertyChange(PropertyChangeEvent evt) { - final int completedTaskCount = getComputationService(evt).getCompletedTaskCount(); - final int totalTaskCount = getComputationService(evt).getTotalTaskCount(); + int completedTaskCount = getComputationService(evt).getCompletedTaskCount(); + int totalTaskCount = getComputationService(evt).getTotalTaskCount(); // invoke on EDT - SwingUtilities.invokeLater(new Runnable() { + SwingUtilities.invokeLater(() -> { + if (completedTaskCount == totalTaskCount) { + // delayed hide on reset, immediate hide on finish + delayed.toggle(HIDE, totalTaskCount == 0 ? millisToSetVisible : 0, visibilityActionHandler); + } else if (totalTaskCount != 0) { + delayed.toggle(SHOW, millisToSetVisible, visibilityActionHandler); + } - @Override - public void run() { - - if (completedTaskCount == totalTaskCount) { - // delayed hide on reset, immediate hide on finish - delayed.toggle(HIDE, totalTaskCount == 0 ? millisToSetVisible : 0, visibilityActionHandler); - } else if (totalTaskCount != 0) { - delayed.toggle(SHOW, millisToSetVisible, visibilityActionHandler); - } - - if (totalTaskCount != 0) { - progressBar.setValue(completedTaskCount); - progressBar.setMaximum(totalTaskCount); - - progressBar.setString(String.format("%d / %d", completedTaskCount, totalTaskCount)); - } - }; + if (totalTaskCount != 0) { + progressBar.setValue(completedTaskCount); + progressBar.setMaximum(totalTaskCount); + progressBar.setString(String.format("%d / %d", completedTaskCount, totalTaskCount)); + } }); } - private ChecksumComputationService getComputationService(PropertyChangeEvent evt) { return ((ChecksumComputationService) evt.getSource()); } @@ -89,12 +78,10 @@ class TotalProgressPanel extends JComponent { }; - protected static class DelayedToggle { private Timer timer = null; - public void toggle(String action, int delay, final ActionListener actionHandler) { if (timer != null) { if (action.equals(timer.getActionCommand())) { diff --git a/source/net/filebot/ui/subtitle/SubtitleAutoMatchDialog.java b/source/net/filebot/ui/subtitle/SubtitleAutoMatchDialog.java index 8905c77a..7e0d59aa 100644 --- a/source/net/filebot/ui/subtitle/SubtitleAutoMatchDialog.java +++ b/source/net/filebot/ui/subtitle/SubtitleAutoMatchDialog.java @@ -219,7 +219,7 @@ class SubtitleAutoMatchDialog extends JDialog { @Override protected void done() { - SwingUtilities.invokeLater(() -> mappingModel.fireTableStructureChanged()); // make sure UI is refershed after completion + SwingUtilities.invokeLater(mappingModel::fireTableStructureChanged); // make sure UI is refershed after completion } }; diff --git a/source/net/filebot/ui/transfer/BackgroundFileTransferablePolicy.java b/source/net/filebot/ui/transfer/BackgroundFileTransferablePolicy.java index 89a14c98..6d6aa1d6 100644 --- a/source/net/filebot/ui/transfer/BackgroundFileTransferablePolicy.java +++ b/source/net/filebot/ui/transfer/BackgroundFileTransferablePolicy.java @@ -75,14 +75,8 @@ public abstract class BackgroundFileTransferablePolicy extends FileTransferab worker.offer(chunks); } - protected final void publish(final Exception exception) { - SwingUtilities.invokeLater(new Runnable() { - - @Override - public void run() { - process(exception); - } - }); + protected final void publish(Exception exception) { + SwingUtilities.invokeLater(() -> process(exception)); } protected class BackgroundWorker extends SwingWorker {