classpath-patches
[Top][All Lists]
Advanced

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

[cp-patches] FYI: MetalComboBoxUI


From: David Gilbert
Subject: [cp-patches] FYI: MetalComboBoxUI
Date: Mon, 19 Sep 2005 10:18:09 +0000
User-agent: Mozilla Thunderbird 1.0.6 (X11/20050728)

I committed this patch which implements most of the missing methods in MetalComboBoxUI. A few changes were needed in BasicComboBoxUI, and there are still some issues to fix, but it mostly works (well enough for others to hack on anyway). I'll continue to work on this, as time permits, during the next week or two:

2005-09-19  David Gilbert  <address@hidden>

        * javax/swing/plaf/basic/BasicComboBoxUI.java
        (installComponents): call configureArrowButton after creating button,
        (configureEditor): set the selected item,
        (configureArrowButton): set a zero margin,
        (getPreferredSize): delegate to getMinimumSize(),
        (getMinimumSize): now uses code that was in getPreferredSize(),
        (getDefaultSize): reduce default height,
        (ComboBoxLayoutManager.layoutComponent): use comboBox height as button
        width,
        (PropertyChangeHandler.propertyChange): set font on arrow button,
        * javax/swing/plaf/metal/MetalComboBoxUI.java
        (instances): deleted field,
        (MetalComboBoxLayoutManager): new class,
        (MetalPropertyChangeListener): new class,
        (MetalComboPopup): new class,
        (createUI): just return new instance,
        (createEditor): implemented,
        (createPopup): implemented,
        (createArrowButton): implemented,
        (createPropertyChangeListener): implemented,
        (paint): implemented,
        (editablePropertyChanged): implemented,
        (createLayoutManager): implemented,
        (removeListeners): implemented,
        (getMinimumSize): implemented.

Regards,

Dave
Index: javax/swing/plaf/basic/BasicComboBoxUI.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicComboBoxUI.java,v
retrieving revision 1.17
diff -u -r1.17 BasicComboBoxUI.java
--- javax/swing/plaf/basic/BasicComboBoxUI.java 18 Sep 2005 18:16:52 -0000      
1.17
+++ javax/swing/plaf/basic/BasicComboBoxUI.java 19 Sep 2005 07:52:45 -0000
@@ -452,7 +452,7 @@
   {
     // create and install arrow button
     arrowButton = createArrowButton();
-
+    configureArrowButton();
     comboBox.add(arrowButton);
 
     // Set list that will be used by BasicComboBoxRender 
@@ -516,6 +516,7 @@
   protected void configureEditor()
   {
     editor.setFont(comboBox.getFont());
+    comboBox.getEditor().setItem(comboBox.getSelectedItem());
     // FIXME: Need to implement. Set font and add listeners.
   }
 
@@ -536,6 +537,7 @@
   {
     arrowButton.setEnabled(comboBox.isEnabled());
     arrowButton.setFont(comboBox.getFont());
+    arrowButton.setMargin(new Insets(0, 0, 0, 0));
   }
 
   /**
@@ -625,11 +627,7 @@
    */
   public Dimension getPreferredSize(JComponent c)
   {
-    Dimension d = getDisplaySize();
-    Dimension arrowDim = arrowButton.getPreferredSize();
-    Dimension result = new Dimension(d.width + arrowDim.width, 
-            Math.max(d.height, arrowDim.height));
-    return result;
+    return getMinimumSize(c);
   }
 
   /**
@@ -642,7 +640,11 @@
    */
   public Dimension getMinimumSize(JComponent c)
   {
-    return getPreferredSize(c);
+    Dimension d = getDisplaySize();
+    Dimension arrowDim = arrowButton.getPreferredSize();
+    Dimension result = new Dimension(d.width + arrowDim.width, 
+            Math.max(d.height, arrowDim.height));
+    return result;
   }
 
   /** The value returned by the getMaximumSize() method. */
@@ -810,7 +812,7 @@
   protected Dimension getDefaultSize()
   {
     // FIXME: Not implemented properly.
-    return new Dimension(100, 20);
+    return new Dimension(100, 5);
   }
 
   /**
@@ -852,9 +854,7 @@
           size.width = compSize.width;
         if (compSize.height > size.height)
           size.height = compSize.height;
-        
       }
-
     displaySize = size;
     return displaySize;
   }
@@ -952,14 +952,13 @@
     {
       // Position editor component to the left of arrow button if combo box is 
       // editable
-      Dimension arrowPrefSize = arrowButton.getPreferredSize();
-      int editorWidth = comboBox.getBounds().width - arrowPrefSize.width;
+      int arrowSize = comboBox.getHeight();
+      int editorWidth = comboBox.getBounds().width - arrowSize;
 
       if (comboBox.isEditable())
         editor.setBounds(0, 0, editorWidth, comboBox.getBounds().height);
       
-      arrowButton.setBounds(editorWidth, 0, arrowPrefSize.width,
-                            comboBox.getBounds().height);
+      arrowButton.setBounds(editorWidth, 0, arrowSize, arrowSize);
       comboBox.revalidate();
     }
   }
@@ -1163,6 +1162,7 @@
           Font font = (Font) e.getNewValue();
           editor.setFont(font);
           listBox.setFont(font);
+          arrowButton.setFont(font);
           comboBox.revalidate();
           comboBox.repaint();
         }
Index: javax/swing/plaf/metal/MetalComboBoxUI.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/javax/swing/plaf/metal/MetalComboBoxUI.java,v
retrieving revision 1.3
diff -u -r1.3 MetalComboBoxUI.java
--- javax/swing/plaf/metal/MetalComboBoxUI.java 2 Jul 2005 20:32:50 -0000       
1.3
+++ javax/swing/plaf/metal/MetalComboBoxUI.java 19 Sep 2005 07:52:46 -0000
@@ -38,19 +38,130 @@
 
 package javax.swing.plaf.metal;
 
-import java.util.HashMap;
+import java.awt.Container;
+import java.awt.Dimension;
+import java.awt.Graphics;
+import java.awt.LayoutManager;
+import java.awt.Rectangle;
+import java.awt.event.MouseEvent;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
 
+import javax.swing.CellRendererPane;
+import javax.swing.ComboBoxEditor;
+import javax.swing.JButton;
+import javax.swing.JComboBox;
 import javax.swing.JComponent;
 import javax.swing.plaf.ComponentUI;
 import javax.swing.plaf.basic.BasicComboBoxUI;
+import javax.swing.plaf.basic.BasicComboPopup;
+import javax.swing.plaf.basic.ComboPopup;
 
+
+/**
+ * A UI delegate for the address@hidden JComboBox} component.
+ */
 public class MetalComboBoxUI
   extends BasicComboBoxUI
 {
+  /**
+   * A layout manager that arranges the editor component (if active) and the
+   * button that make up the combo box.
+   */
+  public class MetalComboBoxLayoutManager
+    extends BasicComboBoxUI.ComboBoxLayoutManager
+  {
+    /**
+     * Creates a new instance of the layout manager.
+     */
+    public MetalComboBoxLayoutManager()
+    {      
+    }
+    
+    /**
+     * Arranges the editor (if visible) and button that comprise the combo
+     * box.
+     * 
+     * @param parent  the parent.
+     */
+    public void layoutContainer(Container parent)
+    {
+      JComboBox cb = (JComboBox) parent;
+      if (!cb.isEditable())
+        {
+          Rectangle bounds = parent.getBounds();
+          arrowButton.setBounds(0, 0, bounds.width, bounds.height);
+        }
+      else 
+        superLayout(parent);
+    }
+    
+    /**
+     * Calls the <code>layoutContainer(Container)</code> method in the super 
+     * class.
+     * 
+     * @param parent  the container.
+     */
+    public void superLayout(Container parent)
+    {
+      super.layoutContainer(parent);
+    }
+  }
+  
+  /**
+   * A listener used to handle property changes in the address@hidden 
JComboBox} 
+   * component, to ensure that the UI delegate accurately reflects the current
+   * state in the rendering onscreen.
+   */
+  public class MetalPropertyChangeListener
+    extends BasicComboBoxUI.PropertyChangeHandler
+  {
+    /**
+     * Creates a new listener.
+     */
+    public MetalPropertyChangeListener()
+    {
+    }
+    
+    /**
+     * Handles a property change event, updating the UI components as
+     * appropriate.
+     * 
+     * @param e  the event.
+     */
+    public void propertyChange(PropertyChangeEvent e)
+    {
+      if (e.getPropertyName().equals("editable"))
+        editablePropertyChanged(e);
+      super.propertyChange(e);
+    }
+  }
 
-  /** The UI instances for JComboBoxes. */
-  private static HashMap instances = null;
-
+  /**
+   * A popup menu for the combo-box.
+   * 
+   * @see #createPopup()
+   *
+   * @deprecated 1.4
+   */
+  public class MetalComboPopup extends BasicComboPopup
+  {
+    /**
+     * Creates a new popup.
+     * 
+     * @param cBox  the combo box.
+     */
+    public MetalComboPopup(JComboBox cBox)
+    {
+      super(cBox); 
+    }
+    
+    public void delegateFocus(MouseEvent e)
+    {
+      super.delegateFocus(e);
+    }
+  }
+  
   /**
    * Constructs a new instance of MetalComboBoxUI.
    */
@@ -68,19 +179,135 @@
    */
   public static ComponentUI createUI(JComponent component)
   {
-    if (instances == null)
-      instances = new HashMap();
-
-    Object o = instances.get(component);
-    MetalComboBoxUI instance;
-    if (o == null)
+    return new MetalComboBoxUI();
+  }
+  
+  /**
+   * Creates an editor for the combo box.
+   * 
+   * @return An editor.
+   */
+  protected ComboBoxEditor createEditor()
+  {
+    return new MetalComboBoxEditor.UIResource();   
+  }
+  
+  /**
+   * Creates a popup for the combo box.
+   * 
+   * @return A popup.
+   */
+  protected ComboPopup createPopup()
+  {
+    return new MetalComboPopup(comboBox);
+  }
+  
+  /**
+   * Creates a new button for use in rendering the JComboBox.
+   * 
+   * @return A button.
+   */
+  protected JButton createArrowButton()
+  {
+    return new MetalComboBoxButton(comboBox, new MetalComboBoxIcon(), 
+            new CellRendererPane(), listBox);  
+  }
+  
+  /**
+   * Creates a new property change listener.
+   * 
+   * @return A new property change listener.
+   */
+  public PropertyChangeListener createPropertyChangeListener()
+  {
+    return new MetalPropertyChangeListener();
+  }
+  
+  public void paint(Graphics g, JComponent c)
+  {
+    // do nothing, the button and text field are painted elsewhere
+  }
+  
+  /**
+   * Updates the button and text field to reflect a change in the 'editable'
+   * property.
+   * 
+   * @param e  the event.
+   * 
+   * @deprecated 1.4
+   */
+  protected void editablePropertyChanged(PropertyChangeEvent e)
+  {
+    if (arrowButton instanceof MetalComboBoxButton)
+      {
+        MetalComboBoxButton b = (MetalComboBoxButton) arrowButton;
+        b.setIconOnly(comboBox.isEditable());
+      }
+    if (comboBox.isEditable())
       {
-       instance = new MetalComboBoxUI();
-       instances.put(component, instance);
+        arrowButton.setText(null);
+        if (editor != null)
+          editor.setVisible(true);
       }
     else
-      instance = (MetalComboBoxUI) o;
-
-    return instance;
+      {
+        arrowButton.setText(comboBox.getSelectedItem().toString());
+        if (editor != null)
+          editor.setVisible(true);
+      }
+  }
+  
+  /**
+   * Creates a new layout manager for the UI delegate.
+   * 
+   * @return A new layout manager.
+   */
+  protected LayoutManager createLayoutManager()
+  {
+    return new MetalComboBoxLayoutManager();
+  }
+  
+  /**
+   * Not used in Classpath.
+   * 
+   * @deprecated 1.4
+   */
+  protected void removeListeners()
+  {
+    // no longer used in JDK 1.4 
+  }
+  
+  /**
+   * The API spec says this method is implementation specific and should be
+   * treated as though it were private (i.e. don't call this method).
+   */
+  public void configureEditor()
+  {
+    super.configureEditor();
+  }
+  
+  /**
+   * The API spec says this method is implementation specific and should be
+   * treated as though it were private (i.e. don't call this method).
+   */
+  public void unconfigureEditor()
+  {
+    super.unconfigureEditor();
+  }
+  
+  /**
+   * Returns the minimum size for the combo.
+   * 
+   * @param c  the component
+   * 
+   * @return The minimum size for the combo box.
+   */
+  public Dimension getMinimumSize(JComponent c)
+  {
+    // FIXME: this needs work
+    Dimension result = super.getMinimumSize(c);
+    result.height = result.height + 9;
+    return result;   
   }
+  
 }

reply via email to

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