neilcsmith-net commented on code in PR #9812:
URL: https://github.com/apache/netbeans/pull/9812#discussion_r4630583538


##########
platform/core.output2/src/org/netbeans/core/output2/ui/AbstractOutputPane.java:
##########
@@ -1025,4 +1037,237 @@ public void setSelectionVisible(boolean vis) {
             }
         }
     }
+    
+    
+    private static class HoverSupport {
+        private final JEditorPane pane;
+
+        private Object hightlightHandle;
+        private int[] highlightRange;
+        private Point lastMouse;            // so key presses can re-evaluate 
without mouse movement
+
+        public HoverSupport(JEditorPane pane) {
+            this.pane = pane;
+
+            MouseAdapter mouse = new MouseAdapter() {
+                @Override
+                public void mouseMoved(MouseEvent e) {
+                    lastMouse = e.getPoint();
+                    update(isCtrlDown(e));
+                }
+
+                @Override
+                public void mouseDragged(MouseEvent e) {
+                    clear();
+                }
+
+                @Override
+                public void mouseExited(MouseEvent e) {
+                    lastMouse = null;
+                    clear();
+                }
+
+                @Override
+                public void mouseClicked(MouseEvent e) {
+                    if (isCtrlDown(e) && highlightRange != null) {
+                        onActivate(highlightRange[0], highlightRange[1]);
+                        e.consume();
+                    }
+                }
+            };
+            pane.addMouseListener(mouse);
+            pane.addMouseMotionListener(mouse);
+
+            pane.addKeyListener(new KeyAdapter() {
+                @Override
+                public void keyPressed(KeyEvent e) {
+                    update(isCtrlDown(e));
+                }
+
+                @Override
+                public void keyReleased(KeyEvent e) {
+                    update(isCtrlDown(e));
+                }
+            });
+        }
+
+        private static boolean isCtrlDown(InputEvent e) {
+            // Cmd on Mac OS X, Ctrl elsewhere
+            int MOD = Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx();
+            return (e.getModifiersEx() & MOD) != 0;
+        }
+
+        private void update(boolean modifierDown) {
+            if (!modifierDown || lastMouse == null) {
+                clear();
+                return;
+            }
+
+            int offset = offsetAt(lastMouse);
+            if (offset < 0) {
+                clear();
+                return;
+            }
+
+            try {
+                int[] startEnd = findPathBoundaries(pane, offset);
+                String path = startEnd == null ? "" : 
pane.getDocument().getText(startEnd[0], startEnd[1] - startEnd[0]);
+                if (path.isBlank()) {
+                    clear();
+                    return;
+                }
+                if (highlightRange != null && startEnd[0] == highlightRange[0] 
&& startEnd[1] == highlightRange[1]) {
+                    return;   // unchanged, avoid repaint churn
+                }
+                clear();
+                hightlightHandle = 
pane.getHighlighter().addHighlight(startEnd[0], startEnd[1], UNDERLINE);
+                highlightRange = startEnd;
+                pane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
+            } catch (BadLocationException ex) {
+                clear();
+            }
+        }
+
+        /**
+         * Document offset under the point, or -1 if the pointer isn't actually
+         * over a character.
+         */
+        private int offsetAt(Point p) {
+            Position.Bias[] bias = new Position.Bias[1];
+            int offset = pane.getUI().viewToModel2D(pane, p, bias);
+            if (offset < 0) {
+                return -1;
+            }
+            // viewToModel returns the *nearest* offset, even in empty space 
past line end — verify
+            if (bias[0] == Position.Bias.Backward && offset > 0) {
+                offset--;
+            }
+            try {
+                int len = pane.getDocument().getLength();
+                if (offset >= len) {
+                    return -1;
+                }
+                Rectangle2D a = pane.modelToView2D(offset);
+                Rectangle2D b = pane.modelToView2D(Math.min(offset + 1, len));
+                double right = (b.getY() == a.getY()) ? b.getX() : a.getX() + 
8; // char box
+                return (p.x >= a.getX() && p.x <= right
+                        && p.y >= a.getY() && p.y <= a.getY() + a.getHeight()) 
? offset : -1;
+            } catch (BadLocationException e) {
+                return -1;
+            }
+        }
+
+        private void clear() {
+            if (hightlightHandle != null) {
+                pane.getHighlighter().removeHighlight(hightlightHandle);
+                hightlightHandle = null;
+            }
+            highlightRange = null;
+            pane.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
+        }
+
+        private void onActivate(int start, int end) {
+            try {
+                String path = pane.getText(start, end - start);
+                File file = new File(path);
+                if (file.exists()) {
+                    if (FileUtil.toFileObject(file) instanceof FileObject 
toOpen) {
+                        if (toOpen.getLookup().lookup(Openable.class) 
instanceof Openable doIt) {
+                            doIt.open();
+                            return;
+                        }
+                    }
+                }
+            } catch (BadLocationException ex) {
+                ex.printStackTrace();
+            }
+            Toolkit.getDefaultToolkit().beep();
+        }
+
+        /**
+         * Painter that draws an underline instead of filling the background.
+         */
+        private static final Highlighter.HighlightPainter UNDERLINE
+                = new LayeredHighlighter.LayerPainter() {
+            @Override
+            public Shape paintLayer(Graphics g, int p0, int p1, Shape bounds,
+                    JTextComponent c, View view) {
+                try {
+                    Shape s = view.modelToView(p0, Position.Bias.Forward,
+                            p1, Position.Bias.Backward, bounds);
+                    Rectangle r = s.getBounds();
+                    g.setColor(new Color(0x2060D0));
+                    int y = r.y + r.height - 2;
+                    g.drawLine(r.x, y, r.x + r.width - 1, y);
+                    return r;
+                } catch (BadLocationException e) {
+                    return null;
+                }
+            }
+
+            @Override
+            public void paint(Graphics g, int p0, int p1, Shape bounds, 
JTextComponent c) {
+                // not used: DefaultHighlighter calls paintLayer for 
LayerPainters
+            }
+        };
+
+        private static int[] findPathBoundaries(JEditorPane pane, int offset) 
throws BadLocationException {
+            int start = Utilities.getWordStart(pane, offset);
+            int end = Utilities.getWordEnd(pane, offset);
+            
+            Set<String> candidates = new TreeSet<>((s1, s2) -> {
+                int lenDiff = s2.length() - s1.length();
+                if (lenDiff != 0) {
+                    return lenDiff;
+                } else {
+                    return s2.compareTo(s1);
+                }
+            });
+            while (start < end) {
+                candidates.add(pane.getText(start, end - start));
+                start--;
+                if (start < 0 || !isPathSeparator(pane.getText(start, 1))) {
+                    break;
+                } else {
+                    candidates.add(pane.getText(start, end - start));
+                    start = Utilities.getPreviousWord(pane, start);
+                }
+            }
+            
+            File existingPrefix = null;
+            for (String path : candidates) {
+                File f = new File(path).getAbsoluteFile();

Review Comment:
   That sounds better.  I also have a sense we'd be better using `Path` rather 
than `File` throughout this.  Too many odd little things cropping up with 
legacy `File` use as it is, particularly on Windows.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to