diff gnv-artifacts/src/main/java/de/intevation/gnv/raster/Palette.java @ 483:c8089cd7d777

Added explicit index scheme to palette classes to allow external references. gnv-artifacts/trunk@558 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 18 Jan 2010 10:56:41 +0000
parents c7ca2fce041f
children 9a828e5a2390
line wrap: on
line diff
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/raster/Palette.java	Sun Jan 17 21:26:21 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/raster/Palette.java	Mon Jan 18 10:56:41 2010 +0000
@@ -28,6 +28,8 @@
 
         private int    index;
 
+        private int    externalIndex;
+
         private double from;
         private double to;
 
@@ -39,25 +41,28 @@
         }
 
         public Entry(Entry other) {
-            index       = other.index;
-            from        = other.from;
-            to          = other.to;
-            description = other.description;
-            color       = other.color;
+            index         = other.index;
+            externalIndex = other.externalIndex;
+            from          = other.from;
+            to            = other.to;
+            description   = other.description;
+            color         = other.color;
         }
 
         public Entry(
             int    index, 
+            int    externalIndex,
             double from, 
             double to, 
             Color  color,
             String description
         ) {
-            this.index       = index;
-            this.from        = from;
-            this.to          = to;
-            this.color       = color;
-            this.description = description;
+            this.index         = index;
+            this.externalIndex = externalIndex;
+            this.from          = from;
+            this.to            = to;
+            this.color         = color;
+            this.description   = description;
         }
 
         public int compareTo(Object other) {
@@ -79,6 +84,10 @@
             return color;
         }
 
+        public int getExternalIndex() {
+            return externalIndex;
+        }
+
         public Entry locateEntry(double value) {
             Entry current = this;
             while (current != null) {
@@ -150,12 +159,13 @@
 
         for (int i = 0; i < entries.length; ++i) {
             Element rangeElement = (Element)rangeNodes.item(i);
-            double from  = doubleValue(rangeElement.getAttribute("from"));
-            double to    = doubleValue(rangeElement.getAttribute("to"));
-            Color  color = color(rangeElement.getAttribute("rgb"));
-            String desc = rangeElement.getAttribute("description");
+            double from     = doubleValue(rangeElement.getAttribute("from"));
+            double to       = doubleValue(rangeElement.getAttribute("to"));
+            int    extIndex = intValue(rangeElement.getAttribute("index"), i);
+            Color  color    = color(rangeElement.getAttribute("rgb"));
+            String desc     = rangeElement.getAttribute("description");
             if (from > to) { double t = from; from = to; to = t; }
-            entries[i] = new Entry(i, from, to, color, desc);
+            entries[i] = new Entry(i, extIndex, from, to, color, desc);
             rgbs[i] = color;
         }
 
@@ -207,6 +217,19 @@
         buildLookup();
     }
 
+    private static final int intValue(String s, int def) {
+        if (s == null || (s = s.trim()).length() == 0) {
+            return def;
+        }
+        try {
+            return Integer.parseInt(s);
+        }
+        catch (NumberFormatException nfe) {
+            log.error(nfe.getLocalizedMessage(), nfe);
+        }
+        return def;
+    }
+
     private static final double doubleValue(String s) {
         if (s == null || (s = s.trim()).length() == 0) {
             return 0d;

http://dive4elements.wald.intevation.org