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);
|
JToolBar tools = new JToolBar("Run", JToolBar.HORIZONTAL);
|
||||||
tools.setFloatable(true);
|
tools.setFloatable(true);
|
||||||
tools.add(action_run);
|
tools.add(run);
|
||||||
tools.add(action_cancel);
|
tools.add(cancel);
|
||||||
c.add(tools, BorderLayout.NORTH);
|
c.add(tools, BorderLayout.NORTH);
|
||||||
|
|
||||||
action_run.setEnabled(true);
|
run.setEnabled(true);
|
||||||
action_cancel.setEnabled(false);
|
cancel.setEnabled(false);
|
||||||
|
|
||||||
installAction(c, KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), action_run);
|
installAction(c, KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), run);
|
||||||
installAction(c, KeyStroke.getKeyStroke(KeyEvent.VK_R, KeyEvent.CTRL_DOWN_MASK), action_run);
|
installAction(c, KeyStroke.getKeyStroke(KeyEvent.VK_R, KeyEvent.CTRL_DOWN_MASK), run);
|
||||||
|
|
||||||
addWindowListener(new WindowAdapter() {
|
addWindowListener(new WindowAdapter() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void windowClosed(WindowEvent evt) {
|
public void windowClosed(WindowEvent evt) {
|
||||||
action_cancel.actionPerformed(null);
|
cancel.actionPerformed(null);
|
||||||
console.unhook();
|
console.unhook();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -165,54 +165,61 @@ public class GroovyPad extends JFrame {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected final Action action_run = new AbstractAction("Run", ResourceManager.getIcon("script.go")) {
|
protected final Action run = newAction("Run", ResourceManager.getIcon("script.go"), this::runScript);
|
||||||
|
protected final Action cancel = newAction("Cancel", ResourceManager.getIcon("script.cancel"), this::cancelScript);
|
||||||
@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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private Runner currentRunner = null;
|
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> {
|
protected class Runner extends SwingWorker<Object, Object> {
|
||||||
|
|
||||||
private Thread executionThread;
|
private Thread executionThread;
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
package net.filebot.util.ui;
|
package net.filebot.util.ui;
|
||||||
|
|
||||||
|
import static net.filebot.util.ui.SwingUI.*;
|
||||||
|
|
||||||
import java.awt.Cursor;
|
import java.awt.Cursor;
|
||||||
import java.awt.Window;
|
import java.awt.Window;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
|
|
||||||
import javax.swing.AbstractAction;
|
|
||||||
import javax.swing.Action;
|
import javax.swing.Action;
|
||||||
import javax.swing.Icon;
|
import javax.swing.Icon;
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
|
@ -42,7 +43,7 @@ public class ProgressDialog extends JDialog {
|
||||||
c.add(headerLabel, "gap 3mm, wrap paragraph");
|
c.add(headerLabel, "gap 3mm, wrap paragraph");
|
||||||
c.add(progressBar, "hmin 25px, grow, 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);
|
setSize(350, 155);
|
||||||
}
|
}
|
||||||
|
@ -81,14 +82,12 @@ public class ProgressDialog extends JDialog {
|
||||||
dispose();
|
dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected final Action cancelAction = new AbstractAction("Cancel") {
|
public void cancel(ActionEvent evt) {
|
||||||
|
cancel.setEnabled(false);
|
||||||
|
cancellable.cancel();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
protected final Action cancel = newAction("Cancel", this::cancel);
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
cancelAction.setEnabled(false);
|
|
||||||
cancellable.cancel();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public static interface Cancellable {
|
public static interface Cancellable {
|
||||||
|
|
||||||
|
|
|
@ -143,24 +143,18 @@ public final class SwingUI {
|
||||||
component.getDocument().addUndoableEditListener(undoSupport);
|
component.getDocument().addUndoableEditListener(undoSupport);
|
||||||
|
|
||||||
// install undo action
|
// install undo action
|
||||||
installAction(component, KeyStroke.getKeyStroke(KeyEvent.VK_Z, KeyEvent.CTRL_MASK), new AbstractAction("Undo") {
|
installAction(component, KeyStroke.getKeyStroke(KeyEvent.VK_Z, KeyEvent.CTRL_MASK), newAction("Undo", evt -> {
|
||||||
|
if (undoSupport.canUndo()) {
|
||||||
@Override
|
undoSupport.undo();
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
if (undoSupport.canUndo())
|
|
||||||
undoSupport.undo();
|
|
||||||
}
|
}
|
||||||
});
|
}));
|
||||||
|
|
||||||
// install redo action
|
// install redo action
|
||||||
installAction(component, KeyStroke.getKeyStroke(KeyEvent.VK_Y, KeyEvent.CTRL_MASK), new AbstractAction("Redo") {
|
installAction(component, KeyStroke.getKeyStroke(KeyEvent.VK_Y, KeyEvent.CTRL_MASK), newAction("Redo", evt -> {
|
||||||
|
if (undoSupport.canRedo()) {
|
||||||
@Override
|
undoSupport.redo();
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
if (undoSupport.canRedo())
|
|
||||||
undoSupport.redo();
|
|
||||||
}
|
}
|
||||||
});
|
}));
|
||||||
|
|
||||||
return undoSupport;
|
return undoSupport;
|
||||||
}
|
}
|
||||||
|
@ -271,6 +265,10 @@ public final class SwingUI {
|
||||||
return new JButton(new LambdaAction(name, icon, action));
|
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) {
|
public static Action newAction(String name, Icon icon, Consumer<ActionEvent> action) {
|
||||||
return new LambdaAction(name, icon, action);
|
return new LambdaAction(name, icon, action);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue