public Rectangle getVisualBounds(JComponent c, int type, int width, int height) {
    Rectangle bounds = new Rectangle(0, 0, width, height);
    if (type == VisuallyLayoutable.CLIP_BOUNDS) {
      return bounds;
    }

    AbstractButton b = (AbstractButton) c;

    if (type == VisuallyLayoutable.COMPONENT_BOUNDS
        && b.getBorder() != null
        && b.isBorderPainted()) {
      Border border = b.getBorder();
      if (border instanceof BackgroundBorder) {
        border = ((BackgroundBorder) border).getBackgroundBorder();
        if (border instanceof VisualMargin) {
          InsetsUtil.subtractInto(((VisualMargin) border).getVisualMargin(c), bounds);
        } else if (border instanceof QuaquaButtonBorder) {
          InsetsUtil.subtractInto(((QuaquaButtonBorder) border).getVisualMargin(c), bounds);
        }
      }
      return bounds;
    }

    String text = b.getText();
    boolean isEmpty = (text == null || text.length() == 0);
    if (isEmpty) {
      text = " ";
    }
    Icon icon = (b.isEnabled()) ? b.getIcon() : b.getDisabledIcon();

    if ((icon == null) && (text == null)) {
      return null;
    }

    FontMetrics fm = c.getFontMetrics(c.getFont());
    Insets insets = c.getInsets(viewInsets);

    viewR.x = insets.left;
    viewR.y = insets.top;
    viewR.width = width - (insets.left + insets.right);
    viewR.height = height - (insets.top + insets.bottom);

    iconR.x = iconR.y = iconR.width = iconR.height = 0;
    textR.x = textR.y = textR.width = textR.height = 0;

    String clippedText = layoutCL(b, fm, text, icon, viewR, iconR, textR);

    Rectangle textBounds = Fonts.getPerceivedBounds(text, c.getFont(), c);
    if (isEmpty) {
      textBounds.width = 0;
    }
    int ascent = fm.getAscent();
    textR.x += textBounds.x;
    textR.width = textBounds.width;
    textR.y += ascent + textBounds.y;
    textR.height -= fm.getHeight() - textBounds.height;

    bounds.setBounds(textR);
    return bounds;
  }
  /**
   * This method sets the border of the given component to a rollover border.
   *
   * @param c The Component whose border needs to be set.
   */
  protected void setBorderToRollover(Component c) {
    if (c instanceof AbstractButton) {
      AbstractButton b = (AbstractButton) c;
      b.setRolloverEnabled(false);

      // Save old border in hashtable.
      if (b.getBorder() != null) borders.put(b, b.getBorder());

      b.setBorder(rolloverBorder);
    }
  }
 protected void paintBackground(Graphics g, AbstractButton b) {
   if (!b.isContentAreaFilled() || (b.getParent() instanceof JMenuBar)) {
     return;
   }
   int width = b.getWidth();
   int height = b.getHeight();
   Graphics2D g2D = (Graphics2D) g;
   Shape savedClip = g.getClip();
   if ((b.getBorder() != null) && b.isBorderPainted() && (b.getBorder() instanceof UIResource)) {
     Area clipArea = new Area(new Rectangle2D.Double(1, 1, width - 2, height - 2));
     if (savedClip != null) {
       clipArea.intersect(new Area(savedClip));
     }
     g2D.setClip(clipArea);
   }
   super.paintBackground(g, b);
   g2D.setClip(savedClip);
 }
示例#4
0
  /**
   * Initializes the layout of this <code>GroupBox</code>.
   *
   * @param buttons The buttons to be shown at the top-left section of the group box
   * @param panes The panes to be shown as a group where a button is shown as a pane's title
   */
  protected void initializeLayout(AbstractButton[] buttons, JComponent[] panes) {
    checkIntegrity(buttons, panes);

    GridBagConstraints constraints = new GridBagConstraints();

    for (int index = 0; index < buttons.length; index++) {
      boolean paneWasAddedBefore = ((index > 0) && (panes[index - 1] != NO_PANE));

      // First add the button
      AbstractButton button = buttons[index];
      button.setOpaque(false); // Requires to paint the titled border properly
      button.setBorder(
          BorderFactory.createCompoundBorder(
              BorderFactory.createEmptyBorder(0, 5, 0, 0), button.getBorder()));

      if (panes[index] != NO_PANE) {
        button.addItemListener(buildRepainterHandler());
      }

      constraints.gridx = 0;
      constraints.gridy = index;
      constraints.gridwidth = 1;
      constraints.gridheight = 1;
      constraints.weightx = 0;
      constraints.weighty = 0;
      constraints.fill = GridBagConstraints.NONE;
      constraints.anchor = GridBagConstraints.FIRST_LINE_START;
      constraints.insets = new Insets(paneWasAddedBefore ? 5 : 0, 0, 0, 0);

      add(button, constraints);

      // Now add the pane if one is associated with the button
      JComponent pane = panes[index];

      if (pane != NO_PANE) {
        boolean fillVertical = shouldFillVertical(pane);
        int top = button.getPreferredSize().height / 2 - 6;

        pane.setBorder(
            BorderFactory.createCompoundBorder(createPaneBorder(button, pane), pane.getBorder()));

        constraints.gridx = 0;
        constraints.gridy = index;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.weightx = 1;
        constraints.weighty = fillVertical ? 1 : 0;
        constraints.fill = fillVertical ? GridBagConstraints.BOTH : GridBagConstraints.HORIZONTAL;
        constraints.anchor = GridBagConstraints.CENTER;
        constraints.insets = new Insets(paneWasAddedBefore ? top + 5 : top, 0, 0, 0);

        add(pane, constraints);
      }
    }
  }
 private boolean borderHasPressedCue(AbstractButton c) {
   if (c.isBorderPainted()) {
     Border b = c.getBorder();
     if (b != null && b instanceof BackgroundBorder) {
       b = ((BackgroundBorder) b).getBackgroundBorder();
       if (b != null && b instanceof QuaquaButtonBorder) {
         return ((QuaquaButtonBorder) b).hasPressedCue(c);
       }
     }
     return b != null;
   } else {
     return false;
   }
 }
示例#6
0
  protected void paintBackground(Graphics g, AbstractButton b) {
    if (!b.isContentAreaFilled() || (b.getParent() instanceof JMenuBar)) {
      return;
    }

    if (!(b.isBorderPainted() && (b.getBorder() instanceof UIResource))) {
      super.paintBackground(g, b);
      return;
    }

    int width = b.getWidth();
    int height = b.getHeight();
    ButtonModel model = b.getModel();
    Graphics2D g2D = (Graphics2D) g;
    Composite composite = g2D.getComposite();
    Object savedRederingHint = g2D.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
    g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    if (((width < 64) || (height < 16)) && ((b.getText() == null) || b.getText().length() == 0)) {
      if (model.isSelected()) {
        Color color = ColorHelper.darker(b.getBackground(), 20);
        g2D.setColor(color);
        g2D.fillRect(0, 0, width - 1, height - 1);
        if (model.isEnabled()) {
          g2D.setColor(AbstractLookAndFeel.getFrameColor());
        } else {
          g2D.setColor(ColorHelper.brighter(AbstractLookAndFeel.getFrameColor(), 20));
        }
        g2D.drawRect(0, 0, width - 1, height - 1);
      } else {
        Color[] colors = null;
        if (b.getBackground() instanceof ColorUIResource) {
          if (!model.isEnabled()) {
            colors = AbstractLookAndFeel.getTheme().getDisabledColors();
          } else if (b.isRolloverEnabled() && model.isRollover()) {
            colors = AbstractLookAndFeel.getTheme().getRolloverColors();
          } else {
            colors = AbstractLookAndFeel.getTheme().getButtonColors();
          }
        } else {
          colors =
              ColorHelper.createColorArr(
                  ColorHelper.brighter(b.getBackground(), 20),
                  ColorHelper.darker(b.getBackground(), 20),
                  20);
        }
        JTattooUtilities.fillHorGradient(g, colors, 0, 0, width - 1, height - 1);
        if (model.isEnabled()) {
          g2D.setColor(AbstractLookAndFeel.getFrameColor());
        } else {
          g2D.setColor(ColorHelper.brighter(AbstractLookAndFeel.getFrameColor(), 20));
        }
        g2D.drawRect(0, 0, width - 1, height - 1);
        AlphaComposite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.6f);
        g2D.setComposite(alpha);
        g2D.setColor(Color.white);
        g2D.drawRect(1, 1, width - 3, height - 3);
      }
    } else if (model.isPressed() && model.isArmed()) {
      int d = height - 2;
      Color color = AbstractLookAndFeel.getTheme().getSelectionBackgroundColor();
      g2D.setColor(color);
      g2D.fillRoundRect(0, 0, width - 1, height - 1, d, d);

      g2D.setColor(ColorHelper.darker(color, 40));
      g2D.drawRoundRect(0, 0, width - 1, height - 1, d, d);
    } else if (model.isSelected()) {
      int d = height - 2;
      Color color = ColorHelper.darker(b.getBackground(), 20);
      g2D.setColor(color);
      g2D.fillRoundRect(0, 0, width - 1, height - 1, d, d);

      if (model.isEnabled()) {
        g2D.setColor(AbstractLookAndFeel.getFrameColor());
      } else {
        g2D.setColor(ColorHelper.brighter(AbstractLookAndFeel.getFrameColor(), 20));
      }
      g2D.drawRoundRect(0, 0, width - 1, height - 1, d, d);
    } else {
      int d = height - 2;

      Color[] colors = null;
      if (b.getBackground() instanceof ColorUIResource) {
        if (!model.isEnabled()) {
          colors = AbstractLookAndFeel.getTheme().getDisabledColors();
        } else if (b.isRolloverEnabled() && model.isRollover()) {
          colors = AbstractLookAndFeel.getTheme().getRolloverColors();
        } else {
          colors = AbstractLookAndFeel.getTheme().getButtonColors();
        }
      } else {
        colors =
            ColorHelper.createColorArr(
                ColorHelper.brighter(b.getBackground(), 20),
                ColorHelper.darker(b.getBackground(), 20),
                20);
      }

      Shape savedClip = g.getClip();
      Area clipArea = new Area(new RoundRectangle2D.Double(0, 0, width - 1, height - 1, d, d));
      clipArea.intersect(new Area(savedClip));
      g2D.setClip(clipArea);
      JTattooUtilities.fillHorGradient(g, colors, 0, 0, width, height);
      g2D.setClip(savedClip);

      if (model.isEnabled()) {
        g2D.setColor(AbstractLookAndFeel.getFrameColor());
      } else {
        g2D.setColor(ColorHelper.brighter(AbstractLookAndFeel.getFrameColor(), 20));
      }
      g2D.drawRoundRect(0, 0, width - 1, height - 1, d, d);

      AlphaComposite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.6f);
      g2D.setComposite(alpha);
      g2D.setColor(Color.white);
      g2D.drawRoundRect(1, 1, width - 3, height - 3, d - 2, d - 2);
    }
    g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, savedRederingHint);
    g2D.setComposite(composite);
  }