Changeset 15834 in josm for trunk/src


Ignore:
Timestamp:
2020-02-10T22:58:02+01:00 (5 years ago)
Author:
simon04
Message:

TagEditHelper: use Java 8 features

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/properties/TagEditHelper.java

    r15716 r15834  
    3232import java.util.Collections;
    3333import java.util.Comparator;
    34 import java.util.HashMap;
    3534import java.util.List;
    3635import java.util.Map;
    3736import java.util.Objects;
     37import java.util.Optional;
    3838import java.util.TreeMap;
     39import java.util.stream.Collectors;
    3940import java.util.stream.IntStream;
    4041
     
    547548                commands.add(new ChangePropertyCommand(sel, key, null));
    548549                if (value.equals(tr("<different>"))) {
    549                     Map<String, List<OsmPrimitive>> map = new HashMap<>();
    550                     for (OsmPrimitive osm: sel) {
    551                         String val = osm.get(key);
    552                         if (val != null) {
    553                             if (map.containsKey(val)) {
    554                                 map.get(val).add(osm);
    555                             } else {
    556                                 List<OsmPrimitive> v = new ArrayList<>();
    557                                 v.add(osm);
    558                                 map.put(val, v);
    559                             }
    560                         }
    561                     }
    562                     for (Map.Entry<String, List<OsmPrimitive>> e: map.entrySet()) {
    563                         commands.add(new ChangePropertyCommand(e.getValue(), newkey, e.getKey()));
    564                     }
     550                    String newKey = newkey;
     551                    sel.stream()
     552                            .filter(osm -> osm.hasKey(key))
     553                            .collect(Collectors.groupingBy(osm -> osm.get(key)))
     554                            .forEach((newValue, osmPrimitives) -> commands.add(new ChangePropertyCommand(osmPrimitives, newKey, newValue)));
    565555                } else {
    566556                    commands.add(new ChangePropertyCommand(sel, newkey, value));
     
    667657                   ComboBoxModel<AutoCompletionItem> currentModel = values.getModel();
    668658                   final int size = correctItems.size();
    669                    boolean valuesOK = size == currentModel.getSize();
    670                    for (int i = 0; valuesOK && i < size; i++) {
    671                        valuesOK = Objects.equals(currentModel.getElementAt(i), correctItems.get(i));
    672                    }
     659                   boolean valuesOK = size == currentModel.getSize()
     660                           && IntStream.range(0, size).allMatch(i -> Objects.equals(currentModel.getElementAt(i), correctItems.get(i)));
    673661                   if (!valuesOK) {
    674662                       values.setPossibleAcItems(correctItems);
     
    682670           editor.addFocusListener(focus);
    683671           return focus;
     672        }
     673
     674        private Optional<ImageIcon> findIcon(Tag tag) {
     675            // Find and display icon
     676            ImageIcon icon = MapPaintStyles.getNodeIcon(tag, false); // Filters deprecated icon
     677            if (icon != null) {
     678                return Optional.of(icon);
     679            }
     680            // If no icon found in map style look at presets
     681            return TaggingPresets.getMatchingPresets(null, tag.getKeys(), false).stream()
     682                    .map(TaggingPreset::getIcon)
     683                    .filter(Objects::nonNull)
     684                    .findFirst();
    684685        }
    685686
     
    947948                    action.setEnabled(false);
    948949                }
    949                 // Find and display icon
    950                 ImageIcon icon = MapPaintStyles.getNodeIcon(t, false); // Filters deprecated icon
    951                 if (icon == null) {
    952                     // If no icon found in map style look at presets
    953                     Map<String, String> map = new HashMap<>();
    954                     map.put(t.getKey(), t.getValue());
    955                     for (TaggingPreset tp : TaggingPresets.getMatchingPresets(null, map, false)) {
    956                         icon = tp.getIcon();
    957                         if (icon != null) {
    958                             break;
    959                         }
    960                     }
    961                     // If still nothing display an empty icon
    962                     if (icon == null) {
    963                         icon = new ImageIcon(new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB));
    964                     }
    965                 }
     950                ImageIcon icon = findIcon(t)
     951                        // If still nothing display an empty icon
     952
     953                        .orElseGet(() -> new ImageIcon(new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB)));
    966954                GridBagConstraints gbc = new GridBagConstraints();
    967955                gbc.ipadx = 5;
Note: See TracChangeset for help on using the changeset viewer.