Changeset 18257 in josm for trunk/src/org
- Timestamp:
- 2021-10-07T13:07:57+02:00 (3 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/gui
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/tagging/presets/TaggingPresetItemGuiSupport.java
r18254 r18257 31 31 32 32 /** whether to fire events or not */ 33 private boolean enabled = false;33 private boolean enabled; 34 34 35 35 /** -
trunk/src/org/openstreetmap/josm/gui/tagging/presets/items/Combo.java
r18254 r18257 13 13 import java.util.Arrays; 14 14 import java.util.Comparator; 15 import java.util.TreeMap;16 15 17 16 import javax.swing.AbstractAction; … … 89 88 initializeLocaleText(null); 90 89 usage = determineTextUsage(support.getSelected(), key); 91 seenValues = new TreeMap<>();90 seenValues.clear(); 92 91 // get the standard values from the preset definition 93 92 initListEntries(); 94 93 95 94 // init the model 96 dropDownModel = new AutoCompComboBoxModel< PresetListEntry>(Comparator.naturalOrder());95 dropDownModel = new AutoCompComboBoxModel<>(Comparator.<PresetListEntry>naturalOrder()); 97 96 98 97 if (!usage.hasUniqueValue() && !usage.unused()) { … … 125 124 combobox.setEditable(editable); 126 125 127 autoCompModel = new AutoCompComboBoxModel< AutoCompletionItem>(Comparator.naturalOrder());126 autoCompModel = new AutoCompComboBoxModel<>(Comparator.<AutoCompletionItem>naturalOrder()); 128 127 getAllForKeys(Arrays.asList(key)).forEach(autoCompModel::addElement); 129 128 getDisplayValues().forEach(s -> autoCompModel.addElement(new AutoCompletionItem(s, AutoCompletionPriority.IS_IN_STANDARD))); … … 155 154 } 156 155 157 String valueToSelect = getInitialValue( default_);156 String valueToSelect = getInitialValue(usage); 158 157 if (valueToSelect != null) { 159 158 PresetListEntry selItem = find(valueToSelect); -
trunk/src/org/openstreetmap/josm/gui/tagging/presets/items/ComboMultiSelect.java
r18254 r18257 5 5 6 6 import java.awt.Component; 7 import java.awt.Font; 7 8 import java.lang.reflect.Method; 8 9 import java.lang.reflect.Modifier; … … 91 92 protected final List<PresetListEntry> presetListEntries = new ArrayList<>(); 92 93 /** Helps avoid duplicate list entries */ 93 protected Map<String, PresetListEntry> seenValues = new TreeMap<>();94 protected final Map<String, PresetListEntry> seenValues = new TreeMap<>(); 94 95 protected Usage usage; 95 96 /** Used to see if the user edited the value. May be null. */ … … 131 132 132 133 JLabel l = (JLabel) renderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 134 l.setComponentOrientation(component.getComponentOrientation()); 133 135 if (index != -1) { 134 136 // index -1 is set when measuring the size of the cell and when painting the … … 136 138 l.setText(value.getListDisplay(width)); 137 139 } 140 if (value.getCount() > 0) { 141 l.setFont(l.getFont().deriveFont(Font.ITALIC + Font.BOLD)); 142 } 143 l.setIcon(value.getIcon()); 138 144 l.setToolTipText(value.getToolTipText(key)); 139 l.setIcon(value.getIcon());140 145 return l; 141 146 } … … 221 226 } 222 227 223 private List<String> getValuesFromCode(String values _from) {228 private List<String> getValuesFromCode(String valuesFrom) { 224 229 // get the values from a Java function 225 String[] classMethod = values _from.split("#", -1);230 String[] classMethod = valuesFrom.split("#", -1); 226 231 if (classMethod.length == 2) { 227 232 try { … … 315 320 * The initial value is the value shown in the control when the preset dialogs opens. 316 321 * 317 * @param def The default value322 * @param usage The key Usage 318 323 * @return The initial value to use. 319 324 */ 320 protected String getInitialValue( String def) {325 protected String getInitialValue(Usage usage) { 321 326 String initialValue = null; 327 originalValue = null; 322 328 323 329 if (usage.hasUniqueValue()) { 324 330 // all selected primitives have the same not empty value for this key 325 331 initialValue = usage.getFirst(); 332 originalValue = initialValue; 326 333 } else if (!usage.unused()) { 327 334 // at least one primitive has a value for this key (but not all have the same one) 328 335 initialValue = DIFFERENT; 336 originalValue = initialValue; 329 337 } else if (PROP_FILL_DEFAULT.get() || isForceUseLastAsDefault()) { 330 338 // at this point no primitive had any value for this key … … 336 344 } else if (!usage.hadKeys()) { 337 345 // use the default only on objects with no keys at all 338 initialValue = def; 339 } 340 originalValue = initialValue; 346 initialValue = default_; 347 } 341 348 return initialValue; 342 349 } -
trunk/src/org/openstreetmap/josm/gui/tagging/presets/items/KeyedItem.java
r18254 r18257 107 107 private boolean hadKeys; 108 108 private boolean hadEmpty; 109 private int selectedCount; 109 110 110 111 /** … … 139 140 public boolean hadKeys() { 140 141 return hadKeys; 142 } 143 144 /** 145 * Returns the number of primitives selected. 146 * @return the number of primitives selected. 147 */ 148 public int getSelectedCount() { 149 return selectedCount; 150 } 151 152 /** 153 * Splits multiple values and adds their usage counts as single value. 154 * <p> 155 * A value of {@code regional;pizza} will increment the count of {@code regional} and of 156 * {@code pizza}. 157 * @param delimiter The delimiter used for splitting. 158 * @return A new usage object with the new counts. 159 */ 160 public Usage splitValues(String delimiter) { 161 Usage usage = new Usage(); 162 usage.hadEmpty = hadEmpty; 163 usage.hadKeys = hadKeys; 164 usage.selectedCount = selectedCount; 165 map.forEach((value, count) -> { 166 for (String v : value.split(String.valueOf(delimiter), -1)) { 167 usage.map.merge(v, count, Integer::sum); 168 } 169 }); 170 return usage; 141 171 } 142 172 } … … 150 180 public static Usage determineTextUsage(Collection<OsmPrimitive> sel, String key) { 151 181 Usage returnValue = new Usage(); 182 returnValue.selectedCount = sel.size(); 152 183 for (OsmPrimitive s : sel) { 153 184 String v = s.get(key); … … 166 197 protected static Usage determineBooleanUsage(Collection<OsmPrimitive> sel, String key) { 167 198 Usage returnValue = new Usage(); 199 returnValue.selectedCount = sel.size(); 168 200 for (OsmPrimitive s : sel) { 169 201 String booleanValue = OsmUtils.getNamedOsmBoolean(s.get(key)); -
trunk/src/org/openstreetmap/josm/gui/tagging/presets/items/MultiSelect.java
r18254 r18257 2 2 package org.openstreetmap.josm.gui.tagging.presets.items; 3 3 4 import java.util.TreeMap; 4 import java.awt.Dimension; 5 import java.awt.Insets; 6 import java.awt.Rectangle; 5 7 import java.util.stream.Collectors; 6 8 … … 41 43 initializeLocaleText(null); 42 44 usage = determineTextUsage(support.getSelected(), key); 43 seenValues = new TreeMap<>();45 seenValues.clear(); 44 46 initListEntries(); 45 47 46 48 model.clear(); 47 if (!usage.hasUniqueValue() && !usage.unused()) { 48 addEntry(PresetListEntry.ENTRY_DIFFERENT); 49 // disable if the selected primitives have different values 50 list.setEnabled(usage.hasUniqueValue() || usage.unused()); 51 String initialValue = getInitialValue(usage); 52 53 // Add values from the preset. 54 presetListEntries.forEach(this::addEntry); 55 56 // Add all values used in the selected primitives. This also adds custom values and makes 57 // sure we won't lose them. 58 usage = usage.splitValues(String.valueOf(delimiter)); 59 for (String value: usage.map.keySet()) { 60 addEntry(new PresetListEntry(value, this)); 49 61 } 50 62 51 String initialValue = getInitialValue(default_); 52 if (initialValue != null) { 53 // add all values already present to the list, otherwise we would remove all 54 // custom entries unknown to the preset 63 // Select the values in the initial value. 64 if (initialValue != null && !DIFFERENT.equals(initialValue)) { 55 65 for (String value : initialValue.split(String.valueOf(delimiter), -1)) { 56 66 PresetListEntry e = new PresetListEntry(value, this); … … 61 71 } 62 72 63 presetListEntries.forEach(this::addEntry);64 65 73 ComboMultiSelectListCellRenderer renderer = new ComboMultiSelectListCellRenderer(list, list.getCellRenderer(), 200, key); 66 74 list.setCellRenderer(renderer); 75 JLabel label = addLabel(p); 76 label.setLabelFor(list); 77 JScrollPane sp = new JScrollPane(list); 67 78 68 79 if (rows > 0) { 69 80 list.setVisibleRowCount(rows); 81 // setVisibleRowCount() only works when all cells have the same height, but sometimes we 82 // have icons of different sizes. Calculate the size of the first {@code rows} entries 83 // and size the scrollpane accordingly. 84 Rectangle r = list.getCellBounds(0, Math.min(rows, model.size() - 1)); 85 Insets insets = list.getInsets(); 86 r.width += insets.left + insets.right; 87 r.height += insets.top + insets.bottom; 88 insets = sp.getInsets(); 89 r.width += insets.left + insets.right; 90 r.height += insets.top + insets.bottom; 91 sp.setPreferredSize(new Dimension(r.width, r.height)); 70 92 } 71 JLabel label = addLabel(p); 72 p.add(new JScrollPane(list), GBC.eol().fill(GBC.HORIZONTAL)); // NOSONAR 73 label.setLabelFor(list); 93 p.add(sp, GBC.eol().fill(GBC.HORIZONTAL)); // NOSONAR 74 94 75 95 list.addListSelectionListener(l -> support.fireItemValueModified(this, key, getSelectedItem().value)); … … 83 103 protected PresetListEntry getSelectedItem() { 84 104 return new PresetListEntry(list.getSelectedValuesList() 85 .stream().map(e -> e.value). collect(Collectors.joining(String.valueOf(delimiter))), this);105 .stream().map(e -> e.value).distinct().sorted().collect(Collectors.joining(String.valueOf(delimiter))), this); 86 106 } 87 107 } -
trunk/src/org/openstreetmap/josm/gui/tagging/presets/items/PresetListEntry.java
r18254 r18257 48 48 public String locale_short_description; // NOSONAR 49 49 50 private String cachedDisplayValue = null;51 private String cachedShortDescription = null;52 private ImageIcon cachedIcon = null;50 private String cachedDisplayValue; 51 private String cachedShortDescription; 52 private ImageIcon cachedIcon; 53 53 54 54 /** … … 83 83 public String getListDisplay(int width) { 84 84 String displayValue = getDisplayValue(); 85 Integer count = cms == null ? null : cms.usage.map.get(value);85 Integer count = getCount(); 86 86 87 if (count != null) {88 displayValue = String.format("%s (%d)", displayValue, count);87 if (count > 0 && cms.usage.getSelectedCount() > 1) { 88 displayValue = tr("{0} ({1})", displayValue, count); 89 89 } 90 90 … … 184 184 } 185 185 186 /** 187 * Returns how many selected primitives had this value set. 188 * @return see above 189 */ 190 public int getCount() { 191 Integer count = cms == null ? null : cms.usage.map.get(value); 192 return count == null ? 0 : count; 193 } 194 186 195 @Override 187 196 public int compareTo(PresetListEntry o) { -
trunk/src/org/openstreetmap/josm/gui/widgets/JosmComboBox.java
r18221 r18257 48 48 49 49 /** the configured maximum row count or null */ 50 private Integer configMaximumRowCount = null;50 private Integer configMaximumRowCount; 51 51 52 52 /**
Note:
See TracChangeset
for help on using the changeset viewer.