You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2022/06/23 13:59:03 UTC

[groovy] 02/03: GROOVY-10661: reduce some duplication in ObjectBrowser

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 bfb89e0ec03c69620bc6dd76ea2de7274786df31
Author: Paul King <pa...@asert.com.au>
AuthorDate: Thu Jun 23 16:29:15 2022 +1000

    GROOVY-10661: reduce some duplication in ObjectBrowser
---
 .../groovy/groovy/console/ui/ObjectBrowser.groovy  | 83 +++++++---------------
 1 file changed, 26 insertions(+), 57 deletions(-)

diff --git a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ObjectBrowser.groovy b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ObjectBrowser.groovy
index fcd4fda663..40c2630adf 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ObjectBrowser.groovy
+++ b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ObjectBrowser.groovy
@@ -57,12 +57,8 @@ class ObjectBrowser {
         inspect('some String')
     }
 
-    public ObjectBrowser(String path = '') {
-        this.path = (path == null ? '' : path)
-    }
-
     static void inspect(objectUnderInspection, String path = '') {
-        def browser = new ObjectBrowser(path)
+        def browser = new ObjectBrowser(path: path ?: '')
         browser.inspector = new Inspector(objectUnderInspection)
         if (objectUnderInspection != null && browser.path == '') {
             browser.path = "${objectUnderInspection.getClass().name} instance"
@@ -108,19 +104,8 @@ class ObjectBrowser {
                                 maxWidth = 0
                                 width = 0
                             }
-                            arrayTable.addMouseListener(new MouseAdapter() {
-                                void mouseClicked(MouseEvent e) {
-                                    if (e.clickCount == 2) {
-                                        def selectedRow = arrayTable.selectedRow
-                                        if (selectedRow != -1) {
-                                            def value = arrayTable.model.getValueAt(selectedRow, 2)
-                                            if (value != null) {
-                                                closeFrameIfAltDoubleClick(e)
-                                                ObjectBrowser.inspect(value, path + "[${arrayTable.model.getValueAt(selectedRow, 0)}]")
-                                            }
-                                        }
-                                    }
-                                }
+                            arrayTable.addMouseListener(makeClickAdapter(arrayTable, 2) { row ->
+                                path + "[${arrayTable.model.getValueAt(row, 0)}]"
                             })
                         }
                     } else if (inspector.object instanceof Collection) {
@@ -137,19 +122,8 @@ class ObjectBrowser {
                                 maxWidth = 0
                                 width = 0
                             }
-                            collectionTable.addMouseListener(new MouseAdapter() {
-                                void mouseClicked(MouseEvent e) {
-                                    if (e.clickCount == 2) {
-                                        def selectedRow = collectionTable.selectedRow
-                                        if (selectedRow != -1) {
-                                            def value = collectionTable.model.getValueAt(selectedRow, 2)
-                                            if (value != null) {
-                                                closeFrameIfAltDoubleClick(e)
-                                                ObjectBrowser.inspect(value, path + "[${collectionTable.model.getValueAt(selectedRow, 0)}]")
-                                            }
-                                        }
-                                    }
-                                }
+                            collectionTable.addMouseListener(makeClickAdapter(collectionTable, 2) { row ->
+                                path + "[${collectionTable.model.getValueAt(row, 0)}]"
                             })
                         }
                     } else if (inspector.object instanceof Map) {
@@ -167,19 +141,8 @@ class ObjectBrowser {
                                 maxWidth = 0
                                 width = 0
                             }
-                            mapTable.addMouseListener(new MouseAdapter() {
-                                void mouseClicked(MouseEvent e) {
-                                    if (e.clickCount == 2) {
-                                        def selectedRow = mapTable.selectedRow
-                                        if (selectedRow != -1) {
-                                            def value = mapTable.model.getValueAt(selectedRow, 2)
-                                            if (value != null) {
-                                                closeFrameIfAltDoubleClick(e)
-                                                ObjectBrowser.inspect(value, path + "[${mapTable.model.getValueAt(selectedRow, 1)}]")
-                                            }
-                                        }
-                                    }
-                                }
+                            mapTable.addMouseListener(makeClickAdapter(mapTable, 2) { row ->
+                                path + "[${mapTable.model.getValueAt(row, 1)}]"
                             })
                         }
                     }
@@ -201,19 +164,8 @@ class ObjectBrowser {
                             maxWidth = 0
                             width = 0
                         }
-                        fieldTable.addMouseListener(new MouseAdapter() {
-                            void mouseClicked(MouseEvent e) {
-                                if (e.clickCount == 2) {
-                                    def selectedRow = fieldTable.selectedRow
-                                    if (selectedRow != -1) {
-                                        def value = fieldTable.model.getValueAt(selectedRow, MEMBER_RAW_VALUE_IDX)
-                                        if (value != null) {
-                                            closeFrameIfAltDoubleClick(e)
-                                            ObjectBrowser.inspect(value, path + (path.length() === 0 ? '' : '.') + "${fieldTable.model.getValueAt(selectedRow, 0)}")
-                                        }
-                                    }
-                                }
-                            }
+                        fieldTable.addMouseListener(makeClickAdapter(fieldTable, MEMBER_RAW_VALUE_IDX) { row ->
+                            path + (path.size() == 0 ? '' : '.') + "${fieldTable.model.getValueAt(row, 0)}"
                         })
                     }
                     scrollPane(name: ' (Meta) Methods ') {
@@ -252,6 +204,23 @@ class ObjectBrowser {
         frame.toFront()
     }
 
+    def makeClickAdapter(table, int valueCol, Closure pathClosure) {
+        new MouseAdapter() {
+            void mouseClicked(MouseEvent e) {
+                if (e.clickCount == 2) {
+                    def selectedRow = table.selectedRow
+                    if (selectedRow != -1) {
+                        def value = table.model.getValueAt(selectedRow, valueCol)
+                        if (value != null) {
+                            closeFrameIfAltDoubleClick(e)
+                            ObjectBrowser.inspect(value, pathClosure(selectedRow))
+                        }
+                    }
+                }
+            }
+        }
+    }
+
     void addSorter(table) {
         if (table != null) {
             def sorter = new TableSorter(table.model)