changeset 438:7399bb8f83ea

Added manager to handle several levels of palette subdivsions. Added parameter ids to identify the palette associated with a parameter gnv-artifacts/trunk@486 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 28 Dec 2009 11:50:27 +0000
parents b624879d2902
children 8975de9d7483
files gnv-artifacts/ChangeLog gnv-artifacts/doc/conf/conf.xml gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/context/GNVArtifactContextFactory.java gnv-artifacts/src/main/java/de/intevation/gnv/raster/Palette.java gnv-artifacts/src/main/java/de/intevation/gnv/raster/PaletteManager.java
diffstat 5 files changed, 121 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog	Sun Dec 27 05:25:40 2009 +0000
+++ b/gnv-artifacts/ChangeLog	Mon Dec 28 11:50:27 2009 +0000
@@ -1,3 +1,24 @@
+2009-12-28	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/gnv/raster/PaletteManager.java: New.
+	  Used to manage several levels of palette subdivsions derived from
+	  base palette. Contains name and description of palette, too.
+	  
+	* src/main/java/de/intevation/gnv/raster/Palette.java: Removed
+	  palette description because it is hold by the PaletteManager now.
+
+	* src/main/java/de/intevation/gnv/artifacts/context/GNVArtifactContextFactory.java:
+	  The palettes are not stored under the name of the palette any longer.
+	  Instead the new attribute 'parameter-id' is splitted (comma separated)
+	  into integers which are use as keys now. The values are PaletteManagers.
+	  This should ease the access to an adequate PaletteManager when
+	  generating a chart for a certain parameter.
+
+	* doc/conf/conf.xml: Added the 'parameter-ids' for the four palettes.
+	  The values are extracted from the gdiintern wiki.
+
+	  !!! This have to be in sync with the database parameter ids !!!
+
 2009-12-27	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/gnv/raster/IsoPolygonSeriesProducer.java:
--- a/gnv-artifacts/doc/conf/conf.xml	Sun Dec 27 05:25:40 2009 +0000
+++ b/gnv-artifacts/doc/conf/conf.xml	Mon Dec 28 11:50:27 2009 +0000
@@ -393,15 +393,19 @@
     <palettes>
         <!-- This section configures the palettes used in 2D diagrams. -->
         <palette name="flow-velocity" 
+                 parameter-ids="11"
                  description="Palette for flow velocity"
                  file="${artifacts.config.dir}/palette/flow-velocity.xml"/>
         <palette name="salinity"
+                 parameter-ids="2"
                  description="Palette for salinity"
                  file="${artifacts.config.dir}/palette/salinity.xml"/>
         <palette name="water-levels"
+                 parameter-ids="35"
                  description="Palette for water levels"
                  file="${artifacts.config.dir}/palette/water-levels.xml"/>
         <palette name="water-temperature"
+                 parameter-ids="1"
                  description="Palette for water temperature"
                  file="${artifacts.config.dir}/palette/water-temperature.xml"/>
     </palettes>
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/context/GNVArtifactContextFactory.java	Sun Dec 27 05:25:40 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/context/GNVArtifactContextFactory.java	Mon Dec 28 11:50:27 2009 +0000
@@ -11,6 +11,7 @@
 
 import java.util.Properties;
 import java.util.HashMap;
+import java.util.ArrayList;
 
 import org.apache.log4j.Logger;
 
@@ -29,6 +30,7 @@
 import de.intevation.gnv.artifacts.cache.CacheFactory;
 
 import de.intevation.gnv.raster.Palette;
+import de.intevation.gnv.raster.PaletteManager;
 
 import de.intevation.artifacts.ArtifactContextFactory;
 
@@ -160,7 +162,7 @@
     ) {
         log.info("configure palettes");
 
-        HashMap palettes = new HashMap();
+        HashMap<Integer, PaletteManager> palettes = new HashMap();
 
         Node node = Config.getNodeXPath(config, PALETTES_PATH);
 
@@ -171,17 +173,33 @@
             NodeList pals = Config.getNodeSetXPath(PALETTE_ITEMS);
             for (int i = 0, N = pals == null ? 0 : pals.getLength(); i < N; ++i) {
                 Element pal = (Element)pals.item(i);
-                String name        = pal.getAttribute("name");
-                String description = pal.getAttribute("description");
-                String filename    = pal.getAttribute("file");
+                String name         = pal.getAttribute("name");
+                String description  = pal.getAttribute("description");
+                String filename     = pal.getAttribute("file");
+                String parameterIds = pal.getAttribute("parameter-ids");
 
-                if (name == null || name.length() == 0) {
+                if (name == null || (name = name.trim()).length() == 0) {
                     log.error("Palette has no 'name' attribute.");
                 }
-                else if (filename == null || filename.length() == 0) {
-                    log.error("Palette has no 'file' attribute.");
+                else if (filename == null
+                || (filename = filename.trim()).length() == 0) {
+                    log.error("Palette '" + name + "' has no 'file' attribute.");
+                }
+                else if (parameterIds == null 
+                || (parameterIds = parameterIds.trim()).length() == 0) {
+                    log.error("no parameter ids given for '" + name + "'");
                 }
                 else {
+                    ArrayList<Integer> ids = new ArrayList<Integer>();
+                    for (String idString: parameterIds.split("[\t ,]+")) {
+                        try {
+                            ids.add(Integer.valueOf(idString));
+                        }
+                        catch (NumberFormatException nfe) {
+                            log.error(
+                                "parameter id '" + idString + "' is integer");
+                        }
+                    }
                     filename = Config.replaceConfigDir(filename);
                     Document document = XMLUtils.parseDocument(
                         new File(filename));
@@ -190,11 +208,16 @@
                             filename + "'");
                     }
                     else {
-                        Palette p = new Palette(document, description);
-                        palettes.put(name, p);
+                        PaletteManager manager = new PaletteManager(
+                            name,
+                            description,
+                            new Palette(document));
+                        for (Integer id: ids) {
+                            palettes.put(id, manager);
+                        }
                     }
                 }
-            }
+            } // for all palettes
         }
 
         context.put(PALETTES, palettes);
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/raster/Palette.java	Sun Dec 27 05:25:40 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/raster/Palette.java	Mon Dec 28 11:50:27 2009 +0000
@@ -110,7 +110,6 @@
     protected Entry [] entries;
     protected Entry    lookup;
     protected Color [] rgbs;
-    protected String   description;
 
     private Entry buildLookup(Entry [] entries, int lo, int hi) {
         if (lo > hi) {
@@ -127,12 +126,6 @@
     }
 
     public Palette(Document document) {
-        this(document, null);
-    }
-
-    public Palette(Document document, String description) {
-
-        this.description = description;
 
         NodeList rangeNodes = document.getElementsByTagName("range");
 
@@ -230,10 +223,6 @@
         return new Palette(this, N);
     }
 
-    public String getDescription() {
-        return description;
-    }
-
     public int getSize() {
         return rgbs.length;
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/raster/PaletteManager.java	Mon Dec 28 11:50:27 2009 +0000
@@ -0,0 +1,63 @@
+package de.intevation.gnv.raster;
+
+import java.util.HashMap;
+
+import java.lang.ref.SoftReference;
+
+/**
+ * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
+ */
+public class PaletteManager
+{
+    protected Palette base;
+
+    protected String  description;
+    protected String  name;
+
+    protected HashMap<Integer, SoftReference<Palette>> levels;
+
+    public PaletteManager() {
+    }
+
+    public PaletteManager(
+        String  name,
+        String  description,
+        Palette base
+    ) {
+        this.name        = name;
+        this.description = description;
+        this.base        = base;
+        levels = new HashMap<Integer, SoftReference<Palette>>();
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public Palette getLevel(int n) {
+        if (n < 2) {
+            return base;
+        }
+
+        Integer N = Integer.valueOf(n);
+
+        Palette palette;
+
+        synchronized (levels) {
+            SoftReference<Palette> ref = levels.get(N);
+
+            if (ref != null && (palette = ref.get()) != null) {
+                return palette;
+            }
+
+            palette = base.subdivide(n);
+
+            ref = new SoftReference(palette);
+
+            levels.put(N, ref);
+        }
+
+        return palette;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org