Exemplo n.º 1
0
  @Override
  public void paintComponent(Graphics2D g2) {
    if (!isVisible()) return;

    final Color textColor = new Color(40, 40, 40);
    final Color borderColor = new Color(65, 65, 65);
    final GradientPaint backGradient =
        new GradientPaint(
            bounds.x,
            bounds.y,
            getColor(),
            bounds.x + bounds.width,
            bounds.y + bounds.height,
            getColor().darker());

    final String className = component.getName();

    final FontMetrics classNameMetrics = g2.getFontMetrics(entityName.getEffectivFont());
    final int classNameWidth = classNameMetrics.stringWidth(className);
    final int classNameHeight = classNameMetrics.getHeight();

    final Dimension classNameSize = new Dimension(classNameWidth, classNameHeight);

    stereotypeFont = stereotypeFont.deriveFont(stereotypeFontBasic.getSize() * parent.getZoom());

    g2.setFont(stereotypeFont);
    final String stereotype =
        Utility.truncate(g2, "<<" + component.getStereotype() + " >>", bounds.width - 15);

    final FontMetrics stereotypeMetrics = g2.getFontMetrics(stereotypeFont);
    final int stereotypeWidth = stereotypeMetrics.stringWidth(stereotype);
    final int stereotypeHeight = stereotypeMetrics.getHeight();

    final Dimension stereotypeSize = new Dimension(stereotypeWidth, stereotypeHeight);

    final FontMetrics metrics = g2.getFontMetrics(entityName.getEffectivFont());
    final int textBoxHeight = metrics.getHeight();

    bounds.height = computeHeight(classNameSize.height, stereotypeHeight, textBoxHeight);

    final Rectangle bounds = getBounds();

    int offset = bounds.y + VERTICAL_SPACEMENT / 2;
    final int stereotypeLocationWidth = bounds.x + (bounds.width - stereotypeSize.width) / 2;

    entityName.setBounds(new Rectangle(1, 1, bounds.width - 15, textBoxHeight + 2));
    final Rectangle entityNameBounds = entityName.getBounds();
    final int classNameLocationX = bounds.x + (bounds.width - entityNameBounds.width) / 2;

    // draw background
    g2.setPaint(backGradient);
    g2.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);

    // draw border
    g2.setStroke(new BasicStroke(BORDER_WIDTH));
    g2.setColor(borderColor);
    g2.drawRect(bounds.x, bounds.y, bounds.width, bounds.height);

    // draw stereotype
    if (!component.getStereotype().isEmpty()) {
      offset += stereotypeSize.height;

      g2.setFont(stereotypeFont);
      g2.setColor(textColor);
      g2.drawString(stereotype, stereotypeLocationWidth, offset);
    }

    // draw class name
    offset += /* classNameSize.height + */ VERTICAL_SPACEMENT / 2;

    entityName.setBounds(
        new Rectangle(classNameLocationX, offset, bounds.width - 15, textBoxHeight + 2));
    entityName.paintComponent(g2);

    offset += entityNameBounds.height;

    // draw separator
    offset += 10;
    g2.setStroke(new BasicStroke(BORDER_WIDTH));
    g2.setColor(borderColor);
    g2.drawLine(bounds.x, offset, bounds.x + bounds.width, offset);

    // draw attributes
    if (displayAttributes && attributesView.size() > 0)
      // draw methods
      for (final TextBoxAttribute tb : attributesView) {
        tb.setBounds(new Rectangle(bounds.x + 8, offset + 2, bounds.width - 15, textBoxHeight + 2));
        tb.paintComponent(g2);

        offset += textBoxHeight;
      }

    // draw separator
    offset += 10;
    g2.setStroke(new BasicStroke(BORDER_WIDTH));
    g2.setColor(borderColor);
    g2.drawLine(bounds.x, offset, bounds.x + bounds.width, offset);

    // draw methods
    if (displayMethods && methodsView.size() > 0)
      // draw methods
      for (final TextBoxMethod tb : methodsView) {
        tb.setBounds(new Rectangle(bounds.x + 8, offset + 2, bounds.width - 15, textBoxHeight + 2));
        tb.paintComponent(g2);

        offset += textBoxHeight;
      }

    // is component selected? -> draw selected style
    if (parent.getSelectedComponents().contains(this)) drawSelectedStyle(g2);
  }