/[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 332 by alfonx, Wed Aug 26 17:15:49 2009 UTC revision 452 by alfonx, Fri Oct 9 19:16:27 2009 UTC
# Line 42  Line 42 
42   **/   **/
43  package skrueger.geotools;  package skrueger.geotools;
44    
 import java.awt.Canvas;  
45  import java.awt.Color;  import java.awt.Color;
46  import java.awt.Dimension;  import java.awt.Dimension;
 import java.awt.FontMetrics;  
 import java.awt.Graphics;  
47  import java.awt.Graphics2D;  import java.awt.Graphics2D;
48  import java.awt.RenderingHints;  import java.awt.RenderingHints;
49  import java.awt.image.BufferedImage;  import java.awt.image.BufferedImage;
 import java.awt.image.ImageObserver;  
 import java.util.ArrayList;  
 import java.util.List;  
50    
51  import javax.swing.tree.DefaultTreeCellRenderer;  import javax.swing.tree.DefaultTreeCellRenderer;
52    
# Line 101  import com.vividsolutions.jts.geom.Polyg Line 95  import com.vividsolutions.jts.geom.Polyg
95   * @author Stefan Alfons Krüger   * @author Stefan Alfons Krüger
96   */   */
97  public class LegendIconFeatureRenderer extends DefaultTreeCellRenderer {  public class LegendIconFeatureRenderer extends DefaultTreeCellRenderer {
98          private static final Dimension SIZE = new Dimension(30,20);  //      private static final Dimension SIZE = new Dimension(30,20);
99    
100          Logger LOGGER = Logger.getLogger(LegendIconFeatureRenderer.class);          Logger LOGGER = Logger.getLogger(LegendIconFeatureRenderer.class);
101    
# Line 115  public class LegendIconFeatureRenderer e Line 109  public class LegendIconFeatureRenderer e
109    
110          public static LegendIconFeatureRenderer getInstance() {          public static LegendIconFeatureRenderer getInstance() {
111                  // In GT 2.4.5 it we have to create a new one all the time!                  // In GT 2.4.5 it we have to create a new one all the time!
112                    
113                    // TODO TEST, whether we still need to recreate the renderer here....
114                    // TODO TEST, whether we still need to recreate the renderer here....
115                    // TODO TEST, whether we still need to recreate the renderer here....
116                    // TODO TEST, whether we still need to recreate the renderer here....
117                    // TODO TEST, whether we still need to recreate the renderer here....
118                    // TODO TEST, whether we still need to recreate the renderer here....
119                    // TODO TEST, whether we still need to recreate the renderer here....
120                    // TODO TEST, whether we still need to recreate the renderer here....
121                  return new LegendIconFeatureRenderer();                  return new LegendIconFeatureRenderer();
122  //               return instance;  //               return instance;
123          }          }
124    
         /** The image produced at <code>produceLegendGraphic</code> */  
         private BufferedImage legendGraphic;  
   
125          /**          /**
126           * used to create sample point shapes with LiteShape (not lines nor           * used to create sample point shapes with LiteShape (not lines nor
127           * polygons)           * polygons)
128           */           */
129          private static final GeometryFactory geomFac = new GeometryFactory();          private static final GeometryFactory geomFac = FeatureUtil.GEOMETRY_FACTORY;
130    
131          /** padding percentage factor at both sides of the legend. */          /** padding percentage factor at both sides of the legend. */
132          private static final float hpaddingFactor = 0.11f; // was 0.15          private static final float hpaddingFactor = 0.11f; // was 0.15
# Line 135  public class LegendIconFeatureRenderer e Line 135  public class LegendIconFeatureRenderer e
135          private static final float vpaddingFactor = 0.08f; // was 0.15          private static final float vpaddingFactor = 0.08f; // was 0.15
136    
137          /**          /**
          * Image observer to help in creating the stack like legend graphic from the  
          * images created for each rule  
          */  
         private static final ImageObserver imgObs = new Canvas();  
   
         /**  
138           * Just a holder to avoid creating many polygon shapes from inside           * Just a holder to avoid creating many polygon shapes from inside
139           * <code>getSampleShape()</code>           * <code>getSampleShape()</code>
140           */           */
# Line 243  public class LegendIconFeatureRenderer e Line 237  public class LegendIconFeatureRenderer e
237    
238                  return sampleShape;                  return sampleShape;
239          }          }
240    //
241          /**  //      /**
242           * Puts a BufferedImage into this.legendGraphic  //       * Puts a BufferedImage into this.legendGraphic
243           */  //       */
244          public void produceLegendGraphic(SimpleFeatureType featureType, Style gt2Style,  //      public void produceLegendGraphic(SimpleFeatureType featureType, Style gt2Style,
245                          Rule[] applicableRules) {  //                      Rule[] applicableRules) {
246    //
247                  // final FeatureTypeStyle[] ftStyles = gt2Style.getFeatureTypeStyles();  //              // final FeatureTypeStyle[] ftStyles = gt2Style.getFeatureTypeStyles();
248    //
249                  // if (request.getRule() != null) {  //              // if (request.getRule() != null) {
250                  // applicableRules = new Rule[] { request.getRule() };  //              // applicableRules = new Rule[] { request.getRule() };
251                  // } else {  //              // } else {
252                  // applicableRules = getApplicableRules(ftStyles, scaleDenominator);  //              // applicableRules = getApplicableRules(ftStyles, scaleDenominator);
253                  // }  //              // }
254    //
255                  final int ruleCount = applicableRules.length;  //              final int ruleCount = applicableRules.length;
256    //
257                  /**  //              /**
258                   * A legend graphic is produced for each applicable rule. They're being  //               * A legend graphic is produced for each applicable rule. They're being
259                   * 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"
260                   * like legend.  //               * like legend.
261                   */  //               */
262                  final List<BufferedImage> legendsStack = new ArrayList<BufferedImage>(  //              final List<BufferedImage> legendsStack = new ArrayList<BufferedImage>(
263                                  ruleCount);  //                              ruleCount);
264    //
265                  for (int i = 0; i < ruleCount; i++) {  //              for (int i = 0; i < ruleCount; i++) {
266                          BufferedImage image = createImageForRule(applicableRules[i],  //                      BufferedImage image = createImageForRule(applicableRules[i],
267                                          featureType, SIZE);  //                                      featureType, SIZE);
268                          legendsStack.add(image);  //                      legendsStack.add(image);
269                  }  //              }
270    //
271                  // this.legendGraphic =  //              // this.legendGraphic =
272                  // scaleImage(mergeLegends(legendsStack,applicableRules), request);  //              // scaleImage(mergeLegends(legendsStack,applicableRules), request);
273                  this.legendGraphic = mergeLegends(legendsStack, applicableRules);  //              this.legendGraphic = mergeLegends(legendsStack, applicableRules);
274          }  //      }
275    //
276          /**  //      /**
277           * Recieves a list of <code>BufferedImages</code> and produces a new one  //       * Recieves a list of <code>BufferedImages</code> and produces a new one
278           * which holds all the images in <code>imageStack</code> one above the  //       * which holds all the images in <code>imageStack</code> one above the
279           * other.  //       * other.
280           *  //       *
281           * @param imageStack  //       * @param imageStack
282           *            the list of BufferedImages, one for each applicable Rule  //       *            the list of BufferedImages, one for each applicable Rule
283           * @param rules  //       * @param rules
284           *            The applicable rules, one for each image in the stack  //       *            The applicable rules, one for each image in the stack
285           * @param request  //       * @param request
286           *            The request.  //       *            The request.
287           *  //       *
288           * @return the stack image with all the images on the argument list.  //       * @return the stack image with all the images on the argument list.
289           *  //       *
290           * @throws IllegalArgumentException  //       * @throws IllegalArgumentException
291           *             if the list is empty  //       *             if the list is empty
292           */  //       */
293          private static BufferedImage mergeLegends(List<BufferedImage> imageStack,  //      private static BufferedImage mergeLegends(List<BufferedImage> imageStack,
294                          Rule[] rules) {  //                      Rule[] rules) {
295                  if (imageStack.size() == 0) {  //              if (imageStack.size() == 0) {
296                          throw new IllegalArgumentException("No legend graphics passed");  //                      throw new IllegalArgumentException("No legend graphics passed");
297                  }  //              }
298    //
299                  BufferedImage finalLegend = null;  //              BufferedImage finalLegend = null;
300    //
301                  if (imageStack.size() == 1) {  //              if (imageStack.size() == 1) {
302                          finalLegend = (BufferedImage) imageStack.get(0);  //                      finalLegend = (BufferedImage) imageStack.get(0);
303                  } else {  //              } else {
304                          final int imgCount = imageStack.size();  //                      final int imgCount = imageStack.size();
305                          final String[] labels = new String[imgCount];  //                      final String[] labels = new String[imgCount];
306    //
307                          BufferedImage img = ((BufferedImage) imageStack.get(0));  //                      BufferedImage img = ((BufferedImage) imageStack.get(0));
308                          FontMetrics fontMetrics = img.getGraphics().getFontMetrics();  //                      FontMetrics fontMetrics = img.getGraphics().getFontMetrics();
309    //
310                          final int rowHeight = Math.max(fontMetrics.getHeight(), img  //                      final int rowHeight = Math.max(fontMetrics.getHeight(), img
311                                          .getHeight());  //                                      .getHeight());
312    //
313                          // calculate the total dimensions of the image  //                      // calculate the total dimensions of the image
314                          int totalHeight = rowHeight * imgCount;  //                      int totalHeight = rowHeight * imgCount;
315                          int totalWidth = 0;  //                      int totalWidth = 0;
316    //
317                          for (int i = 0; i < imgCount; i++) {  //                      for (int i = 0; i < imgCount; i++) {
318                                  img = (BufferedImage) imageStack.get(i);  //                              img = (BufferedImage) imageStack.get(i);
319    //
320                                  Rule rule = rules[i];  //                              Rule rule = rules[i];
321    //
322                                  // does this rule have a label  //                              // does this rule have a label
323                                  labels[i] = rule.getTitle();  //                              labels[i] = rule.getTitle();
324    //
325                                  if (labels[i] == null) {  //                              if (labels[i] == null) {
326                                          labels[i] = rule.getName();  //                                      labels[i] = rule.getName();
327                                  }  //                              }
328    //
329                                  int w = img.getWidth();  //                              int w = img.getWidth();
330    //
331                                  if (labels[i] != null) {  //                              if (labels[i] != null) {
332                                          Graphics g = img.getGraphics();  //                                      Graphics g = img.getGraphics();
333                                          w += g.getFontMetrics().stringWidth(labels[i]);  //                                      w += g.getFontMetrics().stringWidth(labels[i]);
334                                  }  //                              }
335    //
336                                  totalWidth = Math.max(w, totalWidth);  //                              totalWidth = Math.max(w, totalWidth);
337                          }  //                      }
338    //
339                          // create the final image  //                      // create the final image
340                          finalLegend = new BufferedImage(totalWidth, totalHeight,  //                      finalLegend = new BufferedImage(totalWidth, totalHeight,
341                                          BufferedImage.TYPE_INT_ARGB);  //                                      BufferedImage.TYPE_INT_ARGB);
342    //
343                          Graphics2D finalGraphics = finalLegend.createGraphics();  //                      Graphics2D finalGraphics = finalLegend.createGraphics();
344    //
345                          finalGraphics.setColor(Color.white);  //                      finalGraphics.setColor(Color.white);
346                          finalGraphics.fillRect(0, 0, totalWidth, totalHeight);  //                      finalGraphics.fillRect(0, 0, totalWidth, totalHeight);
347    //
348                          int h = 0;  //                      int h = 0;
349    //
350                          for (int i = 0; i < imgCount; i++) {  //                      for (int i = 0; i < imgCount; i++) {
351                                  img = (BufferedImage) imageStack.get(i);  //                              img = (BufferedImage) imageStack.get(i);
352    //
353                                  // draw the image  //                              // draw the image
354                                  int y = h;  //                              int y = h;
355    //
356                                  if (img.getHeight() < rowHeight) {  //                              if (img.getHeight() < rowHeight) {
357                                          // move the image to the center of the row  //                                      // move the image to the center of the row
358                                          y += (int) ((rowHeight - img.getHeight()) / 2d);  //                                      y += (int) ((rowHeight - img.getHeight()) / 2d);
359                                  }  //                              }
360    //
361                                  finalGraphics.drawImage(img, 0, y, imgObs);  //                              finalGraphics.drawImage(img, 0, y, imgObs);
362    //
363                                  // draw the label  //                              // draw the label
364                                  if (labels[i] != null) {  //                              if (labels[i] != null) {
365                                          finalGraphics.setColor(Color.BLACK);  //                                      finalGraphics.setColor(Color.BLACK);
366    //
367                                          y = (h + rowHeight) - fontMetrics.getDescent();  //                                      y = (h + rowHeight) - fontMetrics.getDescent();
368    //
369                                          if (fontMetrics.getHeight() < rowHeight) {  //                                      if (fontMetrics.getHeight() < rowHeight) {
370                                                  // move the baseline to the center of the row  //                                              // move the baseline to the center of the row
371                                                  y -= (int) ((rowHeight - fontMetrics.getHeight()) / 2d);  //                                              y -= (int) ((rowHeight - fontMetrics.getHeight()) / 2d);
372                                          }  //                                      }
373    //
374                                          finalGraphics.drawString(labels[i], img.getWidth(), y);  //                                      finalGraphics.drawString(labels[i], img.getWidth(), y);
375                                  }  //                              }
376    //
377                                  h += rowHeight;  //                              h += rowHeight;
378                          }  //                      }
379                  }  //              }
380    //
381                  return finalLegend;  //              return finalLegend;
382          }  //      }
383    //
384          /**  //      /**
385           * DOCUMENT ME!  //       * DOCUMENT ME!
386           *  //       *
387           * @return  //       * @return
388           *  //       *
389           * @throws IllegalStateException  //       * @throws IllegalStateException
390           *             DOCUMENT ME!  //       *             DOCUMENT ME!
391           */  //       */
392          public BufferedImage getLegendGraphic() {  //      public BufferedImage getLegendGraphic() {
393                  if (this.legendGraphic == null) {  //              if (this.legendGraphic == null) {
394                          throw new IllegalStateException();  //                      throw new IllegalStateException();
395                  }  //              }
396                  return this.legendGraphic;  //              return this.legendGraphic;
397          }  //      }
398    
399          // /**          // /**
400          // * Paints a little rectangle in the color defined by the          // * Paints a little rectangle in the color defined by the
# Line 484  public class LegendIconFeatureRenderer e Line 478  public class LegendIconFeatureRenderer e
478    
479                  // TODO scaleDenominator = 100000000000000000000000000000d; Was ist das                  // TODO scaleDenominator = 100000000000000000000000000000d; Was ist das
480                  // fuer ein Quatsch?                  // fuer ein Quatsch?
481                  final double scaleDenominator = 100000000000000000000000000000d;                  final NumberRange<Integer> scaleRange = NumberRange.create(Integer.MAX_VALUE,Integer.MAX_VALUE);
                 final NumberRange scaleRange = new NumberRange(scaleDenominator,  
                                 scaleDenominator);  
482    
483                  try {                  try {
484    
# Line 517  public class LegendIconFeatureRenderer e Line 509  public class LegendIconFeatureRenderer e
509    
510                                  if (style2d != null) {                                  if (style2d != null) {
511                                          new StyledShapePainter(null).paint(graphics, shape,                                          new StyledShapePainter(null).paint(graphics, shape,
512                                                          style2d, scaleDenominator);                                                          style2d, Integer.MAX_VALUE );
513                                  }                                  }
514                          }                          }
515                  } catch (Exception e) {                  } catch (Exception e) {

Legend:
Removed from v.332  
changed lines
  Added in v.452

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26