How to write an OpenMap Layer: Part 2
Introduction
This is the second part of a series of tutorials on writing OpenMap layers. The previous tutorial [hazelnutcafe.net] discussed the implementation of a simple layer, the CenteredDotPlugIn layer. This plugin simply drew a dot in the center of the map no matter what the projection or scale. In this tutorial the developed plugin will be extended with properties, allowing a developer to dictate various aspects of the layer (namely the dot) from within the properties file.
Reading Properties
Adding properties to a plugin layer is a really easy process that is implemented in almost a templated manner. The following code is an updated version of the CenteredDotPlugIn with the hooks added for retrieving properties from the properties file.
The comments explain what is going on fairly well. The plugin now has two properties, a color and an author, we'll see how these are used in just a bit. Getting string properties is rudimentary as is evident from the author property however, more complex properties (like colors) are a little tricky. Luckily the OpenMap team has provided utility methods to help in parsing these complex properties, namely the ColorFactory and PropUtils utility classes. These implement various color parsing routines and property file handling methods, respectively.
The color property needs a default provided just in case it wasn't specified in the properties file. In this case a fully opaque black is used. The ColorFactory methods can parse both 24 and 32-bit colors from text allowing the developer to choose to use transparency or not. The strings it expects are of the form AARRGGBB (32-bit) or RRGGBB (24-bit), where AA is the transparency, RR red, GG green, and BB blue. Each value is represented in hexadecimal values ranging from 00 (0) to ff (255). So, a fully opaque white would be either "ffffffff" or "ffffff" and a half transparent blue would be "800000ff".
Using Properties
Now that some properties are set within the the plugin they can be used in some novel way, such as (gasp!) setting the color of the drawn dot. The following code illustrates just that.
Setting Properties
Setting the properties of a layer is just as easy as the initial addition of the layer to the openmap.properties file. As previously stated, the properties file is nothing more than a flat text file containing key = value pairs. Also, a reference for each layer is created in the openmap.layers property and each of these references point to a specific layer class to load and its properties.
The previous code block showed how to set the two properties the plugin was expecting. Just prepend the plugin reference string (with a dot) to the name of the property and set it equal to something, easy as that!
In this case the color is set to a 75% opaque green and the author is set to the string "Guy". You may be asking yourself what the plugin is doing with the author property. The answer is nothing, it's just here to illustrate setting and retrieving a string property.
Conclusion
Stay tuned for the next tutorial where a graphical interface for setting the layer properties will be developed.
