diff options
Diffstat (limited to 'libjava/classpath/javax/swing/plaf/basic/BasicComboBoxRenderer.java')
-rw-r--r-- | libjava/classpath/javax/swing/plaf/basic/BasicComboBoxRenderer.java | 72 |
1 files changed, 46 insertions, 26 deletions
diff --git a/libjava/classpath/javax/swing/plaf/basic/BasicComboBoxRenderer.java b/libjava/classpath/javax/swing/plaf/basic/BasicComboBoxRenderer.java index e4fbb8352a9..8115605b77a 100644 --- a/libjava/classpath/javax/swing/plaf/basic/BasicComboBoxRenderer.java +++ b/libjava/classpath/javax/swing/plaf/basic/BasicComboBoxRenderer.java @@ -1,5 +1,5 @@ /* BasicComboBoxRenderer.java -- - Copyright (C) 2004 Free Software Foundation, Inc. + Copyright (C) 2004, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -40,36 +40,39 @@ package javax.swing.plaf.basic; import java.awt.Component; import java.awt.Dimension; +import java.awt.FontMetrics; import java.io.Serializable; +import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.ListCellRenderer; import javax.swing.SwingConstants; -import javax.swing.UIDefaults; -import javax.swing.UIManager; +import javax.swing.SwingUtilities; import javax.swing.border.Border; import javax.swing.border.EmptyBorder; /** - * This class is renderer for the combo box. + * A renderer for a {@link JComboBox}. * * @author Olga Rodimina */ -public class BasicComboBoxRenderer extends JLabel implements ListCellRenderer, - Serializable +public class BasicComboBoxRenderer + extends JLabel + implements ListCellRenderer, Serializable { /** - * This border is used whenever renderer doesn't have a focus. + * A shared border instance for all renderers. */ protected static Border noFocusBorder = new EmptyBorder(0, 0, 0, 0); /** - * Creates a new BasicComboBoxRenderer object. + * Creates a new <code>BasicComboBoxRenderer</code> object. */ public BasicComboBoxRenderer() { setHorizontalAlignment(SwingConstants.LEFT); + setBorder(noFocusBorder); } /** @@ -83,7 +86,8 @@ public class BasicComboBoxRenderer extends JLabel implements ListCellRenderer, } /** - * getListCellRendererComponent + * Returns a component that has been configured to display the given + * <code>value</code>. * * @param list List of items for which to the background and foreground * colors @@ -100,43 +104,59 @@ public class BasicComboBoxRenderer extends JLabel implements ListCellRenderer, boolean cellHasFocus) { String s = value.toString(); - setText(s); + + // String maybe larger than comboBox. + FontMetrics fm = getToolkit().getFontMetrics(list.getFont()); + int strWidth = SwingUtilities.computeStringWidth(fm, s); + int cbWidth = getSize().width; + if (cbWidth != 0 && strWidth > cbWidth) + { + char[] str = s.toCharArray(); + int currWidth = 0; + int i = 0; + String postStr = "... "; + cbWidth -= SwingUtilities.computeStringWidth(fm, postStr); + while (i < str.length && currWidth < cbWidth) + { + ++i; + currWidth = SwingUtilities.computeStringWidth(fm, new String(str, 0, i)); + } + setText(new String(str, 0, i) + postStr); + } + else + setText(s); + setOpaque(true); - UIDefaults defaults = UIManager.getLookAndFeelDefaults(); - - if (isSelected) + if (isSelected || cellHasFocus) { - setBackground(list.getSelectionBackground()); - setForeground(list.getSelectionForeground()); + setBackground(list.getSelectionBackground()); + setForeground(list.getSelectionForeground()); } else { - setBackground(list.getBackground()); - setForeground(list.getForeground()); + setBackground(list.getBackground()); + setForeground(list.getForeground()); } setEnabled(list.isEnabled()); setFont(list.getFont()); - - // Use focusCellHighlightBorder when renderer has focus and - // noFocusBorder otherwise - if (cellHasFocus) - setBorder(UIManager.getBorder("List.focusCellHighlightBorder")); - else - setBorder(noFocusBorder); - return this; } + /** + * A subclass of {@link BasicComboBoxRenderer} that implements the + * {@link javax.swing.plaf.UIResource} interface. + */ public static class UIResource extends BasicComboBoxRenderer implements javax.swing.plaf.UIResource { /** - * Creates a new UIResource object. + * Creates a new <code>UIResource</code> object. */ public UIResource() { + // Nothing to do here. } } } |