-
- /**
- * Returns the horizontal alignment of the title text in relation to
- * the border, mapping the component-dependent alignment constants
- * {@link #LEADING}, {@link #TRAILING} and {@link #DEFAULT_JUSTIFICATION}
- * to the correct value according to the embedded component’s
- * orientation.
- *
- * @param c the Component for which this TitledBorder is the border.
- *
- * @return one of the values {@link #LEFT}, {@link #CENTER}, or {@link
- * #RIGHT}.
- */
- private int getRealTitleJustification(Component c)
- {
- switch (titleJustification)
- {
- case DEFAULT_JUSTIFICATION:
- case LEADING:
- if ((c == null) || c.getComponentOrientation().isLeftToRight())
- return LEFT;
- else
- return RIGHT;
-
- case TRAILING:
- if ((c == null) || c.getComponentOrientation().isLeftToRight())
- return RIGHT;
- else
- return LEFT;
-
- default:
- return titleJustification;
- }
- }
-
-
- /**
- * Performs various measurements for the current state of this TitledBorder
- * and the given Component.
- *
- * @param c the component (<code>null</code> not permitted).
- *
- * @return Various measurements.
- */
- private Measurements getMeasurements(Component c)
- {
- Measurements m = new Measurements();
- FontMetrics fmet;
-
- m.font = getFont(c);
- fmet = c.getFontMetrics(m.font);
- m.border = getBorder();
- if (m.border != null)
- m.borderInsets = m.border.getBorderInsets(c);
- else
- m.borderInsets = new Insets(0, 0, 0, 0);
-
- if (title != null)
- {
- m.trimmedText = title.trim();
- if (m.trimmedText.length() == 0)
- m.trimmedText = null;
- }
-
- if (m.trimmedText != null)
- {
- m.textAscent = fmet.getAscent();
- m.textDescent = fmet.getDescent() + fmet.getLeading();
-
- FontRenderContext frc = new FontRenderContext(new AffineTransform(),
- false, false);
- LineMetrics lmet = m.font.getLineMetrics(m.trimmedText, 0,
- m.trimmedText.length(), frc);
- m.lineHeight = (int) lmet.getStrikethroughOffset();
-
- // Fallback in case that LineMetrics is not available/working.
- if (m.lineHeight == 0)
- m.lineHeight = (int) (0.3333 * (double) m.textAscent);
- m.textWidth = fmet.stringWidth(m.trimmedText) + 3;
- }
- else
- {
- m.textAscent = 0;
- m.textDescent = 0;
- }
-
- m.innerSpacing = new Insets(EDGE_SPACING, EDGE_SPACING, EDGE_SPACING,
- EDGE_SPACING);
- m.outerSpacing = new Insets(EDGE_SPACING, EDGE_SPACING, EDGE_SPACING,
- EDGE_SPACING);
-
- switch (titlePosition)
- {
- case ABOVE_TOP:
- m.outerSpacing.top += m.textAscent + m.textDescent + TEXT_SPACING;
- break;
-
- case TOP:
- m.outerSpacing.top += m.textDescent + m.lineHeight;
- m.innerSpacing.top += m.textAscent - m.lineHeight;
- break;
-
- case BELOW_TOP:
- m.innerSpacing.top += m.textAscent + m.textDescent + TEXT_SPACING;
- break;
-
- case ABOVE_BOTTOM:
- m.innerSpacing.bottom += m.textAscent + m.textDescent + TEXT_SPACING;
- break;
-
- case BOTTOM:
- m.innerSpacing.bottom += Math.max(m.textAscent - m.lineHeight, 0);
- m.outerSpacing.bottom += m.textDescent + m.lineHeight;
- break;
-
- case BELOW_BOTTOM:
- m.outerSpacing.bottom += m.textAscent + m.textDescent;
- break;
-
- default:
- m.outerSpacing.top += m.textAscent;
- }
-
- return m;
- }
-
-
- /**
- * A private helper class for holding the result of measuring the
- * distances of a TitledBorder. While it would be possible to cache
- * these objects, it does not seem to be worth the effort. Note that
- * invalidating the cache would be tricky, especially since there is
- * no notification mechanism that would inform the cache when
- * border has changed, so it would return different insets.
- */
- private static class Measurements
- {
- /**
- * The font used for displaying the title text. Note that it can
- * well be that the TitledBorder’s font is <code>null</code>,
- * which means that the font is to be retrieved from the current
- * LookAndFeel. In this case, this <code>font</code> field will
- * contain the result of the retrieval. Therefore, it is safe
- * to assume that this <code>font</code> field will never have
- * a <code>null</code> value.
- */
- Font font;
-
-
- /**
- * The number of pixels between the base line and the top of the
- * text box.
- */
- int textAscent;
-
-
- /**
- * The number of pixels between the base line and the bottom of
- * the text box.
- */
- int textDescent;
-
- /**
- * The number of pixels between the base line and the height where
- * a strike-through would be drawn.
- */
- int lineHeight;
-
- /**
- * The title text after removing leading and trailing white space
- * characters. If the title consists only of white space, the
- * value of <code>trimmedText</code> will be <code>null</code>.
- */
- String trimmedText;
-
-
- /**
- * The width of the trimmed title text in pixels.
- */
- int textWidth;
-
-
- /**
- * The border that constitutes the interior border
- * underneath the title text.
- */
- Border border;
-
-
- /**
- * The distance between the TitledBorder and the interior border.
- */
- Insets outerSpacing;
-
- /**
- * The width of the interior border, as returned by
- * <code>border.getBorderInsets()</code>.
- */
- Insets borderInsets;
-
-
- /**
- * The distance between the interior border and the nested
- * Component for which this TitledBorder is a border.
- */
- Insets innerSpacing;
-
-
- /**
- * Determines the insets of the nested component when it has a
- * TitledBorder as its border. Used by {@link
- * TitledBorder#getBorderInsets(Component, Insets)}.
- *
- * @param i an Insets object for storing the results into, or
- * <code>null</code> to cause the creation of a
- * new instance.
- *
- * @return the <code>i</code> object, or a new Insets object
- * if <code>null</code> was passed for <code>i</code>.
- */
- public Insets getContentInsets(Insets i)
- {
- if (i == null)
- i = new Insets(0, 0, 0, 0);
- i.left = outerSpacing.left + borderInsets.left + innerSpacing.left;
- i.right = outerSpacing.right + borderInsets.right + innerSpacing.right;
- i.top = outerSpacing.top + borderInsets.top + innerSpacing.top;
- i.bottom = outerSpacing.bottom + borderInsets.bottom + innerSpacing.bottom;
- return i;
- }
-
-
- /**
- * Calculates the minimum size needed for displaying the border
- * and its title. Used by {@link TitledBorder#getMinimumSize(Component)}.
- *
- * @return The minimum size.
- */
- public Dimension getMinimumSize()
- {
- int width;
- Insets insets;
-
- insets = getContentInsets(null);
- width = Math.max(insets.left + insets.right, textWidth + 2
- * TEXT_INSET_H);
- return new Dimension(width, insets.top + insets.bottom);
- }
- }