merging of editor tooltip changes
authorKirill Kalishev <kirill.kalishev@jetbrains.com>
Sun, 12 Sep 2010 09:26:27 +0000 (13:26 +0400)
committerKirill Kalishev <kirill.kalishev@jetbrains.com>
Sun, 12 Sep 2010 09:26:27 +0000 (13:26 +0400)
1  2 
platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonTooltipRendererProvider.java
platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/TrafficLightRenderer.java
platform/platform-impl/src/com/intellij/codeInsight/hint/LineTooltipRenderer.java
platform/platform-impl/src/com/intellij/codeInsight/hint/TooltipController.java
platform/platform-impl/src/com/intellij/openapi/editor/impl/EditorMarkupModelImpl.java

index dc7ad09ead153079360a2a56e3beba1d3391e142,a5283a871c959b360cd664f8ea827b6a32805f57..0f5f233cba571f5f1f6bb49397952d6e20bef0cd
@@@ -23,9 -23,9 +23,10 @@@ import com.intellij.openapi.actionSyste
  import com.intellij.openapi.editor.Editor;
  import com.intellij.openapi.extensions.Extensions;
  import com.intellij.openapi.keymap.KeymapManager;
+ import com.intellij.openapi.util.Comparing;
  import com.intellij.openapi.util.Ref;
  import com.intellij.openapi.util.text.StringUtil;
 +import com.intellij.ui.HintHint;
  import com.intellij.ui.LightweightHint;
  import com.intellij.ui.ScrollPaneFactory;
  import com.intellij.util.ui.UIUtil;
@@@ -57,7 -57,7 +58,11 @@@ public class LineTooltipRenderer implem
      myCurrentWidth = width;
    }
  
-   public LightweightHint show(final Editor editor, final Point p, final boolean alignToRight, final TooltipGroup group, final HintHint hintHint) {
 -  public LightweightHint show(final Editor editor, final Point p, final boolean alignToRight, final TooltipGroup group) {
++  public LightweightHint show(final Editor editor,
++                              final Point p,
++                              final boolean alignToRight,
++                              final TooltipGroup group,
++                              final HintHint hintHint) {
      if (myText == null) return null;
  
      //setup text
      final HintManagerImpl hintManager = HintManagerImpl.getInstanceImpl();
      final JComponent contentComponent = editor.getContentComponent();
  
 -    final JEditorPane pane = initPane(myText);
 -    correctLocation(editor, pane, p, alignToRight, expanded, myCurrentWidth);
 +    final JComponent editorComponent = editor.getComponent();
 +    final JLayeredPane layeredPane = editorComponent.getRootPane().getLayeredPane();
 +
-     //pane
 +    final JEditorPane pane = initPane(myText, hintHint, layeredPane);
-     pane.setCaretPosition(0);
-     int widthLimit = layeredPane.getWidth() - 10;
-     int heightLimit = layeredPane.getHeight() - 5;
-     int width = expanded ? 3 * myCurrentWidth / 2 : pane.getPreferredSize().width;
-     int height = expanded ? Math.max(pane.getPreferredSize().height, 150) : pane.getPreferredSize().height;
-     if (alignToRight) {
-       p.x = Math.max(0, p.x - width);
-     }
-     // try to make cursor outside tooltip. SCR 15038
-     p.x += 3;
-     p.y += 3;
-     if (p.x >= widthLimit - width) {
-       p.x = widthLimit - width;
-       width = Math.min(width, widthLimit);
-       height += 20;
-     }
-     if (p.x < 3) {
-       p.x = 3;
-     }
-     if (p.y > heightLimit - height) {
-       p.y = heightLimit - height;
-       height = Math.min(heightLimit, height);
-     }
-     if (p.y < 3) {
-       p.y = 3;
-     }
 +    if (!hintHint.isAwtTooltip()) {
-       locateOutsideMouseCursor(editor, layeredPane, p, width, height, heightLimit);
-       // in order to restrict tooltip size
-       pane.setSize(width, height);
-       pane.setMaximumSize(new Dimension(width, height));
-       pane.setMinimumSize(new Dimension(width, height));
-       pane.setPreferredSize(new Dimension(width, height));
++      correctLocation(editor, pane, p, alignToRight, expanded, myCurrentWidth);
 +    }
  
      final JScrollPane scrollPane = ScrollPaneFactory.createScrollPane(pane);
      scrollPane.setBorder(null);
  
-     scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
-     scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
+     scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
+     scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
  
 +    scrollPane.setOpaque(hintHint.isOpaqueAllowed());
 +    scrollPane.getViewport().setOpaque(hintHint.isOpaqueAllowed());
 +
 +    scrollPane.setBackground(hintHint.getTextBackground());
 +    scrollPane.getViewport().setBackground(hintHint.getTextBackground());
 +
 +    scrollPane.setViewportBorder(null);
 +
      final Ref<AnAction> anAction = new Ref<AnAction>();
      final LightweightHint hint = new LightweightHint(scrollPane) {
        public void hide() {
          }
        }
      };
--    anAction.set(new AnAction() { //action to expand description when tooltip was shown after mouse move; need to unregister from editor component
--      {
--        registerCustomShortcutSet(new CustomShortcutSet(KeymapManager.getInstance().getActiveKeymap().getShortcuts(IdeActions.ACTION_SHOW_ERROR_DESCRIPTION)), contentComponent);
--      }
--      public void actionPerformed(final AnActionEvent e) {
--        hint.hide();
--        if (myCurrentWidth > 0) {
--          stripDescription();
++    anAction
++      .set(new AnAction() { //action to expand description when tooltip was shown after mouse move; need to unregister from editor component
++
++        {
++          registerCustomShortcutSet(
++            new CustomShortcutSet(KeymapManager.getInstance().getActiveKeymap().getShortcuts(IdeActions.ACTION_SHOW_ERROR_DESCRIPTION)),
++            contentComponent);
          }
-         createRenderer(myText, myCurrentWidth > 0 ? 0 : pane.getWidth()).show(editor, new Point(p.x -3, p.y -3), false, group, hintHint);
 -        createRenderer(myText, myCurrentWidth > 0 ? 0 : pane.getWidth()).show(editor, new Point(p.x -3, p.y -3), false, group);
--      }
--    });
++
++        public void actionPerformed(final AnActionEvent e) {
++          hint.hide();
++          if (myCurrentWidth > 0) {
++            stripDescription();
++          }
++          createRenderer(myText, myCurrentWidth > 0 ? 0 : pane.getWidth())
++            .show(editor, new Point(p.x - 3, p.y - 3), false, group, hintHint);
++        }
++      });
  
      pane.addHyperlinkListener(new HyperlinkListener() {
        public void hyperlinkUpdate(final HyperlinkEvent e) {
              if (e.getURL() != null) {
                BrowserUtil.launchBrowser(e.getURL().toString());
              }
--          } else { //less -> more
++          }
++          else { //less -> more
              if (e.getURL() != null) {
                BrowserUtil.launchBrowser(e.getURL().toString());
                return;
        }
      });
  
--    hintManager.showEditorHint(hint, editor, p,
-                                HintManagerImpl.HIDE_BY_ANY_KEY | HintManagerImpl.HIDE_BY_TEXT_CHANGE | HintManagerImpl.HIDE_BY_OTHER_HINT |
-                                HintManagerImpl.HIDE_BY_SCROLLING, 0, false, hintHint);
 -                               HintManager.HIDE_BY_ANY_KEY | HintManager.HIDE_BY_TEXT_CHANGE | HintManager.HIDE_BY_OTHER_HINT |
 -                               HintManager.HIDE_BY_SCROLLING, 0, false);
++    hintManager.showEditorHint(hint, editor, p, HintManager.HIDE_BY_ANY_KEY |
++                                                HintManager.HIDE_BY_TEXT_CHANGE |
++                                                HintManager.HIDE_BY_OTHER_HINT |
++                                                HintManager.HIDE_BY_SCROLLING, 0, false, hintHint);
      return hint;
    }
  
-   private static void locateOutsideMouseCursor(Editor editor,
-                                                JComponent editorComponent,
-                                                Point p,
-                                                int width,
-                                                int height,
-                                                int heightLimit) {
+   public static void correctLocation(Editor editor,
+                                      JComponent tooltipComponent,
+                                      Point p,
+                                      boolean alignToRight,
+                                      boolean expanded,
+                                      int currentWidth) {
+     final JComponent editorComponent = editor.getComponent();
+     final JLayeredPane layeredPane = editorComponent.getRootPane().getLayeredPane();
+     int widthLimit = layeredPane.getWidth() - 10;
+     int heightLimit = layeredPane.getHeight() - 5;
 -    Dimension dimension = correctLocation(editor, p, alignToRight, expanded, tooltipComponent, layeredPane, widthLimit, heightLimit, currentWidth);
++    Dimension dimension =
++      correctLocation(editor, p, alignToRight, expanded, tooltipComponent, layeredPane, widthLimit, heightLimit, currentWidth);
+     // in order to restrict tooltip size
+     tooltipComponent.setSize(dimension);
+     tooltipComponent.setMaximumSize(dimension);
+     tooltipComponent.setMinimumSize(dimension);
+     tooltipComponent.setPreferredSize(dimension);
+   }
+   private static Dimension correctLocation(Editor editor,
+                                            Point p,
+                                            boolean alignToRight,
+                                            boolean expanded,
+                                            JComponent tooltipComponent,
+                                            JLayeredPane layeredPane,
+                                            int widthLimit,
+                                            int heightLimit,
+                                            int currentWidth) {
+     Dimension preferredSize = tooltipComponent.getPreferredSize();
+     int width = expanded ? 3 * currentWidth / 2 : preferredSize.width;
+     int height = expanded ? Math.max(preferredSize.height, 150) : preferredSize.height;
+     Dimension dimension = new Dimension(width, height);
+     if (alignToRight) {
+       p.x = Math.max(0, p.x - width);
+     }
+     // try to make cursor outside tooltip. SCR 15038
+     p.x += 3;
+     p.y += 3;
+     if (p.x >= widthLimit - width) {
+       p.x = widthLimit - width;
+       width = Math.min(width, widthLimit);
+       height += 20;
+       dimension = new Dimension(width, height);
+     }
+     if (p.x < 3) {
+       p.x = 3;
+     }
+     if (p.y > heightLimit - height) {
+       p.y = heightLimit - height;
+       height = Math.min(heightLimit, height);
+       dimension = new Dimension(width, height);
+     }
+     if (p.y < 3) {
+       p.y = 3;
+     }
+     locateOutsideMouseCursor(editor, layeredPane, p, width, height, heightLimit);
+     return dimension;
+   }
 -  private static void locateOutsideMouseCursor(Editor editor,
 -                                               JComponent editorComponent,
 -                                               Point p,
 -                                               int width,
 -                                               int height,
 -                                               int heightLimit) {
++  private static void locateOutsideMouseCursor(Editor editor, JComponent editorComponent, Point p, int width, int height, int heightLimit) {
      Point mouse = MouseInfo.getPointerInfo().getLocation();
      SwingUtilities.convertPointFromScreen(mouse, editorComponent);
      Rectangle tooltipRect = new Rectangle(p, new Dimension(width, height));
      return new LineTooltipRenderer(text, width);
    }
  
--  protected boolean dressDescription(Editor editor) { return false; }
--  protected void stripDescription() {}
++  protected boolean dressDescription(Editor editor) {
++    return false;
++  }
 -  static JEditorPane initPane(@NonNls String text) {
 -    text = "<html><head>" + UIUtil.getCssFontDeclaration(UIUtil.getLabelFont()) + "</head><body>" + getHtmlBody(text) + "</body></html>";
 -    final JEditorPane pane = new JEditorPane(UIUtil.HTML_MIME, text);
 -    pane.setEditable(false);
 -    setColors(pane);
 -    setBorder(pane);
++  protected void stripDescription() {
++  }
 +
 +  static JEditorPane initPane(@NonNls String text, HintHint hintHint, JLayeredPane layeredPane) {
 +    final Ref<Dimension> prefSize = new Ref<Dimension>(null);
-     text = "<html><head>" + UIUtil.getCssFontDeclaration(hintHint.getTextFont(), hintHint.getTextForeground()) + "</head><body>" + getHtmlBody(text) + "</body></html>";
++    text = "<html><head>" +
++           UIUtil.getCssFontDeclaration(hintHint.getTextFont(), hintHint.getTextForeground()) +
++           "</head><body>" +
++           getHtmlBody(text) +
++           "</body></html>";
 +    final JEditorPane pane = new JEditorPane(UIUtil.HTML_MIME, text) {
 +      @Override
 +      public Dimension getPreferredSize() {
 +        return prefSize.get() != null ? prefSize.get() : super.getPreferredSize();
 +      }
 +    };
+     pane.setCaretPosition(0);
 +    pane.setEditable(false);
 +
 +    if (hintHint.isOwnBorderAllowed()) {
-       pane.setBorder(
-         BorderFactory.createCompoundBorder(
-           BorderFactory.createLineBorder(hintHint.getBorderColor()),
-           BorderFactory.createEmptyBorder(0, 5, 0, 5)
-         )
-       );
-     } else {
++      setBorder(pane);
++      setColors(pane);
++    }
++    else {
 +      pane.setBorder(null);
 +    }
 +
 +    if (hintHint.isAwtTooltip()) {
 +      Dimension size = layeredPane.getSize();
 +      int fitWidth = (int)(size.width * 0.8);
 +      Dimension prefSizeOriginal = pane.getPreferredSize();
 +      if (prefSizeOriginal.width > fitWidth) {
 +        pane.setSize(new Dimension(fitWidth, Integer.MAX_VALUE));
 +        Dimension fixedWidthSize = pane.getPreferredSize();
 +        prefSize.set(new Dimension(fitWidth, fixedWidthSize.height));
-       } else {
++      }
++      else {
 +        prefSize.set(prefSizeOriginal);
 +      }
 +    }
 +
 +    pane.setOpaque(hintHint.isOpaqueAllowed());
 +    pane.setBackground(hintHint.getTextBackground());
 +
      return pane;
    }
  
 -      BorderFactory.createCompoundBorder(
 -        BorderFactory.createLineBorder(Color.black),
 -        BorderFactory.createEmptyBorder(0, 5, 0, 5)
 -      )
 -    );
+   public static void setColors(JComponent pane) {
+     pane.setForeground(Color.black);
+     pane.setBackground(HintUtil.INFORMATION_COLOR);
+     pane.setOpaque(true);
+   }
+   public static void setBorder(JComponent pane) {
+     pane.setBorder(
++      BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.black), BorderFactory.createEmptyBorder(0, 5, 0, 5)));
+   }
    public void addBelow(String text) {
      @NonNls String newBody;
--    if (myText ==null) {
++    if (myText == null) {
        newBody = getHtmlBody(text);
      }
      else {
  
    protected static String getHtmlBody(@NonNls String text) {
      if (!text.startsWith("<html>")) {
--      return text.replaceAll("\n","<br>");
++      return text.replaceAll("\n", "<br>");
      }
      final int bodyIdx = text.indexOf("<body>");
      final int closedBodyIdx = text.indexOf("</body>");
index a3ecb8bf1d3ca6d0095375a7a86f50cd7046eac2,cbbc9f074261d19ce348df75f795498a75bb1e55..532ec68d806bd56bf9947bea442204c119acdcdf
@@@ -39,10 -39,9 +39,11 @@@ import com.intellij.openapi.editor.acti
  import com.intellij.openapi.editor.ex.*;
  import com.intellij.openapi.editor.markup.ErrorStripeRenderer;
  import com.intellij.openapi.editor.markup.RangeHighlighter;
 +import com.intellij.openapi.ui.popup.Balloon;
  import com.intellij.openapi.util.IconLoader;
  import com.intellij.ui.ColorUtil;
 +import com.intellij.ui.HintHint;
+ import com.intellij.ui.LightweightHint;
  import com.intellij.ui.PopupHandler;
  import com.intellij.util.Processor;
  import com.intellij.util.ui.ButtonlessScrollBarUI;
@@@ -575,6 -584,27 +586,27 @@@ public class EditorMarkupModelImpl exte
        }
      }
  
 -      showTooltip(e, myTrafficTooltipRenderer);
+     private TrafficTooltipRenderer myTrafficTooltipRenderer;
+     private void showTrafficLightTooltip(MouseEvent e) {
+       //final String tooltipMessage = myErrorStripeRenderer.getTooltipMessage();
+       //if (tooltipMessage == null) return;
+       if (myTrafficTooltipRenderer == null) {
+         myTrafficTooltipRenderer = myTooltipRendererProvider.createTrafficTooltipRenderer(new Runnable() {
+           @Override
+           public void run() {
+             myTrafficTooltipRenderer = null;
+           }
+         });
+       }
++      showTooltip(e, myTrafficTooltipRenderer, new HintHint(e).setAwtTooltip(true).setPreferredPosition(Balloon.Position.atLeft));
+     }
+     private void repaintTrafficTooltip() {
+       if (myTrafficTooltipRenderer != null) {
+         myTrafficTooltipRenderer.repaintTooltipWindow();
+       }
+     }
      private void cancelMyToolTips(final MouseEvent e) {
        final TooltipController tooltipController = TooltipController.getInstance();
        if (!tooltipController.shouldSurvive(e)) {
      public TooltipRenderer calcTooltipRenderer(@NotNull final String text, final int width) {
        return new LineTooltipRenderer(text, width);
      }
 -        public LightweightHint show(Editor editor, Point p, boolean alignToRight, TooltipGroup group) {
+     @Override
+     public TrafficTooltipRenderer createTrafficTooltipRenderer(final Runnable onHide) {
+       return new TrafficTooltipRenderer() {
+         @Override
+         public void repaintTooltipWindow() {
+         }
+         @Override
++        public LightweightHint show(Editor editor, Point p, boolean alignToRight, TooltipGroup group, HintHint hintHint) {
+           JLabel label = new JLabel("WTF");
+           return new LightweightHint(label){
+             @Override
+             public void hide() {
+               super.hide();
+               onHide.run();
+             }
+           };
+         }
+       };
+     }
    }
  
    private int offsetToYPosition(int offset) {