Exemplo n.º 1
0
  public void jumpToError() {
    if (parseError != null && parseError.hasLineNumbers()) {
      try {
        int offset = computeDocumentOffset(parseError.getBeginLine(), parseError.getBeginColumn());

        // scroll to center as much as possible.
        editor.setCaretPosition(offset);
        Rectangle r1 = editor.modelToView(offset);
        int dy = (editor.getVisibleRect().height - r1.height) / 2;
        Rectangle r2 =
            new Rectangle(0, r1.y - dy, editor.getVisibleRect().width, r1.height + 2 * dy);
        editor.scrollRectToVisible(r2);
      } catch (BadLocationException e) {

      }
    }
  }
  public static JEditorPane initPane(
      @NonNls Html html, final HintHint hintHint, @Nullable final JLayeredPane layeredPane) {
    final Ref<Dimension> prefSize = new Ref<Dimension>(null);
    String htmlBody = UIUtil.getHtmlBody(html);
    @NonNls
    String text =
        "<html><head>"
            + UIUtil.getCssFontDeclaration(
                hintHint.getTextFont(),
                hintHint.getTextForeground(),
                hintHint.getLinkForeground(),
                hintHint.getUlImg())
            + "</head><body>"
            + htmlBody
            + "</body></html>";

    final boolean[] prefSizeWasComputed = {false};
    final JEditorPane pane =
        new JEditorPane() {
          @Override
          public Dimension getPreferredSize() {
            if (!prefSizeWasComputed[0] && hintHint.isAwtTooltip()) {
              JLayeredPane lp = layeredPane;
              if (lp == null) {
                JRootPane rootPane = UIUtil.getRootPane(this);
                if (rootPane != null) {
                  lp = rootPane.getLayeredPane();
                }
              }

              Dimension size;
              if (lp != null) {
                size = lp.getSize();
                prefSizeWasComputed[0] = true;
              } else {
                size = ScreenUtil.getScreenRectangle(0, 0).getSize();
              }
              int fitWidth = (int) (size.width * 0.8);
              Dimension prefSizeOriginal = super.getPreferredSize();
              if (prefSizeOriginal.width > fitWidth) {
                setSize(new Dimension(fitWidth, Integer.MAX_VALUE));
                Dimension fixedWidthSize = super.getPreferredSize();
                Dimension minSize = super.getMinimumSize();
                prefSize.set(
                    new Dimension(
                        fitWidth > minSize.width ? fitWidth : minSize.width,
                        fixedWidthSize.height));
              } else {
                prefSize.set(new Dimension(prefSizeOriginal));
              }
            }

            Dimension s =
                prefSize.get() != null ? new Dimension(prefSize.get()) : super.getPreferredSize();
            Border b = getBorder();
            if (b != null) {
              Insets insets = b.getBorderInsets(this);
              if (insets != null) {
                s.width += insets.left + insets.right;
                s.height += insets.top + insets.bottom;
              }
            }
            return s;
          }
        };

    final HTMLEditorKit.HTMLFactory factory =
        new HTMLEditorKit.HTMLFactory() {
          @Override
          public View create(Element elem) {
            AttributeSet attrs = elem.getAttributes();
            Object elementName = attrs.getAttribute(AbstractDocument.ElementNameAttribute);
            Object o =
                elementName != null ? null : attrs.getAttribute(StyleConstants.NameAttribute);
            if (o instanceof HTML.Tag) {
              HTML.Tag kind = (HTML.Tag) o;
              if (kind == HTML.Tag.HR) {
                return new CustomHrView(elem, hintHint.getTextForeground());
              }
            }
            return super.create(elem);
          }
        };

    HTMLEditorKit kit =
        new HTMLEditorKit() {
          @Override
          public ViewFactory getViewFactory() {
            return factory;
          }
        };
    pane.setEditorKit(kit);
    pane.setText(text);

    pane.setCaretPosition(0);
    pane.setEditable(false);

    if (hintHint.isOwnBorderAllowed()) {
      setBorder(pane);
      setColors(pane);
    } else {
      pane.setBorder(null);
    }

    if (!hintHint.isAwtTooltip()) {
      prefSizeWasComputed[0] = true;
    }

    final boolean opaque = hintHint.isOpaqueAllowed();
    pane.setOpaque(opaque);
    if (UIUtil.isUnderNimbusLookAndFeel() && !opaque) {
      pane.setBackground(UIUtil.TRANSPARENT_COLOR);
    } else {
      pane.setBackground(hintHint.getTextBackground());
    }

    return pane;
  }