Class ElemStyles
- java.lang.Object
-
- org.openstreetmap.josm.gui.mappaint.ElemStyles
-
- All Implemented Interfaces:
PreferenceChangedListener
public class ElemStyles extends java.lang.Object implements PreferenceChangedListener
Generates a list ofStyleElement
s for a primitive, to be drawn on the map. There are several steps to derive the list of elements for display:generateStyles(IPrimitive, double, boolean)
applies theStyleSource
s one after another to get a key-value map of MapCSS properties. Then a preliminary set of StyleElements is derived from the properties map.getImpl(IPrimitive, double, NavigatableComponent)
handles the different forms of multipolygon tagging.getStyleCacheWithRange(IPrimitive, double, NavigatableComponent)
adds a default StyleElement for primitives that would be invisible otherwise. (For example untagged nodes and ways.)
setStyleSources(Collection)
to select the StyleSources that are applied.
-
-
Field Summary
Fields Modifier and Type Field Description private java.awt.Color
backgroundColorCache
private short
cacheIdx
private boolean
defaultLines
private short
defaultLinesIdx
private boolean
defaultNodes
private short
defaultNodesIdx
private boolean
drawMultipolygon
private java.util.Map<java.lang.String,java.lang.String>
preferenceCache
private java.util.List<StyleSource>
styleSources
-
Constructor Summary
Constructors Constructor Description ElemStyles()
Constructs a newElemStyles
.ElemStyles(java.util.Collection<StyleSource> sources)
Constructs a newElemStyles
with specific style sources.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) void
add(StyleSource style)
add a style source; only accessed from MapPaintStylesprivate static <T> void
addIfNotNull(java.util.List<T> list, T obj)
(package private) void
clear()
remove all style sources; only accessed from MapPaintStylesvoid
clearCached()
Clear the style cache for all primitives of all DataSets.private <T> T
fromCanvas(java.lang.String key, T def, java.lang.Class<T> c)
Pair<StyleElementList,Range>
generateStyles(IPrimitive osm, double scale, boolean pretendWayIsClosed)
Create the list of styles and its valid scale range for one primitive.StyleElementList
get(IPrimitive osm, double scale, NavigatableComponent nc)
Create the list of styles for one primitive.static AreaElement
getAreaElemStyle(IPrimitive p, boolean pretendWayIsClosed)
Returns the first AreaElement for a given primitive.java.awt.Color
getBackgroundColor()
Returns the background color.private Pair<StyleElementList,Range>
getImpl(IPrimitive osm, double scale, NavigatableComponent nc)
Create the list of styles and its valid scale range for one primitive.java.lang.String
getPreferenceCached(StyleSource source, java.lang.String key, java.lang.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.Pair<StyleElementList,Range>
getStyleCacheWithRange(IPrimitive osm, double scale, NavigatableComponent nc)
Create the list of styles and its valid scale range for one primitive.java.util.List<StyleSource>
getStyleSources()
Returns the list of style sources.static boolean
hasAreaElemStyle(IPrimitive p, boolean pretendWayIsClosed)
Determines whether primitive has an AreaElement.static boolean
hasOnlyAreaElements(IPrimitive p)
Determines whether primitive has area-typeStyleElement
s, but no line-type StyleElements.private boolean
isDefaultLines()
Draw a default line for ways that do not have an own line style?private boolean
isDefaultNodes()
Draw a default node symbol for nodes that have no style?boolean
isDrawMultipolygon()
Determines whether multipolygons must be drawn.void
preferenceChanged(PreferenceChangeEvent e)
Triggered when a preference entry value changes.(package private) boolean
remove(StyleSource style)
remove a style source; only accessed from MapPaintStylesvoid
setDrawMultipolygon(boolean drawMultipolygon)
Sets whether multipolygons must be drawn.(package private) void
setStyleSources(java.util.Collection<StyleSource> sources)
set the style sources; only accessed from MapPaintStyles
-
-
-
Field Detail
-
styleSources
private final java.util.List<StyleSource> styleSources
-
drawMultipolygon
private boolean drawMultipolygon
-
cacheIdx
private short cacheIdx
-
defaultNodes
private boolean defaultNodes
-
defaultLines
private boolean defaultLines
-
defaultNodesIdx
private short defaultNodesIdx
-
defaultLinesIdx
private short defaultLinesIdx
-
preferenceCache
private final java.util.Map<java.lang.String,java.lang.String> preferenceCache
-
backgroundColorCache
private volatile java.awt.Color backgroundColorCache
-
-
Constructor Detail
-
ElemStyles
public ElemStyles()
Constructs a newElemStyles
.
-
ElemStyles
public ElemStyles(java.util.Collection<StyleSource> sources)
Constructs a newElemStyles
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 Detail
-
clearCached
public void clearCached()
Clear the style cache for all primitives of all DataSets.
-
getStyleSources
public java.util.List<StyleSource> getStyleSources()
Returns the list of style sources.- Returns:
- the list of style sources
-
getBackgroundColor
public java.awt.Color 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 primitivescale
- the scale (in meters per 100 pixel)nc
- display component- Returns:
- list of styles
- Since:
- 13810 (signature)
-
getStyleCacheWithRange
public Pair<StyleElementList,Range> getStyleCacheWithRange(IPrimitive osm, double scale, NavigatableComponent nc)
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 primitivescale
- scalenc
- 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 primitivescale
- scalenc
- 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 forscale
- the scale (in meters per 100 px), must be > 0pretendWayIsClosed
- 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(java.util.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(java.lang.String key, T def, java.lang.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
void setStyleSources(java.util.Collection<StyleSource> sources)
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 primitivepretendWayIsClosed
- 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 primitivepretendWayIsClosed
- 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-typeStyleElement
s, 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 java.lang.String getPreferenceCached(StyleSource source, java.lang.String key, java.lang.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 (usingConfig.getPref().get()
) for each primitive can be slow during rendering. If the default value can be converted to aColor
, theNamedColorProperty
is retrieved as string.- Parameters:
source
- style sourcekey
- preference keydef
- default value- Returns:
- the corresponding preference value
- See Also:
AbstractPreferences.get(String, String)
-
preferenceChanged
public void preferenceChanged(PreferenceChangeEvent e)
Description copied from interface:PreferenceChangedListener
Triggered when a preference entry value changes.- Specified by:
preferenceChanged
in interfacePreferenceChangedListener
- Parameters:
e
- the preference change event
-
-