summaryrefslogtreecommitdiff
path: root/javax/swing/plaf
diff options
context:
space:
mode:
authorRoman Kennke <roman@kennke.org>2006-08-17 14:45:45 +0000
committerRoman Kennke <roman@kennke.org>2006-08-17 14:45:45 +0000
commit16bf524b4d99016e294349cc49e9dbf6a20b4059 (patch)
treeae29c0c913a531223d23882ed70c0e4cbfe37afa /javax/swing/plaf
parent36626a9b845ef643f8030fa7dd51608a6d680bfc (diff)
downloadclasspath-16bf524b4d99016e294349cc49e9dbf6a20b4059.tar.gz
2006-08-17 Roman Kennke <kennke@aicas.com>
* javax/swing/SwingUtilities.java (clipString): New helper method for trimming strings. (layoutCompoundLabelImpl): Fixed algorithm to conform testsuites. Trim text if it's too long. Avoid creating new Rectangles. Optimized for performance. (layoutCompoundLabel): Use switch rather then if-else-chain. * javax/swing/plaf/basic/BasicButtonUI.java (viewR): New field. (iconR): New field. (textR): New field. (paint): Reset and use cached rectangles. Only call paintIcon() if icon is not null. Don't call paintButtonPressed() when button is selected, only when it is both armed and pressed. * javax/swing/plaf/basic/BasicGraphicsUtils.java (getPreferredButtonSize): Reused cached rectangles rather then creating new ones. Don't create new Rectangle via Rectangle.union(). * javax/swing/plaf/basic/BasicLabelUI.java (getPreferredSize): Correctly reset cached rectangles. Especially the view rect must have a big size to give it room for layouting. Short cut layout when text == null. (paint): Correctly reset cached rectangles. * javax/swing/plaf/basic/BasicMenuItemUI.java (resetRectangles): New helper method. (getPreferredMenuItemSize): Correctly reset the cached rectangles. (paintMenuItem): Correctly reset the cached rectangles. * javax/swing/plaf/basic/BasicRadioButtonUI.java (getPreferredSize): Use cached Rectangle objects and initialize them correctly. (paint): Use cached Rectangle objects and initialize them correctly.
Diffstat (limited to 'javax/swing/plaf')
-rw-r--r--javax/swing/plaf/basic/BasicButtonUI.java77
-rw-r--r--javax/swing/plaf/basic/BasicGraphicsUtils.java55
-rw-r--r--javax/swing/plaf/basic/BasicLabelUI.java57
-rw-r--r--javax/swing/plaf/basic/BasicMenuItemUI.java45
-rw-r--r--javax/swing/plaf/basic/BasicRadioButtonUI.java57
5 files changed, 192 insertions, 99 deletions
diff --git a/javax/swing/plaf/basic/BasicButtonUI.java b/javax/swing/plaf/basic/BasicButtonUI.java
index cdaec2543..96b11a00d 100644
--- a/javax/swing/plaf/basic/BasicButtonUI.java
+++ b/javax/swing/plaf/basic/BasicButtonUI.java
@@ -42,6 +42,7 @@ import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
+import java.awt.Insets;
import java.awt.Rectangle;
import javax.swing.AbstractButton;
@@ -64,6 +65,24 @@ import javax.swing.text.View;
public class BasicButtonUI extends ButtonUI
{
/**
+ * Cached rectangle for layouting the label. Used in paint() and
+ * BasicGraphicsUtils.getPreferredButtonSize().
+ */
+ static Rectangle viewR = new Rectangle();
+
+ /**
+ * Cached rectangle for layouting the label. Used in paint() and
+ * BasicGraphicsUtils.getPreferredButtonSize().
+ */
+ static Rectangle iconR = new Rectangle();
+
+ /**
+ * Cached rectangle for layouting the label. Used in paint() and
+ * BasicGraphicsUtils.getPreferredButtonSize().
+ */
+ static Rectangle textR = new Rectangle();
+
+ /**
* A constant used to pad out elements in the button's layout and
* preferred size calculations.
*/
@@ -378,44 +397,50 @@ public class BasicButtonUI extends ButtonUI
{
AbstractButton b = (AbstractButton) c;
- Rectangle tr = new Rectangle();
- Rectangle ir = new Rectangle();
- Rectangle vr = new Rectangle();
+ Insets i = c.getInsets();
+ viewR.x = i.left;
+ viewR.y = i.top;
+ viewR.width = c.getWidth() - i.left - i.right;
+ viewR.height = c.getHeight() - i.top - i.bottom;
+ textR.x = 0;
+ textR.y = 0;
+ textR.width = 0;
+ textR.height = 0;
+ iconR.x = 0;
+ iconR.y = 0;
+ iconR.width = 0;
+ iconR.height = 0;
Font f = c.getFont();
-
g.setFont(f);
+ Icon icon = b.getIcon();
+ String text = b.getText();
+ text = SwingUtilities.layoutCompoundLabel(c, g.getFontMetrics(f),
+ text, icon,
+ b.getVerticalAlignment(),
+ b.getHorizontalAlignment(),
+ b.getVerticalTextPosition(),
+ b.getHorizontalTextPosition(),
+ viewR, iconR, textR,
+ text == null ? 0
+ : b.getIconTextGap());
- if (b.isBorderPainted())
- SwingUtilities.calculateInnerArea(b, vr);
- else
- vr = SwingUtilities.getLocalBounds(b);
- String text = SwingUtilities.layoutCompoundLabel(c, g.getFontMetrics(f),
- b.getText(),
- currentIcon(b),
- b.getVerticalAlignment(),
- b.getHorizontalAlignment(),
- b.getVerticalTextPosition(),
- b.getHorizontalTextPosition(),
- vr, ir, tr,
- b.getIconTextGap()
- + defaultTextShiftOffset);
-
- if ((b.getModel().isArmed() && b.getModel().isPressed())
- || b.isSelected())
+ ButtonModel model = b.getModel();
+ if (model.isArmed() && model.isPressed())
paintButtonPressed(g, b);
-
- paintIcon(g, c, ir);
+
+ if (icon != null)
+ paintIcon(g, c, iconR);
if (text != null)
{
View html = (View) b.getClientProperty(BasicHTML.propertyKey);
if (html != null)
- html.paint(g, tr);
+ html.paint(g, textR);
else
- paintText(g, b, tr, text);
+ paintText(g, b, textR, text);
}
if (b.isFocusOwner() && b.isFocusPainted())
- paintFocus(g, b, vr, tr, ir);
+ paintFocus(g, b, viewR, textR, iconR);
}
/**
diff --git a/javax/swing/plaf/basic/BasicGraphicsUtils.java b/javax/swing/plaf/basic/BasicGraphicsUtils.java
index 1e84be932..4c270682d 100644
--- a/javax/swing/plaf/basic/BasicGraphicsUtils.java
+++ b/javax/swing/plaf/basic/BasicGraphicsUtils.java
@@ -748,7 +748,6 @@ public class BasicGraphicsUtils
}
}
-
/**
* Determines the preferred width and height of an AbstractButton,
* given the gap between the button&#x2019;s text and icon.
@@ -769,24 +768,31 @@ public class BasicGraphicsUtils
public static Dimension getPreferredButtonSize(AbstractButton b,
int textIconGap)
{
- Rectangle contentRect;
- Rectangle viewRect;
- Rectangle iconRect = new Rectangle();
- Rectangle textRect = new Rectangle();
- Insets insets = b.getInsets();
-
- viewRect = new Rectangle();
-
- /* 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.
- */
+ // These cached rectangles are use here and in BasicButtonUI.paint(),
+ // so these two methods must never be executed concurrently. Maybe
+ // we must use other Rectangle instances here. OTOH, Swing is
+ // designed to be not thread safe, and every layout and paint operation
+ // should be performed from the EventDispatchThread, so it _should_ be
+ // OK to do this optimization.
+ Rectangle viewRect = BasicButtonUI.viewR;
+ viewRect.x = 0;
+ viewRect.y = 0;
+ viewRect.width = Short.MAX_VALUE;
+ viewRect.height = Short.MAX_VALUE;
+ Rectangle iconRect = BasicButtonUI.iconR;
+ iconRect.x = 0;
+ iconRect.y = 0;
+ iconRect.width = 0;
+ iconRect.height = 0;
+ Rectangle textRect = BasicButtonUI.textR;
+ textRect.x = 0;
+ textRect.y = 0;
+ textRect.width = 0;
+ textRect.height = 0;
+
SwingUtilities.layoutCompoundLabel(
b, // for the component orientation
- b.getToolkit().getFontMetrics(b.getFont()), // see comment above
+ b.getFontMetrics(b.getFont()), // see comment above
b.getText(),
b.getIcon(),
b.getVerticalAlignment(),
@@ -804,13 +810,12 @@ public class BasicGraphicsUtils
* +------------------------+ +------------------------+
*/
- contentRect = textRect.union(iconRect);
-
- return new Dimension(insets.left
- + contentRect.width
- + insets.right + b.getHorizontalAlignment(),
- insets.top
- + contentRect.height
- + insets.bottom);
+ Rectangle contentRect =
+ SwingUtilities.computeUnion(textRect.x, textRect.y, textRect.width,
+ textRect.height, iconRect);
+
+ Insets insets = b.getInsets();
+ return new Dimension(insets.left + contentRect.width + insets.right,
+ insets.top + contentRect.height + insets.bottom);
}
}
diff --git a/javax/swing/plaf/basic/BasicLabelUI.java b/javax/swing/plaf/basic/BasicLabelUI.java
index 304e13ad7..1ec020b1c 100644
--- a/javax/swing/plaf/basic/BasicLabelUI.java
+++ b/javax/swing/plaf/basic/BasicLabelUI.java
@@ -119,13 +119,37 @@ public class BasicLabelUI extends LabelUI implements PropertyChangeListener
{
JLabel lab = (JLabel) c;
Insets insets = lab.getInsets();
- FontMetrics fm = lab.getFontMetrics(lab.getFont());
- layoutCL(lab, fm, lab.getText(), lab.getIcon(), vr, ir, tr);
- Rectangle cr = SwingUtilities.computeUnion(tr.x, tr.y, tr.width, tr.height,
- ir);
- return new Dimension(insets.left + cr.width + insets.right, insets.top
- + cr.height + insets.bottom);
-
+ int insetsX = insets.left + insets.right;
+ int insetsY = insets.top + insets.bottom;
+ Icon icon = lab.getIcon();
+ String text = lab.getText();
+ Dimension ret;
+ if (icon == null && text == null)
+ ret = new Dimension(insetsX, insetsY);
+ else if (icon != null && text == null)
+ ret = new Dimension(icon.getIconWidth() + insetsX,
+ icon.getIconHeight() + insetsY);
+ else
+ {
+ FontMetrics fm = lab.getFontMetrics(lab.getFont());
+ ir.x = 0;
+ ir.y = 0;
+ ir.width = 0;
+ ir.height = 0;
+ tr.x = 0;
+ tr.y = 0;
+ tr.width = 0;
+ tr.height = 0;
+ vr.x = 0;
+ vr.y = 0;
+ vr.width = Short.MAX_VALUE;
+ vr.height = Short.MAX_VALUE;
+ layoutCL(lab, fm, text, icon, vr, ir, tr);
+ Rectangle cr = SwingUtilities.computeUnion(tr.x, tr.y, tr.width,
+ tr.height, ir);
+ ret = new Dimension(cr.width + insetsX, cr.height + insetsY);
+ }
+ return ret;
}
/**
@@ -166,13 +190,20 @@ public class BasicLabelUI extends LabelUI implements PropertyChangeListener
{
JLabel b = (JLabel) c;
FontMetrics fm = g.getFontMetrics();
- vr = SwingUtilities.calculateInnerArea(c, vr);
-
- if (vr.width < 0)
- vr.width = 0;
- if (vr.height < 0)
- vr.height = 0;
+ Insets i = c.getInsets();
+ vr.x = i.left;
+ vr.y = i.right;
+ vr.width = c.getWidth() - i.left + i.right;
+ vr.height = c.getHeight() - i.top + i.bottom;
+ ir.x = 0;
+ ir.y = 0;
+ ir.width = 0;
+ ir.height = 0;
+ tr.x = 0;
+ tr.y = 0;
+ tr.width = 0;
+ tr.height = 0;
Icon icon = (b.isEnabled()) ? b.getIcon() : b.getDisabledIcon();
String text = layoutCL(b, fm, b.getText(), icon, vr, ir, tr);
diff --git a/javax/swing/plaf/basic/BasicMenuItemUI.java b/javax/swing/plaf/basic/BasicMenuItemUI.java
index 6110aca66..85eefb9ee 100644
--- a/javax/swing/plaf/basic/BasicMenuItemUI.java
+++ b/javax/swing/plaf/basic/BasicMenuItemUI.java
@@ -449,6 +449,7 @@ public class BasicMenuItemUI extends MenuItemUI
// Layout the menu item. The result gets stored in the rectangle
// fields of this class.
+ resetRectangles(null);
layoutMenuItem(m, accelText);
// The union of the text and icon areas is the label area.
@@ -700,6 +701,8 @@ public class BasicMenuItemUI extends MenuItemUI
// Layout menu item. The result gets stored in the rectangle fields
// of this class.
+ resetRectangles(m);
+
layoutMenuItem(m, accelText);
// Paint the background.
@@ -1271,6 +1274,33 @@ public class BasicMenuItemUI extends MenuItemUI
}
/**
+ * Resets the cached layout rectangles. If <code>i</code> is not null, then
+ * the view rectangle is set to the inner area of the component, otherwise
+ * it is set to (0, 0, Short.MAX_VALUE, Short.MAX_VALUE), this is needed
+ * for layouting.
+ *
+ * @param i the component for which to initialize the rectangles
+ */
+ private void resetRectangles(JMenuItem i)
+ {
+ // Reset rectangles.
+ iconRect.setBounds(0, 0, 0, 0);
+ textRect.setBounds(0, 0, 0, 0);
+ accelRect.setBounds(0, 0, 0, 0);
+ checkIconRect.setBounds(0, 0, 0, 0);
+ arrowIconRect.setBounds(0, 0, 0, 0);
+ if (i == null)
+ viewRect.setBounds(0, 0, Short.MAX_VALUE, Short.MAX_VALUE);
+ else
+ {
+ Insets insets = i.getInsets();
+ viewRect.setBounds(insets.left, insets.top,
+ i.getWidth() - insets.left - insets.right,
+ i.getHeight() - insets.top - insets.bottom);
+ }
+ }
+
+ /**
* A helper method that lays out the menu item. The layout is stored
* in the fields of this class.
*
@@ -1282,21 +1312,6 @@ public class BasicMenuItemUI extends MenuItemUI
int width = m.getWidth();
int height = m.getHeight();
- // Reset rectangles.
- iconRect.setBounds(0, 0, 0, 0);
- textRect.setBounds(0, 0, 0, 0);
- accelRect.setBounds(0, 0, 0, 0);
- checkIconRect.setBounds(0, 0, 0, 0);
- arrowIconRect.setBounds(0, 0, 0, 0);
- viewRect.setBounds(0, 0, width, height);
-
- // Substract insets to the view rect.
- Insets insets = m.getInsets();
- viewRect.x += insets.left;
- viewRect.y += insets.top;
- viewRect.width -= insets.left + insets.right;
- viewRect.height -= insets.top + insets.bottom;
-
// Fetch the fonts.
Font font = m.getFont();
FontMetrics fm = m.getFontMetrics(font);
diff --git a/javax/swing/plaf/basic/BasicRadioButtonUI.java b/javax/swing/plaf/basic/BasicRadioButtonUI.java
index aed4d69d6..802b25339 100644
--- a/javax/swing/plaf/basic/BasicRadioButtonUI.java
+++ b/javax/swing/plaf/basic/BasicRadioButtonUI.java
@@ -129,9 +129,22 @@ public class BasicRadioButtonUI extends BasicToggleButtonUI
{
AbstractButton b = (AbstractButton) c;
- Rectangle tr = new Rectangle();
- Rectangle ir = new Rectangle();
- Rectangle vr = new Rectangle();
+ Insets i = b.getInsets();
+ Rectangle tr = textR;
+ textR.x = 0;
+ textR.y = 0;
+ textR.width = 0;
+ textR.height = 0;
+ Rectangle ir = iconR;
+ iconR.x = 0;
+ iconR.y = 0;
+ iconR.width = 0;
+ iconR.height = 0;
+ Rectangle vr = viewR;
+ viewR.x = i.left;
+ viewR.y = i.right;
+ viewR.width = b.getWidth() - i.left - i.right;
+ viewR.height = b.getHeight() - i.top - i.bottom;
Font f = c.getFont();
@@ -149,13 +162,12 @@ public class BasicRadioButtonUI extends BasicToggleButtonUI
currentIcon = getDefaultIcon();
}
- SwingUtilities.calculateInnerArea(b, vr);
String text = SwingUtilities.layoutCompoundLabel(c, g.getFontMetrics(f),
b.getText(), currentIcon,
b.getVerticalAlignment(), b.getHorizontalAlignment(),
b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
vr, ir, tr, b.getIconTextGap() + defaultTextShiftOffset);
-
+
currentIcon.paintIcon(c, g, ir.x, ir.y);
if (text != null)
@@ -174,17 +186,25 @@ public class BasicRadioButtonUI extends BasicToggleButtonUI
// The other icon properties are ignored.
AbstractButton b = (AbstractButton) c;
- Rectangle contentRect;
- Rectangle viewRect;
- Rectangle iconRect = new Rectangle();
- Rectangle textRect = new Rectangle();
Insets insets = b.getInsets();
-
+
+ String text = b.getText();
Icon i = b.getIcon();
if (i == null)
i = getDefaultIcon();
- viewRect = new Rectangle();
+ textR.x = 0;
+ textR.y = 0;
+ textR.width = 0;
+ textR.height = 0;
+ iconR.x = 0;
+ iconR.y = 0;
+ iconR.width = 0;
+ iconR.height = 0;
+ viewR.x = 0;
+ viewR.y = 0;
+ viewR.width = Short.MAX_VALUE;
+ viewR.height = Short.MAX_VALUE;
SwingUtilities.layoutCompoundLabel(
b, // for the component orientation
@@ -195,17 +215,14 @@ public class BasicRadioButtonUI extends BasicToggleButtonUI
b.getHorizontalAlignment(),
b.getVerticalTextPosition(),
b.getHorizontalTextPosition(),
- viewRect, iconRect, textRect,
+ viewR, iconR, textR,
defaultTextIconGap + defaultTextShiftOffset);
- contentRect = textRect.union(iconRect);
-
- return new Dimension(insets.left
- + contentRect.width
- + insets.right + b.getHorizontalAlignment(),
- insets.top
- + contentRect.height
- + insets.bottom);
+ Rectangle r = SwingUtilities.computeUnion(textR.x, textR.y, textR.width,
+ textR.height, iconR);
+
+ return new Dimension(insets.left + r.width + insets.right,
+ insets.top + r.height + insets.bottom);
}
/**