Changeset 15889 in josm


Ignore:
Timestamp:
2020-02-21T21:19:34+01:00 (4 years ago)
Author:
simon04
Message:

fix #14088 - Add tags dialog: use padded preset/style icons for okay button

Location:
trunk/src/org/openstreetmap/josm
Files:
3 edited

Legend:

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

    r15885 r15889  
    3232import java.util.Collections;
    3333import java.util.Comparator;
     34import java.util.EnumSet;
     35import java.util.Iterator;
    3436import java.util.List;
    3537import java.util.Map;
     
    7274import org.openstreetmap.josm.data.osm.OsmDataManager;
    7375import org.openstreetmap.josm.data.osm.OsmPrimitive;
     76import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    7477import org.openstreetmap.josm.data.osm.Tag;
    7578import org.openstreetmap.josm.data.osm.search.SearchCompiler;
     
    8689import org.openstreetmap.josm.gui.MainApplication;
    8790import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
    88 import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
    8991import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingComboBox;
    9092import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
    91 import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
    9293import org.openstreetmap.josm.gui.util.GuiHelper;
    9394import org.openstreetmap.josm.gui.util.WindowGeometry;
     
    684685                return;
    685686            }
    686             final Tag tag = new Tag(keys.getSelectedOrEditItem(), values.getSelectedOrEditItem());
    687             buttons.get(0).setIcon(findIcon(tag)
     687            buttons.get(0).setIcon(findIcon(keys.getSelectedOrEditItem(), values.getSelectedOrEditItem())
    688688                    .orElse(ImageProvider.get("ok", ImageProvider.ImageSizes.LARGEICON)));
    689689        }
    690690
    691         protected Optional<ImageIcon> findIcon(Tag tag) {
    692             final Optional<ImageIcon> taggingPresetIcon = TaggingPresets.getMatchingPresets(null, tag.getKeys(), false).stream()
    693                     .map(preset -> preset.getIcon(Action.LARGE_ICON_KEY))
    694                     .filter(Objects::nonNull)
    695                     .findFirst();
    696             // Java 9: use Optional.or
    697             return taggingPresetIcon.isPresent() ? taggingPresetIcon : Optional.ofNullable(MapPaintStyles.getNodeIcon(tag, false));
     691        protected Optional<ImageIcon> findIcon(String key, String value) {
     692            final Iterator<OsmPrimitive> osmPrimitiveIterator = sel.iterator();
     693            final OsmPrimitive virtual = (osmPrimitiveIterator.hasNext() ? osmPrimitiveIterator.next().getType() : OsmPrimitiveType.NODE)
     694                    .newInstance(0, false);
     695            virtual.put(key, value);
     696            final ImageIcon padded = ImageProvider.getPadded(virtual, ImageProvider.ImageSizes.LARGEICON.getImageDimension(),
     697                    EnumSet.of(ImageProvider.GetPaddedOptions.NO_DEFAULT, ImageProvider.GetPaddedOptions.NO_DEPRECATED));
     698            return Optional.ofNullable(padded);
    698699        }
    699700
     
    963964                    action.setEnabled(false);
    964965                }
    965                 ImageIcon icon = findIcon(t)
     966                ImageIcon icon = findIcon(t.getKey(), t.getValue())
    966967                        // If still nothing display an empty icon
    967968
  • trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

    r15755 r15889  
    77import java.util.Arrays;
    88import java.util.Collection;
     9import java.util.EnumSet;
    910import java.util.LinkedList;
    1011import java.util.List;
     
    2021import org.openstreetmap.josm.data.preferences.sources.SourceEntry;
    2122import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
    22 import org.openstreetmap.josm.gui.mappaint.styleelement.MapImage;
    23 import org.openstreetmap.josm.gui.mappaint.styleelement.NodeElement;
    24 import org.openstreetmap.josm.gui.mappaint.styleelement.StyleElement;
    2523import org.openstreetmap.josm.io.CachedFile;
    2624import org.openstreetmap.josm.io.FileWatcher;
     
    215213     * @param tag The tag to look an icon for
    216214     * @return {@code null} if no icon found
    217      */
     215     * @deprecated use {@link ImageProvider#getPadded}
     216     */
     217    @Deprecated
    218218    public static ImageIcon getNodeIcon(Tag tag) {
    219         return getNodeIcon(tag, true);
    220     }
    221 
    222     /**
    223      * Returns the node icon that would be displayed for the given tag.
    224      * @param tag The tag to look an icon for
    225      * @param includeDeprecatedIcon if {@code true}, the special deprecated icon will be returned if applicable
    226      * @return {@code null} if no icon found, or if the icon is deprecated and not wanted
    227      */
    228     public static ImageIcon getNodeIcon(Tag tag, boolean includeDeprecatedIcon) {
    229219        if (tag != null) {
    230220            DataSet ds = new DataSet();
    231221            Node virtualNode = new Node(LatLon.ZERO);
    232222            virtualNode.put(tag.getKey(), tag.getValue());
    233             StyleElementList styleList;
    234223            MapCSSStyleSource.STYLE_SOURCE_LOCK.readLock().lock();
    235224            try {
    236225                // Add primitive to dataset to avoid DataIntegrityProblemException when evaluating selectors
    237226                ds.addPrimitive(virtualNode);
    238                 styleList = getStyles().generateStyles(virtualNode, 0.5, false).a;
     227                return ImageProvider.getPadded(virtualNode, ImageProvider.ImageSizes.SMALLICON.getImageDimension(),
     228                        EnumSet.of(ImageProvider.GetPaddedOptions.NO_PRESETS, ImageProvider.GetPaddedOptions.NO_DEFAULT));
     229            } finally {
    239230                ds.removePrimitive(virtualNode);
    240             } finally {
    241231                MapCSSStyleSource.STYLE_SOURCE_LOCK.readLock().unlock();
    242             }
    243             if (styleList != null) {
    244                 for (StyleElement style : styleList) {
    245                     if (style instanceof NodeElement) {
    246                         MapImage mapImage = ((NodeElement) style).mapImage;
    247                         if (mapImage != null) {
    248                             if (includeDeprecatedIcon || mapImage.name == null || !DEPRECATED_IMAGE_NAMES.contains(mapImage.name)) {
    249                                 return new ImageIcon(mapImage.getImage(false));
    250                             } else {
    251                                 return null; // Deprecated icon found but not wanted
    252                             }
    253                         }
    254                     }
    255                 }
    256232            }
    257233        }
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r15675 r15889  
    3535import java.util.Base64;
    3636import java.util.Collection;
     37import java.util.Comparator;
    3738import java.util.EnumMap;
     39import java.util.EnumSet;
    3840import java.util.HashMap;
    3941import java.util.Hashtable;
     
    4345import java.util.Map;
    4446import java.util.Objects;
    45 import java.util.TreeSet;
     47import java.util.Optional;
    4648import java.util.concurrent.CompletableFuture;
    4749import java.util.concurrent.ConcurrentHashMap;
     
    6365import javax.xml.parsers.ParserConfigurationException;
    6466
     67import com.kitfox.svg.SVGDiagram;
     68import com.kitfox.svg.SVGException;
     69import com.kitfox.svg.SVGUniverse;
    6570import org.openstreetmap.josm.data.Preferences;
    6671import org.openstreetmap.josm.data.osm.DataSet;
     
    8590import org.xml.sax.XMLReader;
    8691import org.xml.sax.helpers.DefaultHandler;
    87 
    88 import com.kitfox.svg.SVGDiagram;
    89 import com.kitfox.svg.SVGException;
    90 import com.kitfox.svg.SVGUniverse;
    9192
    9293/**
     
    308309
    309310    /** larger cache of critical padded image icons used in many parts of the application */
    310     private static final Map<Dimension, Map<MapImage, ImageIcon>> paddedImageCache = new HashMap<>();
     311    private static final Map<Dimension, Map<Image, ImageIcon>> paddedImageCache = new HashMap<>();
    311312
    312313    private static final ExecutorService IMAGE_FETCHER =
     
    14931494
    14941495    /**
     1496     * Options used in {@link #getPadded(OsmPrimitive, Dimension, Collection)}.
     1497     * @since 15889
     1498     */
     1499    public enum GetPaddedOptions {
     1500        /**
     1501         * Exclude icon indicating deprecated tag usage.
     1502         */
     1503        NO_DEPRECATED,
     1504        /**
     1505         * Exclude default icon for {@link OsmPrimitiveType} from {@link #get(OsmPrimitiveType)}
     1506         */
     1507        NO_DEFAULT,
     1508        /**
     1509         * Exclude tagging preset icons.
     1510         */
     1511        NO_PRESETS,
     1512        /**
     1513         * Exclude tagging preset icons for {@linkplain OsmPrimitiveType#WAY ways}.
     1514         */
     1515        NO_WAY_PRESETS,
     1516    }
     1517
     1518    /**
    14951519     * @param primitive Object for which an icon shall be fetched. The icon is chosen based on tags.
    14961520     * @param iconSize Target size of icon. Icon is padded if required.
     
    14991523     */
    15001524    public static ImageIcon getPadded(OsmPrimitive primitive, Dimension iconSize) {
     1525        return getPadded(primitive, iconSize, EnumSet.of(GetPaddedOptions.NO_WAY_PRESETS));
     1526    }
     1527
     1528    /**
     1529     * @param primitive Object for which an icon shall be fetched. The icon is chosen based on tags.
     1530     * @param iconSize Target size of icon. Icon is padded if required.
     1531     * @param options zero or more {@linkplain GetPaddedOptions options}.
     1532     * @return Icon for {@code primitive} that fits in cell or {@code null}.
     1533     * @since 15889
     1534     */
     1535    public static ImageIcon getPadded(OsmPrimitive primitive, Dimension iconSize, Collection<GetPaddedOptions> options) {
    15011536        // Check if the current styles have special icon for tagged objects.
    15021537        if (primitive.isTagged()) {
    1503             ImageIcon icon = getTaggedPadded(primitive, iconSize);
     1538            ImageIcon icon = getTaggedPadded(primitive, iconSize, options);
    15041539            if (icon != null) {
    15051540                return icon;
     
    15081543
    15091544        // Check if the presets have icons for nodes/relations.
    1510         if (OsmPrimitiveType.WAY != primitive.getType()) {
    1511             final Collection<TaggingPreset> presets = new TreeSet<>((o1, o2) -> {
    1512                 final int o1TypesSize = o1.types == null || o1.types.isEmpty() ? Integer.MAX_VALUE : o1.types.size();
    1513                 final int o2TypesSize = o2.types == null || o2.types.isEmpty() ? Integer.MAX_VALUE : o2.types.size();
    1514                 return Integer.compare(o1TypesSize, o2TypesSize);
    1515             });
    1516             presets.addAll(TaggingPresets.getMatchingPresets(primitive));
    1517             for (final TaggingPreset preset : presets) {
    1518                 if (preset.getIcon() != null) {
    1519                     return preset.getIcon();
    1520                 }
     1545        if (!options.contains(GetPaddedOptions.NO_WAY_PRESETS) || OsmPrimitiveType.WAY != primitive.getType()) {
     1546            final Optional<ImageIcon> icon = TaggingPresets.getMatchingPresets(primitive).stream()
     1547                    .sorted(Comparator.comparing(p -> p.types == null || p.types.isEmpty() ? Integer.MAX_VALUE : p.types.size()))
     1548                    .map(TaggingPreset::getIcon)
     1549                    .filter(Objects::nonNull)
     1550                    .map(imageIcon -> getPaddedIcon(imageIcon.getImage(), iconSize))
     1551                    .findFirst();
     1552            if (icon.isPresent()) {
     1553                return icon.get();
    15211554            }
    15221555        }
    15231556
    15241557        // Use generic default icon.
    1525         return ImageProvider.get(primitive.getDisplayType());
     1558        return options.contains(GetPaddedOptions.NO_DEFAULT)
     1559                ? null
     1560                : getPaddedIcon(get(primitive.getDisplayType()).getImage(), iconSize);
    15261561    }
    15271562
     
    15311566     * @param primitive tagged OSM primitive
    15321567     * @param iconSize icon size in pixels
     1568     * @param options zero or more {@linkplain GetPaddedOptions options}.
    15331569     * @return a new padded icon for the given tagged primitive, or null
    15341570     */
    1535     private static ImageIcon getTaggedPadded(OsmPrimitive primitive, Dimension iconSize) {
     1571    private static ImageIcon getTaggedPadded(OsmPrimitive primitive, Dimension iconSize, Collection<GetPaddedOptions> options) {
    15361572        Pair<StyleElementList, Range> nodeStyles;
    15371573        DataSet ds = primitive.getDataSet();
     
    15501586                NodeElement nodeStyle = (NodeElement) style;
    15511587                MapImage icon = nodeStyle.mapImage;
    1552                 if (icon != null) {
     1588                if (icon != null &&
     1589                        (icon.name == null || !options.contains(GetPaddedOptions.NO_DEPRECATED) || !icon.name.contains("deprecated"))) {
    15531590                    return getPaddedIcon(icon, iconSize);
    15541591                }
     
    15681605     */
    15691606    public static ImageIcon getPaddedIcon(MapImage mapImage, Dimension iconSize) {
     1607        return getPaddedIcon(mapImage.getImage(false), iconSize);
     1608    }
     1609
     1610    private static ImageIcon getPaddedIcon(Image mapImage, Dimension iconSize) {
    15701611        synchronized (paddedImageCache) {
    15711612            return paddedImageCache.computeIfAbsent(iconSize, x -> new HashMap<>()).computeIfAbsent(mapImage, icon -> {
    15721613                int backgroundRealWidth = GuiSizesHelper.getSizeDpiAdjusted(iconSize.width);
    15731614                int backgroundRealHeight = GuiSizesHelper.getSizeDpiAdjusted(iconSize.height);
    1574                 int iconRealWidth = icon.getWidth();
    1575                 int iconRealHeight = icon.getHeight();
     1615                int iconRealWidth = icon.getWidth(null);
     1616                int iconRealHeight = icon.getHeight(null);
    15761617                BufferedImage image = new BufferedImage(backgroundRealWidth, backgroundRealHeight, BufferedImage.TYPE_INT_ARGB);
    15771618                double scaleFactor = Math.min(
    15781619                        backgroundRealWidth / (double) iconRealWidth,
    15791620                        backgroundRealHeight / (double) iconRealHeight);
    1580                 Image iconImage = icon.getImage(false);
    15811621                Image scaledIcon;
    15821622                final int scaledWidth;
     
    15861626                    scaledWidth = (int) (iconRealWidth * scaleFactor);
    15871627                    scaledHeight = (int) (iconRealHeight * scaleFactor);
    1588                     scaledIcon = iconImage.getScaledInstance(scaledWidth, scaledHeight, Image.SCALE_SMOOTH);
     1628                    scaledIcon = icon.getScaledInstance(scaledWidth, scaledHeight, Image.SCALE_SMOOTH);
    15891629                } else {
    15901630                    // Use original size, don't upscale.
    15911631                    scaledWidth = iconRealWidth;
    15921632                    scaledHeight = iconRealHeight;
    1593                     scaledIcon = iconImage;
     1633                    scaledIcon = icon;
    15941634                }
    15951635                image.getGraphics().drawImage(scaledIcon,
Note: See TracChangeset for help on using the changeset viewer.