*
* While we are doing this, determine the longest row so we can properly
* draw the graphic's border.
*/
for( int i = context.getMapLayers().size() - 1; i >= 0; i-- ) {
ILayer layer = context.getMapLayers().get(i);
IGeoResource geoResource = layer.getGeoResource();
boolean isMapgraphic = geoResource.canResolve(MapGraphicResource.class);
if (!isMapgraphic && layer.isVisible()) {
// String layerName = LayerGeneratedGlyphDecorator.generateLabel((Layer) layer);
String layerName = layer.getName();
if (layerName != null && layerName.length() != 0) {
FeatureTypeStyle[] styles = locateStyle(layer);
if (styles != null && rules(styles).size() > 0) {
numberOfEntries[0] += rules(styles).size();
List<Rule> rules = rules(styles);
for( Rule rule : rules ) {
String text = getText(rule);
Rectangle2D bounds = graphics.getStringBounds(text);
int length = indentSize + boxWidth + horizontalSpacing
+ (int) bounds.getWidth();
if (length > longestRow) {
longestRow = length;
}
}
} else if (!layer.hasResource(MapGraphic.class)) {
// TODO for other layer types
continue;
} else {
continue;
}
Map<ILayer, FeatureTypeStyle[]> map = Collections.singletonMap(layer, styles);
layers.add(map);
if (styles != null && rules(styles).size() > 1) {
numberOfEntries[0]++; // add a line for the layer label
}
Rectangle2D bounds = graphics.getStringBounds(layerName);
int length = (int) bounds.getWidth();
if (styles != null && rules(styles).size() < 2) {
length += boxWidth + horizontalSpacing;
}
if (length > longestRow) {
longestRow = length;
}
}
}
}
if (numberOfEntries[0] == 0) {
// nothing to draw!
return;
}
// total width of the graphic
int width = longestRow + horizontalMargin * 2;
if (maxWidth > 0) {
if (maxWidth > width) {
width = maxWidth;
}
// width = Math.min(width, maxWidth);
}
// total height of the graphic
int height = rowHeight * numberOfEntries[0] + verticalMargin * 2 + verticalSpacing
* (numberOfEntries[0] - 1);
if (maxHeight > 0) {
if (maxHeight > height) {
height = maxHeight;
}
// height = Math.min(height, maxHeight);
}
if (locationStyle.width < 1 || locationStyle.getHeight() < 1) {
// we want to grow and shrink as we desire so we'll use a different
// rectangle than the one on the blackboard.
int x = locationStyle.x;
int y = locationStyle.y;
locationStyle = new Rectangle();
locationStyle.x = x;
locationStyle.y = y;
locationStyle.width = width;
locationStyle.height = height;
}
// ensure box within the display
Dimension displaySize = context.getMapDisplay().getDisplaySize();
if (locationStyle.x < 0) {
locationStyle.x = displaySize.width - locationStyle.width + locationStyle.x;
}
if ((locationStyle.x + locationStyle.width + 6) > displaySize.width) {
locationStyle.x = displaySize.width - width - 5;
}
if (locationStyle.y < 0) {
locationStyle.y = displaySize.height - locationStyle.height - 5 + locationStyle.y;
}
if ((locationStyle.y + height + 6) > displaySize.height) {
locationStyle.y = displaySize.height - locationStyle.height - 5;
}
graphics.setClip(new Rectangle(locationStyle.x, locationStyle.y, locationStyle.width + 1,
locationStyle.height + 1));
/*
* Draw the box containing the layers/icons
*/
drawOutline(graphics, context, locationStyle);
/*
* Draw the layer names/icons
*/
final int[] rowsDrawn = new int[1];
rowsDrawn[0] = 0;
final int[] x = new int[1];
x[0] = locationStyle.x + horizontalMargin;
final int[] y = new int[1];
y[0] = locationStyle.y + verticalMargin;
for( int i = 0; i < layers.size(); i++ ) {
Map<ILayer, FeatureTypeStyle[]> map = layers.get(i);
final ILayer layer = map.keySet().iterator().next();
final FeatureTypeStyle[] styles = map.values().iterator().next();
final String layerName = layer.getName();
PlatformGIS.syncInDisplayThread(new Runnable(){
public void run() {
if (styles != null && rules(styles).size() > 1) {
drawRow(graphics, x[0], y[0], null, layerName, false);
y[0] += rowHeight;
if ((rowsDrawn[0] + 1) < numberOfEntries[0]) {
y[0] += verticalSpacing;
}
rowsDrawn[0]++;
List<Rule> rules = rules(styles);
for( Rule rule : rules ) {
BufferedImage awtIcon = null;
if (layer.hasResource(FeatureSource.class) && rule != null) {
SimpleFeatureType type = layer.getSchema();
GeometryDescriptor geom = type.getGeometryDescriptor();
if (geom != null) {
Class geom_type = geom.getType().getBinding();
if (geom_type == Point.class || geom_type == MultiPoint.class) {
awtIcon = point(rule, boxWidth, boxHeight);