This commit is contained in:
Reinhard Pointner 2016-03-10 04:42:39 +00:00
parent 4cd606d644
commit 537144187a
3 changed files with 79 additions and 75 deletions

View File

@ -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,10 +165,12 @@ 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) {
private Runner currentRunner = null;
protected void runScript(ActionEvent evt) {
// persist script file and clear output
try {
editor.save();
@ -182,36 +184,41 @@ public class GroovyPad extends JFrame {
@Override
protected void done() {
action_run.setEnabled(true);
action_cancel.setEnabled(false);
run.setEnabled(true);
cancel.setEnabled(false);
}
};
action_run.setEnabled(false);
action_cancel.setEnabled(true);
run.setEnabled(false);
cancel.setEnabled(true);
currentRunner.execute();
}
}
};
protected final Action action_cancel = new AbstractAction("Cancel", ResourceManager.getIcon("script.cancel")) {
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
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 void done() {
run.setEnabled(true);
cancel.setEnabled(false);
}
};
private Runner currentRunner = null;
run.setEnabled(false);
cancel.setEnabled(true);
currentRunner.execute();
}
}
protected class Runner extends SwingWorker<Object, Object> {

View File

@ -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") {
@Override
public void actionPerformed(ActionEvent e) {
cancelAction.setEnabled(false);
public void cancel(ActionEvent evt) {
cancel.setEnabled(false);
cancellable.cancel();
}
};
protected final Action cancel = newAction("Cancel", this::cancel);
public static interface Cancellable {

View File

@ -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())
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())
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);
}