/[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

branches/1.0-gt2-2.6/src/skrueger/geotools/LegendIconFeatureRenderer.java revision 452 by alfonx, Fri Oct 9 19:16:27 2009 UTC trunk/src/skrueger/geotools/LegendIconFeatureRenderer.java revision 1223 by alfonx, Wed Nov 3 15:44:54 2010 UTC
# Line 25  Line 25 
25   *   *
26   * Contributors:   * Contributors:
27   *     Martin O. J. Schmitz - initial API and implementation   *     Martin O. J. Schmitz - initial API and implementation
28   *     Stefan A. Krüger - additional utility classes   *     Stefan A. Tzeggai - additional utility classes
29   ******************************************************************************/   ******************************************************************************/
30  /**  /**
31   Copyright 2008 Stefan Alfons Krüger and parts from some Geotools code     Copyright 2008 Stefan Alfons Tzeggai and parts from some Geotools code  
32    
33   atlas-framework - This file is part of the Atlas Framework   atlas-framework - This file is part of the Atlas Framework
34    
35   This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.   This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
# Line 87  import com.vividsolutions.jts.geom.Polyg Line 87  import com.vividsolutions.jts.geom.Polyg
87   * <ul>   * <ul>
88   * <li>07.02.2008:<br>   * <li>07.02.2008:<br>
89   * Determining the default values of a {@link SimpleFeatureType} by   * Determining the default values of a {@link SimpleFeatureType} by
90   * {@link FeatureUtil#getDefaultAttributeValues(SimpleFeatureType)} instead of using   * {@link FeatureUtil#getDefaultAttributeValues(SimpleFeatureType)} instead of
91   * {@link AttributeDescriptor#createDefaultValue()} directly, because the latter   * using {@link AttributeDescriptor#createDefaultValue()} directly, because the
92   * returns {@code null} even though the attribut is not nillable.</li>   * latter returns {@code null} even though the attribut is not nillable.</li>
93   * </ul>   * </ul>
94   *   *
95   * @author Stefan Alfons Krüger   * @author Stefan Alfons Tzeggai
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 109  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....                  // TODO TEST, whether we still need to recreate the renderer here....
114                  // TODO TEST, whether we still need to recreate the renderer here....                  // TODO TEST, whether we still need to recreate the renderer here....
115                  // TODO TEST, whether we still need to recreate the renderer here....                  // TODO TEST, whether we still need to recreate the renderer here....
# Line 119  public class LegendIconFeatureRenderer e Line 119  public class LegendIconFeatureRenderer e
119                  // TODO TEST, whether we still need to recreate the renderer here....                  // TODO TEST, whether we still need to recreate the renderer here....
120                  // TODO TEST, whether we still need to recreate the renderer here....                  // 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    
125          /**          /**
# Line 222  public class LegendIconFeatureRenderer e Line 222  public class LegendIconFeatureRenderer e
222                                                  legendHeight / 2);                                                  legendHeight / 2);
223    
224                                  try {                                  try {
225                                          this.samplePoint = new LiteShape2(geomFac                                          this.samplePoint = new LiteShape2(
226                                                          .createPoint(coord), null, null, false);                                                          geomFac.createPoint(coord), null, null, false);
227                                  } catch (Exception e) {                                  } catch (Exception e) {
228                                          this.samplePoint = null;                                          this.samplePoint = null;
229                                  }                                  }
# Line 234  public class LegendIconFeatureRenderer e Line 234  public class LegendIconFeatureRenderer e
234                          throw new IllegalArgumentException("Unknown symbolizer: "                          throw new IllegalArgumentException("Unknown symbolizer: "
235                                          + symbolizer);                                          + symbolizer);
236                  }                  }
   
237                  return sampleShape;                  return sampleShape;
238          }          }
 //  
 //      /**  
 //       * Puts a BufferedImage into this.legendGraphic  
 //       */  
 //      public void produceLegendGraphic(SimpleFeatureType featureType, Style gt2Style,  
 //                      Rule[] applicableRules) {  
 //  
 //              // final FeatureTypeStyle[] ftStyles = gt2Style.getFeatureTypeStyles();  
 //  
 //              // if (request.getRule() != null) {  
 //              // applicableRules = new Rule[] { request.getRule() };  
 //              // } else {  
 //              // applicableRules = getApplicableRules(ftStyles, scaleDenominator);  
 //              // }  
 //  
 //              final int ruleCount = applicableRules.length;  
 //  
 //              /**  
 //               * A legend graphic is produced for each applicable rule. They're being  
 //               * holded here until the process is done and then painted on a "stack"  
 //               * like legend.  
 //               */  
 //              final List<BufferedImage> legendsStack = new ArrayList<BufferedImage>(  
 //                              ruleCount);  
 //  
 //              for (int i = 0; i < ruleCount; i++) {  
 //                      BufferedImage image = createImageForRule(applicableRules[i],  
 //                                      featureType, SIZE);  
 //                      legendsStack.add(image);  
 //              }  
 //  
 //              // this.legendGraphic =  
 //              // scaleImage(mergeLegends(legendsStack,applicableRules), request);  
 //              this.legendGraphic = mergeLegends(legendsStack, applicableRules);  
 //      }  
 //  
 //      /**  
 //       * Recieves a list of <code>BufferedImages</code> and produces a new one  
 //       * which holds all the images in <code>imageStack</code> one above the  
 //       * other.  
 //       *  
 //       * @param imageStack  
 //       *            the list of BufferedImages, one for each applicable Rule  
 //       * @param rules  
 //       *            The applicable rules, one for each image in the stack  
 //       * @param request  
 //       *            The request.  
 //       *  
 //       * @return the stack image with all the images on the argument list.  
 //       *  
 //       * @throws IllegalArgumentException  
 //       *             if the list is empty  
 //       */  
 //      private static BufferedImage mergeLegends(List<BufferedImage> imageStack,  
 //                      Rule[] rules) {  
 //              if (imageStack.size() == 0) {  
 //                      throw new IllegalArgumentException("No legend graphics passed");  
 //              }  
 //  
 //              BufferedImage finalLegend = null;  
 //  
 //              if (imageStack.size() == 1) {  
 //                      finalLegend = (BufferedImage) imageStack.get(0);  
 //              } else {  
 //                      final int imgCount = imageStack.size();  
 //                      final String[] labels = new String[imgCount];  
 //  
 //                      BufferedImage img = ((BufferedImage) imageStack.get(0));  
 //                      FontMetrics fontMetrics = img.getGraphics().getFontMetrics();  
 //  
 //                      final int rowHeight = Math.max(fontMetrics.getHeight(), img  
 //                                      .getHeight());  
 //  
 //                      // calculate the total dimensions of the image  
 //                      int totalHeight = rowHeight * imgCount;  
 //                      int totalWidth = 0;  
 //  
 //                      for (int i = 0; i < imgCount; i++) {  
 //                              img = (BufferedImage) imageStack.get(i);  
 //  
 //                              Rule rule = rules[i];  
 //  
 //                              // does this rule have a label  
 //                              labels[i] = rule.getTitle();  
 //  
 //                              if (labels[i] == null) {  
 //                                      labels[i] = rule.getName();  
 //                              }  
 //  
 //                              int w = img.getWidth();  
 //  
 //                              if (labels[i] != null) {  
 //                                      Graphics g = img.getGraphics();  
 //                                      w += g.getFontMetrics().stringWidth(labels[i]);  
 //                              }  
 //  
 //                              totalWidth = Math.max(w, totalWidth);  
 //                      }  
 //  
 //                      // create the final image  
 //                      finalLegend = new BufferedImage(totalWidth, totalHeight,  
 //                                      BufferedImage.TYPE_INT_ARGB);  
 //  
 //                      Graphics2D finalGraphics = finalLegend.createGraphics();  
 //  
 //                      finalGraphics.setColor(Color.white);  
 //                      finalGraphics.fillRect(0, 0, totalWidth, totalHeight);  
 //  
 //                      int h = 0;  
 //  
 //                      for (int i = 0; i < imgCount; i++) {  
 //                              img = (BufferedImage) imageStack.get(i);  
 //  
 //                              // draw the image  
 //                              int y = h;  
 //  
 //                              if (img.getHeight() < rowHeight) {  
 //                                      // move the image to the center of the row  
 //                                      y += (int) ((rowHeight - img.getHeight()) / 2d);  
 //                              }  
 //  
 //                              finalGraphics.drawImage(img, 0, y, imgObs);  
 //  
 //                              // draw the label  
 //                              if (labels[i] != null) {  
 //                                      finalGraphics.setColor(Color.BLACK);  
 //  
 //                                      y = (h + rowHeight) - fontMetrics.getDescent();  
 //  
 //                                      if (fontMetrics.getHeight() < rowHeight) {  
 //                                              // move the baseline to the center of the row  
 //                                              y -= (int) ((rowHeight - fontMetrics.getHeight()) / 2d);  
 //                                      }  
 //  
 //                                      finalGraphics.drawString(labels[i], img.getWidth(), y);  
 //                              }  
 //  
 //                              h += rowHeight;  
 //                      }  
 //              }  
 //  
 //              return finalLegend;  
 //      }  
 //  
 //      /**  
 //       * DOCUMENT ME!  
 //       *  
 //       * @return  
 //       *  
 //       * @throws IllegalStateException  
 //       *             DOCUMENT ME!  
 //       */  
 //      public BufferedImage getLegendGraphic() {  
 //              if (this.legendGraphic == null) {  
 //                      throw new IllegalStateException();  
 //              }  
 //              return this.legendGraphic;  
 //      }  
   
         // /**  
         // * Paints a little rectangle in the color defined by the  
         // * {@link ColorMapEntry}  
         // *  
         // * unused  
         // *  
         // * @param cme  
         // * {@link ColorMapEntry}  
         // * @param cm  
         // * {@link ColorMap} to determine type (VALUES, INTERVALL )  
         // * @param iconWidth  
         // * Size of the rectangle  
         // * @param iconHeight  
         // * Size of the rectangle  
         // * @return {@link ImageIcon} of that color, maybe with a white border  
         // * @author Stefan Alfons Krüger  
         // */  
         // public static ImageIcon createImageForColorMapEntry(ColorMapEntry cme,  
         // ColorMap cm, Integer iconWidth, Integer iconHeight) {  
         //  
         // BufferedImage imageForRule = new BufferedImage(iconWidth, iconHeight,  
         // BufferedImage.TYPE_INT_ARGB);  
         //  
         // final Color color = StylingUtil.getColorFromColorMapEntry(cme);  
         //  
         // // Paint a block  
         // for (int x = 0; x < iconWidth; x++) {  
         // for (int y = 0; y < iconHeight; y++) {  
         // int rgb = color.getRGB();  
         // int rand = 2;  
         //  
         // // Paint a white border if the type is VALUES  
         // if ((x < rand) || (x > iconWidth - rand) || (y < rand)  
         // || (y > iconHeight - rand)) {  
         // if (cm.getType() == ColorMap.TYPE_VALUES) {  
         // rgb = Color.white.getRGB();  
         // }  
         // }  
         // imageForRule.setRGB(x, y, rgb);  
         // }  
         // }  
         // return new ImageIcon(imageForRule);  
         // }  
239    
240          /**          /**
241           * Creates a little BufferedImage that presents the Style/Symbols used by           * Creates a little BufferedImage that presents the Style/Symbols used by
# Line 447  public class LegendIconFeatureRenderer e Line 244  public class LegendIconFeatureRenderer e
244           * @param rule           * @param rule
245           *            {@link Rule} that provides the text and the style to present           *            {@link Rule} that provides the text and the style to present
246           * @param featureType           * @param featureType
247           *            Schema that describes the kind of the sample {@link SimpleFeature}           *            Schema that describes the kind of the sample
248           *            that will be rendered with the {@link Style}           *            {@link SimpleFeature} that will be rendered with the
249             *            {@link Style}
250           * @param bg           * @param bg
251           *            Background {@link Color} or <code>null</code>           *            Background {@link Color} or <code>null</code>
252           *           *
253           * @throws IllegalAttributeException           * @throws IllegalAttributeException
254           */           */
255          public BufferedImage createImageForRule(Rule rule, SimpleFeatureType featureType,          public BufferedImage createImageForRule(Rule rule,
256                          Dimension size, Color bg) {                          SimpleFeatureType featureType, Dimension size, Color bg) {
257    
258                  Symbolizer[] symbolizers = rule.getSymbolizers();                  Symbolizer[] symbolizers = rule.getSymbolizers();
259    
# Line 465  public class LegendIconFeatureRenderer e Line 263  public class LegendIconFeatureRenderer e
263    
264                  if (bg != null) {                  if (bg != null) {
265                          // ****************************************************************************                          // ****************************************************************************
266                          // The following lines define the backgroup color for the rendered                          // The following lines define the background color for the rendered
267                          // images                          // images
268                          // ****************************************************************************                          // ****************************************************************************
269                          graphics.setBackground(bg);                          graphics.setBackground(bg);
# Line 476  public class LegendIconFeatureRenderer e Line 274  public class LegendIconFeatureRenderer e
274                  // Enable anti-aliasing for the legend symbols                  // Enable anti-aliasing for the legend symbols
275                  graphics.setRenderingHints(getHints());                  graphics.setRenderingHints(getHints());
276    
277                  // TODO scaleDenominator = 100000000000000000000000000000d; Was ist das                  final NumberRange<Integer> scaleRange = NumberRange.create(
278                  // fuer ein Quatsch?                                  Integer.MAX_VALUE, Integer.MAX_VALUE);
                 final NumberRange<Integer> scaleRange = NumberRange.create(Integer.MAX_VALUE,Integer.MAX_VALUE);  
279    
280                  try {                  try {
281    
# Line 486  public class LegendIconFeatureRenderer e Line 283  public class LegendIconFeatureRenderer e
283                                  Symbolizer symbolizer = symbolizers[sIdx];                                  Symbolizer symbolizer = symbolizers[sIdx];
284    
285                                  if (symbolizer instanceof TextSymbolizer) {                                  if (symbolizer instanceof TextSymbolizer) {
                                         LOGGER  
                                                         .warn("createImageForRule for a TextSymbolizer. Sure we want that?");  
286                                          continue;                                          continue;
287                                  }                                  }
288    
# Line 496  public class LegendIconFeatureRenderer e Line 291  public class LegendIconFeatureRenderer e
291                                  // RasterSymbolizers..");                                  // RasterSymbolizers..");
292                                  // }                                  // }
293                                  // else                                  // else
294                                  final SimpleFeature sampleFeature = FeatureUtil.createSampleFeature(featureType);                                  final SimpleFeature sampleFeature = FeatureUtil
295                                                    .createSampleFeature(featureType);
296    
297                                    if (sampleFeature == null)
298                                            LOGGER.debug("sampleFeature " + sampleFeature);
299    
300                                  // The SLDStyleFactory has to be recreated, as it seams to cache                                  // The SLDStyleFactory has to be recreated, as it seams to cache
301                                  // some stuff.                                  // some stuff.
# Line 509  public class LegendIconFeatureRenderer e Line 308  public class LegendIconFeatureRenderer e
308    
309                                  if (style2d != null) {                                  if (style2d != null) {
310                                          new StyledShapePainter(null).paint(graphics, shape,                                          new StyledShapePainter(null).paint(graphics, shape,
311                                                          style2d, Integer.MAX_VALUE );                                                          style2d, Integer.MAX_VALUE);
312                                  }                                  }
313                          }                          }
314                  } catch (Exception e) {                  } catch (Exception e) {
315                          LOGGER                          LOGGER.error(
316                                          .error(                                          "Error during createImageForRule, returning empty Image", e);
                                                         "Error during createImageForRule, returning empty Image",  
                                                         e);  
317                  }                  }
318                  return buffImage;                  return buffImage;
319          }          }
# Line 525  public class LegendIconFeatureRenderer e Line 322  public class LegendIconFeatureRenderer e
322           * Define Java2D and other Hints           * Define Java2D and other Hints
323           *           *
324           * @param hints           * @param hints
325           * @author <a href="mailto:[email protected]">Stefan Alfons           * @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a>
          *         Kr&uuml;ger</a>  
326           */           */
327          public void setHints(Hints hints) {          public void setHints(Hints hints) {
328                  getHints().add(hints);                  getHints().add(hints);
# Line 556  public class LegendIconFeatureRenderer e Line 352  public class LegendIconFeatureRenderer e
352           * @param rule           * @param rule
353           *            {@link Rule} that provides the text and the style to present           *            {@link Rule} that provides the text and the style to present
354           * @param featureType           * @param featureType
355           *            Schema that describes the kind of the sample {@link SimpleFeature}           *            Schema that describes the kind of the sample
356           *            that will be rendered with the {@link Style}           *            {@link SimpleFeature} that will be rendered with the
357             *            {@link Style}
358           *           *
359           * @throws IllegalAttributeException           * @throws IllegalAttributeException
360           */           */

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26