/[schmitzm]/branches/2.4.x/src/skrueger/geotools/LegendIconFeatureRenderer.java
ViewVC logotype

Diff of /branches/2.4.x/src/skrueger/geotools/LegendIconFeatureRenderer.java

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

revision 888 by alfonx, Thu Jun 3 10:48:43 2010 UTC revision 1276 by alfonx, Tue Nov 16 11:51:41 2010 UTC
# Line 29  Line 29 
29   ******************************************************************************/   ******************************************************************************/
30  /**  /**
31   Copyright 2008 Stefan Alfons Tzeggai 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 48  import java.awt.Graphics2D; Line 48  import java.awt.Graphics2D;
48  import java.awt.RenderingHints;  import java.awt.RenderingHints;
49  import java.awt.image.BufferedImage;  import java.awt.image.BufferedImage;
50    
 import javax.swing.tree.DefaultTreeCellRenderer;  
   
51  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
52  import org.geotools.factory.GeoTools;  import org.geotools.factory.GeoTools;
53  import org.geotools.factory.Hints;  import org.geotools.factory.Hints;
 import org.geotools.feature.IllegalAttributeException;  
54  import org.geotools.geometry.jts.LiteShape2;  import org.geotools.geometry.jts.LiteShape2;
55  import org.geotools.renderer.lite.StyledShapePainter;  import org.geotools.renderer.lite.StyledShapePainter;
56  import org.geotools.renderer.style.SLDStyleFactory;  import org.geotools.renderer.style.SLDStyleFactory;
# Line 67  import org.geotools.styling.Style; Line 64  import org.geotools.styling.Style;
64  import org.geotools.styling.Symbolizer;  import org.geotools.styling.Symbolizer;
65  import org.geotools.styling.TextSymbolizer;  import org.geotools.styling.TextSymbolizer;
66  import org.geotools.util.NumberRange;  import org.geotools.util.NumberRange;
67    import org.opengis.feature.IllegalAttributeException;
68  import org.opengis.feature.simple.SimpleFeature;  import org.opengis.feature.simple.SimpleFeature;
69  import org.opengis.feature.simple.SimpleFeatureType;  import org.opengis.feature.simple.SimpleFeatureType;
70  import org.opengis.feature.type.AttributeDescriptor;  import org.opengis.feature.type.AttributeDescriptor;
# Line 83  import com.vividsolutions.jts.geom.Polyg Line 81  import com.vividsolutions.jts.geom.Polyg
81   * Based on geoserver!   * Based on geoserver!
82   * <hr>   * <hr>
83   * <b>Changes by <a href="mailto:[email protected]">Martin Schmitz</a></b>   * <b>Changes by <a href="mailto:[email protected]">Martin Schmitz</a></b>
  * <br>  
  * <ul>  
84   * <li>07.02.2008:<br>   * <li>07.02.2008:<br>
85   * Determining the default values of a {@link SimpleFeatureType} by   * Determining the default values of a {@link SimpleFeatureType} by
86   * {@link FeatureUtil#getDefaultAttributeValues(SimpleFeatureType)} instead of using   * {@link FeatureUtil#getDefaultAttributeValues(SimpleFeatureType)} instead of
87   * {@link AttributeDescriptor#createDefaultValue()} directly, because the latter   * using {@link AttributeDescriptor#createDefaultValue()} directly, because the
88   * returns {@code null} even though the attribut is not nillable.</li>   * latter returns {@code null} even though the attribut is not nillable.</li>
89   * </ul>   * </ul>
90   *   *
91   * @author Stefan Alfons Tzeggai   * @author Stefan Alfons Tzeggai
92   */   */
93  public class LegendIconFeatureRenderer extends DefaultTreeCellRenderer {  public class LegendIconFeatureRenderer {
94  //      private static final Dimension SIZE = new Dimension(30,20);          // private static final Dimension SIZE = new Dimension(30,20);
95    
96          Logger LOGGER = Logger.getLogger(LegendIconFeatureRenderer.class);          Logger LOGGER = Logger.getLogger(LegendIconFeatureRenderer.class);
97    
# Line 105  public class LegendIconFeatureRenderer e Line 101  public class LegendIconFeatureRenderer e
101          private LegendIconFeatureRenderer() {          private LegendIconFeatureRenderer() {
102          }          }
103    
104          final static LegendIconFeatureRenderer instance = new LegendIconFeatureRenderer();          /**
105             * We have to return new instances here. Otherwise the sample features are
106             * not recreated and the sizes of the previews are locaked to the size of
107             * the first request.
108             */
109          public static LegendIconFeatureRenderer getInstance() {          public static LegendIconFeatureRenderer getInstance() {
                 // In GT 2.4.5 it we have to create a new one all the time!  
                   
                 // TODO TEST, whether we still need to recreate the renderer here....  
                 // TODO TEST, whether we still need to recreate the renderer here....  
                 // TODO TEST, whether we still need to recreate the renderer here....  
                 // TODO TEST, whether we still need to recreate the renderer here....  
                 // TODO TEST, whether we still need to recreate the renderer here....  
                 // TODO TEST, whether we still need to recreate the renderer here....  
                 // TODO TEST, whether we still need to recreate the renderer here....  
                 // TODO TEST, whether we still need to recreate the renderer here....  
110                  return new LegendIconFeatureRenderer();                  return new LegendIconFeatureRenderer();
 //               return instance;  
111          }          }
112    
113          /**          /**
# Line 155  public class LegendIconFeatureRenderer e Line 143  public class LegendIconFeatureRenderer e
143          private Hints hints;          private Hints hints;
144    
145          /**          /**
146           * Returns a <code>java.awt.Shape</code> appropiate to render a legend           * Returns a <code>java.awt.Shape</code> appropriate to render a legend
147           * graphic given the symbolizer type and the legend dimensions.           * graphic given the symbolizer type and the legend dimensions.
148           *           *
149           * @param symbolizer           * @param symbolizer
# Line 169  public class LegendIconFeatureRenderer e Line 157  public class LegendIconFeatureRenderer e
157           *         symbolizer, wether it is a LineSymbolizer, a PolygonSymbolizer,           *         symbolizer, wether it is a LineSymbolizer, a PolygonSymbolizer,
158           *         or a Point ot Text Symbolizer           *         or a Point ot Text Symbolizer
159           *           *
          * @throws IllegalArgumentException  
          *             if an unknown symbolizer impl was passed in.  
160           */           */
161          private LiteShape2 getSampleShape(Symbolizer symbolizer, int legendWidth,          private LiteShape2 getSampleShape(Symbolizer symbolizer, int legendWidth,
162                          int legendHeight) {                          int legendHeight) {
# Line 222  public class LegendIconFeatureRenderer e Line 208  public class LegendIconFeatureRenderer e
208                                                  legendHeight / 2);                                                  legendHeight / 2);
209    
210                                  try {                                  try {
211                                          this.samplePoint = new LiteShape2(geomFac                                          this.samplePoint = new LiteShape2(
212                                                          .createPoint(coord), null, null, false);                                                          geomFac.createPoint(coord), null, null, false);
213                                  } catch (Exception e) {                                  } catch (Exception e) {
214                                          this.samplePoint = null;                                          this.samplePoint = null;
215                                  }                                  }
# Line 236  public class LegendIconFeatureRenderer e Line 222  public class LegendIconFeatureRenderer e
222                  }                  }
223                  return sampleShape;                  return sampleShape;
224          }          }
225            
226          /**          /**
227           * Creates a little BufferedImage that presents the Style/Symbols used by           * Creates a little BufferedImage that presents the Style/Symbols used by
228           * the {@link MapLegend} to show a legend for the {@link SimpleFeatureType}           * the {@link MapLegend} to show a legend for the {@link SimpleFeatureType}
# Line 244  public class LegendIconFeatureRenderer e Line 230  public class LegendIconFeatureRenderer e
230           * @param rule           * @param rule
231           *            {@link Rule} that provides the text and the style to present           *            {@link Rule} that provides the text and the style to present
232           * @param featureType           * @param featureType
233           *            Schema that describes the kind of the sample {@link SimpleFeature}           *            Schema that describes the kind of the sample
234           *            that will be rendered with the {@link Style}           *            {@link SimpleFeature} that will be rendered with the
235             *            {@link Style}
236           * @param bg           * @param bg
237           *            Background {@link Color} or <code>null</code>           *            Background {@link Color} or <code>null</code>
238           *           *
239           * @throws IllegalAttributeException           * @throws IllegalAttributeException
240           */           */
241          public BufferedImage createImageForRule(Rule rule, SimpleFeatureType featureType,          public BufferedImage createImageForRule(Rule rule,
242                          Dimension size, Color bg) {                          SimpleFeatureType featureType, Dimension size, Color bg) {
243    
244                  Symbolizer[] symbolizers = rule.getSymbolizers();                  Symbolizer[] symbolizers = rule.getSymbolizers();
245    
# Line 262  public class LegendIconFeatureRenderer e Line 249  public class LegendIconFeatureRenderer e
249    
250                  if (bg != null) {                  if (bg != null) {
251                          // ****************************************************************************                          // ****************************************************************************
252                          // The following lines define the backgroup color for the rendered                          // The following lines define the background color for the rendered
253                          // images                          // images
254                          // ****************************************************************************                          // ****************************************************************************
255                          graphics.setBackground(bg);                          graphics.setBackground(bg);
# Line 273  public class LegendIconFeatureRenderer e Line 260  public class LegendIconFeatureRenderer e
260                  // Enable anti-aliasing for the legend symbols                  // Enable anti-aliasing for the legend symbols
261                  graphics.setRenderingHints(getHints());                  graphics.setRenderingHints(getHints());
262    
263                  // TODO scaleDenominator = 100000000000000000000000000000d; Was ist das                  final NumberRange<Integer> scaleRange = NumberRange.create(
264                  // fuer ein Quatsch?                                  Integer.MAX_VALUE, Integer.MAX_VALUE);
                 final NumberRange<Integer> scaleRange = NumberRange.create(Integer.MAX_VALUE,Integer.MAX_VALUE);  
265    
266                  try {                  try {
267    
# Line 283  public class LegendIconFeatureRenderer e Line 269  public class LegendIconFeatureRenderer e
269                                  Symbolizer symbolizer = symbolizers[sIdx];                                  Symbolizer symbolizer = symbolizers[sIdx];
270    
271                                  if (symbolizer instanceof TextSymbolizer) {                                  if (symbolizer instanceof TextSymbolizer) {
                                         LOGGER  
                                                         .warn("createImageForRule for a TextSymbolizer. Sure we want that?");  
272                                          continue;                                          continue;
273                                  }                                  }
274    
# Line 293  public class LegendIconFeatureRenderer e Line 277  public class LegendIconFeatureRenderer e
277                                  // RasterSymbolizers..");                                  // RasterSymbolizers..");
278                                  // }                                  // }
279                                  // else                                  // else
280                                  final SimpleFeature sampleFeature = FeatureUtil.createSampleFeature(featureType);                                  final SimpleFeature sampleFeature = FeatureUtil
281                                                    .createSampleFeature(featureType);
282    
283                                    if (sampleFeature == null)
284                                            LOGGER.debug("sampleFeature " + sampleFeature);
285    
286                                  // The SLDStyleFactory has to be recreated, as it seams to cache                                  // The SLDStyleFactory has to be recreated, as it seams to cache
287                                  // some stuff.                                  // some stuff.
# Line 305  public class LegendIconFeatureRenderer e Line 293  public class LegendIconFeatureRenderer e
293                                                  size.height);                                                  size.height);
294    
295                                  if (style2d != null) {                                  if (style2d != null) {
296                                          new StyledShapePainter(null).paint(graphics, shape,                                          StyledShapePainter styledShapePainter = new StyledShapePainter(
297                                                          style2d, Integer.MAX_VALUE );                                                          null);
298                                            styledShapePainter.paint(graphics, shape, style2d,
299                                                            Integer.MAX_VALUE);
300                                  }                                  }
301                          }                          }
302                  } catch (Exception e) {                  } catch (Exception e) {
303                          LOGGER                          LOGGER.error(
304                                          .error(                                          "Error during createImageForRule, returning empty Image", e);
                                                         "Error during createImageForRule, returning empty Image",  
                                                         e);  
305                  }                  }
306                  return buffImage;                  return buffImage;
307          }          }
# Line 322  public class LegendIconFeatureRenderer e Line 310  public class LegendIconFeatureRenderer e
310           * Define Java2D and other Hints           * Define Java2D and other Hints
311           *           *
312           * @param hints           * @param hints
313           * @author <a href="mailto:[email protected]">Stefan Alfons           * @author <a href="mailto:[email protected]">Stefan Alfons Tzeggai</a>
          *         Tzeggai</a>  
314           */           */
315          public void setHints(Hints hints) {          public void setHints(Hints hints) {
316                  getHints().add(hints);                  getHints().add(hints);
# Line 353  public class LegendIconFeatureRenderer e Line 340  public class LegendIconFeatureRenderer e
340           * @param rule           * @param rule
341           *            {@link Rule} that provides the text and the style to present           *            {@link Rule} that provides the text and the style to present
342           * @param featureType           * @param featureType
343           *            Schema that describes the kind of the sample {@link SimpleFeature}           *            Schema that describes the kind of the sample
344           *            that will be rendered with the {@link Style}           *            {@link SimpleFeature} that will be rendered with the
345             *            {@link Style}
346           *           *
347           * @throws IllegalAttributeException           * @throws IllegalAttributeException
348           */           */

Legend:
Removed from v.888  
changed lines
  Added in v.1276

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26