classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] FYI: BasicComboBoxUI fixes


From: David Gilbert
Subject: [cp-patches] FYI: BasicComboBoxUI fixes
Date: Mon, 07 Nov 2005 17:08:54 +0000
User-agent: Mozilla Thunderbird 1.0.7 (X11/20051026)

This patch fixes some layout problems for editable JComboBoxes:

2005-11-07  David Gilbert  <address@hidden>

        * javax/swing/plaf/basic/BasicComboBoxUI.java
        (installComponents): update local reference to editor component always,
        (getDisplaySize): implement new calculation for editable combo boxes,
        * javax/swing/plaf/metal/MetalComboBoxEditor.java
        (editorBorderInsets): initialise with correct value,
        * javax/swing/plaf/metal/MetalComboBoxUI.java
        (getMinimumSize): implemented different calculation for editable combo
        boxes.

Regards,

Dave
Index: javax/swing/plaf/basic/BasicComboBoxUI.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicComboBoxUI.java,v
retrieving revision 1.26
diff -u -r1.26 BasicComboBoxUI.java
--- javax/swing/plaf/basic/BasicComboBoxUI.java 19 Oct 2005 14:54:55 -0000      
1.26
+++ javax/swing/plaf/basic/BasicComboBoxUI.java 7 Nov 2005 16:48:42 -0000
@@ -149,8 +149,11 @@
    * Popup list containing the combo box's menu items.
    */
   protected ComboPopup popup;
+  
   protected KeyListener popupKeyListener;
+  
   protected MouseListener popupMouseListener;
+  
   protected MouseMotionListener popupMouseMotionListener;
 
   /**
@@ -480,9 +483,10 @@
     ComboBoxEditor currentEditor = comboBox.getEditor();
     if (currentEditor == null || currentEditor instanceof UIResource)
       {
-        comboBox.setEditor(createEditor());
-        editor = comboBox.getEditor().getEditorComponent();
-      }
+        currentEditor = createEditor();
+        comboBox.setEditor(currentEditor);
+      } 
+    editor = currentEditor.getEditorComponent();
 
     comboBox.revalidate();
   }
@@ -851,57 +855,75 @@
   }
 
   /**
-   * Returns size of the largest item in the combo box. This size will be the
-   * size of the combo box, not including the arrowButton.
+   * Returns the size of the display area for the combo box. This size will be 
+   * the size of the combo box, not including the arrowButton.
    *
-   * @return dimensions of the largest item in the combo box.
+   * @return The size of the display area for the combo box.
    */
   protected Dimension getDisplaySize()
   {
-    Object prototype = comboBox.getPrototypeDisplayValue();
-    if (prototype != null)
-      {
-        // calculate result based on prototype
-        ListCellRenderer renderer = comboBox.getRenderer();
-        Component comp = renderer.getListCellRendererComponent(listBox, 
-                prototype, -1, false, false);
-        Dimension compSize = comp.getPreferredSize();
-        compSize.width += 2;  // add 1 pixel margin around area
-        compSize.height += 2;
-        return compSize;
-      }
-    else
+    if (!comboBox.isEditable()) 
       {
-        ComboBoxModel model = comboBox.getModel();
-        int numItems = model.getSize();
-
-        // if combo box doesn't have any items then simply
-        // return its default size
-        if (numItems == 0)
+        Object prototype = comboBox.getPrototypeDisplayValue();
+        if (prototype != null)
           {
-            displaySize = getDefaultSize();
-            return displaySize;
+            // calculate result based on prototype
+            ListCellRenderer renderer = comboBox.getRenderer();
+            Component comp = renderer.getListCellRendererComponent(listBox, 
+                prototype, -1, false, false);
+            Dimension compSize = comp.getPreferredSize();
+            compSize.width += 2;  // add 1 pixel margin around area
+            compSize.height += 2;
+            return compSize;
           }
-
-        Dimension size = new Dimension(0, 0);
-
-        // ComboBox's display size should be equal to the 
-        // size of the largest item in the combo box. 
-        ListCellRenderer renderer = comboBox.getRenderer();
-
-        for (int i = 0; i < numItems; i++)
+        else
           {
-            Object item = model.getElementAt(i);
-            Component comp = renderer.getListCellRendererComponent(listBox, 
+            ComboBoxModel model = comboBox.getModel();
+            int numItems = model.getSize();
+
+            // if combo box doesn't have any items then simply
+            // return its default size
+            if (numItems == 0)
+              {
+                displaySize = getDefaultSize();
+                return displaySize;
+              }
+
+            Dimension size = new Dimension(0, 0);
+
+            // ComboBox's display size should be equal to the 
+            // size of the largest item in the combo box. 
+            ListCellRenderer renderer = comboBox.getRenderer();
+
+            for (int i = 0; i < numItems; i++)
+              {
+                Object item = model.getElementAt(i);
+                Component comp = 
renderer.getListCellRendererComponent(listBox, 
                     item, -1, false, false);
 
-            Dimension compSize = comp.getPreferredSize();
-            if (compSize.width + 2 > size.width)
-              size.width = compSize.width + 2;
-            if (compSize.height + 2 > size.height)
-              size.height = compSize.height + 2;
+                Dimension compSize = comp.getPreferredSize();
+                if (compSize.width + 2 > size.width)
+                  size.width = compSize.width + 2;
+                if (compSize.height + 2 > size.height)
+                  size.height = compSize.height + 2;
+              }
+            displaySize = size;
+            return displaySize;
+          }
+      }
+    else // an editable combo,  
+      {
+        Component comp = comboBox.getEditor().getEditorComponent();
+        Dimension prefSize = comp.getPreferredSize();
+        int width = prefSize.width;
+        int height = prefSize.height + 2;
+        Object prototype = comboBox.getPrototypeDisplayValue();
+        if (prototype != null)
+          {
+            FontMetrics fm = comboBox.getFontMetrics(comboBox.getFont());
+            width = Math.max(width, fm.stringWidth(prototype.toString()) + 2);
           }
-        displaySize = size;
+        displaySize = new Dimension(width, height);
         return displaySize;
       }
   }
Index: javax/swing/plaf/metal/MetalComboBoxEditor.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/javax/swing/plaf/metal/MetalComboBoxEditor.java,v
retrieving revision 1.4
diff -u -r1.4 MetalComboBoxEditor.java
--- javax/swing/plaf/metal/MetalComboBoxEditor.java     7 Nov 2005 12:09:44 
-0000       1.4
+++ javax/swing/plaf/metal/MetalComboBoxEditor.java     7 Nov 2005 16:48:43 
-0000
@@ -129,7 +129,7 @@
   }
   
   /** The editor's border insets. */
-  protected static Insets editorBorderInsets = new Insets(2, 2, 2, 2);
+  protected static Insets editorBorderInsets = new Insets(4, 2, 4, 0);
   
   /**
    * Creates a new editor.
Index: javax/swing/plaf/metal/MetalComboBoxUI.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/javax/swing/plaf/metal/MetalComboBoxUI.java,v
retrieving revision 1.7
diff -u -r1.7 MetalComboBoxUI.java
--- javax/swing/plaf/metal/MetalComboBoxUI.java 19 Oct 2005 14:54:55 -0000      
1.7
+++ javax/swing/plaf/metal/MetalComboBoxUI.java 7 Nov 2005 16:48:43 -0000
@@ -295,15 +295,22 @@
    */
   public Dimension getMinimumSize(JComponent c)
   {
+    Dimension d = getDisplaySize();
     MetalComboBoxButton b = (MetalComboBoxButton) arrowButton;
-    Icon icon = b.getComboIcon();
     Insets insets = b.getInsets();
-    Dimension d = getDisplaySize();
     int insetsH = insets.top + insets.bottom;
     int insetsW = insets.left + insets.right;
-    int iconWidth = icon.getIconWidth() + 6;
-    return new Dimension(d.width + insetsW + iconWidth, 
-            d.height + insetsH);
+    if (!comboBox.isEditable())
+      {
+        Icon icon = b.getComboIcon();
+        int iconWidth = icon.getIconWidth() + 6;
+        return new Dimension(d.width + insetsW + iconWidth, d.height + 
insetsH);
+      }
+    else
+      // FIXME: the following dimensions pass most of the Mauve tests, but
+      // I don't yet understand the logic behind this...it is probably wrong
+      return new Dimension(d.width + insetsW + (d.height + insetsH) - 4, 
+          d.height + insetsH + 1);
   }
   
 }

reply via email to

[Prev in Thread] Current Thread [Next in Thread]