groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jwagenleit...@apache.org
Subject [1/2] groovy git commit: GROOVY-3270: GroovyConsole: Configurable output area size (closes #548)
Date Fri, 26 May 2017 00:48:05 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_6_X 27562c34f -> ec6c596a2


GROOVY-3270: GroovyConsole: Configurable output area size (closes #548)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/15fe82e5
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/15fe82e5
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/15fe82e5

Branch: refs/heads/GROOVY_2_6_X
Commit: 15fe82e5fa2f3bace29f670157a0d2998f1dbfd5
Parents: 27562c3
Author: Emilian Bold <emi@apache.org>
Authored: Sun May 21 23:18:39 2017 +0300
Committer: John Wagenleitner <jwagenleitner@apache.org>
Committed: Thu May 25 17:47:03 2017 -0700

----------------------------------------------------------------------
 .../src/main/groovy/groovy/ui/Console.groovy    |  17 +++
 .../main/groovy/groovy/ui/ConsoleActions.groovy |   7 +
 .../groovy/groovy/ui/ConsolePreferences.groovy  | 129 +++++++++++++++++++
 .../groovy/groovy/ui/view/BasicMenuBar.groovy   |   2 +
 .../groovy/groovy/ui/view/MacOSXMenuBar.groovy  |  10 +-
 .../main/resources/groovy/ui/Console.properties |   7 +
 6 files changed, 170 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/15fe82e5/subprojects/groovy-console/src/main/groovy/groovy/ui/Console.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-console/src/main/groovy/groovy/ui/Console.groovy b/subprojects/groovy-console/src/main/groovy/groovy/ui/Console.groovy
index 9db505b..4bfb2d4 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/ui/Console.groovy
+++ b/subprojects/groovy-console/src/main/groovy/groovy/ui/Console.groovy
@@ -200,6 +200,8 @@ class Console implements CaretListener, HyperlinkListener, ComponentListener,
Fo
     Action selectWordAction
     Action selectPreviousWordAction
 
+    ConsolePreferences consolePreferences;
+
     static void main(args) {
         CliBuilder cli = new CliBuilder(usage: 'groovyConsole [options] [filename]', stopAtNonOption:
false)
         MessageSource messages = new MessageSource(Console)
@@ -250,6 +252,20 @@ class Console implements CaretListener, HyperlinkListener, ComponentListener,
Fo
 
     }
 
+    int loadMaxOutputChars() {
+        // For backwards compatibility 'maxOutputChars' remains defined in the Console class
+        // and the System Property takes precedence as the default value.
+        int max = prefs.getInt('maxOutputChars', ConsolePreferences.DEFAULT_MAX_OUTPUT_CHARS)
+        return System.getProperty('groovy.console.output.limit', "${max}") as int
+    }
+
+    void preferences(EventObject evt = null) {
+        if (!consolePreferences) {
+            consolePreferences = new ConsolePreferences(this)
+        }
+        consolePreferences.show()
+    }
+
     Console() {
         this(new Binding())
     }
@@ -268,6 +284,7 @@ class Console implements CaretListener, HyperlinkListener, ComponentListener,
Fo
 
     Console(ClassLoader parent, Binding binding, CompilerConfiguration baseConfig) {
         this.baseConfig = baseConfig
+        this.maxOutputChars = loadMaxOutputChars()
         indy = indy || isIndyEnabled(baseConfig)
         if (indy) {
             enableIndy(baseConfig)

http://git-wip-us.apache.org/repos/asf/groovy/blob/15fe82e5/subprojects/groovy-console/src/main/groovy/groovy/ui/ConsoleActions.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-console/src/main/groovy/groovy/ui/ConsoleActions.groovy b/subprojects/groovy-console/src/main/groovy/groovy/ui/ConsoleActions.groovy
index ddb2d3d..3382c2b 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/ui/ConsoleActions.groovy
+++ b/subprojects/groovy-console/src/main/groovy/groovy/ui/ConsoleActions.groovy
@@ -407,3 +407,10 @@ indyAction = action(
     mnemonic: 'I',
     shortDescription: 'Enable InvokeDynamic (Indy) compilation for scripts'
 )
+
+preferencesAction = action(
+    name: 'Preferences',
+    closure: controller.&preferences,
+    mnemonic: 'S',
+    shortDescription: 'Preference Settings'
+)
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/groovy/blob/15fe82e5/subprojects/groovy-console/src/main/groovy/groovy/ui/ConsolePreferences.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-console/src/main/groovy/groovy/ui/ConsolePreferences.groovy
b/subprojects/groovy-console/src/main/groovy/groovy/ui/ConsolePreferences.groovy
new file mode 100644
index 0000000..7296762
--- /dev/null
+++ b/subprojects/groovy-console/src/main/groovy/groovy/ui/ConsolePreferences.groovy
@@ -0,0 +1,129 @@
+/*
+ * Copyright 2003-2017 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package groovy.ui
+
+import groovy.beans.Bindable
+import groovy.swing.SwingBuilder
+import org.codehaus.groovy.tools.shell.util.MessageSource
+
+import java.awt.Dimension
+import javax.swing.JDialog
+import static java.awt.GridBagConstraints.*
+
+class ConsolePreferences {
+
+    // Default maximum number of characters to show on console at any time
+    static int DEFAULT_MAX_OUTPUT_CHARS = 20000
+
+    @Bindable int maxOutputChars
+
+    private final console
+    private final MessageSource T
+
+    private JDialog dialog
+
+    ConsolePreferences(console) {
+        this.console = console
+        T = new MessageSource(Console)
+
+        maxOutputChars = console.loadMaxOutputChars()
+        console.maxOutputChars = maxOutputChars
+    }
+
+    void show() {
+        console.swing.edt {
+            if (!dialog) {
+                buildDialog()
+            }
+            dialog.setLocationRelativeTo(console.frame)
+            dialog.pack()
+            dialog.getRootPane().setDefaultButton(console.swing.closePrefsButton)
+            console.swing.doLater console.swing.closePrefsButton.&requestFocusInWindow
+            dialog.setVisible(true)
+        }
+    }
+
+    private void buildDialog() {
+        dialog = console.swing.dialog(
+                title: T['prefs.dialog.title'], owner: console.frame, modal: true
+        ) {
+            vbox {
+                hbox(border: titledBorder(T['prefs.output.settings.title'])) {
+                    label "${T['prefs.max.characters.output']}:"
+
+                    formattedTextField value: maxOutputChars, id: 'txtMaxOutputChars',
+                            text:
+                                    bind(target: this, targetProperty: 'maxOutputChars',
+                                            validator: this.&isInteger, converter: Integer.&parseInt),
+                            columns: 6
+                }
+
+                vglue()
+
+                hbox {
+                    button T['prefs.reset.defaults'], id: 'resetPrefsButton', actionPerformed:
this.&onReset
+                    hglue()
+                    button T['prefs.close'], id: 'closePrefsButton', actionPerformed: this.&onClose
+                }
+            }
+        }
+
+        console.swing.txtMaxOutputChars.maximumSize=new Dimension(Integer.MAX_VALUE, (int)
console.swing.txtMaxOutputChars.preferredSize.height)
+    }
+
+    private boolean isInteger(value) {
+        try {
+            Integer.parseInt(value)
+            return true
+        } catch (NumberFormatException ignore) {
+            return false
+        }
+    }
+
+    private void onReset(EventObject event) {
+        console.swing.txtMaxOutputChars.text = DEFAULT_MAX_OUTPUT_CHARS
+    }
+
+    private void onClose(EventObject event) {
+        console.prefs.putInt('maxOutputChars', maxOutputChars)
+        // For backwards compatibility 'maxOutputChars' remains defined in the Console class
+        // and so we update the value to keep it in sync.
+        if (maxOutputChars != console.maxOutputChars) {
+            console.maxOutputChars = maxOutputChars
+        }
+        dialog.dispose()
+    }
+
+    // Useful for testing gui
+    static void main(args) {
+        javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName())
+        def c = new Expando().with {
+            swing = new SwingBuilder()
+            frame = swing.frame(title: 'foo', size:[800, 800])
+            DEFAULT_MAX_OUTPUT_CHARS = 25000
+            maxOutputChars = 25000
+            loadMaxOutputChars = { 20000 }
+            prefs = [putInt: { s, t -> }, getInt: { s, t -> t }]
+            it
+        }
+        ConsolePreferences cp = new ConsolePreferences(c)
+        cp.show()
+        c.frame.dispose()
+        println "maxOutputChars==${cp.maxOutputChars}"
+        println "console.maxOutputChars==${c.maxOutputChars}"
+        println 'done'
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/groovy/blob/15fe82e5/subprojects/groovy-console/src/main/groovy/groovy/ui/view/BasicMenuBar.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-console/src/main/groovy/groovy/ui/view/BasicMenuBar.groovy
b/subprojects/groovy-console/src/main/groovy/groovy/ui/view/BasicMenuBar.groovy
index dc169cf..ea84739 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/ui/view/BasicMenuBar.groovy
+++ b/subprojects/groovy-console/src/main/groovy/groovy/ui/view/BasicMenuBar.groovy
@@ -53,6 +53,8 @@ menuBar {
 	separator()
 	menuItem(commentAction)
         menuItem(selectBlockAction)
+        separator()
+        menuItem(preferencesAction)
     }
 
     menu(text: 'View', mnemonic: 'V') {

http://git-wip-us.apache.org/repos/asf/groovy/blob/15fe82e5/subprojects/groovy-console/src/main/groovy/groovy/ui/view/MacOSXMenuBar.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-console/src/main/groovy/groovy/ui/view/MacOSXMenuBar.groovy
b/subprojects/groovy-console/src/main/groovy/groovy/ui/view/MacOSXMenuBar.groovy
index 1461f7b..78e9686 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/ui/view/MacOSXMenuBar.groovy
+++ b/subprojects/groovy-console/src/main/groovy/groovy/ui/view/MacOSXMenuBar.groovy
@@ -26,10 +26,11 @@ package groovy.ui
 
 import com.apple.mrj.*
 
-class ConsoleMacOsSupport implements MRJQuitHandler, MRJAboutHandler {
+class ConsoleMacOsSupport implements MRJQuitHandler, MRJAboutHandler, MRJPrefsHandler {
 
     def quitHandler
     def aboutHandler
+    def prefHandler
 
     public void handleAbout() {
         aboutHandler()
@@ -39,11 +40,16 @@ class ConsoleMacOsSupport implements MRJQuitHandler, MRJAboutHandler {
         quitHandler()
     }
 
+
+    public void handlePrefs() throws IllegalStateException {
+        prefHandler()
+    }
 }
 
-def handler = new ConsoleMacOsSupport(quitHandler:controller.&exit, aboutHandler:controller.&showAbout)
+def handler = new ConsoleMacOsSupport(quitHandler:controller.&exit, aboutHandler:controller.&showAbout,
prefHandler:controller.&preferences)
 MRJApplicationUtils.registerAboutHandler(handler)
 MRJApplicationUtils.registerQuitHandler(handler)
+MRJApplicationUtils.registerPrefsHandler(handler)
 
 return handler
 """, new GroovyClassLoader(this.class.classLoader))

http://git-wip-us.apache.org/repos/asf/groovy/blob/15fe82e5/subprojects/groovy-console/src/main/resources/groovy/ui/Console.properties
----------------------------------------------------------------------
diff --git a/subprojects/groovy-console/src/main/resources/groovy/ui/Console.properties b/subprojects/groovy-console/src/main/resources/groovy/ui/Console.properties
index 0793ebc..b04c0ec 100644
--- a/subprojects/groovy-console/src/main/resources/groovy/ui/Console.properties
+++ b/subprojects/groovy-console/src/main/resources/groovy/ui/Console.properties
@@ -29,3 +29,10 @@ cli.option.parameters.description=Generate metadata for reflection on method
par
 cli.option.indy.description=Enable InvokeDynamic (Indy) compilation for scripts
 
 cli.info.version=GroovyConsole {0}
+
+# Preferences Dialog
+prefs.dialog.title=Preferences
+prefs.output.settings.title=Output Settings
+prefs.max.characters.output=Maximum Characters to Output
+prefs.reset.defaults=Reset To Defaults
+prefs.close=Close


Mime
View raw message