taoujouti / macwidgets

Automatically exported from code.google.com/p/macwidgets
0 stars 0 forks source link

HUDComboBoxUI ignores Renderer #69

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I'm creating a JCombobox and setting the HUDComboboxUI as it's UI. I then
set the renderer on the combobox. However, the renderer seems to never be
called.

Original issue reported on code.google.com by t.s.mae...@gmail.com on 18 Mar 2009 at 2:29

GoogleCodeExporter commented 9 years ago
This is somewhat intentional at the moment, as the most straight forward thing 
to do with this UI delegate was 
to force a single, simple renderer. I would like to add the capability your 
asking for, though (your the second to 
request it).

Could you provide me what it is your rendering in the HUD style combo box?

Original comment by kenneth....@gmail.com on 20 Mar 2009 at 11:08

GoogleCodeExporter commented 9 years ago
I'm just using my domain model objects as the model of the combo-box (a whole 
object
tree), since these are really what I'm selecting. Implementing toString() is not
really an option, since the objects in the model may come from a library. 

Original comment by t.s.mae...@gmail.com on 20 Mar 2009 at 12:32

GoogleCodeExporter commented 9 years ago
I've created a patch for this. If the renderer is a JLabel it extracts the text 
and icon from it and uses that to 
render.

Original comment by laurie.c...@gmail.com on 14 Dec 2009 at 11:58

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks Laurie -- I'll take a look at this.

Original comment by kenneth....@gmail.com on 15 Dec 2009 at 12:54

GoogleCodeExporter commented 9 years ago
In addition to the patch, it turns out to be wise to put null guards on the 
item to render, and the combo box's 
selected item in both those classes.

Original comment by laurie.c...@gmail.com on 15 Dec 2009 at 11:00

GoogleCodeExporter commented 9 years ago
Also, the size calculation in HUDComboBoxUI changes to this:

    /**
     * Calculates the display width in pixels of the given object.
     */
    private int getDisplayWidth(Object object) {
        assert object != null : "The given object cannot be null";
        // TODO refactor this logic into utility class that looks for TextProvider.
        FontMetrics fontMetrics = comboBox.getFontMetrics(comboBox.getFont());
        ListCellRenderer r = comboBox.getRenderer();
        final Component c = r == null ? null : r.getListCellRendererComponent(fDummyList, object, 0, false, false);
        if (c instanceof JLabel) {
            final JLabel l = (JLabel) c;
            int iconwidth = l.getIcon() == null ? 0 : l.getIcon().getIconWidth() + l.getIconTextGap();
            // 10 just seems to look ok.
            return fontMetrics.stringWidth(l.getText()) + iconwidth + 10;
        }
        else {
            return fontMetrics.stringWidth(object.toString());
        }
    }

Original comment by laurie.c...@gmail.com on 17 Dec 2009 at 3:15