mirror of
https://github.com/mitb-archive/filebot
synced 2024-11-02 08:25:02 -04:00
Refactor
This commit is contained in:
parent
4cd606d644
commit
537144187a
@ -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<Object, Object> {
|
||||
|
||||
private Thread executionThread;
|
||||
|
@ -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 {
|
||||
|
||||
|
@ -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<ActionEvent> action) {
|
||||
return new LambdaAction(name, null, action);
|
||||
}
|
||||
|
||||
public static Action newAction(String name, Icon icon, Consumer<ActionEvent> action) {
|
||||
return new LambdaAction(name, icon, action);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user