public static String layoutCompoundLabel(
      final JComponent c,
      final FontMetrics fm,
      final String text,
      final Icon icon,
      final int verticalAlignment,
      final int horizontalAlignment,
      final int verticalTextPosition,
      final int horizontalTextPosition,
      final Rectangle viewR,
      final Rectangle iconR,
      final Rectangle textR,
      final int textIconGap) {

    final int hTextPos = Utilities.convertLeadTrail(horizontalTextPosition, c);
    final int hPos = Utilities.convertLeadTrail(horizontalAlignment, c);

    return doLayoutCompoundLabel(
        fm,
        text,
        icon,
        verticalAlignment,
        hPos,
        verticalTextPosition,
        hTextPos,
        viewR,
        iconR,
        textR,
        textIconGap);
  }
  private static void layoutRects(
      final int verticalAlignment,
      final int horizontalAlignment,
      final int vTextPos,
      final int hTextPos,
      final Rectangle viewR,
      final Rectangle iconR,
      final Rectangle textR,
      final int gap) {

    boolean horizontal = hTextPos != CENTER;
    boolean vertical = vTextPos != CENTER;

    int hIconPos = hTextPos;
    int vIconPos = vTextPos;
    int width = Math.max(iconR.width, textR.width);
    int height = Math.max(iconR.height, textR.height);

    if (horizontal) {
      hIconPos = hTextPos != LEFT ? LEFT : RIGHT;
      width = iconR.width + textR.width + gap;
    } else if (vertical) {
      vIconPos = vTextPos != TOP ? TOP : BOTTOM;
      height = iconR.height + textR.height + gap;
    }

    Rectangle labelR = new Rectangle(width, height);

    Utilities.alignRect(labelR, viewR, horizontalAlignment, verticalAlignment);
    Utilities.alignRect(textR, labelR, hTextPos, vTextPos);
    Utilities.alignRect(iconR, labelR, hIconPos, vIconPos);
  }
  public void paint(final Graphics g, final JComponent c) {
    JComponent component = tooltip.getComponent();
    if (component != null && component.isEnabled() || backgroundInactiveColor == null) {
      g.setColor(tooltip.getBackground());
      LookAndFeel.installBorder(c, "ToolTip.border");
    } else {
      g.setColor(backgroundInactiveColor);
      LookAndFeel.installBorder(c, "ToolTip.borderInactive");
    }
    g.fillRect(0, 0, tooltip.getWidth(), tooltip.getHeight());

    String tipText = tooltip.getTipText();
    FontMetrics fm = Utilities.getFontMetrics(tooltip);
    Dimension stringSize = Utilities.getStringSize(tipText, fm);
    int textX =
        component instanceof AbstractButton && ((AbstractButton) component).getMnemonic() != 0
            ? 4
            : (tooltip.getWidth() - stringSize.width) / 2;
    int textY = fm.getAscent();
    Color foreground =
        component != null && component.isEnabled() || foregroundInactiveColor == null
            ? tooltip.getForeground()
            : foregroundInactiveColor;
    Utilities.drawString(g, tipText, textX, textY, fm, foreground, -1);
  }
 public static Container getAncestorNamed(final String name, final Component component) {
   Component ancestor = null;
   if (component != null && name != null) {
     for (ancestor = Utilities.getNotWindowParent(component);
         !((ancestor == null) || name.equals(ancestor.getName()));
         ancestor = Utilities.getNotWindowParent(ancestor)) ;
   }
   return (Container) ancestor;
 }
 public static void installColors(
     final JComponent comp, final String background, final String foreground) {
   Color foregroundColor = comp.getForeground();
   if (Utilities.isUIResource(foregroundColor)) {
     comp.setForeground(UIManager.getColor(foreground));
   }
   Color backgroundColor = comp.getBackground();
   if (Utilities.isUIResource(backgroundColor)) {
     comp.setBackground(UIManager.getColor(background));
   }
 }
  public Dimension getPreferredSize(final JComponent c) {
    if (tooltip == null) {
      tooltip = (JToolTip) c;
    }
    Font f = c.getFont();
    if (f == null) {
      return new Dimension();
    }
    Dimension result = Utilities.getStringSize(tooltip.getTipText(), c.getFontMetrics(f));
    result.width += 6;

    return Utilities.addInsets(result, tooltip.getInsets());
  }
 private static Component getWindowOrAppletAncestor(final Component component) {
   Component result = component;
   while (result != null && !(result instanceof Window || result instanceof Applet)) {
     result = Utilities.getNotWindowParent(result);
   }
   return result;
 }
  public static boolean isDescendingFrom(final Component child, final Component parent) {
    if (parent == child) {
      return true;
    }

    for (Component ancestor = Utilities.getNotWindowParent(child);
        ancestor != null;
        ancestor = Utilities.getNotWindowParent(ancestor)) {

      if (ancestor == parent) {
        return true;
      }
    }

    return false;
  }
  protected void uninstallDefaults(final JComponent c) {
    Utilities.uninstallColorsAndFont(c);
    LookAndFeel.uninstallBorder(c);

    backgroundInactiveColor = null;
    foregroundInactiveColor = null;
  }
  /** method that actually implements functionality of both public layoutCompoundLabel() methods */
  private static String doLayoutCompoundLabel(
      final FontMetrics fm,
      final String text,
      final Icon icon,
      final int verticalAlignment,
      final int horizontalAlignment,
      final int verticalTextPosition,
      final int horizontalTextPosition,
      final Rectangle viewR,
      final Rectangle iconR,
      final Rectangle textR,
      final int textIconGap) {

    final int gap = icon != null && !Utilities.isEmptyString(text) ? textIconGap : 0;

    if (icon != null) {
      iconR.setSize(icon.getIconWidth(), icon.getIconHeight());
    } else {
      iconR.setSize(0, 0);
    }

    String clippedText = "";
    if (!Utilities.isEmptyString(text)) {
      final int adjust = horizontalTextPosition != CENTER ? iconR.width + gap : 0;
      final int availableLength = viewR.width - adjust;
      clippedText = Utilities.clipString(fm, text, availableLength);
      textR.setSize(Utilities.getStringSize(clippedText, fm));
    } else {
      textR.setSize(0, 0);
    }

    layoutRects(
        verticalAlignment,
        horizontalAlignment,
        verticalTextPosition,
        horizontalTextPosition,
        viewR,
        iconR,
        textR,
        gap);

    return clippedText;
  }
 private static void translateRelatedPoint(
     final Point point, final Component c, final int direction, final boolean stopAtRootPane) {
   Component currentComponent = c;
   while ((currentComponent != null)
       && (!stopAtRootPane || stopAtRootPane && !(currentComponent instanceof JRootPane))) {
     Point componentLocation = currentComponent.getLocation();
     point.x += direction * componentLocation.x;
     point.y += direction * componentLocation.y;
     currentComponent = Utilities.getNotWindowParent(currentComponent);
   }
 }
 public static void installColorsAndFont(
     final JComponent comp,
     final String background,
     final String foreground,
     final String fontName) {
   installColors(comp, background, foreground);
   Font font = comp.getFont();
   if (Utilities.isUIResource(font)) {
     comp.setFont(UIManager.getFont(fontName));
   }
 }
 public static void paintComponent(
     final Graphics graphics,
     final Component component,
     final Container container,
     final int x,
     final int y,
     final int width,
     final int height) {
   Container auxContainer = Utilities.getNotWindowParent(component);
   if (auxContainer instanceof CellRendererPane) {
     if (Utilities.getNotWindowParent(auxContainer) != container) {
       container.add(auxContainer);
     }
   } else {
     auxContainer = new CellRendererPane();
     container.add(auxContainer);
   }
   ((CellRendererPane) auxContainer)
       .paintComponent(graphics, component, container, x, y, width, height, false);
 }
  protected void installKeyboardActions(final JRootPane root) {
    SwingUtilities.replaceUIActionMap(root, getActionMap(root));

    Utilities.installKeyboardActions(
        root,
        JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
        "RootPane.ancestorInputMap",
        "RootPane.ancestorInputMap.RightToLeft");
    if (root.getDefaultButton() != null) {
      loadDefaultButtonKeyBindings(root);
    }
  }
 public static void uninstallBorder(final JComponent comp) {
   if (comp != null && Utilities.isUIResource(comp.getBorder())) {
     comp.setBorder(null);
   }
 }
 public static void installBorder(final JComponent comp, final String borderName) {
   if (Utilities.isUIResource(comp.getBorder())) {
     comp.setBorder((Border) UIManager.get(borderName));
   }
 }
 protected void uninstallKeyboardActions(final JRootPane root) {
   SwingUtilities.replaceUIActionMap(root, null);
   Utilities.uninstallKeyboardActions(root, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
 }