--- branches/2.0-RC1/src/skrueger/RasterLegendData.java 2009/12/09 14:46:29 606 +++ branches/2.0-RC1/src/skrueger/RasterLegendData.java 2009/12/09 15:13:42 607 @@ -28,11 +28,13 @@ * Stefan A. Krüger - additional utility classes ******************************************************************************/ package skrueger; + import java.util.Arrays; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.TreeMap; import org.apache.log4j.Logger; import org.geotools.coverage.grid.GridCoverage2D; @@ -45,17 +47,23 @@ import skrueger.i8n.Translation; /** - * Holds all the additional information needed to paint a Legend for a RasterLayer. - * So far, only Legends for one-band raster layers are supported. - * + * Holds all the additional information needed to paint a Legend for a + * RasterLayer. So far, only Legends for one-band raster layers are supported. + * * @author Stefan Alfons Krüger - * - * TODO implements {@link Copyable} + * + * TODO implements {@link Copyable} */ -public class RasterLegendData extends HashMap { - static private final Logger LOGGER = Logger.getLogger(RasterLegendData.class); +public class RasterLegendData extends TreeMap implements + Copyable { + static private final Logger LOGGER = Logger + .getLogger(RasterLegendData.class); + private Boolean paintGaps = false; + /** + * Shall bigger gaps be painted between the raster images + */ public Boolean isPaintGaps() { return paintGaps; } @@ -65,49 +73,71 @@ } /** - * {@link #paintGaps} defines, if gaps should be painted between the legends colors, - * indicating nominal values in the raster (e.g. classifications) + * {@link #paintGaps} defines, if gaps should be painted between the legends + * colors, indicating nominal values in the raster (e.g. classifications) */ public RasterLegendData(boolean paintGaps) { this.paintGaps = paintGaps; } - public boolean getPaintGaps() { - return paintGaps ; - } - - public List getSortedKeys(){ - Object[] array = keySet().toArray(); + /** + * Returns a new list containing all {@link Double} values that shall apear + * in the legend. + */ + public List getSortedKeys() { + Double[] array = keySet().toArray(new Double[] {}); Arrays.sort(array); final LinkedList linkedList = new LinkedList(); - for (Object o : array){ - linkedList.add( (Double)o); + for (Double o : array) { + linkedList.add(o); } return linkedList; + } + + /** + * Creates a sample {@link GridCoverage2D} (size 1x1, WGS84) for each legend + * value. These rasters can be used to do visualize the legend item in the + * corresponding color via {@link GridCoverageRenderer}. + */ + public Map createSampleRasters() { + Map sampleRaster = new HashMap(); + for (Double rasterValue : keySet()) { + GridCoverage2D grid = GridUtil.GRID_FAC.create("Legend_" + + rasterValue, + new float[][] { { rasterValue.floatValue() } }, + new Envelope2D(GTUtil.WGS84, 0, 0, 1, 1)); + sampleRaster.put(rasterValue, grid); + } + return sampleRaster; + } + + /** + * Creates a new {@link RasterLegendData} object with identical values + */ + @Override + public RasterLegendData copy() { + RasterLegendData copy = (RasterLegendData) super.clone(); + return copyTo(copy); } - /** - * Creates a sample {@link GridCoverage2D} (size 1x1, WGS84) for each - * legend value. These rasters can be used to do visualize the - * legend item in the corresponding color via {@link GridCoverageRenderer}. - */ - public Map createSampleRasters() { - Map sampleRaster = new HashMap(); - - for (Double rasterValue : keySet()) { - GridCoverage2D grid = GridUtil.GRID_FAC.create( - "Legend_"+rasterValue, - new float[][] { { rasterValue.floatValue() } }, - new Envelope2D(GTUtil.WGS84, 0,0,1,1) - ); - sampleRaster.put(rasterValue, grid); - } - - - return sampleRaster; - } + /** + * Deep-copies all values of this {@link RasterLegendData} to another + * {@link RasterLegendData} + */ + @Override + public RasterLegendData copyTo(RasterLegendData target) { + target.clear(); + + target.setPaintGaps(isPaintGaps()); + + for (Double o : keySet()) { + target.put(new Double(o), get(o).copy()); + } + + return target; + } }