groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject [groovy] 01/04: Loop mode for Groovy Console
Date Tue, 11 Jun 2019 12:28:35 GMT
This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 4aac3e109aa48c0439574b4fa5d15c03b44b8a5e
Author: Jacob Aae Mikkelsen <github@grydeske.dk>
AuthorDate: Mon May 27 22:01:29 2019 +0200

    Loop mode for Groovy Console
---
 .../main/groovy/groovy/console/ui/Console.groovy   | 28 ++++++++++++++++++++--
 .../groovy/groovy/console/ui/ConsoleActions.groovy |  7 ++++++
 .../groovy/console/ui/view/BasicMenuBar.groovy     |  1 +
 3 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Console.groovy b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Console.groovy
index cfc46f3..32f42e9 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Console.groovy
+++ b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Console.groovy
@@ -57,6 +57,7 @@ import javax.swing.JSplitPane
 import javax.swing.JTextPane
 import javax.swing.RootPaneContainer
 import javax.swing.SwingUtilities
+import javax.swing.Timer
 import javax.swing.UIManager
 import javax.swing.event.CaretEvent
 import javax.swing.event.CaretListener
@@ -143,8 +144,12 @@ class Console implements CaretListener, HyperlinkListener, ComponentListener,
Fo
     boolean threadInterrupt = prefs.getBoolean('threadInterrupt', false)
     Action threadInterruptAction
 
-    boolean saveOnRun = prefs.getBoolean('saveOnRun', false)
     Action saveOnRunAction
+    boolean saveOnRun = prefs.getBoolean('saveOnRun', false)
+
+    Action loopModeAction
+    boolean loopMode = prefs.getBoolean('loopMode', false)
+    int inputAreaContentHash
 
     boolean indy = prefs.getBoolean('indy', false)
     Action indyAction
@@ -1106,8 +1111,8 @@ class Console implements CaretListener, HyperlinkListener, ComponentListener,
Fo
     }
 
     // actually run the script
-
     void runScript(EventObject evt = null) {
+        saveInputAreaContentHash()
         if (saveOnRun && scriptFile != null) {
             if (fileSave(evt)) runScriptImpl(false)
         } else {
@@ -1120,6 +1125,11 @@ class Console implements CaretListener, HyperlinkListener, ComponentListener,
Fo
         prefs.putBoolean('saveOnRun', saveOnRun)
     }
 
+    void loopMode(EventObject evt = null) {
+        loopMode = evt.source.selected
+        prefs.putBoolean('loopMode', loopMode)
+    }
+
     void indy(EventObject evt = null) {
         indy = evt.source.selected
         prefs.putBoolean('indy', indy)
@@ -1145,6 +1155,7 @@ class Console implements CaretListener, HyperlinkListener, ComponentListener,
Fo
     }
 
     void runSelectedScript(EventObject evt = null) {
+        saveInputAreaContentHash()
         runScriptImpl(true)
     }
 
@@ -1210,6 +1221,10 @@ class Console implements CaretListener, HyperlinkListener, ComponentListener,
Fo
         binding.variables._outputTransforms = OutputTransforms.loadOutputTransforms()
     }
 
+    private void saveInputAreaContentHash() {
+        inputAreaContentHash = inputArea.getText().hashCode()
+    }
+
     private void runScriptImpl(boolean selected) {
         if (scriptRunning) {
             statusLabel.text = 'Cannot run script now as a script is already running. Please
wait or use "Interrupt Script" option.'
@@ -1274,6 +1289,15 @@ class Console implements CaretListener, HyperlinkListener, ComponentListener,
Fo
                 scriptRunning = false
                 interruptAction.enabled = false
                 systemOutInterceptor.removeConsoleId()
+                if( loopMode ) {
+                    Timer timer = new Timer(1000, {
+                        if( inputAreaContentHash == inputArea.getText().hashCode() ) {
+                            runScriptImpl(selected)
+                        }
+                    })
+                    timer.repeats = false
+                    timer.start()
+                }
             }
         }
     }
diff --git a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ConsoleActions.groovy
b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ConsoleActions.groovy
index 33419ed..2b93fe8 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ConsoleActions.groovy
+++ b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ConsoleActions.groovy
@@ -204,6 +204,13 @@ runAction = action(
         shortDescription: 'Execute Groovy Script'
 )
 
+loopModeAction = action(
+        name: 'Loop Mode',
+        closure: controller.&loopMode,
+        mnemonic: 'p',
+        shortDescription: 'Run script continuously in a loop when run is envoked. Uncheck
to stop loop'
+)
+
 runSelectionAction = action(
         name: 'Run Selection',
         closure: controller.&runSelectedScript,
diff --git a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/view/BasicMenuBar.groovy
b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/view/BasicMenuBar.groovy
index 8fe74e0..84328a5 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/view/BasicMenuBar.groovy
+++ b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/view/BasicMenuBar.groovy
@@ -78,6 +78,7 @@ menuBar {
 
     menu(text: 'Script', mnemonic: 'S') {
         menuItem(runAction)
+        checkBoxMenuItem(loopModeAction, selected: controller.loopMode)
         checkBoxMenuItem(saveOnRunAction, selected: controller.saveOnRun)
         menuItem(runSelectionAction)
         checkBoxMenuItem(threadInterruptAction, selected: controller.threadInterrupt)


Mime
View raw message