/*
  * (non-Javadoc)
  *
  * @see org.jvnet.flamingo.ribbon.ui.BasicRibbonUI#paintBackground(java.awt.
  * Graphics )
  */
 @Override
 protected void paintBackground(Graphics g) {
   BackgroundPaintingUtils.update(g, this.ribbon, false);
 }
  /*
   * (non-Javadoc)
   *
   * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
   */
  @Override
  public void paintComponent(Graphics g) {
    // if (this.isPalette) {
    // this.paintPalette(g);
    // return;
    // }

    DecorationAreaType decorationType = getThisDecorationType();

    Graphics2D graphics = (Graphics2D) g.create();
    // Desktop icon is translucent.
    final float coef = (this.getParent() instanceof JDesktopIcon) ? 0.6f : 1.0f;
    graphics.setComposite(LafWidgetUtilities.getAlphaComposite(this.frame, coef, g));

    boolean leftToRight = this.frame.getComponentOrientation().isLeftToRight();

    int width = this.getWidth();
    int height = this.getHeight() + 2;

    SubstanceColorScheme scheme =
        SubstanceCoreUtilities.getSkin(this.frame).getEnabledColorScheme(decorationType);
    JInternalFrame hostFrame =
        (JInternalFrame) SwingUtilities.getAncestorOfClass(JInternalFrame.class, this);
    JComponent hostForColorization = hostFrame;
    if (hostFrame == null) {
      // try desktop icon
      JDesktopIcon desktopIcon =
          (JDesktopIcon) SwingUtilities.getAncestorOfClass(JDesktopIcon.class, this);
      if (desktopIcon != null) hostFrame = desktopIcon.getInternalFrame();
      hostForColorization = desktopIcon;
    }
    // if ((hostFrame != null) && SubstanceCoreUtilities.hasColorization(
    // this)) {
    Color backgr = hostFrame.getBackground();
    if (!(backgr instanceof UIResource)) {
      double colorization = SubstanceCoreUtilities.getColorizationFactor(hostForColorization);
      scheme = ShiftColorScheme.getShiftedScheme(scheme, backgr, colorization, null, 0.0);
    }
    // }
    String theTitle = this.frame.getTitle();

    // offset of border
    int xOffset;
    int leftEnd;
    int rightEnd;

    if (leftToRight) {
      xOffset = 5;
      Icon icon = this.frame.getFrameIcon();
      if (icon != null) {
        xOffset += icon.getIconWidth() + 5;
      }

      leftEnd = (this.menuBar == null) ? 0 : (this.menuBar.getWidth() + 5);
      xOffset += leftEnd;
      if (icon != null) leftEnd += (icon.getIconWidth() + 5);

      rightEnd = width - 5;

      // find the leftmost button for the right end
      AbstractButton leftmostButton = null;
      if (this.frame.isIconifiable()) {
        leftmostButton = this.iconButton;
      } else {
        if (this.frame.isMaximizable()) {
          leftmostButton = this.maxButton;
        } else {
          if (this.frame.isClosable()) {
            leftmostButton = this.closeButton;
          }
        }
      }

      if (leftmostButton != null) {
        Rectangle rect = leftmostButton.getBounds();
        rightEnd = rect.getBounds().x - 5;
      }
      if (theTitle != null) {
        FontMetrics fm = this.frame.getFontMetrics(graphics.getFont());
        int titleWidth = rightEnd - leftEnd;
        String clippedTitle = SubstanceCoreUtilities.clipString(fm, titleWidth, theTitle);
        // show tooltip with full title only if necessary
        if (theTitle.equals(clippedTitle)) this.setToolTipText(null);
        else this.setToolTipText(theTitle);
        theTitle = clippedTitle;
      }
    } else {
      xOffset = width - 5;

      Icon icon = this.frame.getFrameIcon();
      if (icon != null) {
        xOffset -= (icon.getIconWidth() + 5);
      }

      rightEnd = (this.menuBar == null) ? xOffset : xOffset - this.menuBar.getWidth() - 5;

      // find the rightmost button for the left end
      AbstractButton rightmostButton = null;
      if (this.frame.isIconifiable()) {
        rightmostButton = this.iconButton;
      } else {
        if (this.frame.isMaximizable()) {
          rightmostButton = this.maxButton;
        } else {
          if (this.frame.isClosable()) {
            rightmostButton = this.closeButton;
          }
        }
      }

      leftEnd = 5;
      if (rightmostButton != null) {
        Rectangle rect = rightmostButton.getBounds();
        leftEnd = rect.getBounds().x + 5;
      }
      if (theTitle != null) {
        FontMetrics fm = this.frame.getFontMetrics(graphics.getFont());
        int titleWidth = rightEnd - leftEnd;
        String clippedTitle = SubstanceCoreUtilities.clipString(fm, titleWidth, theTitle);
        // show tooltip with full title only if necessary
        if (theTitle.equals(clippedTitle)) {
          this.setToolTipText(null);
        } else {
          this.setToolTipText(theTitle);
        }
        theTitle = clippedTitle;
        xOffset = rightEnd - fm.stringWidth(theTitle);
      }
    }

    BackgroundPaintingUtils.update(
        graphics, SubstanceInternalFrameTitlePane.this, false, decorationType);
    // DecorationPainterUtils.paintDecorationBackground(graphics,
    // SubstanceInternalFrameTitlePane.this, false);

    // draw the title (if needed)
    if (theTitle != null) {
      JRootPane rootPane = this.getRootPane();
      FontMetrics fm = rootPane.getFontMetrics(graphics.getFont());
      int yOffset = ((height - fm.getHeight()) / 2) + fm.getAscent();

      SubstanceTextUtilities.paintTextWithDropShadow(
          this,
          graphics,
          SubstanceColorUtilities.getForegroundColor(scheme),
          theTitle,
          width,
          height,
          xOffset,
          yOffset);
    }

    Icon icon = this.frame.getFrameIcon();
    if (icon != null) {
      if (leftToRight) {
        int iconY = ((height / 2) - (icon.getIconHeight() / 2));
        icon.paintIcon(this.frame, graphics, 5, iconY);
      } else {
        int iconY = ((height / 2) - (icon.getIconHeight() / 2));
        icon.paintIcon(this.frame, graphics, width - 5 - icon.getIconWidth(), iconY);
      }
    }

    graphics.dispose();
  }