From a00d0634ff26f7c9658b7b51c1886a21ae56e4e6 Mon Sep 17 00:00:00 2001 From: mdb Date: Tue, 12 Nov 2002 06:41:29 +0000 Subject: [PATCH] Sweet Mary Mother of Jesus, after fiddling with this god damned label layout business for hours, I've come to the conclusion that the only thing to do is to go with the "standard" solution of relaying ourselves out after we know how wide or tall we actually want to be. Three cheers for Swing. git-svn-id: https://samskivert.googlecode.com/svn/trunk@926 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../com/samskivert/swing/MultiLineLabel.java | 60 +++++++++++++++---- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/projects/samskivert/src/java/com/samskivert/swing/MultiLineLabel.java b/projects/samskivert/src/java/com/samskivert/swing/MultiLineLabel.java index f88915f7..caceded0 100644 --- a/projects/samskivert/src/java/com/samskivert/swing/MultiLineLabel.java +++ b/projects/samskivert/src/java/com/samskivert/swing/MultiLineLabel.java @@ -1,5 +1,5 @@ // -// $Id: MultiLineLabel.java,v 1.7 2002/11/12 01:15:19 mdb Exp $ +// $Id: MultiLineLabel.java,v 1.8 2002/11/12 06:41:29 mdb Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2002 Walter Korman @@ -105,8 +105,12 @@ public class MultiLineLabel extends JComponent * #GOLDEN} if the label should be laid out in a rectangle whose * bounds approximate the golden ratio. * @param size the width or height respectively to be targeted by the - * label. This is ignored if constrain is {@link - * #GOLDEN}. + * label or 0 if the label should react the first time it is laid out + * and use the dimension available at that point. Note: this + * requires that the label invalidate itself during its first + * validation which will cause it to change size visibly in the user + * interface. This argument is ignored if constrain is + * {@link #GOLDEN}. */ public void setLayout (int constrain, int size) { @@ -120,11 +124,19 @@ public class MultiLineLabel extends JComponent { switch (constrain) { case HORIZONTAL: - _label.setTargetWidth(size); + if (size == 0) { + _constrain = HORIZONTAL; + } else { + _label.setTargetWidth(size); + } break; case VERTICAL: - _label.setTargetHeight(size); + if (size == 0) { + _constrain = VERTICAL; + } else { + _label.setTargetHeight(size); + } break; case GOLDEN: @@ -146,9 +158,11 @@ public class MultiLineLabel extends JComponent */ public void setText (String text) { - _label.setText(text); - _dirty = true; - repaint(); + if (_label.setText(text)) { + _dirty = true; + revalidate(); + repaint(); + } } /** @@ -236,6 +250,30 @@ public class MultiLineLabel extends JComponent { super.doLayout(); + // if we have been configured to relay ourselves out once we know + // our constrained width or height, take care of that here + switch (_constrain) { + case HORIZONTAL: + // sanity check; sometimes labels are laid out with completely + // invalid dimensions, so we just quietly play along + if (getWidth() > 0) { + _constrain = NONE; + _label.setTargetWidth(getWidth()); + revalidate(); + } + break; + + case VERTICAL: + if (getHeight() > 0) { + _constrain = NONE; + _label.setTargetHeight(getHeight()); + revalidate(); + } + break; + } + + // go ahead and lay out the label in all cases so that we assume + // some sort of size layoutLabel(); } @@ -256,9 +294,6 @@ public class MultiLineLabel extends JComponent _label.layout(gfx); gfx.dispose(); - // update our size and force a layout - revalidate(); - // note that we're no longer dirty _dirty = false; } @@ -285,6 +320,9 @@ public class MultiLineLabel extends JComponent /** The off-axis alignment with which the label is positioned. */ protected int _offalign; + /** Pending constraint adjustments. */ + protected int _constrain = NONE; + /** Whether to render the label with anti-aliasing. */ protected boolean _antialiased;