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); 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,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 private Runner currentRunner = null;
public void actionPerformed(ActionEvent evt) {
protected void runScript(ActionEvent evt) {
// persist script file and clear output // persist script file and clear output
try { try {
editor.save(); editor.save();
@ -182,36 +184,41 @@ public class GroovyPad extends JFrame {
@Override @Override
protected void done() { protected void done() {
action_run.setEnabled(true); run.setEnabled(true);
action_cancel.setEnabled(false); cancel.setEnabled(false);
} }
}; };
action_run.setEnabled(false); run.setEnabled(false);
action_cancel.setEnabled(true); cancel.setEnabled(true);
currentRunner.execute(); 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 @Override
public void actionPerformed(ActionEvent evt) { protected void done() {
if (currentRunner != null && !currentRunner.isDone()) { run.setEnabled(true);
currentRunner.cancel(true); cancel.setEnabled(false);
currentRunner.getExecutionThread().stop();
try {
currentRunner.get(2, TimeUnit.SECONDS);
} catch (Exception e) {
// ignore
}
}
} }
}; };
private Runner currentRunner = null; run.setEnabled(false);
cancel.setEnabled(true);
currentRunner.execute();
}
}
protected class Runner extends SwingWorker<Object, Object> { protected class Runner extends SwingWorker<Object, Object> {

View File

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

View File

@ -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
public void actionPerformed(ActionEvent e) {
if (undoSupport.canUndo())
undoSupport.undo(); 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
public void actionPerformed(ActionEvent e) {
if (undoSupport.canRedo())
undoSupport.redo(); 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);
} }