Changeset 5558 in josm for trunk/src/org/openstreetmap/josm/gui/widgets/JosmComboBox.java
- Timestamp:
- 2012-11-03T21:55:13+01:00 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/widgets/JosmComboBox.java
r5451 r5558 2 2 package org.openstreetmap.josm.gui.widgets; 3 3 4 import java.awt.Component; 5 import java.awt.Dimension; 4 6 import java.awt.Toolkit; 7 import java.util.ArrayList; 8 import java.util.Arrays; 9 import java.util.Collection; 5 10 import java.util.Vector; 6 11 … … 70 75 public JosmComboBox(ComboBoxModel aModel) { 71 76 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)); 73 82 } 74 83 … … 83 92 public JosmComboBox(Object[] items) { 84 93 super(items); 85 init( items != null && items.length > 0 ? items[0] : null);94 init(findPrototypeDisplayValue(Arrays.asList(items))); 86 95 } 87 96 … … 96 105 public JosmComboBox(Vector<?> items) { 97 106 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; 99 164 } 100 165 … … 108 173 // If possible, adjust the maximum number of items with the real height of items 109 174 // 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; 124 183 } 125 184 } … … 127 186 } 128 187 } 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 } 129 197 }
Note:
See TracChangeset
for help on using the changeset viewer.