Changeset 16838 in josm for trunk/src/org
- Timestamp:
- 2020-08-03T22:06:40+02:00 (4 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/dialogs/properties/TagEditHelper.java
r16201 r16838 32 32 import java.util.Collections; 33 33 import java.util.Comparator; 34 import java.util.EnumSet;35 34 import java.util.Iterator; 36 35 import java.util.List; … … 71 70 import org.openstreetmap.josm.command.SequenceCommand; 72 71 import org.openstreetmap.josm.data.UndoRedoHandler; 73 import org.openstreetmap.josm.data.coor.LatLon;74 72 import org.openstreetmap.josm.data.osm.DataSet; 75 import org.openstreetmap.josm.data.osm.INode;76 73 import org.openstreetmap.josm.data.osm.OsmDataManager; 77 74 import org.openstreetmap.josm.data.osm.OsmPrimitive; … … 100 97 import org.openstreetmap.josm.tools.ImageProvider; 101 98 import org.openstreetmap.josm.tools.Logging; 99 import org.openstreetmap.josm.tools.OsmPrimitiveImageProvider; 102 100 import org.openstreetmap.josm.tools.PlatformManager; 103 101 import org.openstreetmap.josm.tools.Shortcut; … … 695 693 protected Optional<ImageIcon> findIcon(String key, String value) { 696 694 final Iterator<OsmPrimitive> osmPrimitiveIterator = sel.iterator(); 697 final OsmPrimitive virtual = (osmPrimitiveIterator.hasNext() ? osmPrimitiveIterator.next().getType() : OsmPrimitiveType.NODE) 698 .newInstance(0, false); 699 if (virtual instanceof INode) { 700 ((INode) virtual).setCoor(LatLon.ZERO); 701 } 702 virtual.put(key, value); 703 try { 704 final ImageIcon padded = ImageProvider.getPadded(virtual, ImageProvider.ImageSizes.LARGEICON.getImageDimension(), 705 EnumSet.of(ImageProvider.GetPaddedOptions.NO_DEFAULT, ImageProvider.GetPaddedOptions.NO_DEPRECATED)); 706 return Optional.ofNullable(padded); 707 } catch (Exception e) { 708 Logging.warn("Failed to find icon for {0} {1}={2}", virtual.getType(), key, value); 709 Logging.warn(e); 710 return Optional.empty(); 711 } 695 final OsmPrimitiveType type = osmPrimitiveIterator.hasNext() ? osmPrimitiveIterator.next().getType() : OsmPrimitiveType.NODE; 696 return OsmPrimitiveImageProvider.getResource(key, value, type) 697 .map(resource -> resource.getPaddedIcon(ImageProvider.ImageSizes.LARGEICON.getImageDimension())); 712 698 } 713 699 -
trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java
r16578 r16838 7 7 import java.util.Arrays; 8 8 import java.util.Collection; 9 import java.util.EnumSet;10 9 import java.util.LinkedList; 11 10 import java.util.List; … … 14 13 import javax.swing.SwingUtilities; 15 14 16 import org.openstreetmap.josm.data.coor.LatLon; 17 import org.openstreetmap.josm.data.osm.DataSet; 18 import org.openstreetmap.josm.data.osm.Node; 15 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 19 16 import org.openstreetmap.josm.data.osm.Tag; 20 17 import org.openstreetmap.josm.data.preferences.sources.MapPaintPrefHelper; … … 30 27 import org.openstreetmap.josm.tools.ListenerList; 31 28 import org.openstreetmap.josm.tools.Logging; 29 import org.openstreetmap.josm.tools.OsmPrimitiveImageProvider; 32 30 import org.openstreetmap.josm.tools.Stopwatch; 33 31 import org.openstreetmap.josm.tools.Utils; … … 238 236 * @param tag The tag to look an icon for 239 237 * @return {@code null} if no icon found 240 * @deprecated use {@link ImageProvider#getPadded}238 * @deprecated use {@link OsmPrimitiveImageProvider#getResource} 241 239 */ 242 240 @Deprecated 243 241 public static ImageIcon getNodeIcon(Tag tag) { 244 242 if (tag != null) { 245 DataSet ds = new DataSet(); 246 Node virtualNode = new Node(LatLon.ZERO); 247 virtualNode.put(tag.getKey(), tag.getValue()); 248 MapCSSStyleSource.STYLE_SOURCE_LOCK.readLock().lock(); 249 try { 250 // Add primitive to dataset to avoid DataIntegrityProblemException when evaluating selectors 251 ds.addPrimitive(virtualNode); 252 return ImageProvider.getPadded(virtualNode, ImageProvider.ImageSizes.SMALLICON.getImageDimension(), 253 EnumSet.of(ImageProvider.GetPaddedOptions.NO_PRESETS, ImageProvider.GetPaddedOptions.NO_DEFAULT)); 254 } finally { 255 ds.removePrimitive(virtualNode); 256 MapCSSStyleSource.STYLE_SOURCE_LOCK.readLock().unlock(); 257 } 243 return OsmPrimitiveImageProvider.getResource(tag.getKey(), tag.getValue(), OsmPrimitiveType.NODE) 244 .map(resource -> resource.getPaddedIcon(ImageProvider.ImageSizes.SMALLICON.getImageDimension())) 245 .orElse(null); 258 246 } 259 247 return null; -
trunk/src/org/openstreetmap/josm/tools/ImageProvider.java
r16794 r16838 35 35 import java.util.Base64; 36 36 import java.util.Collection; 37 import java.util.Collections;38 import java.util.Comparator;39 37 import java.util.EnumMap; 40 38 import java.util.HashMap; … … 45 43 import java.util.Map; 46 44 import java.util.Objects; 47 import java.util.Optional;48 45 import java.util.concurrent.CompletableFuture; 49 46 import java.util.concurrent.ConcurrentHashMap; … … 67 64 68 65 import org.openstreetmap.josm.data.Preferences; 69 import org.openstreetmap.josm.data.osm.DataSet;70 66 import org.openstreetmap.josm.data.osm.OsmPrimitive; 71 67 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 72 import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;73 import org.openstreetmap.josm.gui.mappaint.Range;74 import org.openstreetmap.josm.gui.mappaint.StyleElementList;75 import org.openstreetmap.josm.gui.mappaint.styleelement.MapImage;76 import org.openstreetmap.josm.gui.mappaint.styleelement.NodeElement;77 import org.openstreetmap.josm.gui.mappaint.styleelement.StyleElement;78 import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;79 import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;80 68 import org.openstreetmap.josm.io.CachedFile; 81 69 import org.openstreetmap.josm.spi.preferences.Config; … … 1539 1527 1540 1528 /** 1541 * Options used in {@link #getPadded(OsmPrimitive, Dimension, Collection)}.1542 * @since 158891543 */1544 public enum GetPaddedOptions {1545 /**1546 * Exclude icon indicating deprecated tag usage.1547 */1548 NO_DEPRECATED,1549 /**1550 * Exclude default icon for {@link OsmPrimitiveType} from {@link #get(OsmPrimitiveType)}1551 */1552 NO_DEFAULT,1553 /**1554 * Exclude tagging preset icons.1555 */1556 NO_PRESETS,1557 /**1558 * Exclude tagging preset icons for {@linkplain OsmPrimitiveType#WAY ways}.1559 */1560 NO_WAY_PRESETS;1561 1562 private static final Collection<GetPaddedOptions> DEFAULT = Collections.singleton(GetPaddedOptions.NO_WAY_PRESETS);1563 }1564 1565 /**1566 1529 * Returns an {@link ImageIcon} for the given OSM object, at the specified size. 1567 1530 * This is a slow operation. … … 1572 1535 */ 1573 1536 public static ImageIcon getPadded(OsmPrimitive primitive, Dimension iconSize) { 1574 return getPadded(primitive, iconSize, GetPaddedOptions.DEFAULT);1575 }1576 1577 /**1578 * Returns an {@link ImageIcon} for the given OSM object, at the specified size.1579 * This is a slow operation.1580 * @param primitive Object for which an icon shall be fetched. The icon is chosen based on tags.1581 * @param iconSize Target size of icon. Icon is padded if required.1582 * @param options zero or more {@linkplain GetPaddedOptions options}.1583 * @return Icon for {@code primitive} that fits in cell or {@code null}.1584 * @since 158891585 */1586 public static ImageIcon getPadded(OsmPrimitive primitive, Dimension iconSize, Collection<GetPaddedOptions> options) {1587 1537 if (iconSize.width <= 0 || iconSize.height <= 0) { 1588 1538 return null; 1589 1539 } 1590 1591 // Check if the current styles have special icon for tagged objects. 1592 if (primitive.isTagged()) { 1593 ImageIcon icon = getTaggedPadded(primitive, iconSize, options); 1594 if (icon != null) { 1595 return icon; 1596 } 1597 } 1598 1599 // Check if the presets have icons for nodes/relations. 1600 if (primitive.isTagged() && (!options.contains(GetPaddedOptions.NO_WAY_PRESETS) || OsmPrimitiveType.WAY != primitive.getType())) { 1601 final Optional<ImageIcon> icon = TaggingPresets.getMatchingPresets(primitive).stream() 1602 .sorted(Comparator.comparing(p -> (p.iconName != null && p.iconName.contains("multipolygon")) 1603 || p.types == null || p.types.isEmpty() ? Integer.MAX_VALUE : p.types.size())) 1604 .map(TaggingPreset::getImageResource) 1605 .filter(Objects::nonNull) 1606 .map(resource -> resource.getPaddedIcon(iconSize)) 1607 .findFirst(); 1608 if (icon.isPresent()) { 1609 return icon.get(); 1610 } 1611 } 1612 1613 // Use generic default icon. 1614 return options.contains(GetPaddedOptions.NO_DEFAULT) 1615 ? null 1616 : new ImageProvider("data", primitive.getDisplayType().getAPIName()).getResource().getPaddedIcon(iconSize); 1617 } 1618 1619 /** 1620 * Computes a new padded icon for the given tagged primitive, using map paint styles. 1621 * This is a slow operation. 1622 * @param primitive tagged OSM primitive 1623 * @param iconSize icon size in pixels 1624 * @param options zero or more {@linkplain GetPaddedOptions options}. 1625 * @return a new padded icon for the given tagged primitive, or null 1626 */ 1627 private static ImageIcon getTaggedPadded(OsmPrimitive primitive, Dimension iconSize, Collection<GetPaddedOptions> options) { 1628 Pair<StyleElementList, Range> nodeStyles; 1629 DataSet ds = primitive.getDataSet(); 1630 if (ds != null) { 1631 ds.getReadLock().lock(); 1632 } 1633 try { 1634 nodeStyles = MapPaintStyles.getStyles().generateStyles(primitive, 100, false); 1635 } finally { 1636 if (ds != null) { 1637 ds.getReadLock().unlock(); 1638 } 1639 } 1640 for (StyleElement style : nodeStyles.a) { 1641 if (style instanceof NodeElement) { 1642 NodeElement nodeStyle = (NodeElement) style; 1643 MapImage icon = nodeStyle.mapImage; 1644 if (icon != null && icon.getImageResource() != null && 1645 (icon.name == null || !options.contains(GetPaddedOptions.NO_DEPRECATED) || !icon.name.contains("deprecated"))) { 1646 return icon.getImageResource().getPaddedIcon(iconSize); 1647 } 1648 } 1649 } 1650 return null; 1540 ImageResource resource = OsmPrimitiveImageProvider.getResource(primitive, OsmPrimitiveImageProvider.Options.DEFAULT); 1541 return resource != null ? resource.getPaddedIcon(iconSize) : null; 1651 1542 } 1652 1543
Note:
See TracChangeset
for help on using the changeset viewer.