This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/GROOVY_2_5_X by this push:
     new aee11b6  GROOVY-9029: Save output log file to preferences & clode 
cleanup (closes #894)
aee11b6 is described below

commit aee11b6470267ec39af81a61f7c47047289e74fd
Author: tttao <big...@gmail.com>
AuthorDate: Sun Mar 10 12:19:35 2019 +0100

    GROOVY-9029: Save output log file to preferences & clode cleanup (closes 
#894)
---
 .../src/main/groovy/groovy/ui/Console.groovy       | 110 +++++++++++----------
 .../groovy/groovy/ui/ConsolePreferences.groovy     |  10 ++
 2 files changed, 69 insertions(+), 51 deletions(-)

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 24f57ea..02172cf 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/ui/Console.groovy
+++ b/subprojects/groovy-console/src/main/groovy/groovy/ui/Console.groovy
@@ -267,7 +267,6 @@ class Console implements CaretListener, HyperlinkListener, 
ComponentListener, Fo
         if (remaining && !remaining[-1].startsWith("-")) {
             console.loadScriptFile(remaining[-1] as File)
         }
-
     }
 
     int loadMaxOutputChars() {
@@ -285,12 +284,14 @@ class Console implements CaretListener, 
HyperlinkListener, ComponentListener, Fo
     }
 
     void setOutputPreferences(boolean useOutputFile, File outputFile) {
+        prefs.remove('outputLogFileName')
         if (!useOutputFile) {
             closeOutputPrintWriter(outputFile)
         } else {
             if (outputFile != null) {
                 closeOutputPrintWriter()
                 createOutputPrintWriter(outputFile)
+                prefs.put('outputLogFileName', outputFile.getAbsolutePath())
             }
         }
     }
@@ -331,13 +332,20 @@ class Console implements CaretListener, 
HyperlinkListener, ComponentListener, Fo
         if (indy) {
             enableIndy(baseConfig)
         }
-        newScript(parent, binding);
+
+        // Set up output file for stdout/stderr, if any
+        def outputLogFileName = prefs.get('outputLogFileName', null)
+        if (outputLogFileName) {
+            createOutputPrintWriter(new File(outputLogFileName))
+        }
+
+        newScript(parent, binding)
         try {
             System.setProperty('groovy.full.stacktrace', 
System.getProperty('groovy.full.stacktrace',
                     Boolean.toString(prefs.getBoolean('fullStackTraces', 
false))))
 
         } catch (SecurityException se) {
-            fullStackTracesAction.enabled = false;
+            fullStackTracesAction.enabled = false
         }
         consoleControllers += this
 
@@ -379,7 +387,7 @@ class Console implements CaretListener, HyperlinkListener, 
ComponentListener, Fo
             },
             menuBarDelegate: {arg->
                 current.JMenuBar = build(arg)}
-        ];
+        ]
 
     void run() {
         run(frameConsoleDelegates)
@@ -455,8 +463,7 @@ class Console implements CaretListener, HyperlinkListener, 
ComponentListener, Fo
         }
     }
 
-
-    public void installInterceptor() {
+    void installInterceptor() {
         systemOutInterceptor = new 
SystemOutputInterceptor(this.&notifySystemOut, true)
         systemOutInterceptor.start()
         systemErrorInterceptor = new 
SystemOutputInterceptor(this.&notifySystemErr, false)
@@ -500,14 +507,14 @@ class Console implements CaretListener, 
HyperlinkListener, ComponentListener, Fo
     }
 
     void appendOutput(Component component, AttributeSet style) {
-        SimpleAttributeSet sas = new SimpleAttributeSet();
+        SimpleAttributeSet sas = new SimpleAttributeSet()
         sas.addAttribute(StyleConstants.NameAttribute, 'component')
         StyleConstants.setComponent(sas, component)
         appendOutput(component.toString(), sas)
     }
 
     void appendOutput(Icon icon, AttributeSet style) {
-        SimpleAttributeSet sas = new SimpleAttributeSet();
+        SimpleAttributeSet sas = new SimpleAttributeSet()
         sas.addAttribute(StyleConstants.NameAttribute, 'icon')
         StyleConstants.setIcon(sas, icon)
         appendOutput(icon.toString(), sas)
@@ -540,7 +547,7 @@ class Console implements CaretListener, HyperlinkListener, 
ComponentListener, Fo
                 def hrefAttr = new SimpleAttributeSet()
                 // don't pass a GString as it won't be coerced to String as 
addAttribute takes an Object
                 hrefAttr.addAttribute(HTML.Attribute.HREF, 'file://' + 
fileNameAndLineNumber)
-                style.addAttribute(HTML.Tag.A, hrefAttr);
+                style.addAttribute(HTML.Tag.A, hrefAttr)
 
                 insertString(doc, initialLength,                     
line[0..<index],                    stacktraceStyle)
                 insertString(doc, initialLength + index,             
line[index..<(index + length)],     style)
@@ -553,11 +560,11 @@ class Console implements CaretListener, 
HyperlinkListener, ComponentListener, Fo
         ensureNoDocLengthOverflow(doc)
     }
 
-    void insertString(Document doc, int offset, String text, AttributeSet 
attributeSet) {
+    void insertString(Document doc, int offset, String text, AttributeSet 
attributeSet, boolean outputToFile = true) {
         doc.insertString(offset, text, attributeSet)
 
         // Output to file if activated
-        if (outputPrintWriter != null) {
+        if (outputToFile && outputPrintWriter != null) {
             outputPrintWriter.append(text)
             outputPrintWriter.flush()
         }
@@ -579,7 +586,9 @@ class Console implements CaretListener, HyperlinkListener, 
ComponentListener, Fo
         appendOutput(text, style)
         def doc = outputArea.styledDocument
         def len = doc.length
-        insertString(doc, len, ' \n', style)
+
+        // Disable output to log file in this case ('\n' is removed from 
outputArea next line)
+        insertString(doc, len, ' \n', style, false)
         doc.remove(len, 2) // windows hack to fix (improve?) line spacing
     }
 
@@ -855,7 +864,7 @@ class Console implements CaretListener, HyperlinkListener, 
ComponentListener, Fo
                     def hrefAttr = new SimpleAttributeSet()
                     // don't pass a GString as it won't be coerced to String 
as addAttribute takes an Object
                     hrefAttr.addAttribute(HTML.Attribute.HREF, 'file://' + 
scriptFileName + ':' + errorLine)
-                    style.addAttribute(HTML.Tag.A, hrefAttr);
+                    style.addAttribute(HTML.Tag.A, hrefAttr)
 
                     insertString(doc, doc.length, message + ' at ', 
stacktraceStyle)
                     insertString(doc, doc.length, "line: ${se.line}, column: 
${se.startColumn}\n\n", style)
@@ -1345,45 +1354,44 @@ class Console implements CaretListener, 
HyperlinkListener, ComponentListener, Fo
     }
 
     void comment(EventObject evt = null) {
-       def rootElement = inputArea.document.defaultRootElement
-       def cursorPos = inputArea.getCaretPosition()
-       int startRow = rootElement.getElementIndex(cursorPos)
-       int endRow = startRow
-
-       if (inputArea.getSelectedText()) {
-           def selectionStart = inputArea.getSelectionStart()
-           startRow = rootElement.getElementIndex(selectionStart)
-           def selectionEnd = inputArea.getSelectionEnd()
-           endRow = rootElement.getElementIndex(selectionEnd)
-       }
-
-       // If multiple commented lines intermix with uncommented lines, 
consider them uncommented
-       def allCommented = true
-       startRow.upto(endRow) { rowIndex ->
-           def rowElement = rootElement.getElement(rowIndex)
-           int startOffset = rowElement.getStartOffset()
-           int endOffset = rowElement.getEndOffset()
-           String rowText = inputArea.document.getText(startOffset, endOffset 
- startOffset)
-           if (rowText.trim().length() < 2 || !rowText.trim().substring(0, 
2).equals("//")) {
-               allCommented = false
-           }
-       }
-
-       startRow.upto(endRow) { rowIndex ->
-           def rowElement = rootElement.getElement(rowIndex)
-           int startOffset = rowElement.getStartOffset()
-           int endOffset = rowElement.getEndOffset()
-           String rowText = inputArea.document.getText(startOffset, endOffset 
- startOffset)
-           if (allCommented) {
-               // Uncomment this line if it is already commented
-               int slashOffset = rowText.indexOf("//")
-               inputArea.document.remove(slashOffset + startOffset, 2)
-           } else {
-               // Add comment string in front of this line
-               inputArea.document.insertString(startOffset, "//", new 
SimpleAttributeSet())
-           }
-       }
+        def rootElement = inputArea.document.defaultRootElement
+        def cursorPos = inputArea.getCaretPosition()
+        int startRow = rootElement.getElementIndex(cursorPos)
+        int endRow = startRow
+
+        if (inputArea.getSelectedText()) {
+            def selectionStart = inputArea.getSelectionStart()
+            startRow = rootElement.getElementIndex(selectionStart)
+            def selectionEnd = inputArea.getSelectionEnd()
+            endRow = rootElement.getElementIndex(selectionEnd)
+        }
+
+        // If multiple commented lines intermix with uncommented lines, 
consider them uncommented
+        def allCommented = true
+        startRow.upto(endRow) { rowIndex ->
+            def rowElement = rootElement.getElement(rowIndex)
+            int startOffset = rowElement.getStartOffset()
+            int endOffset = rowElement.getEndOffset()
+            String rowText = inputArea.document.getText(startOffset, endOffset 
- startOffset)
+            if (rowText.trim().length() < 2 || !rowText.trim().substring(0, 
2).equals("//")) {
+                allCommented = false
+            }
+        }
 
+        startRow.upto(endRow) { rowIndex ->
+            def rowElement = rootElement.getElement(rowIndex)
+            int startOffset = rowElement.getStartOffset()
+            int endOffset = rowElement.getEndOffset()
+            String rowText = inputArea.document.getText(startOffset, endOffset 
- startOffset)
+            if (allCommented) {
+            // Uncomment this line if it is already commented
+            int slashOffset = rowText.indexOf("//")
+            inputArea.document.remove(slashOffset + startOffset, 2)
+            } else {
+                // Add comment string in front of this line
+                inputArea.document.insertString(startOffset, "//", new 
SimpleAttributeSet())
+            }
+        }
     }
 
     void selectBlock(EventObject evt = null) {
diff --git 
a/subprojects/groovy-console/src/main/groovy/groovy/ui/ConsolePreferences.groovy
 
b/subprojects/groovy-console/src/main/groovy/groovy/ui/ConsolePreferences.groovy
index 6e804b8..63be4a1 100644
--- 
a/subprojects/groovy-console/src/main/groovy/groovy/ui/ConsolePreferences.groovy
+++ 
b/subprojects/groovy-console/src/main/groovy/groovy/ui/ConsolePreferences.groovy
@@ -98,6 +98,12 @@ class ConsolePreferences {
         }
 
         console.swing.txtMaxOutputChars.maximumSize = new 
Dimension(Integer.MAX_VALUE, (int) 
console.swing.txtMaxOutputChars.preferredSize.height)
+
+        def outputLogFileName = console.prefs.get('outputLogFileName', null)
+        if (outputLogFileName != null) {
+            console.swing.outputFileCheckBox.selected = true
+            console.swing.outputFileName.text = outputLogFileName
+        }
     }
 
     private boolean isInteger(value) {
@@ -129,6 +135,10 @@ class ConsolePreferences {
     private void onChooseFile(EventObject event) {
         JFileChooser fileChooser = console.swing.fileChooser()
 
+        if (console.prefs.get('outputLogFileName', null) != null) {
+            fileChooser.setSelectedFile(new 
File(console.prefs.get('outputLogFileName', null)))
+        }
+
         if (fileChooser.showOpenDialog(dialog) == JFileChooser.APPROVE_OPTION) 
{
             outputFile = fileChooser.selectedFile
         }

Reply via email to