Class ElemStyles

java.lang.Object
org.openstreetmap.josm.gui.mappaint.ElemStyles
All Implemented Interfaces:
PreferenceChangedListener

public class ElemStyles extends Object implements PreferenceChangedListener
Generates a list of StyleElements for a primitive, to be drawn on the map. There are several steps to derive the list of elements for display:
  1. generateStyles(IPrimitive, double, boolean) applies the StyleSources one after another to get a key-value map of MapCSS properties. Then a preliminary set of StyleElements is derived from the properties map.
  2. getImpl(IPrimitive, double, NavigatableComponent) handles the different forms of multipolygon tagging.
  3. getStyleCacheWithRange(IPrimitive, double, NavigatableComponent) adds a default StyleElement for primitives that would be invisible otherwise. (For example untagged nodes and ways.)
The results are cached with respect to the current scale. Use setStyleSources(Collection) to select the StyleSources that are applied.
  • Field Details

  • Constructor Details

    • ElemStyles

      public ElemStyles()
      Constructs a new ElemStyles.
    • ElemStyles

      public ElemStyles(Collection<StyleSource> sources)
      Constructs a new ElemStyles with specific style sources. This does not listen to preference changes, and therefore should only be used with layers that have specific drawing requirements.
      Parameters:
      sources - The style sources (these cannot be added to, or removed from)
      Since:
      17862
  • Method Details

    • clearCached

      public void clearCached()
      Clear the style cache for all primitives of all DataSets.
    • getStyleSources

      Returns the list of style sources.
      Returns:
      the list of style sources
    • getBackgroundColor

      Returns the background color.
      Returns:
      the background color
    • get

      public StyleElementList get(IPrimitive osm, double scale, NavigatableComponent nc)
      Create the list of styles for one primitive.
      Parameters:
      osm - the primitive
      scale - the scale (in meters per 100 pixel)
      nc - display component
      Returns:
      list of styles
      Since:
      13810 (signature)
    • getStyleCacheWithRange

      Create the list of styles and its valid scale range for one primitive. Automatically adds default styles in case no proper style was found. Uses the cache, if possible, and saves the results to the cache.
      Parameters:
      osm - OSM primitive
      scale - scale
      nc - navigable component
      Returns:
      pair containing style list and range
      Since:
      13810 (signature)
    • getImpl

      private Pair<StyleElementList,Range> getImpl(IPrimitive osm, double scale, NavigatableComponent nc)
      Create the list of styles and its valid scale range for one primitive. This method does multipolygon handling. If the primitive is a way, look for multipolygon parents. In case it is indeed member of some multipolygon as role "outer", all area styles are removed. (They apply to the multipolygon area.) Outer ways can have their own independent line styles, e.g. a road as boundary of a forest. Otherwise, in case, the way does not have an independent line style, take a line style from the multipolygon. If the multipolygon does not have a line style either, at least create a default line style from the color of the area. Now consider the case that the way is not an outer way of any multipolygon, but is member of a multipolygon as "inner". First, the style list is regenerated, considering only tags of this way. Then check, if the way describes something in its own right. (linear feature or area) If not, add a default line style from the area color of the multipolygon.
      Parameters:
      osm - OSM primitive
      scale - scale
      nc - navigable component
      Returns:
      pair containing style list and range
    • generateStyles

      public Pair<StyleElementList,Range> generateStyles(IPrimitive osm, double scale, boolean pretendWayIsClosed)
      Create the list of styles and its valid scale range for one primitive. Loops over the list of style sources, to generate the map of properties. From these properties, it generates the different types of styles.
      Parameters:
      osm - the primitive to create styles for
      scale - the scale (in meters per 100 px), must be > 0
      pretendWayIsClosed - For styles that require the way to be closed, we pretend it is. This is useful for generating area styles from the (segmented) outer ways of a multipolygon.
      Returns:
      the generated styles and the valid range as a pair
      Since:
      13810 (signature)
    • addIfNotNull

      private static <T> void addIfNotNull(List<T> list, T obj)
    • isDefaultNodes

      private boolean isDefaultNodes()
      Draw a default node symbol for nodes that have no style?
      Returns:
      true if default node symbol must be drawn
    • isDefaultLines

      private boolean isDefaultLines()
      Draw a default line for ways that do not have an own line style?
      Returns:
      true if default line must be drawn
    • fromCanvas

      private <T> T fromCanvas(String key, T def, Class<T> c)
    • isDrawMultipolygon

      public boolean isDrawMultipolygon()
      Determines whether multipolygons must be drawn.
      Returns:
      whether multipolygons must be drawn.
    • setDrawMultipolygon

      public void setDrawMultipolygon(boolean drawMultipolygon)
      Sets whether multipolygons must be drawn.
      Parameters:
      drawMultipolygon - whether multipolygons must be drawn
    • clear

      void clear()
      remove all style sources; only accessed from MapPaintStyles
    • add

      void add(StyleSource style)
      add a style source; only accessed from MapPaintStyles
      Parameters:
      style - style source to add
    • remove

      boolean remove(StyleSource style)
      remove a style source; only accessed from MapPaintStyles
      Parameters:
      style - style source to remove
      Returns:
      true if this list contained the specified element
    • setStyleSources

      set the style sources; only accessed from MapPaintStyles
      Parameters:
      sources - new style sources
    • getAreaElemStyle

      public static AreaElement getAreaElemStyle(IPrimitive p, boolean pretendWayIsClosed)
      Returns the first AreaElement for a given primitive.
      Parameters:
      p - the OSM primitive
      pretendWayIsClosed - For styles that require the way to be closed, we pretend it is. This is useful for generating area styles from the (segmented) outer ways of a multipolygon.
      Returns:
      first AreaElement found or null.
      Since:
      13810 (signature)
    • hasAreaElemStyle

      public static boolean hasAreaElemStyle(IPrimitive p, boolean pretendWayIsClosed)
      Determines whether primitive has an AreaElement.
      Parameters:
      p - the OSM primitive
      pretendWayIsClosed - For styles that require the way to be closed, we pretend it is. This is useful for generating area styles from the (segmented) outer ways of a multipolygon.
      Returns:
      true if primitive has an AreaElement
      Since:
      13810 (signature)
    • hasOnlyAreaElements

      public static boolean hasOnlyAreaElements(IPrimitive p)
      Determines whether primitive has area-type StyleElements, but no line-type StyleElements. TextElement is ignored, as it can be both line and area-type.
      Parameters:
      p - the OSM primitive
      Returns:
      true if primitive has area elements, but no line elements
      Since:
      12700, 13810 (signature)
    • getPreferenceCached

      public String getPreferenceCached(StyleSource source, String key, String def)
      Looks up a preference value and ensures the style cache is invalidated as soon as this preference value is changed by the user. In addition, it adds an intermediate cache for the preference values, as frequent preference lookup (using Config.getPref().get()) for each primitive can be slow during rendering. If the default value can be converted to a Color, the NamedColorProperty is retrieved as string.
      Parameters:
      source - style source
      key - preference key
      def - default value
      Returns:
      the corresponding preference value
      See Also:
    • preferenceChanged

      Description copied from interface: PreferenceChangedListener
      Triggered when a preference entry value changes.
      Specified by:
      preferenceChanged in interface PreferenceChangedListener
      Parameters:
      e - the preference change event