Changeset 5558 in josm for trunk


Ignore:
Timestamp:
2012-11-03T21:55:13+01:00 (11 years ago)
Author:
Don-vip
Message:

fix #8168 - preset combo boxes - text is cut off

Location:
trunk/src/org/openstreetmap/josm/gui
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java

    r5543 r5558  
    757757                    boolean cellHasFocus) {
    758758                PresetListEntry item = (PresetListEntry) value;
    759 
     759               
    760760                // Only return cached size, item is not shown
    761761                if (!list.isShowing() && item.prefferedWidth != -1 && item.prefferedHeight != -1) {
     
    836836            combo.setRenderer(getListCellRenderer());
    837837            combo.setEditable(editable);
    838             //combo.setMaximumRowCount(13);
     838            combo.reinitialize(lhm.values());
    839839            AutoCompletingTextField tf = new AutoCompletingTextField();
    840840            initAutoCompletionField(tf, key);
  • trunk/src/org/openstreetmap/josm/gui/widgets/JosmComboBox.java

    r5451 r5558  
    22package org.openstreetmap.josm.gui.widgets;
    33
     4import java.awt.Component;
     5import java.awt.Dimension;
    46import java.awt.Toolkit;
     7import java.util.ArrayList;
     8import java.util.Arrays;
     9import java.util.Collection;
    510import java.util.Vector;
    611
     
    7075    public JosmComboBox(ComboBoxModel aModel) {
    7176        super(aModel);
    72         init(aModel != null && aModel.getSize() > 0 ? aModel.getElementAt(0) : null);
     77        ArrayList<Object> list = new ArrayList<Object>(aModel.getSize());
     78        for (int i = 0; i<aModel.getSize(); i++) {
     79            list.add(aModel.getElementAt(i));
     80        }
     81        init(findPrototypeDisplayValue(list));
    7382    }
    7483
     
    8392    public JosmComboBox(Object[] items) {
    8493        super(items);
    85         init(items != null && items.length > 0 ? items[0] : null);
     94        init(findPrototypeDisplayValue(Arrays.asList(items)));
    8695    }
    8796
     
    96105    public JosmComboBox(Vector<?> items) {
    97106        super(items);
    98         init(items != null && !items.isEmpty() ? items.get(0) : null);
     107        init(findPrototypeDisplayValue(items));
     108    }
     109   
     110    /**
     111     * Finds the prototype display value to use among the given possible candidates.
     112     * @param possibleValues The possible candidates that will be iterated.
     113     * @return The value that needs the largest display height on screen.
     114     * @since 5558
     115     */
     116    protected Object findPrototypeDisplayValue(Collection<?> possibleValues) {
     117        Object result = null;
     118        int maxHeight = -1;
     119        if (possibleValues != null) {
     120            // Remind old prototype to restore it later
     121            Object oldPrototype = getPrototypeDisplayValue();
     122            // Get internal JList to directly call the renderer
     123            JList list = getList();
     124            try {
     125                // Index to give to renderer
     126                int i = 0;
     127                for (Object value : possibleValues) {
     128                    if (value != null) {
     129                        // These two lines work with a "classic" renderer,
     130                        // but not with TaggingPreset custom renderer that return a dummy height if index is equal to -1
     131                        //setPrototypeDisplayValue(value);
     132                        //Dimension dim = getPreferredSize();
     133                       
     134                        // So we explicitely call the renderer by simulating a correct index for the current value
     135                        Component c = getRenderer().getListCellRendererComponent(list, value, i, true, true);
     136                        if (c != null) {
     137                            // Get the real preferred size for the current value
     138                            Dimension dim = c.getPreferredSize();
     139                            if (dim.height > maxHeight) {
     140                                // Larger ? This is our new prototype
     141                                maxHeight = dim.height;
     142                                result = value;
     143                            }
     144                        }
     145                    }
     146                    i++;
     147                }
     148            } finally {
     149                // Restore original prototype
     150                setPrototypeDisplayValue(oldPrototype);
     151            }
     152        }
     153        return result;
     154    }
     155   
     156    protected final JList getList() {
     157        for (int i = 0; i < getUI().getAccessibleChildrenCount(this); i++) {
     158            Accessible child = getUI().getAccessibleChild(this, i);
     159            if (child instanceof ComboPopup) {
     160                return ((ComboPopup)child).getList();
     161            }
     162        }
     163        return null;
    99164    }
    100165   
     
    108173            // If possible, adjust the maximum number of items with the real height of items
    109174            // It is not granted this works on every platform (tested OK on Windows)
    110             for (int i = 0; i < getUI().getAccessibleChildrenCount(this); i++) {
    111                 Accessible child = getUI().getAccessibleChild(this, i);
    112                 if (child instanceof ComboPopup) {
    113                     JList list = ((ComboPopup)child).getList();
    114                     if (list != null) {
    115                         if (list.getPrototypeCellValue() != prototype) {
    116                             list.setPrototypeCellValue(prototype);
    117                         }
    118                         int height = list.getFixedCellHeight();
    119                         if (height > 0) {
    120                             maxsize = (screenHeight/height) / 2;
    121                         }
    122                     }
    123                     break;
     175            JList list = getList();
     176            if (list != null) {
     177                if (list.getPrototypeCellValue() != prototype) {
     178                    list.setPrototypeCellValue(prototype);
     179                }
     180                int height = list.getFixedCellHeight();
     181                if (height > 0) {
     182                    maxsize = (screenHeight/height) / 2;
    124183                }
    125184            }
     
    127186        }
    128187    }
     188   
     189    /**
     190     * Reinitializes this {@link JosmComboBox} to the specified values. This may needed if a custom renderer is used.
     191     * @param values The values displayed in the combo box.
     192     * @since 5558
     193     */
     194    public final void reinitialize(Collection<?> values) {
     195        init(findPrototypeDisplayValue(values));
     196    }
    129197}
Note: See TracChangeset for help on using the changeset viewer.