/*
   * (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();
    }
  }
    @Override
    public Dimension minimumLayoutSize(Container c) {
      // Compute width.
      int width = 30;
      if (frame.isClosable()) {
        width += 21;
      }
      if (frame.isMaximizable()) {
        width += 16 + (frame.isClosable() ? 10 : 4);
      }
      if (frame.isIconifiable()) {
        width += 16 + (frame.isMaximizable() ? 2 : (frame.isClosable() ? 10 : 4));
      }
      FontMetrics fm = frame.getFontMetrics(getFont());
      String frameTitle = frame.getTitle();
      int title_w = frameTitle != null ? fm.stringWidth(frameTitle) : 0;
      int title_length = frameTitle != null ? frameTitle.length() : 0;

      if (title_length > 2) {
        int subtitle_w = fm.stringWidth(frame.getTitle().substring(0, 2) + "...");
        width += (title_w < subtitle_w) ? title_w : subtitle_w;
      } else {
        width += title_w;
      }

      // Compute height.
      int height;
      // if (isPalette) {
      // height = paletteTitleHeight;
      // } else {
      int fontHeight = fm.getHeight();
      fontHeight += 7;
      Icon icon = frame.getFrameIcon();
      int iconHeight = 0;
      if (icon != null) {
        // SystemMenuBar forces the icon to be 16x16 or less.
        iconHeight = Math.min(icon.getIconHeight(), 16);
      }
      iconHeight += 5;
      height = Math.max(fontHeight, iconHeight);
      // }

      return new Dimension(width, height);
    }
  /**
   * Computes the alpha value for painting the separators.
   *
   * @return Alpha value for painting the separators.
   */
  private float getSeparatorAlpha() {
    ComponentState actionAreaState =
        this.getActionTransitionTracker().getModelStateInfo().getCurrModelState();

    if (!actionAreaState.isFacetActive(ComponentStateFacet.SELECTION)
        && !actionAreaState.isDisabled()) {
      float actionRolloverCycle =
          this.getActionTransitionTracker().getFacetStrength(ComponentStateFacet.ROLLOVER);
      float popupRolloverCycle =
          this.getPopupTransitionTracker().getFacetStrength(ComponentStateFacet.ROLLOVER);
      return Math.min(1.0f, actionRolloverCycle + popupRolloverCycle);
    }
    return 1.0f;
  }