/*
   * (non-Javadoc)
   *
   * @see
   * org.jvnet.flamingo.common.ui.BasicCommandButtonUI#paintButtonBackground
   * (java.awt.Graphics, java.awt.Rectangle)
   */
  @Override
  protected void paintButtonBackground(Graphics graphics, Rectangle toFill) {
    if (SubstanceCoreUtilities.isButtonNeverPainted(this.commandButton)) return;

    ButtonModel actionModel = this.commandButton.getActionModel();
    PopupButtonModel popupModel = ((JCommandButton) this.commandButton).getPopupModel();
    Rectangle actionArea = this.getLayoutInfo().actionClickArea;
    Rectangle popupArea = this.getLayoutInfo().popupClickArea;

    BufferedImage fullAlphaBackground =
        CommandButtonBackgroundDelegate.getCombinedCommandButtonBackground(
            this.commandButton, actionModel, actionArea, popupModel, popupArea);

    // Two special cases here:
    // 1. Button has flat appearance and doesn't show the popup
    // 2. Button is disabled.
    // For both cases, we need to set custom translucency.
    boolean isFlat =
        this.commandButton.isFlat()
            && !((JCommandButton) this.commandButton).getPopupModel().isPopupShowing();
    boolean isSpecial = isFlat || !this.commandButton.isEnabled();
    float extraAlpha = 1.0f;
    if (isSpecial) {
      if (isFlat) {
        float extraActionAlpha = 0.0f;
        for (Map.Entry<ComponentState, StateTransitionTracker.StateContributionInfo> activeEntry :
            getActionTransitionTracker().getModelStateInfo().getStateContributionMap().entrySet()) {
          ComponentState activeState = activeEntry.getKey();
          if (activeState.isDisabled()) continue;
          if (activeState == ComponentState.ENABLED) continue;
          extraActionAlpha += activeEntry.getValue().getContribution();
        }
        float extraPopupAlpha = 0.0f;
        for (Map.Entry<ComponentState, StateTransitionTracker.StateContributionInfo> activeEntry :
            getPopupTransitionTracker().getModelStateInfo().getStateContributionMap().entrySet()) {
          ComponentState activeState = activeEntry.getKey();
          if (activeState.isDisabled()) continue;
          if (activeState == ComponentState.ENABLED) continue;
          extraPopupAlpha += activeEntry.getValue().getContribution();
        }
        extraAlpha = Math.max(extraActionAlpha, extraPopupAlpha);
      } else {
        ComponentState actionAreaState = ComponentState.getState(actionModel, this.commandButton);
        if (actionAreaState.isDisabled()) {
          extraAlpha = SubstanceColorSchemeUtilities.getAlpha(this.commandButton, actionAreaState);
        }
      }
    }
    // System.out.println(extraAlpha);
    extraAlpha = Math.min(1.0f, extraAlpha);
    if (extraAlpha > 0.0f) {
      Graphics2D g2d = (Graphics2D) graphics.create();
      g2d.setComposite(
          LafWidgetUtilities.getAlphaComposite(this.commandButton, extraAlpha, graphics));
      g2d.drawImage(fullAlphaBackground, 0, 0, null);
      g2d.dispose();
    }
  }
  /*
   * (non-Javadoc)
   *
   * @see
   * org.jvnet.flamingo.common.ui.BasicCommandButtonUI#paintButtonIcon(java
   * .awt.Graphics, java.awt.Rectangle)
   */
  @Override
  protected void paintButtonIcon(Graphics g, Rectangle iconRect) {
    JCommandButton jcb = (JCommandButton) this.commandButton;
    Icon regular = jcb.getIcon();
    if (toUseDisabledIcon()
        && (jcb.getDisabledIcon() != null)
        && ((regular != null) && !regular.getClass().isAnnotationPresent(TransitionAware.class)))
      regular = jcb.getDisabledIcon();

    if ((iconRect == null)
        || (regular == null)
        || (iconRect.width == 0)
        || (iconRect.height == 0)) {
      return;
    }

    boolean useThemed = SubstanceCoreUtilities.useThemedDefaultIcon(this.commandButton);
    if (regular != null) {
      Graphics2D g2d = (Graphics2D) g.create();

      GhostPaintingUtils.paintGhostIcon(g2d, jcb, regular, iconRect);
      g2d.setComposite(LafWidgetUtilities.getAlphaComposite(jcb, g));

      if (!useThemed) {
        regular.paintIcon(jcb, g2d, iconRect.x, iconRect.y);
      } else {
        StateTransitionTracker tracker =
            this.substanceVisualStateTracker.getActionStateTransitionTracker();
        ButtonModel model = commandButton.getActionModel();
        if (jcb.getCommandButtonKind() == CommandButtonKind.POPUP_ONLY) {
          tracker = this.substanceVisualStateTracker.getPopupStateTransitionTracker();
          model = jcb.getPopupModel();
        }
        CommandButtonBackgroundDelegate.paintThemedCommandButtonIcon(
            g2d, iconRect, jcb, regular, model, tracker);
      }
      g2d.dispose();
    }
  }
  /*
   * (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();
  }