Procházet zdrojové kódy

Show tooltips in the textpane

Fixes issue 3931

Change-Id: Ic61c2cdcb8a1a24787ec0afb218b3561a585b6e1
Reviewed-on: http://gerrit.dmdirc.com/1054
Automatic-Compile: DMDirc Local Commits <dmdirc@googlemail.com>
Reviewed-by: Gregory Holmes <greg@dmdirc.com>
tags/0.6.4
Chris Smith před 14 roky
rodič
revize
e024e3e3ca

+ 50
- 17
src/com/dmdirc/addons/ui_swing/textpane/TextPaneCanvas.java Zobrazit soubor

@@ -57,6 +57,7 @@ import java.util.Map;
57 57
 import javax.imageio.ImageIO;
58 58
 import javax.swing.JPanel;
59 59
 import javax.swing.SwingUtilities;
60
+import javax.swing.ToolTipManager;
60 61
 import javax.swing.event.MouseInputListener;
61 62
 
62 63
 /** Canvas object to draw text. */
@@ -129,6 +130,7 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
129 130
         manager.addChangeListener("ui", "quickCopy", this);
130 131
 
131 132
         updateCachedSettings();
133
+        ToolTipManager.sharedInstance().registerComponent(this);
132 134
     }
133 135
 
134 136
     /**
@@ -676,30 +678,49 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
676 678
 
677 679
     /** Checks for a link under the cursor and sets appropriately. */
678 680
     private void checkForLink() {
679
-        final LineInfo lineInfo = getClickPosition(getMousePosition(), false);
681
+        final AttributedCharacterIterator iterator = getIterator(getMousePosition());
680 682
 
681
-        if (lineInfo.getLine() != -1 && document.getLine(lineInfo.getLine())
682
-                != null) {
683
-            final AttributedCharacterIterator iterator = document.getStyledLine(
684
-                    lineInfo.getLine());
685
-            if (lineInfo.getIndex() < iterator.getBeginIndex() || lineInfo.
686
-                    getIndex() > iterator.getEndIndex()) {
687
-                return;
688
-            }
689
-            iterator.setIndex(lineInfo.getIndex());
690
-            if (iterator.getAttributes().get(IRCTextAttribute.HYPERLINK) 
691
-                    != null || iterator.getAttributes().get(
692
-                    IRCTextAttribute.CHANNEL) != null || iterator.
693
-                    getAttributes().get(IRCTextAttribute.NICKNAME) != null) {
694
-                setCursor(HAND_CURSOR);
695
-                return;
696
-            }
683
+        if (iterator != null
684
+                && (iterator.getAttribute(IRCTextAttribute.HYPERLINK) != null
685
+                || iterator.getAttribute(IRCTextAttribute.CHANNEL) != null
686
+                || iterator.getAttribute(IRCTextAttribute.NICKNAME) != null)) {
687
+            setCursor(HAND_CURSOR);
688
+            return;
697 689
         }
690
+
698 691
         if (getCursor() == HAND_CURSOR) {
699 692
             setCursor(Cursor.getDefaultCursor());
700 693
         }
701 694
     }
702 695
 
696
+    /**
697
+     * Retrieves a character iterator for the text at the specified mouse
698
+     * position.
699
+     *
700
+     * @since 0.6.4
701
+     * @param mousePosition The mouse position to retrieve text for
702
+     * @return A corresponding character iterator, or null if the specified
703
+     * mouse position doesn't correspond to any text
704
+     */
705
+    private AttributedCharacterIterator getIterator(final Point mousePosition) {
706
+        final LineInfo lineInfo = getClickPosition(mousePosition, false);
707
+
708
+        if (lineInfo.getLine() != -1 && document.getLine(lineInfo.getLine()) != null) {
709
+            final AttributedCharacterIterator iterator
710
+                    = document.getStyledLine(lineInfo.getLine());
711
+
712
+            if (lineInfo.getIndex() < iterator.getBeginIndex()
713
+                    || lineInfo.getIndex() > iterator.getEndIndex()) {
714
+                return null;
715
+            }
716
+
717
+            iterator.setIndex(lineInfo.getIndex());
718
+            return iterator;
719
+        }
720
+
721
+        return null;
722
+    }
723
+
703 724
     /**
704 725
      * Sets the selection for the given event.
705 726
      *
@@ -1011,4 +1032,16 @@ class TextPaneCanvas extends JPanel implements MouseInputListener,
1011 1032
     public void configChanged(final String domain, final String key) {
1012 1033
         updateCachedSettings();
1013 1034
     }
1035
+
1036
+    /** {@inheritDoc} */
1037
+    @Override
1038
+    public String getToolTipText(final MouseEvent event) {
1039
+        final AttributedCharacterIterator iterator = getIterator(event.getPoint());
1040
+
1041
+        if (iterator != null && iterator.getAttribute(IRCTextAttribute.TOOLTIP) != null) {
1042
+            return iterator.getAttribute(IRCTextAttribute.TOOLTIP).toString();
1043
+        }
1044
+
1045
+        return super.getToolTipText(event);
1046
+    }
1014 1047
 }

Načítá se…
Zrušit
Uložit