Widening, and switch to KEY_TEXT_ANTIALIASING when laying out instead of

KEY_ANTIALIASING, since labels are all about text.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@2506 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
samskivert
2008-12-18 01:20:31 +00:00
parent 31bc42b5aa
commit c1f89f9227
@@ -53,8 +53,8 @@ public class MultiLineLabel extends JComponent
} }
/** /**
* Constructs a multi line label that displays the supplied text with * Constructs a multi line label that displays the supplied text with center-alignment. The
* center-alignment. The default layout is all on one line. * default layout is all on one line.
* *
* @see #setLayout * @see #setLayout
*/ */
@@ -64,8 +64,8 @@ public class MultiLineLabel extends JComponent
} }
/** /**
* Constructs a multi line label that displays the supplied text with * Constructs a multi line label that displays the supplied text with the specified
* the specified alignment. The default layout is all on one line. * alignment. The default layout is all on one line.
* *
* @see #setLayout * @see #setLayout
*/ */
@@ -75,8 +75,8 @@ public class MultiLineLabel extends JComponent
} }
/** /**
* Constructs a multi line label that displays the supplied text with * Constructs a multi line label that displays the supplied text with the specified
* the specified alignment. The default layout is all on one line. * alignment. The default layout is all on one line.
* *
* @see #setLayout * @see #setLayout
*/ */
@@ -88,8 +88,7 @@ public class MultiLineLabel extends JComponent
} }
/** /**
* Sets whether this label's text should be rendered with * Sets whether this label's text should be rendered with anti-aliasing.
* anti-aliasing.
*/ */
public void setAntiAliased (boolean antialiased) public void setAntiAliased (boolean antialiased)
{ {
@@ -101,16 +100,13 @@ public class MultiLineLabel extends JComponent
/** /**
* Sets the constraints to be used when laying out the label. * Sets the constraints to be used when laying out the label.
* *
* @param constrain {@link #HORIZONTAL} or {@link #VERTICAL} or {@link * @param constrain {@link #HORIZONTAL} or {@link #VERTICAL} or {@link #GOLDEN} if the label
* #GOLDEN} if the label should be laid out in a rectangle whose * should be laid out in a rectangle whose bounds approximate the golden ratio.
* bounds approximate the golden ratio. * @param size the width or height respectively to be targeted by the label or 0 if the label
* @param size the width or height respectively to be targeted by the * should react the first time it is laid out and use the dimension available at that
* label or 0 if the label should react the first time it is laid out * point. <em>Note:</em> this requires that the label invalidate itself during its first
* and use the dimension available at that point. <em>Note:</em> this * validation which will cause it to change size visibly in the user interface. This argument
* requires that the label invalidate itself during its first * is ignored if <code>constrain</code> is {@link #GOLDEN}.
* validation which will cause it to change size visibly in the user
* interface. This argument is ignored if <code>constrain</code> is
* {@link #GOLDEN}.
*/ */
public void setLayout (int constrain, int size) public void setLayout (int constrain, int size)
{ {
@@ -148,8 +144,7 @@ public class MultiLineLabel extends JComponent
break; break;
default: default:
throw new IllegalArgumentException( throw new IllegalArgumentException("Invalid constraint orientation " + constrain);
"Invalid constraint orientation " + constrain);
} }
} }
@@ -265,15 +260,15 @@ public class MultiLineLabel extends JComponent
{ {
super.doLayout(); super.doLayout();
// if we have been configured to relay ourselves out once we know // if we have been configured to relay ourselves out once we know our constrained width or
// our constrained width or height, take care of that here // height, take care of that here
int size; int size;
boolean delayedRevalidate = false; boolean delayedRevalidate = false;
switch (_constrain) { switch (_constrain) {
case HORIZONTAL: case HORIZONTAL:
size = getWidth(); size = getWidth();
// sanity check; sometimes labels are laid out with completely // sanity check; sometimes labels are laid out with completely invalid dimensions, so
// invalid dimensions, so we just quietly play along // we just quietly play along
if (size > 0 && size != _constrainedSize) { if (size > 0 && size != _constrainedSize) {
_constrainedSize = size; _constrainedSize = size;
_label.setTargetWidth(size); _label.setTargetWidth(size);
@@ -291,16 +286,13 @@ public class MultiLineLabel extends JComponent
break; break;
} }
// we can't just call revalidate() because we're in the middle of // we can't just call revalidate() because we're in the middle of a validation traversal;
// a validation traversal; our parent will, when we return from // our parent will, when we return from this method, declare itself to be valid; if we call
// this method, declare itself to be valid; if we call // revalidate() it will mark us and all of our parents as invalid, but then those of our
// revalidate() it will mark us and all of our parents as invalid, // parents that are involved in the first validation traversal will mark themselves as
// but then those of our parents that are involved in the first // valid and in the validation pass that results from our call to revalidate() we will be
// validation traversal will mark themselves as valid and in the // skipped over; dooh! instead we delay a call to revalidate so that the current validation
// validation pass that results from our call to revalidate() we // traversal will be completed before we mark ourselves and our parents as invalid
// will be skipped over; dooh! instead we delay a call to
// revalidate so that the current validation traversal will be
// completed before we mark ourselves and our parents as invalid
if (delayedRevalidate) { if (delayedRevalidate) {
Runnable callRevalidate = new Runnable() { Runnable callRevalidate = new Runnable() {
public void run() { public void run() {
@@ -310,8 +302,7 @@ public class MultiLineLabel extends JComponent
SwingUtilities.invokeLater(callRevalidate); SwingUtilities.invokeLater(callRevalidate);
} }
// go ahead and lay out the label in all cases so that we assume // go ahead and lay out the label in all cases so that we assume some sort of size
// some sort of size
layoutLabel(); layoutLabel();
} }
@@ -324,19 +315,17 @@ public class MultiLineLabel extends JComponent
} }
/** /**
* Called when the label has changed in some meaningful way and we'd * Called when the label has changed in some meaningful way and we'd accordingly like to
* accordingly like to re-layout the label, update our component's * re-layout the label, update our component's size, and repaint everything to suit.
* size, and repaint everything to suit.
*/ */
protected void layoutLabel () protected void layoutLabel ()
{ {
Graphics2D gfx = (Graphics2D)getGraphics(); Graphics2D gfx = (Graphics2D)getGraphics();
if (gfx != null) { if (gfx != null) {
// re-layout the label // re-layout the label
gfx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, gfx.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, (_antialiased) ?
(_antialiased) ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON :
RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
RenderingHints.VALUE_ANTIALIAS_OFF);
_label.layout(gfx); _label.layout(gfx);
gfx.dispose(); gfx.dispose();
@@ -353,15 +342,13 @@ public class MultiLineLabel extends JComponent
} }
if (_dirty) { if (_dirty) {
// attempt to lay out the label before obtaining its preferred // attempt to lay out the label before obtaining its preferred dimensions
// dimensions
layoutLabel(); layoutLabel();
} }
Dimension size = _label.getSize(); Dimension size = _label.getSize();
if (size != null) { if (size != null) {
// never let our preferred size shrink in our constrained // never let our preferred size shrink in our constrained direction
// direction
switch (_constrain) { switch (_constrain) {
case HORIZONTAL: case HORIZONTAL:
_prefd.width = Math.max(_prefd.width, size.width); _prefd.width = Math.max(_prefd.width, size.width);
@@ -395,8 +382,7 @@ public class MultiLineLabel extends JComponent
/** Pending constraint adjustments. */ /** Pending constraint adjustments. */
protected int _constrain = NONE; protected int _constrain = NONE;
/** The size to which we constrained ourselves when most recently laid /** The size to which we constrained ourselves when most recently laid out. */
* out. */
protected int _constrainedSize; protected int _constrainedSize;
/** Whether to render the label with anti-aliasing. */ /** Whether to render the label with anti-aliasing. */