/[schmitzm]/trunk/src/skrueger/geotools/LegendIconFeatureRenderer.java
ViewVC logotype

Diff of /trunk/src/skrueger/geotools/LegendIconFeatureRenderer.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 360 by alfonx, Wed Aug 26 17:15:49 2009 UTC revision 361 by alfonx, Mon Aug 31 18:21:17 2009 UTC
# Line 243  public class LegendIconFeatureRenderer e Line 243  public class LegendIconFeatureRenderer e
243    
244                  return sampleShape;                  return sampleShape;
245          }          }
246    //
247          /**  //      /**
248           * Puts a BufferedImage into this.legendGraphic  //       * Puts a BufferedImage into this.legendGraphic
249           */  //       */
250          public void produceLegendGraphic(SimpleFeatureType featureType, Style gt2Style,  //      public void produceLegendGraphic(SimpleFeatureType featureType, Style gt2Style,
251                          Rule[] applicableRules) {  //                      Rule[] applicableRules) {
252    //
253                  // final FeatureTypeStyle[] ftStyles = gt2Style.getFeatureTypeStyles();  //              // final FeatureTypeStyle[] ftStyles = gt2Style.getFeatureTypeStyles();
254    //
255                  // if (request.getRule() != null) {  //              // if (request.getRule() != null) {
256                  // applicableRules = new Rule[] { request.getRule() };  //              // applicableRules = new Rule[] { request.getRule() };
257                  // } else {  //              // } else {
258                  // applicableRules = getApplicableRules(ftStyles, scaleDenominator);  //              // applicableRules = getApplicableRules(ftStyles, scaleDenominator);
259                  // }  //              // }
260    //
261                  final int ruleCount = applicableRules.length;  //              final int ruleCount = applicableRules.length;
262    //
263                  /**  //              /**
264                   * A legend graphic is produced for each applicable rule. They're being  //               * A legend graphic is produced for each applicable rule. They're being
265                   * holded here until the process is done and then painted on a "stack"  //               * holded here until the process is done and then painted on a "stack"
266                   * like legend.  //               * like legend.
267                   */  //               */
268                  final List<BufferedImage> legendsStack = new ArrayList<BufferedImage>(  //              final List<BufferedImage> legendsStack = new ArrayList<BufferedImage>(
269                                  ruleCount);  //                              ruleCount);
270    //
271                  for (int i = 0; i < ruleCount; i++) {  //              for (int i = 0; i < ruleCount; i++) {
272                          BufferedImage image = createImageForRule(applicableRules[i],  //                      BufferedImage image = createImageForRule(applicableRules[i],
273                                          featureType, SIZE);  //                                      featureType, SIZE);
274                          legendsStack.add(image);  //                      legendsStack.add(image);
275                  }  //              }
276    //
277                  // this.legendGraphic =  //              // this.legendGraphic =
278                  // scaleImage(mergeLegends(legendsStack,applicableRules), request);  //              // scaleImage(mergeLegends(legendsStack,applicableRules), request);
279                  this.legendGraphic = mergeLegends(legendsStack, applicableRules);  //              this.legendGraphic = mergeLegends(legendsStack, applicableRules);
280          }  //      }
281    //
282          /**  //      /**
283           * Recieves a list of <code>BufferedImages</code> and produces a new one  //       * Recieves a list of <code>BufferedImages</code> and produces a new one
284           * which holds all the images in <code>imageStack</code> one above the  //       * which holds all the images in <code>imageStack</code> one above the
285           * other.  //       * other.
286           *  //       *
287           * @param imageStack  //       * @param imageStack
288           *            the list of BufferedImages, one for each applicable Rule  //       *            the list of BufferedImages, one for each applicable Rule
289           * @param rules  //       * @param rules
290           *            The applicable rules, one for each image in the stack  //       *            The applicable rules, one for each image in the stack
291           * @param request  //       * @param request
292           *            The request.  //       *            The request.
293           *  //       *
294           * @return the stack image with all the images on the argument list.  //       * @return the stack image with all the images on the argument list.
295           *  //       *
296           * @throws IllegalArgumentException  //       * @throws IllegalArgumentException
297           *             if the list is empty  //       *             if the list is empty
298           */  //       */
299          private static BufferedImage mergeLegends(List<BufferedImage> imageStack,  //      private static BufferedImage mergeLegends(List<BufferedImage> imageStack,
300                          Rule[] rules) {  //                      Rule[] rules) {
301                  if (imageStack.size() == 0) {  //              if (imageStack.size() == 0) {
302                          throw new IllegalArgumentException("No legend graphics passed");  //                      throw new IllegalArgumentException("No legend graphics passed");
303                  }  //              }
304    //
305                  BufferedImage finalLegend = null;  //              BufferedImage finalLegend = null;
306    //
307                  if (imageStack.size() == 1) {  //              if (imageStack.size() == 1) {
308                          finalLegend = (BufferedImage) imageStack.get(0);  //                      finalLegend = (BufferedImage) imageStack.get(0);
309                  } else {  //              } else {
310                          final int imgCount = imageStack.size();  //                      final int imgCount = imageStack.size();
311                          final String[] labels = new String[imgCount];  //                      final String[] labels = new String[imgCount];
312    //
313                          BufferedImage img = ((BufferedImage) imageStack.get(0));  //                      BufferedImage img = ((BufferedImage) imageStack.get(0));
314                          FontMetrics fontMetrics = img.getGraphics().getFontMetrics();  //                      FontMetrics fontMetrics = img.getGraphics().getFontMetrics();
315    //
316                          final int rowHeight = Math.max(fontMetrics.getHeight(), img  //                      final int rowHeight = Math.max(fontMetrics.getHeight(), img
317                                          .getHeight());  //                                      .getHeight());
318    //
319                          // calculate the total dimensions of the image  //                      // calculate the total dimensions of the image
320                          int totalHeight = rowHeight * imgCount;  //                      int totalHeight = rowHeight * imgCount;
321                          int totalWidth = 0;  //                      int totalWidth = 0;
322    //
323                          for (int i = 0; i < imgCount; i++) {  //                      for (int i = 0; i < imgCount; i++) {
324                                  img = (BufferedImage) imageStack.get(i);  //                              img = (BufferedImage) imageStack.get(i);
325    //
326                                  Rule rule = rules[i];  //                              Rule rule = rules[i];
327    //
328                                  // does this rule have a label  //                              // does this rule have a label
329                                  labels[i] = rule.getTitle();  //                              labels[i] = rule.getTitle();
330    //
331                                  if (labels[i] == null) {  //                              if (labels[i] == null) {
332                                          labels[i] = rule.getName();  //                                      labels[i] = rule.getName();
333                                  }  //                              }
334    //
335                                  int w = img.getWidth();  //                              int w = img.getWidth();
336    //
337                                  if (labels[i] != null) {  //                              if (labels[i] != null) {
338                                          Graphics g = img.getGraphics();  //                                      Graphics g = img.getGraphics();
339                                          w += g.getFontMetrics().stringWidth(labels[i]);  //                                      w += g.getFontMetrics().stringWidth(labels[i]);
340                                  }  //                              }
341    //
342                                  totalWidth = Math.max(w, totalWidth);  //                              totalWidth = Math.max(w, totalWidth);
343                          }  //                      }
344    //
345                          // create the final image  //                      // create the final image
346                          finalLegend = new BufferedImage(totalWidth, totalHeight,  //                      finalLegend = new BufferedImage(totalWidth, totalHeight,
347                                          BufferedImage.TYPE_INT_ARGB);  //                                      BufferedImage.TYPE_INT_ARGB);
348    //
349                          Graphics2D finalGraphics = finalLegend.createGraphics();  //                      Graphics2D finalGraphics = finalLegend.createGraphics();
350    //
351                          finalGraphics.setColor(Color.white);  //                      finalGraphics.setColor(Color.white);
352                          finalGraphics.fillRect(0, 0, totalWidth, totalHeight);  //                      finalGraphics.fillRect(0, 0, totalWidth, totalHeight);
353    //
354                          int h = 0;  //                      int h = 0;
355    //
356                          for (int i = 0; i < imgCount; i++) {  //                      for (int i = 0; i < imgCount; i++) {
357                                  img = (BufferedImage) imageStack.get(i);  //                              img = (BufferedImage) imageStack.get(i);
358    //
359                                  // draw the image  //                              // draw the image
360                                  int y = h;  //                              int y = h;
361    //
362                                  if (img.getHeight() < rowHeight) {  //                              if (img.getHeight() < rowHeight) {
363                                          // move the image to the center of the row  //                                      // move the image to the center of the row
364                                          y += (int) ((rowHeight - img.getHeight()) / 2d);  //                                      y += (int) ((rowHeight - img.getHeight()) / 2d);
365                                  }  //                              }
366    //
367                                  finalGraphics.drawImage(img, 0, y, imgObs);  //                              finalGraphics.drawImage(img, 0, y, imgObs);
368    //
369                                  // draw the label  //                              // draw the label
370                                  if (labels[i] != null) {  //                              if (labels[i] != null) {
371                                          finalGraphics.setColor(Color.BLACK);  //                                      finalGraphics.setColor(Color.BLACK);
372    //
373                                          y = (h + rowHeight) - fontMetrics.getDescent();  //                                      y = (h + rowHeight) - fontMetrics.getDescent();
374    //
375                                          if (fontMetrics.getHeight() < rowHeight) {  //                                      if (fontMetrics.getHeight() < rowHeight) {
376                                                  // move the baseline to the center of the row  //                                              // move the baseline to the center of the row
377                                                  y -= (int) ((rowHeight - fontMetrics.getHeight()) / 2d);  //                                              y -= (int) ((rowHeight - fontMetrics.getHeight()) / 2d);
378                                          }  //                                      }
379    //
380                                          finalGraphics.drawString(labels[i], img.getWidth(), y);  //                                      finalGraphics.drawString(labels[i], img.getWidth(), y);
381                                  }  //                              }
382    //
383                                  h += rowHeight;  //                              h += rowHeight;
384                          }  //                      }
385                  }  //              }
386    //
387                  return finalLegend;  //              return finalLegend;
388          }  //      }
389    //
390          /**  //      /**
391           * DOCUMENT ME!  //       * DOCUMENT ME!
392           *  //       *
393           * @return  //       * @return
394           *  //       *
395           * @throws IllegalStateException  //       * @throws IllegalStateException
396           *             DOCUMENT ME!  //       *             DOCUMENT ME!
397           */  //       */
398          public BufferedImage getLegendGraphic() {  //      public BufferedImage getLegendGraphic() {
399                  if (this.legendGraphic == null) {  //              if (this.legendGraphic == null) {
400                          throw new IllegalStateException();  //                      throw new IllegalStateException();
401                  }  //              }
402                  return this.legendGraphic;  //              return this.legendGraphic;
403          }  //      }
404    
405          // /**          // /**
406          // * Paints a little rectangle in the color defined by the          // * Paints a little rectangle in the color defined by the

Legend:
Removed from v.360  
changed lines
  Added in v.361

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26