Since labels are glyphs we had to configure the map for the glyph then reconfigure it back to the way it was. Suppose we didn't. If the next glyph line did not have a configuration string it would be another label glyph.
This is clumsy. However, we can set aside a configuration just for labels. The way this is done is to add a "factory" to the map. Then when a label glyph is added the factory can be referred to and the label will be made according to that factory's configuration.
The changes are very small. Add a factory object as a new instance variable.
MapGlyphFactory labelFactory;The factory is created, configured, and added to the map in one step. So add the following line to the constructor.
labelFactory = map.addFactory("-glyphtype LabelGlyph");The following portion of
parseLine
can now be simplified
from
if (parseLabel(theTokens, item)) { this.map.configure(configuration); // Reconfigure because of label. }to
parseLabel(theTokens, item);Finally, the label is added without configuring the map. Instead addItem is called with a reference to our factory. Hence, change the lines
this.map.configure("-glyphtype LabelGlyph"); LabelGlyph label = (LabelGlyph)this.map.addItem(0, 0);to
LabelGlyph label = (LabelGlyph)map.addItem(labelFactory, 0, 0);Here is the way the applet looks now, using the same inputs as on the previous page.
Here is the full source.