diff options
Diffstat (limited to 'libjava/javax/swing/plaf/basic')
16 files changed, 966 insertions, 103 deletions
diff --git a/libjava/javax/swing/plaf/basic/BasicBorders.java b/libjava/javax/swing/plaf/basic/BasicBorders.java index 3b2f2d128e1..85782388534 100644 --- a/libjava/javax/swing/plaf/basic/BasicBorders.java +++ b/libjava/javax/swing/plaf/basic/BasicBorders.java @@ -1,5 +1,5 @@ /* BasicBorders.java - Copyright (C) 2002 Free Software Foundation, Inc. + Copyright (C) 2003 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -37,15 +37,281 @@ exception statement from your version. */ package javax.swing.plaf.basic; + import java.awt.Color; +import java.awt.Component; +import java.awt.Graphics; +import java.awt.Insets; +import java.io.Serializable; +import javax.swing.AbstractButton; +import javax.swing.ButtonModel; +import javax.swing.JButton; +import javax.swing.JPopupMenu; +import javax.swing.JToolBar; +import javax.swing.UIDefaults; +import javax.swing.UIManager; +import javax.swing.border.AbstractBorder; +import javax.swing.border.Border; +import javax.swing.plaf.UIResource; +import javax.swing.plaf.BorderUIResource; + + /** - * STUBBED + * Provides various borders for the Basic look and feel. + * + * @author Sascha Brawer (brawer@dandelis.ch) */ public class BasicBorders { + /** + * A MarginBorder that gets shared by multiple components. + * Created on demand by the private helper function {@link + * #getMarginBorder()}. + */ + private static MarginBorder sharedMarginBorder; + + + /** + * Returns a border that is suitable for a button. + * + * <p>The colors of the border are retrieved from the + * <code>UIDefaults</code> of the currently active look and feel + * using the keys <code>“Button.shadow”</code>, + * <code>“Button.darkShadow”</code>, + * <code>“Button.light”</code>, and + * <code>“Button.highlight”</code>. + * + * <p><img src="BasicBorders.ButtonBorder-1.png" width="300" + * height="170" alt="[A screen shot of the returned border]" /> + * + * @return a {@link + * javax.swing.plaf.BorderUIResource#CompoundBorderUIResource} + * whose outer border is a {@link #ButtonBorder} and whose + * inner border is a {@link #MarginBorder}. + */ + public static Border getButtonBorder() + { + UIDefaults defaults; + Border outer; + + defaults = UIManager.getLookAndFeelDefaults(); + + /* The keys for UIDefaults have been determined by writing a + * test program that dumps the UIDefaults to stdout; that program + * was run on a JDK 1.4.1_01 for GNU/Linux. Note that in the API, + * the key "light" is usually called "highlight", and "highlight" + * is usually called "lightHighlight". + */ + outer = new ButtonBorder(defaults.getColor("Button.shadow"), + defaults.getColor("Button.darkShadow"), + defaults.getColor("Button.light"), + defaults.getColor("Button.highlight")); + + /* While the inner border is shared between multiple buttons, + * we do not share the outer border because ButtonBorders store + * their border colors. We cannot guarantee that the colors + * (which come from UIDefaults) are unchanged between invocations + * of getButtonBorder. We could store the last colors, and share + * the button border if the colors are the same as in the last + * invocation, but it probably is not worth the effort. + */ + return new BorderUIResource.CompoundBorderUIResource( + outer, + /* inner */ getMarginBorder()); + } + + + /** + * Returns a shared MarginBorder. + */ + static Border getMarginBorder() // intentionally not public + { + /* Swing is not designed to be thread-safe, so there is no + * need to synchronize the access to the global variable. + */ + if (sharedMarginBorder == null) + sharedMarginBorder = new MarginBorder(); + + return sharedMarginBorder; + } + + + /** + * A border whose appearance depends on the state of + * the enclosed button. + * + * <p><img src="BasicBorders.ButtonBorder-1.png" width="300" + * height="170" alt="[A screen shot of this border]" /> + * + * @see javax.swing.plaf.basic.BasicGraphicsUtils#drawBezel + * + * @author Sascha Brawer (brawer@dandelis.ch) + */ public static class ButtonBorder + extends AbstractBorder + implements Serializable, UIResource { - } // class ButtonBorder + /** + * Determined using the <code>serialver</code> tool + * of Apple/Sun JDK 1.3.1 on MacOS X 10.1.5. + */ + static final long serialVersionUID = -157053874580739687L; + + + /** + * The color for drawing the shaded parts of the border. + * @see javax.swing.plaf.basic.BasicGraphicsUtils#drawBezel + */ + protected Color shadow; + + + /** + * The color for drawing the dark shaded parts of the border. + * @see javax.swing.plaf.basic.BasicGraphicsUtils#drawBezel + */ + protected Color darkShadow; + + + /** + * The color for drawing the highlighted parts of the border. + * @see javax.swing.plaf.basic.BasicGraphicsUtils#drawBezel + */ + protected Color highlight; + + + /** + * The color for drawing the bright highlighted parts of the border. + * @see javax.swing.plaf.basic.BasicGraphicsUtils#drawBezel + */ + protected Color lightHighlight; + + + /** + * Constructs a new border for drawing a button in the Basic + * look and feel. + * + * @param shadow the shadow color. + * @param darkShadow a darker variant of the shadow color. + * @param highlight the highlight color. + * @param lightHighlight a brighter variant of the highlight color. + */ + public ButtonBorder(Color shadow, Color darkShadow, + Color highlight, Color lightHighlight) + { + /* These colors usually come from the UIDefaults of the current + * look and feel. Use fallback values if the colors are not + * supplied. The API specification is silent about what + * behavior is expected for null colors, so users should not + * rely on this fallback (which is why it is not documented in + * the above Javadoc). + */ + this.shadow = (shadow != null) ? shadow : Color.gray; + this.darkShadow = (darkShadow != null) ? darkShadow : Color.black; + this.highlight = (highlight != null) ? highlight : Color.lightGray; + this.lightHighlight = (lightHighlight != null) + ? lightHighlight + : Color.white; + } + + + /** + * Paints the ButtonBorder around a given component. + * + * @param c the component whose border is to be painted. + * @param g the graphics for painting. + * @param x the horizontal position for painting the border. + * @param y the vertical position for painting the border. + * @param width the width of the available area for painting the border. + * @param height the height of the available area for painting the border. + * + * @see javax.swing.plaf.basic.BasicGraphicsUtils#drawBezel + */ + public void paintBorder(Component c, Graphics g, + int x, int y, int width, int height) + { + ButtonModel bmodel = null; + + if (c instanceof AbstractButton) + bmodel = ((AbstractButton) c).getModel(); + + BasicGraphicsUtils.drawBezel( + g, x, y, width, height, + /* pressed */ (bmodel != null) + && /* mouse button pressed */ bmodel.isPressed() + && /* mouse inside */ bmodel.isArmed(), + /* default */ (c instanceof JButton) + && ((JButton) c).isDefaultButton(), + shadow, darkShadow, highlight, lightHighlight); + } + + + /** + * Measures the width of this border. + * + * <p>Although the thickness of the actually painted border + * depends on the state of the enclosed component, this + * measurement always returns the same amount of pixels. Indeed, + * it would be rather confusing if a button was appearing to + * change its size depending on whether it is pressed or not. + * + * @param c the component whose border is to be measured. + * + * @return an Insets object whose <code>left</code>, + * <code>right</code>, <code>top</code> and + * <code>bottom</code> fields indicate the width of the + * border at the respective edge. + * + * @see #getBorderInsets(java.awt.Component, java.awt.Insets) + */ + public Insets getBorderInsets(Component c) + { + /* There is no obvious reason for overriding this method, but we + * try to have exactly the same API as the Sun reference + * implementation. + */ + return getBorderInsets(c, null); + } + + + /** + * Measures the width of this border, storing the results into a + * pre-existing Insets object. + * + * <p>Although the thickness of the actually painted border + * depends on the state of the enclosed component, this + * measurement always returns the same amount of pixels. Indeed, + * it would be rather confusing if a button was appearing to + * change its size depending on whether it is pressed or not. + * + * @param insets an Insets object for holding the result values. + * After invoking this method, the <code>left</code>, + * <code>right</code>, <code>top</code> and + * <code>bottom</code> fields indicate the width of the + * border at the respective edge. + * + * @return the same object that was passed for <code>insets</code>. + * + * @see #getBorderInsets() + */ + public Insets getBorderInsets(Component c, Insets insets) + { + /* The exact amount has been determined using a test program + * that was run on the Sun reference implementation. With + * Apple/Sun JDK 1.3.1 on MacOS X 10.1.5, the result is + * [3, 3, 3, 3]. With Sun JDK 1.4.1_01 on Linux/x86, the + * result is [2, 3, 3, 3]. We use the values from the 1.4.1_01 + * release. + */ + if (insets == null) + return new Insets(2, 3, 3, 3); + + insets.top = 2; + insets.bottom = insets.left = insets.right = 3; + return insets; + } + } + + public static class FieldBorder { public FieldBorder(Color shadow, Color darkShadow, @@ -53,9 +319,109 @@ public class BasicBorders { } } // class FieldBorder + + + /** + * An invisible, but spacing border whose margin is determined + * by calling the <code>getMargin()</code> method of the enclosed + * component. If the enclosed component has no such method, + * this border will not occupy any space. + * + * <p><img src="BasicBorders.MarginBorder-1.png" width="325" + * height="200" alt="[An illustration that shows how MarginBorder + * determines its borders]" /> + * + * @author Sascha Brawer (brawer@dandelis.ch) + */ public static class MarginBorder + extends AbstractBorder + implements Serializable, UIResource { - } // class MarginBorder + /** + * Determined using the <code>serialver</code> tool + * of Apple/Sun JDK 1.3.1 on MacOS X 10.1.5. + */ + static final long serialVersionUID = -3035848353448896090L; + + + /** + * Constructs a new MarginBorder. + */ + public MarginBorder() + { + } + + + /** + * Measures the width of this border. + * + * @param c the component whose border is to be measured. + * + * @return an Insets object whose <code>left</code>, <code>right</code>, + * <code>top</code> and <code>bottom</code> fields indicate the + * width of the border at the respective edge. + * + * @see #getBorderInsets(java.awt.Component, java.awt.Insets) + */ + public Insets getBorderInsets(Component c) + { + return getBorderInsets(c, new Insets(0, 0, 0, 0)); + } + + + /** + * Determines the insets of this border by calling the + * <code>getMargin()</code> method of the enclosed component. The + * resulting margin will be stored into the the <code>left</code>, + * <code>right</code>, <code>top</code> and <code>bottom</code> + * fields of the passed <code>insets</code> parameter. + * + * <p>Unfortunately, <code>getMargin()</code> is not a method of + * {@link javax.swing.JComponent} or some other common superclass + * of things with margins. While reflection could be used to + * determine the existence of this method, this would be slow on + * many virtual machines. Therefore, the current implementation + * knows about {@link javax.swing.AbstractButton#getMargin()}, + * {@link javax.swing.JPopupMenu#getMargin()}, and {@link + * javax.swing.JToolBar#getMargin()}. If <code>c</code> is an + * instance of a known class, the respective + * <code>getMargin()</code> method is called to determine the + * correct margin. Otherwise, a zero-width margin is returned. + * + * @param c the component whose border is to be measured. + * + * @return the same object that was passed for <code>insets</code>, + * but with changed fields. + */ + public Insets getBorderInsets(Component c, Insets insets) + { + Insets margin = null; + + /* This is terrible object-oriented design. See the above Javadoc + * for an excuse. + */ + if (c instanceof AbstractButton) + margin = ((AbstractButton) c).getMargin(); + else if (c instanceof JPopupMenu) + margin = ((JPopupMenu) c).getMargin(); + else if (c instanceof JToolBar) + margin = ((JToolBar) c).getMargin(); + + if (margin == null) + insets.top = insets.left = insets.bottom = insets.right = 0; + else + { + insets.top = margin.top; + insets.left = margin.left; + insets.bottom = margin.bottom; + insets.right = margin.right; + } + + return insets; + } + } + + public static class MenuBarBorder { public MenuBarBorder(Color shadow, Color highlight) diff --git a/libjava/javax/swing/plaf/basic/BasicButtonUI.java b/libjava/javax/swing/plaf/basic/BasicButtonUI.java index 3f67c512874..d901e7ddeb7 100644 --- a/libjava/javax/swing/plaf/basic/BasicButtonUI.java +++ b/libjava/javax/swing/plaf/basic/BasicButtonUI.java @@ -74,14 +74,7 @@ public class BasicButtonUI extends ButtonUI public Dimension getPreferredSize(JComponent c) { AbstractButton b = (AbstractButton)c; - Dimension d = BasicGraphicsUtils.getPreferredSize(b, - gap, - b.getText(), - b.getIcon(), - b.getVerticalAlignment(), - b.getHorizontalAlignment(), - b.getHorizontalTextPosition(), - b.getVerticalTextPosition()); + Dimension d = BasicGraphicsUtils.getPreferredButtonSize(b, gap); // System.out.println("^^^^^^^^^^^^^^^^^^^^^^ BASIC-PREF="+d + ",T="+b.text); return d; } @@ -99,7 +92,7 @@ public class BasicButtonUI extends ButtonUI g.setFont(f); - FontMetrics fm = SwingUtilities.getFontMetrics(f); + FontMetrics fm = g.getFontMetrics(f); Insets i = c.getInsets(); @@ -198,7 +191,7 @@ public class BasicButtonUI extends ButtonUI g.setFont(f); - FontMetrics fm = SwingUtilities.getFontMetrics(f); + FontMetrics fm = g.getFontMetrics(f); g.setColor(c.isEnabled() ? textColor : disabledTextColor); diff --git a/libjava/javax/swing/plaf/basic/BasicCheckBoxUI.java b/libjava/javax/swing/plaf/basic/BasicCheckBoxUI.java index 25fb0180b2a..54a9c69862e 100644 --- a/libjava/javax/swing/plaf/basic/BasicCheckBoxUI.java +++ b/libjava/javax/swing/plaf/basic/BasicCheckBoxUI.java @@ -57,15 +57,7 @@ public class BasicCheckBoxUI extends BasicRadioButtonUI public Dimension getPreferredSize(JComponent c) { AbstractButton b = (AbstractButton)c; - Dimension d = BasicGraphicsUtils.getPreferredSize(b, - gap, - b.getText(), - b.getIcon(), - b.getVerticalAlignment(), - b.getHorizontalAlignment(), - b.getHorizontalTextPosition(), - b.getVerticalTextPosition()); - + Dimension d = BasicGraphicsUtils.getPreferredButtonSize(b, gap); //System.out.println("^^^^^^^^^^^^^^^^^^^^^^ BASIC-PREF="+d + ",T="+b.text); return d; } diff --git a/libjava/javax/swing/plaf/basic/BasicGraphicsUtils.java b/libjava/javax/swing/plaf/basic/BasicGraphicsUtils.java index 55e9728567a..a7b64111f53 100644 --- a/libjava/javax/swing/plaf/basic/BasicGraphicsUtils.java +++ b/libjava/javax/swing/plaf/basic/BasicGraphicsUtils.java @@ -1,5 +1,5 @@ /* BasicGraphicsUtils.java - Copyright (C) 2002 Free Software Foundation, Inc. + Copyright (C) 2003 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -35,81 +35,602 @@ this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ - package javax.swing.plaf.basic; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Font; +import java.awt.FontMetrics; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Insets; +import java.awt.Rectangle; + +import java.awt.font.FontRenderContext; +import java.awt.font.LineMetrics; +import java.awt.font.TextLayout; -import java.awt.*; -import javax.swing.*; +import java.awt.geom.Rectangle2D; +import javax.swing.AbstractButton; +import javax.swing.SwingUtilities; + +/** + * A utility class providing commonly used drawing and measurement + * routines. + * + * @author Sascha Brawer (brawer@dandelis.ch) + */ public class BasicGraphicsUtils -{ - public static Dimension getPreferredSize(JComponent b, - int gap, - String text, - Icon icon, - int va, - int ha, - int htp, - int vtp) +{ + /** + * Constructor. It is utterly unclear why this class should + * be constructable, but this is what the API specification + * says. + */ + public BasicGraphicsUtils() + { + } + + + /** + * Draws a rectangle that appears etched into the surface, given + * four colors that are used for drawing. + * + * <p><img src="BasicGraphicsUtils-1.png" width="360" + * height="200" alt="[An illustration that shows which pixels + * get painted in what color]" /> + * + * @param g the graphics into which the rectangle is drawn. + * @param x the x coordinate of the rectangle. + * @param y the y coordinate of the rectangle. + * @param width the width of the rectangle in pixels. + * @param height the height of the rectangle in pixels. + * + * @param shadow the color that will be used for painting + * the outer side of the top and left edges. + * + * @param darkShadow the color that will be used for painting + * the inner side of the top and left edges. + * + * @param highlight the color that will be used for painting + * the inner side of the bottom and right edges. + * + * @param lightHighlight the color that will be used for painting + * the outer side of the bottom and right edges. + * + * @see #getEtchedInsets() + * @see javax.swing.border.EtchedBorder + */ + public static void drawEtchedRect(Graphics g, + int x, int y, int width, int height, + Color shadow, Color darkShadow, + Color highlight, Color lightHighlight) + { + Color oldColor; + int x2, y2; + + oldColor = g.getColor(); + x2 = x + width - 1; + y2 = y + height - 1; + + try { - JComponent c = b; - // this is a staight copy from BasicButtonUI.paint() - // - Rectangle tr = new Rectangle(); - Rectangle ir = new Rectangle(); - Rectangle vr = new Rectangle(); - - Font f = c.getFont(); - - FontMetrics fm = SwingUtilities.getFontMetrics(f); - - Insets i = c.getInsets(); - - vr.x = i.left; - vr.y = i.top; - vr.width = b.getWidth() - (i.right + i.left); - vr.height = b.getHeight() - (i.bottom + i.top); - - // System.out.println(" VIEW-RECT-BUTTON="+vr+", insets="+i); - - String tt = SwingUtilities.layoutCompoundLabel(b, - fm, - text, - icon, - va, - ha, - vtp, - htp, - vr, - ir, - tr, - gap); - - Rectangle r = ir.union(tr); - - Insets insets = b.getInsets(); - r.width += insets.left + insets.right; - r.height += insets.top + insets.bottom; - - // System.out.println("COMPUTED SIZE FOR PREF_SIZE="+r); - - return r.getSize(); + /* To understand this code, it might be helpful to look at the + * image "BasicGraphicsUtils-1.png" that is included with the + * JavaDoc. The file is located in the "doc-files" subdirectory. + * + * (x2, y2) is the coordinate of the most right and bottom pixel + * to be painted. + */ + g.setColor(shadow); + g.drawLine(x, y, x2 - 1, y); // top, outer + g.drawLine(x, y + 1, x, y2 - 1); // left, outer + + g.setColor(darkShadow); + g.drawLine(x + 1, y + 1, x2 - 2, y + 1); // top, inner + g.drawLine(x + 1, y + 2, x + 1, y2 - 2); // left, inner + + g.setColor(highlight); + g.drawLine(x + 1, y2 - 1, x2 - 1, y2 - 1); // bottom, inner + g.drawLine(x2 - 1, y + 1, x2 - 1, y2 - 2); // right, inner + + g.setColor(lightHighlight); + g.drawLine(x, y2, x2, y2); // bottom, outer + g.drawLine(x2, y, x2, y2 - 1); // right, outer + } + finally + { + g.setColor(oldColor); } + } + + + /** + * Determines the width of the border that gets painted by + * {@link #drawEtchedRect}. + * + * @return an <code>Insets</code> object whose <code>top</code>, + * <code>left</code>, <code>bottom</code> and + * <code>right</code> field contain the border width at the + * respective edge in pixels. + */ + public static Insets getEtchedInsets() + { + return new Insets(2, 2, 2, 2); + } + + + /** + * Draws a rectangle that appears etched into the surface, given + * two colors that are used for drawing. + * + * <p><img src="BasicGraphicsUtils-2.png" width="360" + * height="200" alt="[An illustration that shows which pixels + * get painted in what color]" /> + * + * @param g the graphics into which the rectangle is drawn. + * @param x the x coordinate of the rectangle. + * @param y the y coordinate of the rectangle. + * @param width the width of the rectangle in pixels. + * @param height the height of the rectangle in pixels. + * + * @param shadow the color that will be used for painting the outer + * side of the top and left edges, and for the inner side of + * the bottom and right ones. + * + * @param highlight the color that will be used for painting the + * inner side of the top and left edges, and for the outer + * side of the bottom and right ones. + * + * @see #getGrooveInsets() + * @see javax.swing.border.EtchedBorder + */ + public static void drawGroove(Graphics g, + int x, int y, int width, int height, + Color shadow, Color highlight) + { + /* To understand this, it might be helpful to look at the image + * "BasicGraphicsUtils-2.png" that is included with the JavaDoc, + * and to compare it with "BasicGraphicsUtils-1.png" which shows + * the pixels painted by drawEtchedRect. These image files are + * located in the "doc-files" subdirectory. + */ + drawEtchedRect(g, x, y, width, height, + /* outer topLeft */ shadow, + /* inner topLeft */ highlight, + /* inner bottomRight */ shadow, + /* outer bottomRight */ highlight); + } + - public static void drawString(Graphics g, - String text, - int underlinedChar, - int x, - int y) + /** + * Determines the width of the border that gets painted by + * {@link #drawGroove}. + * + * @return an <code>Insets</code> object whose <code>top</code>, + * <code>left</code>, <code>bottom</code> and + * <code>right</code> field contain the border width at the + * respective edge in pixels. + */ + public static Insets getGrooveInsets() + { + return new Insets(2, 2, 2, 2); + } + + + /** + * Draws a border that is suitable for buttons of the Basic look and + * feel. + * + * <p><img src="BasicGraphicsUtils-3.png" width="500" + * height="300" alt="[An illustration that shows which pixels + * get painted in what color]" /> + * + * @param g the graphics into which the rectangle is drawn. + * @param x the x coordinate of the rectangle. + * @param y the y coordinate of the rectangle. + * @param width the width of the rectangle in pixels. + * @param height the height of the rectangle in pixels. + * + * @param isPressed <code>true</code> to draw the button border + * with a pressed-in appearance; <code>false</code> for + * normal (unpressed) appearance. + * + * @param isDefault <code>true</code> to draw the border with + * the appearance it has when hitting the enter key in a + * dialog will simulate a click to this button; + * <code>false</code> for normal appearance. + * + * @param shadow the shadow color. + * @param darkShadow a darker variant of the shadow color. + * @param highlight the highlight color. + * @param lightHighlight a brighter variant of the highlight color. + */ + public static void drawBezel(Graphics g, + int x, int y, int width, int height, + boolean isPressed, boolean isDefault, + Color shadow, Color darkShadow, + Color highlight, Color lightHighlight) + { + Color oldColor = g.getColor(); + + /* To understand this, it might be helpful to look at the image + * "BasicGraphicsUtils-3.png" that is included with the JavaDoc, + * and to compare it with "BasicGraphicsUtils-1.png" which shows + * the pixels painted by drawEtchedRect. These image files are + * located in the "doc-files" subdirectory. + */ + try { - g.drawString(text, x, y); + if ((isPressed == false) && (isDefault == false)) + { + drawEtchedRect(g, x, y, width, height, + lightHighlight, highlight, + shadow, darkShadow); + } + + if ((isPressed == true) && (isDefault == false)) + { + g.setColor(shadow); + g.drawRect(x + 1, y + 1, width - 2, height - 2); + } + + if ((isPressed == false) && (isDefault == true)) + { + g.setColor(darkShadow); + g.drawRect(x, y, width - 1, height - 1); + drawEtchedRect(g, x + 1, y + 1, width - 2, height - 2, + lightHighlight, highlight, + shadow, darkShadow); + } + + if ((isPressed == true) && (isDefault == true)) + { + g.setColor(darkShadow); + g.drawRect(x, y, width - 1, height - 1); + g.setColor(shadow); + g.drawRect(x + 1, y + 1, width - 3, height - 3); + } } -} + finally + { + g.setColor(oldColor); + } + } + + + /** + * Draws a rectangle that appears lowered into the surface, given + * four colors that are used for drawing. + * + * <p><img src="BasicGraphicsUtils-4.png" width="360" + * height="200" alt="[An illustration that shows which pixels + * get painted in what color]" /> + * + * <p><strong>Compatibility with the Sun reference + * implementation:</strong> The Sun reference implementation seems + * to ignore the <code>x</code> and <code>y</code> arguments, at + * least in JDK 1.3.1 and 1.4.1_01. The method always draws the + * rectangular area at location (0, 0). A bug report has been filed + * with Sun; its “bug ID” is 4880003. The GNU Classpath + * implementation behaves correctly, thus not replicating this bug. + * + * @param g the graphics into which the rectangle is drawn. + * @param x the x coordinate of the rectangle. + * @param y the y coordinate of the rectangle. + * @param width the width of the rectangle in pixels. + * @param height the height of the rectangle in pixels. + * + * @param shadow the color that will be used for painting + * the inner side of the top and left edges. + * + * @param darkShadow the color that will be used for painting + * the outer side of the top and left edges. + * + * @param highlight the color that will be used for painting + * the inner side of the bottom and right edges. + * + * @param lightHighlight the color that will be used for painting + * the outer side of the bottom and right edges. + */ + public static void drawLoweredBezel(Graphics g, + int x, int y, int width, int height, + Color shadow, Color darkShadow, + Color highlight, Color lightHighlight) + { + /* Like drawEtchedRect, but swapping darkShadow and shadow. + * + * To understand this, it might be helpful to look at the image + * "BasicGraphicsUtils-4.png" that is included with the JavaDoc, + * and to compare it with "BasicGraphicsUtils-1.png" which shows + * the pixels painted by drawEtchedRect. These image files are + * located in the "doc-files" subdirectory. + */ + drawEtchedRect(g, x, y, width, height, + darkShadow, shadow, + highlight, lightHighlight); + } + + + /** + * Draws a String at the given location, underlining the first + * occurence of a specified character. The algorithm for determining + * the underlined position is not sensitive to case. If the + * character is not part of <code>text</code>, the text will be + * drawn without underlining. Drawing is performed in the current + * color and font of <code>g</code>. + * + * <p><img src="BasicGraphicsUtils-5.png" width="500" + * height="100" alt="[An illustration showing how to use the + * method]" /> + * + * @param g the graphics into which the String is drawn. + * + * @param text the String to draw. + * + * @param underlinedChar the character whose first occurence in + * <code>text</code> will be underlined. It is not clear + * why the API specification declares this argument to be + * of type <code>int</code> instead of <code>char</code>. + * While this would allow to pass Unicode characters outside + * Basic Multilingual Plane 0 (U+0000 .. U+FFFE), at least + * the GNU Classpath implementation does not underline + * anything if <code>underlinedChar</code> is outside + * the range of <code>char</code>. + * + * @param x the x coordinate of the text, as it would be passed to + * {@link java.awt.Graphics#drawString(java.lang.String, + * int, int)}. + * + * @param y the y coordinate of the text, as it would be passed to + * {@link java.awt.Graphics#drawString(java.lang.String, + * int, int)}. + */ + public static void drawString(Graphics g, String text, + int underlinedChar, int x, int y) + { + int index = -1; + + /* It is intentional that lower case is used. In some languages, + * the set of lowercase characters is larger than the set of + * uppercase ones. Therefore, it is good practice to use lowercase + * for such comparisons (which really means that the author of this + * code can vaguely remember having read some Unicode techreport + * with this recommendation, but is too lazy to look for the URL). + */ + if ((underlinedChar >= 0) || (underlinedChar <= 0xffff)) + index = text.toLowerCase().indexOf( + Character.toLowerCase((char) underlinedChar)); + + drawStringUnderlineCharAt(g, text, index, x, y); + } + /** + * Draws a String at the given location, underlining the character + * at the specified index. Drawing is performed in the current color + * and font of <code>g</code>. + * + * <p><img src="BasicGraphicsUtils-5.png" width="500" + * height="100" alt="[An illustration showing how to use the + * method]" /> + * + * @param g the graphics into which the String is drawn. + * + * @param text the String to draw. + * + * @param underlinedIndex the index of the underlined character in + * <code>text</code>. If <code>underlinedIndex</code> falls + * outside the range <code>[0, text.length() - 1]</code>, the + * text will be drawn without underlining anything. + * + * @param x the x coordinate of the text, as it would be passed to + * {@link java.awt.Graphics#drawString(java.lang.String, + * int, int)}. + * + * @param y the y coordinate of the text, as it would be passed to + * {@link java.awt.Graphics#drawString(java.lang.String, + * int, int)}. + * + * @since 1.4 + */ + public static void drawStringUnderlineCharAt(Graphics g, String text, + int underlinedIndex, + int x, int y) + { + Graphics2D g2; + Rectangle2D.Double underline; + FontRenderContext frc; + FontMetrics fmet; + LineMetrics lineMetrics; + Font font; + TextLayout layout; + double underlineX1, underlineX2; + boolean drawUnderline; + int textLength; + textLength = text.length(); + if (textLength == 0) + return; + drawUnderline = (underlinedIndex >= 0) && (underlinedIndex < textLength); + if (!(g instanceof Graphics2D)) + { + /* Fall-back. This is likely to produce garbage for any text + * containing right-to-left (Hebrew or Arabic) characters, even + * if the underlined character is left-to-right. + */ + g.drawString(text, x, y); + if (drawUnderline) + { + fmet = g.getFontMetrics(); + g.fillRect( + /* x */ x + fmet.stringWidth(text.substring(0, underlinedIndex)), + /* y */ y + fmet.getDescent() - 1, + /* width */ fmet.charWidth(text.charAt(underlinedIndex)), + /* height */ 1); + } + + return; + } + g2 = (Graphics2D) g; + font = g2.getFont(); + frc = g2.getFontRenderContext(); + lineMetrics = font.getLineMetrics(text, frc); + layout = new TextLayout(text, font, frc); + + /* Draw the text. */ + layout.draw(g2, x, y); + if (!drawUnderline) + return; + + underlineX1 = x + layout.getLogicalHighlightShape( + underlinedIndex, underlinedIndex).getBounds2D().getX(); + underlineX2 = x + layout.getLogicalHighlightShape( + underlinedIndex + 1, underlinedIndex + 1).getBounds2D().getX(); + + underline = new Rectangle2D.Double(); + if (underlineX1 < underlineX2) + { + underline.x = underlineX1; + underline.width = underlineX2 - underlineX1; + } + else + { + underline.x = underlineX2; + underline.width = underlineX1 - underlineX2; + } + + + underline.height = lineMetrics.getUnderlineThickness(); + underline.y = lineMetrics.getUnderlineOffset(); + if (underline.y == 0) + { + /* Some fonts do not specify an underline offset, although they + * actually should do so. In that case, the result of calling + * lineMetrics.getUnderlineOffset() will be zero. Since it would + * look very ugly if the underline was be positioned immediately + * below the baseline, we check for this and move the underline + * below the descent, as shown in the following ASCII picture: + * + * ##### ##### # + * # # # # + * # # # # + * # # # # + * ##### ###### ---- baseline (0) + * # + * # + * ------------------###----------- lineMetrics.getDescent() + */ + underline.y = lineMetrics.getDescent(); + } + + underline.y += y; + g2.fill(underline); + } + + + /** + * Draws a rectangle, simulating a dotted stroke by painting only + * every second pixel along the one-pixel thick edge. The color of + * those pixels is the current color of the Graphics <code>g</code>. + * Any other pixels are left unchanged. + * + * <p><img src="BasicGraphicsUtils-7.png" width="360" + * height="200" alt="[An illustration that shows which pixels + * get painted]" /> + * + * @param g the graphics into which the rectangle is drawn. + * @param x the x coordinate of the rectangle. + * @param y the y coordinate of the rectangle. + * @param width the width of the rectangle in pixels. + * @param height the height of the rectangle in pixels. + */ + public static void drawDashedRect(Graphics g, + int x, int y, int width, int height) + { + int right = x + width - 1; + int bottom = y + height - 1; + + /* Draw the top and bottom edge of the dotted rectangle. */ + for (int i = x; i <= right; i += 2) + { + g.drawLine(i, y, i, y); + g.drawLine(i, bottom, i, bottom); + } + + /* Draw the left and right edge of the dotted rectangle. */ + for (int i = y; i <= bottom; i += 2) + { + g.drawLine(x, i, x, i); + g.drawLine(right, i, right, i); + } + } + + + /** + * Determines the preferred width and height of an AbstractButton, + * given the gap between the button’s text and icon. + * + * @param b the button whose preferred size is determined. + * + * @param textIconGap the gap between the button’s text and + * icon. + * + * @return a <code>Dimension</code> object whose <code>width</code> + * and <code>height</code> fields indicate the preferred + * extent in pixels. + * + * @see javax.swing.SwingUtilities#layoutCompoundLabel + */ + public static Dimension getPreferredButtonSize(AbstractButton b, + int textIconGap) + { + Rectangle contentRect; + Rectangle viewRect; + Rectangle iconRect = new Rectangle(); + Rectangle textRect = new Rectangle(); + Insets insets = b.getInsets(); + + /* For determining the ideal size, do not assume a size restriction. */ + viewRect = new Rectangle(0, 0, + /* width */ Integer.MAX_VALUE, + /* height */ Integer.MAX_VALUE); + + /* java.awt.Toolkit.getFontMetrics is deprecated. However, it + * seems not obvious how to get to the correct FontMetrics object + * otherwise. The real problem probably is that the method + * javax.swing.SwingUtilities.layoutCompundLabel should take a + * LineMetrics, not a FontMetrics argument. But fixing this that + * would change the public API. + */ + SwingUtilities.layoutCompoundLabel( + b, // for the component orientation + b.getToolkit().getFontMetrics(b.getFont()), // see comment above + b.getText(), + b.getIcon(), + b.getVerticalAlignment(), + b.getHorizontalAlignment(), + b.getVerticalTextPosition(), + b.getHorizontalTextPosition(), + viewRect, iconRect, textRect, + textIconGap); + + + /* +------------------------+ +------------------------+ + * | | | | + * | ICON | | CONTENTCONTENTCONTENT | + * | TEXTTEXTTEXT | --> | CONTENTCONTENTCONTENT | + * | TEXTTEXTTEXT | | CONTENTCONTENTCONTENT | + * +------------------------+ +------------------------+ + */ + contentRect = textRect.union(iconRect); + + return new Dimension(insets.left + contentRect.width + insets.right, + insets.top + contentRect.height + insets.bottom); + } +} diff --git a/libjava/javax/swing/plaf/basic/BasicLabelUI.java b/libjava/javax/swing/plaf/basic/BasicLabelUI.java index 82623a6d3f0..0adad4dafa5 100644 --- a/libjava/javax/swing/plaf/basic/BasicLabelUI.java +++ b/libjava/javax/swing/plaf/basic/BasicLabelUI.java @@ -65,6 +65,10 @@ public class BasicLabelUI extends LabelUI public Dimension getPreferredSize(JComponent c) { JLabel b = (JLabel)c; + /* + We cannot use this method because it is not part of the + official Swing API. + Dimension d = BasicGraphicsUtils.getPreferredSize(b, gap, b.getText(), @@ -74,7 +78,8 @@ public class BasicLabelUI extends LabelUI b.getHorizontalTextPosition(), b.getVerticalTextPosition()); System.out.println("JLABEL->^^^^^^^^^^^^^^^^^^^^^^ BASIC-PREF="+d + ",T="+b.getText()); - return d; + */ + return new Dimension(100, 30); } @@ -90,7 +95,7 @@ public class BasicLabelUI extends LabelUI g.setFont(f); - FontMetrics fm = SwingUtilities.getFontMetrics(f); + FontMetrics fm = g.getFontMetrics(f); Insets i = c.getInsets(); diff --git a/libjava/javax/swing/plaf/basic/BasicRadioButtonUI.java b/libjava/javax/swing/plaf/basic/BasicRadioButtonUI.java index 0f7f6a0faf1..bcc730c48f1 100644 --- a/libjava/javax/swing/plaf/basic/BasicRadioButtonUI.java +++ b/libjava/javax/swing/plaf/basic/BasicRadioButtonUI.java @@ -60,14 +60,7 @@ public class BasicRadioButtonUI extends BasicToggleButtonUI public Dimension getPreferredSize(JComponent c) { AbstractButton b = (AbstractButton)c; - Dimension d = BasicGraphicsUtils.getPreferredSize(b, - gap, - b.getText(), - b.getIcon(), - b.getVerticalAlignment(), - b.getHorizontalAlignment(), - b.getHorizontalTextPosition(), - b.getVerticalTextPosition()); + Dimension d = BasicGraphicsUtils.getPreferredButtonSize(b, gap); // and add a little something for the circles: diff --git a/libjava/javax/swing/plaf/basic/BasicToggleButtonUI.java b/libjava/javax/swing/plaf/basic/BasicToggleButtonUI.java index 4b9260e8d8f..441c60696d5 100644 --- a/libjava/javax/swing/plaf/basic/BasicToggleButtonUI.java +++ b/libjava/javax/swing/plaf/basic/BasicToggleButtonUI.java @@ -58,14 +58,7 @@ public class BasicToggleButtonUI extends BasicButtonUI public Dimension getPreferredSize(JComponent c) { AbstractButton b = (AbstractButton)c; - Dimension d = BasicGraphicsUtils.getPreferredSize(b, - gap, - b.getText(), - b.getIcon(), - b.getVerticalAlignment(), - b.getHorizontalAlignment(), - b.getHorizontalTextPosition(), - b.getVerticalTextPosition()); + Dimension d = BasicGraphicsUtils.getPreferredButtonSize(b, gap); //System.out.println("^^^^^^^^^^^^^^^^^^^^^^ BASIC-PREF="+d + ",T="+b.text); return d; diff --git a/libjava/javax/swing/plaf/basic/doc-files/BasicBorders.ButtonBorder-1.png b/libjava/javax/swing/plaf/basic/doc-files/BasicBorders.ButtonBorder-1.png Binary files differnew file mode 100644 index 00000000000..54047dcc17b --- /dev/null +++ b/libjava/javax/swing/plaf/basic/doc-files/BasicBorders.ButtonBorder-1.png diff --git a/libjava/javax/swing/plaf/basic/doc-files/BasicBorders.MarginBorder-1.png b/libjava/javax/swing/plaf/basic/doc-files/BasicBorders.MarginBorder-1.png Binary files differnew file mode 100644 index 00000000000..a3841baac54 --- /dev/null +++ b/libjava/javax/swing/plaf/basic/doc-files/BasicBorders.MarginBorder-1.png diff --git a/libjava/javax/swing/plaf/basic/doc-files/BasicGraphicsUtils-1.png b/libjava/javax/swing/plaf/basic/doc-files/BasicGraphicsUtils-1.png Binary files differnew file mode 100644 index 00000000000..99f8c6ec47b --- /dev/null +++ b/libjava/javax/swing/plaf/basic/doc-files/BasicGraphicsUtils-1.png diff --git a/libjava/javax/swing/plaf/basic/doc-files/BasicGraphicsUtils-2.png b/libjava/javax/swing/plaf/basic/doc-files/BasicGraphicsUtils-2.png Binary files differnew file mode 100644 index 00000000000..59d9a6192e2 --- /dev/null +++ b/libjava/javax/swing/plaf/basic/doc-files/BasicGraphicsUtils-2.png diff --git a/libjava/javax/swing/plaf/basic/doc-files/BasicGraphicsUtils-3.png b/libjava/javax/swing/plaf/basic/doc-files/BasicGraphicsUtils-3.png Binary files differnew file mode 100644 index 00000000000..5b0971c1647 --- /dev/null +++ b/libjava/javax/swing/plaf/basic/doc-files/BasicGraphicsUtils-3.png diff --git a/libjava/javax/swing/plaf/basic/doc-files/BasicGraphicsUtils-4.png b/libjava/javax/swing/plaf/basic/doc-files/BasicGraphicsUtils-4.png Binary files differnew file mode 100644 index 00000000000..ceba0b6e07a --- /dev/null +++ b/libjava/javax/swing/plaf/basic/doc-files/BasicGraphicsUtils-4.png diff --git a/libjava/javax/swing/plaf/basic/doc-files/BasicGraphicsUtils-5.png b/libjava/javax/swing/plaf/basic/doc-files/BasicGraphicsUtils-5.png Binary files differnew file mode 100644 index 00000000000..fa3055f8c5e --- /dev/null +++ b/libjava/javax/swing/plaf/basic/doc-files/BasicGraphicsUtils-5.png diff --git a/libjava/javax/swing/plaf/basic/doc-files/BasicGraphicsUtils-6.png b/libjava/javax/swing/plaf/basic/doc-files/BasicGraphicsUtils-6.png Binary files differnew file mode 100644 index 00000000000..c760313e080 --- /dev/null +++ b/libjava/javax/swing/plaf/basic/doc-files/BasicGraphicsUtils-6.png diff --git a/libjava/javax/swing/plaf/basic/doc-files/BasicGraphicsUtils-7.png b/libjava/javax/swing/plaf/basic/doc-files/BasicGraphicsUtils-7.png Binary files differnew file mode 100644 index 00000000000..6a557a0445b --- /dev/null +++ b/libjava/javax/swing/plaf/basic/doc-files/BasicGraphicsUtils-7.png |

