Another nice addition: This patch adds indentation of the labels in the combobox that is used to choose directories at the top. I implemented this by installing an IndentIcon in the cell renderer that takes another icon (the folder icon) and adds indentation to it.

This fixes a bug and should therefore go into the release branch.

2006-08-02  Roman Kennke  <[EMAIL PROTECTED]>

        PR 27606
        * javax/swing/plaf/basic/BasicListUI.java
        (paintCell): Pass row index to cell renderer.
        * javax/swing/plaf/basic/MetalFileChooserUI.java
        (DirectoryComboBoxRenderer.indentIcon): New field.
        (DirectoryComboBoxRenderer.DirectoryComboBoxRenderer):
        Initialize indentIcon.
        (DirectoryComboBoxRenderer.getListCellRendererComponent):
        Fall back to super and removed standard functionality.
        Handle indentation.
        (IndentIcon): New class. Wraps and indents another icon.

/Roman
Index: javax/swing/plaf/metal/MetalFileChooserUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/metal/MetalFileChooserUI.java,v
retrieving revision 1.25
diff -u -1 -2 -r1.25 MetalFileChooserUI.java
--- javax/swing/plaf/metal/MetalFileChooserUI.java	2 Aug 2006 11:31:43 -0000	1.25
+++ javax/swing/plaf/metal/MetalFileChooserUI.java	2 Aug 2006 21:45:25 -0000
@@ -33,24 +33,25 @@
 or based on this library.  If you modify this library, you may extend
 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.metal;
 
 import java.awt.BorderLayout;
 import java.awt.Component;
 import java.awt.Container;
 import java.awt.Dimension;
+import java.awt.Graphics;
 import java.awt.GridLayout;
 import java.awt.Insets;
 import java.awt.LayoutManager;
 import java.awt.Rectangle;
 import java.awt.Window;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
@@ -559,68 +560,130 @@
       JFileChooser fc = getFileChooser();
       fc.setCurrentDirectory((File) directoryModel.getSelectedItem());
     }
   }
 
   /**
    * A renderer for the items in the directory combo box.
    */
   class DirectoryComboBoxRenderer
     extends DefaultListCellRenderer
   {
     /**
+     * This is the icon that is displayed in the combobox. This wraps
+     * the standard icon and adds indendation.
+     */
+    private IndentIcon indentIcon;
+
+    /**
      * Creates a new renderer.
      */
     public DirectoryComboBoxRenderer(JFileChooser fc)
-    { 
+    {
+      indentIcon = new IndentIcon();
     }
     
     /**
      * Returns a component that can be used to paint the given value within 
      * the list.
      * 
      * @param list  the list.
      * @param value  the value (a [EMAIL PROTECTED] File}).
      * @param index  the item index.
      * @param isSelected  is the item selected?
      * @param cellHasFocus  does the list cell have focus?
      * 
      * @return The list cell renderer.
      */
     public Component getListCellRendererComponent(JList list, Object value,
-        int index, boolean isSelected, boolean cellHasFocus)
+                                                  int index,
+                                                  boolean isSelected,
+                                                  boolean cellHasFocus)
     {
-      FileView fileView = getFileView(getFileChooser());
+      super.getListCellRendererComponent(list, value, index, isSelected,
+                                         cellHasFocus);
       File file = (File) value;
-      setIcon(fileView.getIcon(file));
       setText(getFileChooser().getName(file));
 
-      if (isSelected)
-        {
-          setBackground(list.getSelectionBackground());
-          setForeground(list.getSelectionForeground());
-        }
-      else
-        {
-          setBackground(list.getBackground());
-          setForeground(list.getForeground());
-        }
+      // Install indented icon.
+      Icon icon = getFileChooser().getIcon(file);
+      indentIcon.setIcon(icon);
+      int depth = directoryModel.getDepth(index);
+      indentIcon.setDepth(depth);
+      setIcon(indentIcon);
 
-      setEnabled(list.isEnabled());
-      setFont(list.getFont());
       return this;
     }
   }
 
   /**
+   * An icon that wraps another icon and adds indentation.
+   */
+  class IndentIcon
+    implements Icon
+  {
+
+    /**
+     * The indentation level.
+     */
+    private static final int INDENT = 10;
+
+    /**
+     * The wrapped icon.
+     */
+    private Icon icon;
+
+    /**
+     * The current depth.
+     */
+    private int depth;
+
+    /**
+     * Sets the icon to be wrapped.
+     *
+     * @param i the icon
+     */
+    void setIcon(Icon i)
+    {
+      icon = i;
+    }
+
+    /**
+     * Sets the indentation depth.
+     *
+     * @param d the depth to set
+     */
+    void setDepth(int d)
+    {
+      depth = d;
+    }
+
+    public int getIconHeight()
+    {
+      return icon.getIconHeight();
+    }
+
+    public int getIconWidth()
+    {
+      return icon.getIconWidth() + depth * INDENT;
+    }
+
+    public void paintIcon(Component c, Graphics g, int x, int y)
+    {
+      icon.paintIcon(c, g, x + depth * INDENT, y);
+    }
+      
+  }
+
+  /**
    * A renderer for the files and directories in the file chooser.
    */
   protected class FileRenderer
     extends DefaultListCellRenderer
   {
     
     /**
      * Creates a new renderer.
      */
     protected FileRenderer()
     {
       // Nothing to do here.
Index: javax/swing/plaf/basic/BasicListUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicListUI.java,v
retrieving revision 1.59
diff -u -1 -2 -r1.59 BasicListUI.java
--- javax/swing/plaf/basic/BasicListUI.java	26 Jun 2006 14:05:48 -0000	1.59
+++ javax/swing/plaf/basic/BasicListUI.java	2 Aug 2006 21:45:25 -0000
@@ -1201,25 +1201,25 @@
    * @param data The data to provide to the cell renderer
    * @param sel A selection model to provide to the cell renderer
    * @param lead The lead selection index of the list
    */
   protected void paintCell(Graphics g, int row, Rectangle bounds,
                  ListCellRenderer rend, ListModel data,
                  ListSelectionModel sel, int lead)
   {
     boolean isSel = list.isSelectedIndex(row);
     boolean hasFocus = (list.getLeadSelectionIndex() == row) && BasicListUI.this.list.hasFocus();
     Component comp = rend.getListCellRendererComponent(list,
                                                        data.getElementAt(row),
-                                                       0, isSel, hasFocus);
+                                                       row, isSel, hasFocus);
     rendererPane.paintComponent(g, comp, list, bounds);
   }
 
   /**
    * Paints the list by repeatedly calling [EMAIL PROTECTED] #paintCell} for each visible
    * cell in the list.
    *
    * @param g The graphics context to paint with
    * @param c Ignored; uses the saved [EMAIL PROTECTED] JList} reference 
    */
   public void paint(Graphics g, JComponent c)
   {

Reply via email to