- Timestamp:
- 2020-02-21T21:19:34+01:00 (5 years ago)
- 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 32 32 import java.util.Collections; 33 33 import java.util.Comparator; 34 import java.util.EnumSet; 35 import java.util.Iterator; 34 36 import java.util.List; 35 37 import java.util.Map; … … 72 74 import org.openstreetmap.josm.data.osm.OsmDataManager; 73 75 import org.openstreetmap.josm.data.osm.OsmPrimitive; 76 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 74 77 import org.openstreetmap.josm.data.osm.Tag; 75 78 import org.openstreetmap.josm.data.osm.search.SearchCompiler; … … 86 89 import org.openstreetmap.josm.gui.MainApplication; 87 90 import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils; 88 import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;89 91 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingComboBox; 90 92 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager; 91 import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;92 93 import org.openstreetmap.josm.gui.util.GuiHelper; 93 94 import org.openstreetmap.josm.gui.util.WindowGeometry; … … 684 685 return; 685 686 } 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()) 688 688 .orElse(ImageProvider.get("ok", ImageProvider.ImageSizes.LARGEICON))); 689 689 } 690 690 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); 698 699 } 699 700 … … 963 964 action.setEnabled(false); 964 965 } 965 ImageIcon icon = findIcon(t )966 ImageIcon icon = findIcon(t.getKey(), t.getValue()) 966 967 // If still nothing display an empty icon 967 968 -
trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java
r15755 r15889 7 7 import java.util.Arrays; 8 8 import java.util.Collection; 9 import java.util.EnumSet; 9 10 import java.util.LinkedList; 10 11 import java.util.List; … … 20 21 import org.openstreetmap.josm.data.preferences.sources.SourceEntry; 21 22 import 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;25 23 import org.openstreetmap.josm.io.CachedFile; 26 24 import org.openstreetmap.josm.io.FileWatcher; … … 215 213 * @param tag The tag to look an icon for 216 214 * @return {@code null} if no icon found 217 */ 215 * @deprecated use {@link ImageProvider#getPadded} 216 */ 217 @Deprecated 218 218 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 for225 * @param includeDeprecatedIcon if {@code true}, the special deprecated icon will be returned if applicable226 * @return {@code null} if no icon found, or if the icon is deprecated and not wanted227 */228 public static ImageIcon getNodeIcon(Tag tag, boolean includeDeprecatedIcon) {229 219 if (tag != null) { 230 220 DataSet ds = new DataSet(); 231 221 Node virtualNode = new Node(LatLon.ZERO); 232 222 virtualNode.put(tag.getKey(), tag.getValue()); 233 StyleElementList styleList;234 223 MapCSSStyleSource.STYLE_SOURCE_LOCK.readLock().lock(); 235 224 try { 236 225 // Add primitive to dataset to avoid DataIntegrityProblemException when evaluating selectors 237 226 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 { 239 230 ds.removePrimitive(virtualNode); 240 } finally {241 231 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 wanted252 }253 }254 }255 }256 232 } 257 233 } -
trunk/src/org/openstreetmap/josm/tools/ImageProvider.java
r15675 r15889 35 35 import java.util.Base64; 36 36 import java.util.Collection; 37 import java.util.Comparator; 37 38 import java.util.EnumMap; 39 import java.util.EnumSet; 38 40 import java.util.HashMap; 39 41 import java.util.Hashtable; … … 43 45 import java.util.Map; 44 46 import java.util.Objects; 45 import java.util. TreeSet;47 import java.util.Optional; 46 48 import java.util.concurrent.CompletableFuture; 47 49 import java.util.concurrent.ConcurrentHashMap; … … 63 65 import javax.xml.parsers.ParserConfigurationException; 64 66 67 import com.kitfox.svg.SVGDiagram; 68 import com.kitfox.svg.SVGException; 69 import com.kitfox.svg.SVGUniverse; 65 70 import org.openstreetmap.josm.data.Preferences; 66 71 import org.openstreetmap.josm.data.osm.DataSet; … … 85 90 import org.xml.sax.XMLReader; 86 91 import org.xml.sax.helpers.DefaultHandler; 87 88 import com.kitfox.svg.SVGDiagram;89 import com.kitfox.svg.SVGException;90 import com.kitfox.svg.SVGUniverse;91 92 92 93 /** … … 308 309 309 310 /** 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<>(); 311 312 312 313 private static final ExecutorService IMAGE_FETCHER = … … 1493 1494 1494 1495 /** 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 /** 1495 1519 * @param primitive Object for which an icon shall be fetched. The icon is chosen based on tags. 1496 1520 * @param iconSize Target size of icon. Icon is padded if required. … … 1499 1523 */ 1500 1524 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) { 1501 1536 // Check if the current styles have special icon for tagged objects. 1502 1537 if (primitive.isTagged()) { 1503 ImageIcon icon = getTaggedPadded(primitive, iconSize );1538 ImageIcon icon = getTaggedPadded(primitive, iconSize, options); 1504 1539 if (icon != null) { 1505 1540 return icon; … … 1508 1543 1509 1544 // 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(); 1521 1554 } 1522 1555 } 1523 1556 1524 1557 // 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); 1526 1561 } 1527 1562 … … 1531 1566 * @param primitive tagged OSM primitive 1532 1567 * @param iconSize icon size in pixels 1568 * @param options zero or more {@linkplain GetPaddedOptions options}. 1533 1569 * @return a new padded icon for the given tagged primitive, or null 1534 1570 */ 1535 private static ImageIcon getTaggedPadded(OsmPrimitive primitive, Dimension iconSize ) {1571 private static ImageIcon getTaggedPadded(OsmPrimitive primitive, Dimension iconSize, Collection<GetPaddedOptions> options) { 1536 1572 Pair<StyleElementList, Range> nodeStyles; 1537 1573 DataSet ds = primitive.getDataSet(); … … 1550 1586 NodeElement nodeStyle = (NodeElement) style; 1551 1587 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"))) { 1553 1590 return getPaddedIcon(icon, iconSize); 1554 1591 } … … 1568 1605 */ 1569 1606 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) { 1570 1611 synchronized (paddedImageCache) { 1571 1612 return paddedImageCache.computeIfAbsent(iconSize, x -> new HashMap<>()).computeIfAbsent(mapImage, icon -> { 1572 1613 int backgroundRealWidth = GuiSizesHelper.getSizeDpiAdjusted(iconSize.width); 1573 1614 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); 1576 1617 BufferedImage image = new BufferedImage(backgroundRealWidth, backgroundRealHeight, BufferedImage.TYPE_INT_ARGB); 1577 1618 double scaleFactor = Math.min( 1578 1619 backgroundRealWidth / (double) iconRealWidth, 1579 1620 backgroundRealHeight / (double) iconRealHeight); 1580 Image iconImage = icon.getImage(false);1581 1621 Image scaledIcon; 1582 1622 final int scaledWidth; … … 1586 1626 scaledWidth = (int) (iconRealWidth * scaleFactor); 1587 1627 scaledHeight = (int) (iconRealHeight * scaleFactor); 1588 scaledIcon = icon Image.getScaledInstance(scaledWidth, scaledHeight, Image.SCALE_SMOOTH);1628 scaledIcon = icon.getScaledInstance(scaledWidth, scaledHeight, Image.SCALE_SMOOTH); 1589 1629 } else { 1590 1630 // Use original size, don't upscale. 1591 1631 scaledWidth = iconRealWidth; 1592 1632 scaledHeight = iconRealHeight; 1593 scaledIcon = icon Image;1633 scaledIcon = icon; 1594 1634 } 1595 1635 image.getGraphics().drawImage(scaledIcon,
Note:
See TracChangeset
for help on using the changeset viewer.