1 |
/* TitledBorder.java -- |
/* TitledBorder.java -- |
2 |
Copyright (C) 2003, 2004 Free Software Foundation, Inc. |
Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
58 |
* |
* |
59 |
* @author Sascha Brawer (brawer@dandelis.ch) |
* @author Sascha Brawer (brawer@dandelis.ch) |
60 |
*/ |
*/ |
61 |
public class TitledBorder |
public class TitledBorder extends AbstractBorder |
|
extends AbstractBorder |
|
62 |
{ |
{ |
63 |
/** |
/** |
64 |
* A value for the <code>titlePosition</code> property that vertically |
* A value for the <code>titlePosition</code> property that vertically |
303 |
public TitledBorder(String title) |
public TitledBorder(String title) |
304 |
{ |
{ |
305 |
this(/* border */ null, |
this(/* border */ null, |
306 |
title, DEFAULT_JUSTIFICATION, DEFAULT_POSITION, |
title, LEADING, TOP, |
307 |
/* titleFont */ null, /* titleColor */ null); |
/* titleFont */ null, /* titleColor */ null); |
308 |
} |
} |
309 |
|
|
316 |
*/ |
*/ |
317 |
public TitledBorder(Border border) |
public TitledBorder(Border border) |
318 |
{ |
{ |
319 |
this(border, /* title */ "", DEFAULT_JUSTIFICATION, DEFAULT_POSITION, |
this(border, /* title */ "", LEADING, TOP, |
320 |
/* titleFont */ null, /* titleColor */ null); |
/* titleFont */ null, /* titleColor */ null); |
321 |
} |
} |
322 |
|
|
332 |
*/ |
*/ |
333 |
public TitledBorder(Border border, String title) |
public TitledBorder(Border border, String title) |
334 |
{ |
{ |
335 |
this(border, title, DEFAULT_JUSTIFICATION, DEFAULT_POSITION, |
this(border, title, LEADING, TOP, |
336 |
/* titleFont */ null, /* titleColor */ null); |
/* titleFont */ null, /* titleColor */ null); |
337 |
} |
} |
338 |
|
|
506 |
public void paint(Graphics g) |
public void paint(Graphics g) |
507 |
{ |
{ |
508 |
if (b != null) |
if (b != null) |
509 |
b.paintBorder(c, g, x, y, width - 1, height - 1); |
b.paintBorder(c, g, x, y, width, height); |
510 |
} |
} |
511 |
|
|
512 |
|
|
564 |
if (stripeHeight > 0) |
if (stripeHeight > 0) |
565 |
{ |
{ |
566 |
paint(g, x, holeY, holeX - x, stripeHeight); // patches #2 and #3 |
paint(g, x, holeY, holeX - x, stripeHeight); // patches #2 and #3 |
567 |
paint(g, holeX + holeWidth, holeY, width - (holeX + holeWidth), stripeHeight); |
paint(g, holeX + holeWidth, holeY, x + width - (holeX + holeWidth), stripeHeight); |
568 |
} |
} |
569 |
|
|
570 |
stripeHeight = height - (holeY - y + holeHeight); |
stripeHeight = height - (holeY - y + holeHeight); |
576 |
BorderPainter bp; |
BorderPainter bp; |
577 |
int textX, textY, borderWidth, borderHeight; |
int textX, textY, borderWidth, borderHeight; |
578 |
|
|
579 |
borderWidth = width - (mes.borderSpacing.left + mes.borderSpacing.right); |
borderWidth = width - (mes.outerSpacing.left + mes.outerSpacing.right); |
580 |
borderHeight = height - (mes.borderSpacing.top + mes.borderSpacing.bottom); |
borderHeight = height - (mes.outerSpacing.top + mes.outerSpacing.bottom); |
581 |
bp = new BorderPainter(c, getBorder(), |
bp = new BorderPainter(c, getBorder(), |
582 |
x + mes.borderSpacing.left, y + mes.borderSpacing.top, |
x + mes.outerSpacing.left, y + mes.outerSpacing.top, |
583 |
borderWidth, borderHeight); |
borderWidth, borderHeight); |
584 |
|
|
585 |
switch (getRealTitleJustification(c)) |
switch (getRealTitleJustification(c)) |
586 |
{ |
{ |
587 |
case LEFT: |
case LEFT: |
588 |
textX = x + TEXT_INSET_H; |
textX = x + EDGE_SPACING + TEXT_INSET_H; |
589 |
break; |
break; |
590 |
|
|
591 |
case CENTER: |
case CENTER: |
603 |
switch (titlePosition) |
switch (titlePosition) |
604 |
{ |
{ |
605 |
case ABOVE_TOP: |
case ABOVE_TOP: |
606 |
textY = y; |
textY = y + EDGE_SPACING; |
607 |
break; |
break; |
608 |
|
|
609 |
case TOP: |
case TOP: |
610 |
case DEFAULT_POSITION: |
case DEFAULT_POSITION: |
611 |
default: |
default: |
612 |
textY = y + mes.borderSpacing.top + mes.borderInsets.top - mes.textAscent |
textY = y + mes.outerSpacing.top + mes.borderInsets.top - mes.textAscent |
613 |
+ mes.lineHeight; |
+ mes.lineHeight; |
614 |
break; |
break; |
615 |
|
|
616 |
case BELOW_TOP: |
case BELOW_TOP: |
617 |
textY = y + mes.borderSpacing.top + mes.borderInsets.top + TEXT_SPACING; |
textY = y + mes.outerSpacing.top + mes.borderInsets.top + TEXT_SPACING; |
618 |
break; |
break; |
619 |
|
|
620 |
case ABOVE_BOTTOM: |
case ABOVE_BOTTOM: |
621 |
textY = y + height - mes.borderSpacing.bottom - mes.borderInsets.bottom |
textY = y + height - mes.outerSpacing.bottom - mes.borderInsets.bottom |
622 |
- TEXT_SPACING - (mes.textAscent + mes.textDescent); |
- TEXT_SPACING - (mes.textAscent + mes.textDescent); |
623 |
break; |
break; |
624 |
|
|
643 |
g.setFont(oldFont); |
g.setFont(oldFont); |
644 |
g.setColor(oldColor); |
g.setColor(oldColor); |
645 |
} |
} |
646 |
bp.paintExcept(g, textX - 2, textY, |
bp.paintExcept(g, textX, textY, |
647 |
mes.textWidth + 2, mes.textAscent + mes.textDescent); |
mes.textWidth, mes.textAscent + mes.textDescent); |
648 |
} |
} |
649 |
} |
} |
650 |
|
|
1001 |
m.trimmedText = null; |
m.trimmedText = null; |
1002 |
} |
} |
1003 |
|
|
|
m.textAscent = fmet.getAscent(); |
|
|
m.textDescent = fmet.getDescent(); |
|
|
|
|
|
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); |
|
|
|
|
1004 |
if (m.trimmedText != null) |
if (m.trimmedText != null) |
1005 |
m.textWidth = fmet.stringWidth(m.trimmedText) + 3; |
{ |
1006 |
|
m.textAscent = fmet.getAscent(); |
1007 |
|
m.textDescent = fmet.getDescent() + fmet.getLeading(); |
1008 |
|
|
1009 |
m.edgeSpacing = new Insets(EDGE_SPACING, EDGE_SPACING, EDGE_SPACING, EDGE_SPACING); |
FontRenderContext frc = new FontRenderContext(new AffineTransform(), |
1010 |
m.borderSpacing = new Insets(0, 0, 0, 0); |
false, false); |
1011 |
|
LineMetrics lmet = m.font.getLineMetrics(m.trimmedText, 0, |
1012 |
|
m.trimmedText.length(), frc); |
1013 |
|
m.lineHeight = (int) lmet.getStrikethroughOffset(); |
1014 |
|
|
1015 |
|
// Fallback in case that LineMetrics is not available/working. |
1016 |
|
if (m.lineHeight == 0) |
1017 |
|
m.lineHeight = (int) (0.3333 * (double) m.textAscent); |
1018 |
|
m.textWidth = fmet.stringWidth(m.trimmedText) + 3; |
1019 |
|
} |
1020 |
|
else |
1021 |
|
{ |
1022 |
|
m.textAscent = 0; |
1023 |
|
m.textDescent = 0; |
1024 |
|
} |
1025 |
|
|
1026 |
|
m.innerSpacing = new Insets(EDGE_SPACING, EDGE_SPACING, EDGE_SPACING, |
1027 |
|
EDGE_SPACING); |
1028 |
|
m.outerSpacing = new Insets(EDGE_SPACING, EDGE_SPACING, EDGE_SPACING, |
1029 |
|
EDGE_SPACING); |
1030 |
|
|
1031 |
switch (titlePosition) |
switch (titlePosition) |
1032 |
{ |
{ |
1033 |
case ABOVE_TOP: |
case ABOVE_TOP: |
1034 |
m.borderSpacing.top += m.textAscent + m.textDescent + TEXT_SPACING; |
m.outerSpacing.top += m.textAscent + m.textDescent + TEXT_SPACING; |
1035 |
break; |
break; |
1036 |
|
|
1037 |
|
case TOP: |
1038 |
|
m.outerSpacing.top += m.textDescent + m.lineHeight; |
1039 |
|
m.innerSpacing.top += m.textAscent - m.lineHeight; |
1040 |
|
break; |
1041 |
|
|
1042 |
case BELOW_TOP: |
case BELOW_TOP: |
1043 |
m.edgeSpacing.top += m.textAscent + m.textDescent + TEXT_SPACING; |
m.innerSpacing.top += m.textAscent + m.textDescent + TEXT_SPACING; |
1044 |
break; |
break; |
1045 |
|
|
1046 |
case ABOVE_BOTTOM: |
case ABOVE_BOTTOM: |
1047 |
m.edgeSpacing.bottom += m.textAscent + m.textDescent + TEXT_SPACING; |
m.innerSpacing.bottom += m.textAscent + m.textDescent + TEXT_SPACING; |
1048 |
break; |
break; |
1049 |
|
|
1050 |
case BOTTOM: |
case BOTTOM: |
1051 |
m.edgeSpacing.bottom += Math.max(m.textAscent - m.borderInsets.bottom, 0); |
m.innerSpacing.bottom += Math.max(m.textAscent - m.lineHeight, 0); |
1052 |
m.borderSpacing.bottom += m.textDescent; |
m.outerSpacing.bottom += m.textDescent + m.lineHeight; |
1053 |
break; |
break; |
1054 |
|
|
1055 |
case BELOW_BOTTOM: |
case BELOW_BOTTOM: |
1056 |
m.borderSpacing.bottom += m.textAscent + m.textDescent + TEXT_SPACING; |
m.outerSpacing.bottom += m.textAscent + m.textDescent; |
1057 |
break; |
break; |
1058 |
|
|
1059 |
default: |
default: |
1060 |
m.borderSpacing.top += m.textAscent; |
m.outerSpacing.top += m.textAscent; |
1061 |
} |
} |
1062 |
|
|
1063 |
return m; |
return m; |
1080 |
* which means that the font is to be retrieved from the current |
* which means that the font is to be retrieved from the current |
1081 |
* LookAndFeel. In this case, this <code>font</code> field will |
* LookAndFeel. In this case, this <code>font</code> field will |
1082 |
* contain the result of the retrieval. Therefore, it is safe |
* contain the result of the retrieval. Therefore, it is safe |
1083 |
* to assume that his <code>font</code> field will never have |
* to assume that this <code>font</code> field will never have |
1084 |
* a <code>null</code> value. |
* a <code>null</code> value. |
1085 |
*/ |
*/ |
1086 |
Font font; |
Font font; |
1120 |
|
|
1121 |
|
|
1122 |
/** |
/** |
1123 |
* The border that constitues the interior border |
* The border that constitutes the interior border |
1124 |
* underneath the title text. |
* underneath the title text. |
1125 |
*/ |
*/ |
1126 |
Border border; |
Border border; |
1129 |
/** |
/** |
1130 |
* The distance between the TitledBorder and the interior border. |
* The distance between the TitledBorder and the interior border. |
1131 |
*/ |
*/ |
1132 |
Insets borderSpacing; |
Insets outerSpacing; |
|
|
|
1133 |
|
|
1134 |
/** |
/** |
1135 |
* The width of the interior border, as returned by |
* The width of the interior border, as returned by |
1142 |
* The distance between the interior border and the nested |
* The distance between the interior border and the nested |
1143 |
* Component for which this TitledBorder is a border. |
* Component for which this TitledBorder is a border. |
1144 |
*/ |
*/ |
1145 |
Insets edgeSpacing; |
Insets innerSpacing; |
1146 |
|
|
1147 |
|
|
1148 |
/** |
/** |
1161 |
{ |
{ |
1162 |
if (i == null) |
if (i == null) |
1163 |
i = new Insets(0, 0, 0, 0); |
i = new Insets(0, 0, 0, 0); |
1164 |
i.left = borderSpacing.left + borderInsets.left + edgeSpacing.left; |
i.left = outerSpacing.left + borderInsets.left + innerSpacing.left; |
1165 |
i.right = borderSpacing.right + borderInsets.right + edgeSpacing.right; |
i.right = outerSpacing.right + borderInsets.right + innerSpacing.right; |
1166 |
i.top = borderSpacing.top + borderInsets.top + edgeSpacing.top; |
i.top = outerSpacing.top + borderInsets.top + innerSpacing.top; |
1167 |
i.bottom = borderSpacing.bottom + borderInsets.bottom + edgeSpacing.bottom; |
i.bottom = outerSpacing.bottom + borderInsets.bottom + innerSpacing.bottom; |
1168 |
return i; |
return i; |
1169 |
} |
} |
1170 |
|
|
1179 |
Insets insets; |
Insets insets; |
1180 |
|
|
1181 |
insets = getContentInsets(null); |
insets = getContentInsets(null); |
1182 |
width = Math.max(insets.left + insets.right, textWidth + 2 * TEXT_INSET_H); |
width = Math.max(insets.left + insets.right, textWidth + 2 |
1183 |
|
* TEXT_INSET_H); |
1184 |
return new Dimension(width, insets.top + insets.bottom); |
return new Dimension(width, insets.top + insets.bottom); |
1185 |
} |
} |
1186 |
} |
} |