From 537144187a63b143be82108a71cd5e62d84f44bf Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Thu, 10 Mar 2016 04:42:39 +0000 Subject: [PATCH] Refactor --- source/net/filebot/cli/GroovyPad.java | 111 ++++++++++-------- .../net/filebot/util/ui/ProgressDialog.java | 17 ++- source/net/filebot/util/ui/SwingUI.java | 26 ++-- 3 files changed, 79 insertions(+), 75 deletions(-) diff --git a/source/net/filebot/cli/GroovyPad.java b/source/net/filebot/cli/GroovyPad.java index 87f74d21..7fc9b359 100644 --- a/source/net/filebot/cli/GroovyPad.java +++ b/source/net/filebot/cli/GroovyPad.java @@ -57,21 +57,21 @@ public class GroovyPad extends JFrame { JToolBar tools = new JToolBar("Run", JToolBar.HORIZONTAL); tools.setFloatable(true); - tools.add(action_run); - tools.add(action_cancel); + tools.add(run); + tools.add(cancel); c.add(tools, BorderLayout.NORTH); - action_run.setEnabled(true); - action_cancel.setEnabled(false); + run.setEnabled(true); + cancel.setEnabled(false); - installAction(c, KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), action_run); - installAction(c, KeyStroke.getKeyStroke(KeyEvent.VK_R, KeyEvent.CTRL_DOWN_MASK), action_run); + installAction(c, KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), run); + installAction(c, KeyStroke.getKeyStroke(KeyEvent.VK_R, KeyEvent.CTRL_DOWN_MASK), run); addWindowListener(new WindowAdapter() { @Override public void windowClosed(WindowEvent evt) { - action_cancel.actionPerformed(null); + cancel.actionPerformed(null); console.unhook(); try { @@ -165,54 +165,61 @@ public class GroovyPad extends JFrame { } } - protected final Action action_run = new AbstractAction("Run", ResourceManager.getIcon("script.go")) { - - @Override - public void actionPerformed(ActionEvent evt) { - // persist script file and clear output - try { - editor.save(); - } catch (IOException e) { - // won't happen - } - output.setText(""); - - if (currentRunner == null || currentRunner.isDone()) { - currentRunner = new Runner(editor.getText()) { - - @Override - protected void done() { - action_run.setEnabled(true); - action_cancel.setEnabled(false); - } - }; - - action_run.setEnabled(false); - action_cancel.setEnabled(true); - currentRunner.execute(); - } - } - }; - - protected final Action action_cancel = new AbstractAction("Cancel", ResourceManager.getIcon("script.cancel")) { - - @Override - public void actionPerformed(ActionEvent evt) { - if (currentRunner != null && !currentRunner.isDone()) { - currentRunner.cancel(true); - currentRunner.getExecutionThread().stop(); - - try { - currentRunner.get(2, TimeUnit.SECONDS); - } catch (Exception e) { - // ignore - } - } - } - }; + protected final Action run = newAction("Run", ResourceManager.getIcon("script.go"), this::runScript); + protected final Action cancel = newAction("Cancel", ResourceManager.getIcon("script.cancel"), this::cancelScript); private Runner currentRunner = null; + protected void runScript(ActionEvent evt) { + // persist script file and clear output + try { + editor.save(); + } catch (IOException e) { + // won't happen + } + output.setText(""); + + if (currentRunner == null || currentRunner.isDone()) { + currentRunner = new Runner(editor.getText()) { + + @Override + protected void done() { + run.setEnabled(true); + cancel.setEnabled(false); + } + }; + + run.setEnabled(false); + cancel.setEnabled(true); + currentRunner.execute(); + } + } + + protected void cancelScript(ActionEvent evt) { + // persist script file and clear output + try { + editor.save(); + } catch (IOException e) { + // won't happen + } + output.setText(""); + + if (currentRunner == null || currentRunner.isDone()) { + currentRunner = new Runner(editor.getText()) { + + @Override + protected void done() { + run.setEnabled(true); + cancel.setEnabled(false); + } + }; + + run.setEnabled(false); + cancel.setEnabled(true); + currentRunner.execute(); + } + } + protected class Runner extends SwingWorker { private Thread executionThread; diff --git a/source/net/filebot/util/ui/ProgressDialog.java b/source/net/filebot/util/ui/ProgressDialog.java index 1892b87f..d5c50c09 100644 --- a/source/net/filebot/util/ui/ProgressDialog.java +++ b/source/net/filebot/util/ui/ProgressDialog.java @@ -1,10 +1,11 @@ package net.filebot.util.ui; +import static net.filebot.util.ui.SwingUI.*; + import java.awt.Cursor; import java.awt.Window; import java.awt.event.ActionEvent; -import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.Icon; import javax.swing.JButton; @@ -42,7 +43,7 @@ public class ProgressDialog extends JDialog { c.add(headerLabel, "gap 3mm, wrap paragraph"); c.add(progressBar, "hmin 25px, grow, wrap paragraph"); - c.add(new JButton(cancelAction), "align center"); + c.add(new JButton(cancel), "align center"); setSize(350, 155); } @@ -81,14 +82,12 @@ public class ProgressDialog extends JDialog { dispose(); } - protected final Action cancelAction = new AbstractAction("Cancel") { + public void cancel(ActionEvent evt) { + cancel.setEnabled(false); + cancellable.cancel(); + } - @Override - public void actionPerformed(ActionEvent e) { - cancelAction.setEnabled(false); - cancellable.cancel(); - } - }; + protected final Action cancel = newAction("Cancel", this::cancel); public static interface Cancellable { diff --git a/source/net/filebot/util/ui/SwingUI.java b/source/net/filebot/util/ui/SwingUI.java index ed49217b..089b02d8 100644 --- a/source/net/filebot/util/ui/SwingUI.java +++ b/source/net/filebot/util/ui/SwingUI.java @@ -143,24 +143,18 @@ public final class SwingUI { component.getDocument().addUndoableEditListener(undoSupport); // install undo action - installAction(component, KeyStroke.getKeyStroke(KeyEvent.VK_Z, KeyEvent.CTRL_MASK), new AbstractAction("Undo") { - - @Override - public void actionPerformed(ActionEvent e) { - if (undoSupport.canUndo()) - undoSupport.undo(); + installAction(component, KeyStroke.getKeyStroke(KeyEvent.VK_Z, KeyEvent.CTRL_MASK), newAction("Undo", evt -> { + if (undoSupport.canUndo()) { + undoSupport.undo(); } - }); + })); // install redo action - installAction(component, KeyStroke.getKeyStroke(KeyEvent.VK_Y, KeyEvent.CTRL_MASK), new AbstractAction("Redo") { - - @Override - public void actionPerformed(ActionEvent e) { - if (undoSupport.canRedo()) - undoSupport.redo(); + installAction(component, KeyStroke.getKeyStroke(KeyEvent.VK_Y, KeyEvent.CTRL_MASK), newAction("Redo", evt -> { + if (undoSupport.canRedo()) { + undoSupport.redo(); } - }); + })); return undoSupport; } @@ -271,6 +265,10 @@ public final class SwingUI { return new JButton(new LambdaAction(name, icon, action)); } + public static Action newAction(String name, Consumer action) { + return new LambdaAction(name, null, action); + } + public static Action newAction(String name, Icon icon, Consumer action) { return new LambdaAction(name, icon, action); }