/** @return Point in layered pane coordinate system */
  static Pair<Point, Short> chooseBestHintPosition(
      Project project,
      Editor editor,
      int line,
      int col,
      LightweightHint hint,
      boolean awtTooltip,
      short preferredPosition) {
    HintManagerImpl hintManager = HintManagerImpl.getInstanceImpl();
    Dimension hintSize = hint.getComponent().getPreferredSize();
    JComponent editorComponent = editor.getComponent();
    JLayeredPane layeredPane = editorComponent.getRootPane().getLayeredPane();

    Point p1;
    Point p2;
    boolean isLookupShown = LookupManager.getInstance(project).getActiveLookup() != null;
    if (isLookupShown) {
      p1 = hintManager.getHintPosition(hint, editor, HintManager.UNDER);
      p2 = hintManager.getHintPosition(hint, editor, HintManager.ABOVE);
    } else {
      LogicalPosition pos = new LogicalPosition(line, col);
      p1 = HintManagerImpl.getHintPosition(hint, editor, pos, HintManager.UNDER);
      p2 = HintManagerImpl.getHintPosition(hint, editor, pos, HintManager.ABOVE);
    }

    if (!awtTooltip) {
      p1.x = Math.min(p1.x, layeredPane.getWidth() - hintSize.width);
      p1.x = Math.max(p1.x, 0);
      p2.x = Math.min(p2.x, layeredPane.getWidth() - hintSize.width);
      p2.x = Math.max(p2.x, 0);
    }

    boolean p1Ok = p1.y + hintSize.height < layeredPane.getHeight();
    boolean p2Ok = p2.y >= 0;

    if (isLookupShown) {
      if (p1Ok) return new Pair<Point, Short>(p1, HintManager.UNDER);
      if (p2Ok) return new Pair<Point, Short>(p2, HintManager.ABOVE);
    } else {
      if (preferredPosition != HintManager.DEFAULT) {
        if (preferredPosition == HintManager.ABOVE) {
          if (p2Ok) return new Pair<Point, Short>(p2, HintManager.ABOVE);
        } else if (preferredPosition == HintManager.UNDER) {
          if (p1Ok) return new Pair<Point, Short>(p1, HintManager.UNDER);
        }
      }

      if (p1Ok) return new Pair<Point, Short>(p1, HintManager.UNDER);
      if (p2Ok) return new Pair<Point, Short>(p2, HintManager.ABOVE);
    }

    int underSpace = layeredPane.getHeight() - p1.y;
    int aboveSpace = p2.y;
    return aboveSpace > underSpace
        ? new Pair<Point, Short>(new Point(p2.x, 0), HintManager.UNDER)
        : new Pair<Point, Short>(p1, HintManager.ABOVE);
  }
  ParameterInfoComponent(
      Object[] objects,
      Editor editor,
      @NotNull ParameterInfoHandler handler,
      boolean requestFocus) {
    super(new BorderLayout());
    myRequestFocus = requestFocus;

    if (!ApplicationManager.getApplication().isUnitTestMode()) {
      JComponent editorComponent = editor.getComponent();
      JLayeredPane layeredPane = editorComponent.getRootPane().getLayeredPane();
      myWidthLimit = layeredPane.getWidth();
    }

    NORMAL_FONT = UIUtil.getLabelFont();
    BOLD_FONT = NORMAL_FONT.deriveFont(Font.BOLD);

    myObjects = objects;

    setBackground(BACKGROUND_COLOR);

    myHandler = handler;
    myPanels = new OneElementComponent[myObjects.length];
    final JPanel panel = new JPanel(new GridBagLayout());
    for (int i = 0; i < myObjects.length; i++) {
      myPanels[i] = new OneElementComponent();
      panel.add(
          myPanels[i],
          new GridBagConstraints(
              0,
              i,
              1,
              1,
              1,
              0,
              GridBagConstraints.WEST,
              GridBagConstraints.HORIZONTAL,
              new Insets(0, 0, 0, 0),
              0,
              0));
    }
    if (myRequestFocus) {
      AccessibleContextUtil.setName(
          this, "Parameter Info. Press TAB to navigate through each element. Press ESC to close.");
    }

    final JScrollPane pane = ScrollPaneFactory.createScrollPane(panel);
    pane.setBorder(null);
    pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    add(pane, BorderLayout.CENTER);

    myCurrentParameterIndex = -1;
  }
  @Nullable
  public static LightweightHint showEditorFragmentHint(
      Editor editor, TextRange range, boolean showFolding, boolean hideByAnyKey) {

    JComponent editorComponent = editor.getComponent();
    final JRootPane rootPane = editorComponent.getRootPane();
    if (rootPane == null) return null;
    JLayeredPane layeredPane = rootPane.getLayeredPane();
    int x = -2;
    int y = 0;
    Point point = SwingUtilities.convertPoint(editorComponent, x, y, layeredPane);

    return showEditorFragmentHintAt(
        editor, range, point.x, point.y, true, showFolding, hideByAnyKey);
  }
  private void applyTo(ArrayRenderer renderer, boolean showBigRangeWarning)
      throws ConfigurationException {
    int newStartIndex = getInt(myStartIndex);
    int newEndIndex = getInt(myEndIndex);
    int newLimit = getInt(myEntriesLimit);

    if (newStartIndex < 0) {
      throw new ConfigurationException(
          DebuggerBundle.message("error.array.renderer.configurable.start.index.less.than.zero"));
    }

    if (newEndIndex < newStartIndex) {
      throw new ConfigurationException(
          DebuggerBundle.message("error.array.renderer.configurable.end.index.less.than.start"));
    }

    if (newStartIndex >= 0 && newEndIndex >= 0) {
      if (newStartIndex > newEndIndex) {
        int currentStartIndex = renderer.START_INDEX;
        int currentEndIndex = renderer.END_INDEX;
        newEndIndex = newStartIndex + (currentEndIndex - currentStartIndex);
      }

      if (newLimit <= 0) {
        newLimit = 1;
      }

      if (showBigRangeWarning && (newEndIndex - newStartIndex > 10000)) {
        final int answer =
            Messages.showOkCancelDialog(
                myPanel.getRootPane(),
                DebuggerBundle.message(
                    "warning.range.too.big", ApplicationNamesInfo.getInstance().getProductName()),
                DebuggerBundle.message("title.range.too.big"),
                Messages.getWarningIcon());
        if (answer != Messages.OK) {
          return;
        }
      }
    }

    renderer.START_INDEX = newStartIndex;
    renderer.END_INDEX = newEndIndex;
    renderer.ENTRIES_LIMIT = newLimit;
  }
  public void assertIsDispatchThread(@Nullable final JComponent component) {
    if (component == null) return;

    Thread curThread = Thread.currentThread();
    if (ourDispatchThread == curThread) {
      return;
    }

    if (Boolean.TRUE.equals(component.getClientProperty(WAS_EVER_SHOWN))) {
      assertIsDispatchThread();
    } else {
      final JRootPane root = component.getRootPane();
      if (root != null) {
        component.putClientProperty(WAS_EVER_SHOWN, Boolean.TRUE);
        assertIsDispatchThread();
      }
    }
  }
  /**
   * Obtains the deepest component below the glass pane at the specified point.
   *
   * @param pt The point.
   * @return The deepest component, or <code>null</code> if there is no component at the specified
   *     point.
   */
  protected Component getDeepestComponent(Point pt) {
    // Get hold of the content pane, since this contains the components
    // that we want to relay mouse events to
    SwingUtilities.getRoot(glassPane);

    Container contentPane = glassPane.getRootPane().getContentPane();

    // Convert the mouse point from the glass pane coordinate system
    // into the coordinate system of the content pane.
    Point contentPanePt = SwingUtilities.convertPoint(glassPane, pt, contentPane);

    // Find the deepest component - i.e. the one that should get the mouse
    // event.
    Component deepestComponent =
        SwingUtilities.getDeepestComponentAt(contentPane, contentPanePt.x, contentPanePt.y);

    return deepestComponent;
  }
  public void cancel(InputEvent e) {
    if (isDisposed()) return;

    if (myPopup != null) {
      if (!canClose()) {
        return;
      }
      storeDimensionSize(myContent.getSize());
      if (myUseDimServiceForXYLocation) {
        final JRootPane root = myComponent.getRootPane();
        if (root != null) {
          final Container popupWindow = root.getParent();
          if (popupWindow != null && popupWindow.isShowing()) {
            storeLocation(popupWindow.getLocationOnScreen());
          }
        }
      }

      if (e instanceof MouseEvent) {
        IdeEventQueue.getInstance().blockNextEvents(((MouseEvent) e));
      }

      myPopup.hide(false);

      if (ApplicationManagerEx.getApplicationEx() != null) {
        StackingPopupDispatcher.getInstance().onPopupHidden(this);
      }

      if (myInStack) {
        myFocusTrackback.setForcedRestore(!myOk && myFocusable);
        myFocusTrackback.restoreFocus();
      }

      disposePopup();

      if (myListeners != null) {
        for (JBPopupListener each : myListeners) {
          each.onClosed(new LightweightWindowEvent(this, myOk));
        }
      }
    }

    Disposer.dispose(this, false);
  }
  public void hide(boolean ok) {
    if (isVisible()) {
      if (myIsRealPopup) {
        if (ok) {
          myPopup.closeOk(null);
        } else {
          myPopup.cancel();
        }
        myPopup = null;
      } else {
        if (myCurrentIdeTooltip != null) {
          IdeTooltip tooltip = myCurrentIdeTooltip;
          myCurrentIdeTooltip = null;
          tooltip.hide();
        } else {
          final JRootPane rootPane = myComponent.getRootPane();
          if (rootPane != null) {
            final Rectangle bounds = myComponent.getBounds();
            final JLayeredPane layeredPane = rootPane.getLayeredPane();

            try {
              if (myFocusBackComponent != null) {
                LayoutFocusTraversalPolicyExt.setOverridenDefaultComponent(myFocusBackComponent);
              }
              layeredPane.remove(myComponent);
            } finally {
              LayoutFocusTraversalPolicyExt.setOverridenDefaultComponent(null);
            }

            layeredPane.paintImmediately(bounds.x, bounds.y, bounds.width, bounds.height);
          }
        }
      }
    }
    if (myEscListener != null) {
      myComponent.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0));
    }

    TooltipController.getInstance().hide(this);

    fireHintHidden();
  }
  /**
   * Shows the hint in the layered pane. Coordinates <code>x</code> and <code>y</code> are in <code>
   * parentComponent</code> coordinate system. Note that the component appears on 250 layer.
   */
  @Override
  public void show(
      @NotNull final JComponent parentComponent,
      final int x,
      final int y,
      final JComponent focusBackComponent,
      @NotNull final HintHint hintHint) {
    myParentComponent = parentComponent;
    myHintHint = hintHint;

    myFocusBackComponent = focusBackComponent;

    LOG.assertTrue(myParentComponent.isShowing());
    myEscListener = new MyEscListener();
    myComponent.registerKeyboardAction(
        myEscListener,
        KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
        JComponent.WHEN_IN_FOCUSED_WINDOW);
    myComponent.registerKeyboardAction(
        myEscListener, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_FOCUSED);
    final JLayeredPane layeredPane = parentComponent.getRootPane().getLayeredPane();

    myComponent.validate();

    if (!myForceShowAsPopup
        && (myForceLightweightPopup
            || fitsLayeredPane(
                layeredPane,
                myComponent,
                new RelativePoint(parentComponent, new Point(x, y)),
                hintHint))) {
      beforeShow();
      final Dimension preferredSize = myComponent.getPreferredSize();

      if (hintHint.isAwtTooltip()) {
        IdeTooltip tooltip =
            new IdeTooltip(
                hintHint.getOriginalComponent(),
                hintHint.getOriginalPoint(),
                myComponent,
                hintHint,
                myComponent) {
              @Override
              protected boolean canAutohideOn(TooltipEvent event) {
                if (event.getInputEvent() instanceof MouseEvent) {
                  return !(hintHint.isContentActive() && event.isIsEventInsideBalloon());
                } else if (event.getAction() != null) {
                  return false;
                } else {
                  return true;
                }
              }

              @Override
              protected void onHidden() {
                fireHintHidden();
                TooltipController.getInstance().resetCurrent();
              }

              @Override
              public boolean canBeDismissedOnTimeout() {
                return false;
              }
            }.setToCenterIfSmall(hintHint.isMayCenterTooltip())
                .setPreferredPosition(hintHint.getPreferredPosition())
                .setHighlighterType(hintHint.isHighlighterType())
                .setTextForeground(hintHint.getTextForeground())
                .setTextBackground(hintHint.getTextBackground())
                .setBorderColor(hintHint.getBorderColor())
                .setBorderInsets(hintHint.getBorderInsets())
                .setFont(hintHint.getTextFont())
                .setCalloutShift(hintHint.getCalloutShift())
                .setPositionChangeShift(
                    hintHint.getPositionChangeX(), hintHint.getPositionChangeY())
                .setExplicitClose(hintHint.isExplicitClose())
                .setHint(true);
        myComponent.validate();
        myCurrentIdeTooltip =
            IdeTooltipManager.getInstance()
                .show(tooltip, hintHint.isShowImmediately(), hintHint.isAnimationEnabled());
      } else {
        final Point layeredPanePoint =
            SwingUtilities.convertPoint(parentComponent, x, y, layeredPane);
        myComponent.setBounds(
            layeredPanePoint.x, layeredPanePoint.y, preferredSize.width, preferredSize.height);
        layeredPane.add(myComponent, JLayeredPane.POPUP_LAYER);

        myComponent.validate();
        myComponent.repaint();
      }
    } else {
      myIsRealPopup = true;
      Point actualPoint = new Point(x, y);
      JComponent actualComponent = new OpaquePanel(new BorderLayout());
      actualComponent.add(myComponent, BorderLayout.CENTER);
      if (isAwtTooltip()) {
        fixActualPoint(actualPoint);

        int inset = BalloonImpl.getNormalInset();
        actualComponent.setBorder(new LineBorder(hintHint.getTextBackground(), inset));
        actualComponent.setBackground(hintHint.getTextBackground());
        actualComponent.validate();
      }

      myPopup =
          JBPopupFactory.getInstance()
              .createComponentPopupBuilder(actualComponent, myFocusRequestor)
              .setRequestFocus(myFocusRequestor != null)
              .setFocusable(myFocusRequestor != null)
              .setResizable(myResizable)
              .setMovable(myTitle != null)
              .setTitle(myTitle)
              .setModalContext(false)
              .setShowShadow(isRealPopup() && !isForceHideShadow())
              .setCancelKeyEnabled(false)
              .setCancelOnClickOutside(myCancelOnClickOutside)
              .setCancelCallback(
                  new Computable<Boolean>() {
                    @Override
                    public Boolean compute() {
                      onPopupCancel();
                      return true;
                    }
                  })
              .setCancelOnOtherWindowOpen(myCancelOnOtherWindowOpen)
              .createPopup();

      beforeShow();
      myPopup.show(new RelativePoint(myParentComponent, new Point(actualPoint.x, actualPoint.y)));
    }
  }
Example #10
0
  /**
   * Causes the popup to be displayed at the specified location.
   *
   * @param component The component whose cooredinate system is to be used.
   * @param x The horizontal location of the popup
   * @param y The vertical location of the popup.
   */
  public void showPopup(JComponent component, int x, int y) {
    // Get the root (JFrame probably)
    Component rootComp = SwingUtilities.getRoot(component);

    // Determine where to place the popup
    // (Left or right, top or bottom of the mouse cursor
    // We want to fit the popup within the top level frame

    int componentMaxX = (int) rootComp.getSize().width;

    int componentMaxY = (int) rootComp.getSize().height;

    int xPosOnRoot = SwingUtilities.convertPoint(component, x, y, rootComp).x;

    int yPosOnRoot = SwingUtilities.convertPoint(component, x, y, rootComp).y;

    // Display to the right of the mouse cursor, unless
    // there is not enough room
    int deltaX = xPosOnRoot + content.getWidth() - componentMaxX;

    if (deltaX > 0) {
      xPosOnRoot -= deltaX;

      if (xPosOnRoot < 0) {
        xPosOnRoot = 0;
      }
    }

    int deltaY = yPosOnRoot + content.getHeight() - componentMaxY;

    if (deltaY > 0) {
      yPosOnRoot -= deltaY;

      if (yPosOnRoot < 0) {
        yPosOnRoot = 0;
      }
    }

    // Convert root pos back to component pos

    int xPos = SwingUtilities.convertPoint(rootComp, xPosOnRoot, yPosOnRoot, component).x;

    int yPos = SwingUtilities.convertPoint(rootComp, xPosOnRoot, yPosOnRoot, component).y;

    JRootPane rootPane = component.getRootPane();

    rootPane.setGlassPane(glassPane);

    // Convert the mouse point from the invoking component coordinate
    // system to the glassPane coordinate system
    Point pt = SwingUtilities.convertPoint(component, xPos, yPos, glassPane);

    // Set the location of the popup in the glass pane
    content.setLocation(pt);

    // Show the glass pane.
    glassPane.setVisible(true);

    // If the popup is set to hide automatically after a specified
    // amount of time, then reset the timer.
    if (HIDE_ON_TIMER == true) {
      timer.stop();

      timer.start();
    }
  }