save and laod colormap in xml 90/14190/2
Antoine ELIAS [Thu, 27 Mar 2014 19:07:00 +0000 (20:07 +0100)]
Change-Id: Ica078682a7eac7723df50e4e5ec07de8f931f4c5

scilab/modules/graphic_objects/src/java/org/scilab/modules/graphic_objects/xmlloader/XMLDomLoader.java
scilab/modules/graphic_objects/src/java/org/scilab/modules/graphic_objects/xmlloader/XmlSaver.java

index a82d275..382af42 100644 (file)
@@ -2,6 +2,7 @@ package org.scilab.modules.graphic_objects.xmlloader;
 
 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.*;
 
+import java.awt.Color;
 import java.io.File;
 import java.util.HashMap;
 import java.util.StringTokenizer;
@@ -39,6 +40,8 @@ public class XMLDomLoader {
     private static final int __NODE_TITLE__         = -40;
     private static final int __NODE_STRING__        = -50;
     private static final int __NODE_STRINGITEM__    = -50;
+    private static final int __NODE_COLORMAP__      = -60;
+    private static final int __NODE_COLORMAPITEM__  = -70;
 
     private static HashMap<String, Integer> nameToGO = new HashMap<String, Integer>();
 
@@ -81,6 +84,8 @@ public class XMLDomLoader {
         nameToGO.put("title", __NODE_TITLE__);
         nameToGO.put("string", __NODE_STRING__);
         nameToGO.put("stringitem", __NODE_STRINGITEM__);
+        nameToGO.put("colormap", __NODE_COLORMAP__);
+        nameToGO.put("colormapitem", __NODE_COLORMAPITEM__);
 
         /* system */
         nameToGO.put("scilabgui", __NODE_SCILABGUI__);
@@ -99,7 +104,7 @@ public class XMLDomLoader {
         figPropToGO.put("pixel_drawing_mode", new Pair<Integer, ModelType>(__GO_PIXEL_DRAWING_MODE__, ModelType.STRING));
         figPropToGO.put("anti_aliasing", new Pair<Integer, ModelType>(__GO_ANTIALIASING__, ModelType.BOOLEAN));
         figPropToGO.put("immediate_drawing", new Pair<Integer, ModelType>(__GO_IMMEDIATE_DRAWING__, ModelType.BOOLEAN));
-        //figPropToGO.put("background", new Pair<Integer, ModelType>(__GO_BACKGROUND__, ModelType.INTEGER));
+        figPropToGO.put("background", new Pair<Integer, ModelType>(__GO_BACKGROUND__, ModelType.INTEGER));
         figPropToGO.put("visible", new Pair<Integer, ModelType>(__GO_VISIBLE__, ModelType.BOOLEAN));
         figPropToGO.put("rotation_style", new Pair<Integer, ModelType>(__GO_ROTATION_TYPE__, ModelType.ROTATIONTYPE));
         figPropToGO.put("event_handler", new Pair<Integer, ModelType>(__GO_EVENTHANDLER_NAME__, ModelType.STRING));
@@ -324,6 +329,13 @@ public class XMLDomLoader {
                         break;
                     }
 
+                    case __NODE_COLORMAP__: {
+                        //avoid relationship
+                        child = 0;
+                        createColorMap(parent, childNode);
+                        break;
+                    }
+
                     case __NODE_SCILABGUI__ : {
                         //check version
                         Node nodeVersion = childNode.getAttributes().getNamedItem("version");
@@ -348,7 +360,7 @@ public class XMLDomLoader {
 
 
                     default: {
-                        //ignore TITLED, IN, OU, STRING_AARAY node
+                        //ignore TITLED, IN, OUT, STRING_AARAY, COLORMAPITEM node
                         break;
                     }
                 }
@@ -365,6 +377,49 @@ public class XMLDomLoader {
         }
     }
 
+    private void createColorMap(Integer parent, Node node) {
+        NamedNodeMap attr = node.getAttributes();
+
+        Node rowNode = attr.getNamedItem("size");
+        if (rowNode == null) {
+            return;
+        }
+
+        Integer rows = Integer.parseInt(rowNode.getNodeValue());
+
+        if (node.hasChildNodes()) {
+            Double[] colorMap = new Double[rows * 3];
+            NodeList list = node.getChildNodes();
+            int index = 0;
+            for (int i = 0 ; i < list.getLength() ; i++) {
+                Node childNode = list.item(i);
+                if (childNode.getNodeType() == Node.ELEMENT_NODE) {
+                    attr = childNode.getAttributes();
+                    Node valueNode = attr.getNamedItem("color");
+                    if (valueNode == null) {
+                        colorMap[index] = 0.0;
+                        colorMap[index + rows] = 0.0;
+                        colorMap[index + 2 * rows] = 0.0;
+                    } else {
+                        try {
+                            Color c = Color.decode(valueNode.getNodeValue());
+                            colorMap[index] = c.getRed() / 255.0;
+                            colorMap[index + rows] = c.getGreen() / 255.0;
+                            colorMap[index + 2 * rows] = c.getBlue() / 255.0;
+                        } catch (NumberFormatException e) {
+                            colorMap[index] = 0.0;
+                            colorMap[index + rows] = 0.0;
+                            colorMap[index + 2 * rows] = 0.0;
+                        }
+                    }
+                    index++;
+                }
+            }
+
+            GraphicController.getController().setProperty(parent, __GO_COLORMAP__, colorMap);
+        }
+    }
+
     private void createString(Integer parent, Node node) {
         NamedNodeMap attr = node.getAttributes();
 
@@ -410,7 +465,6 @@ public class XMLDomLoader {
             Integer propId = UiPropToGO.get(property).getFirst();
 
             if (propId == __GO_UI_STRING__ && cols > 1) {
-                System.out.println("set __GO_UI_STRING_COLNB__ : " + cols);
                 GraphicController.getController().setProperty(parent, __GO_UI_STRING_COLNB__, cols);
             }
             GraphicController.getController().setProperty(parent, propId, str);
index 82a606a..4e3e6c3 100644 (file)
@@ -24,9 +24,11 @@ import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
 
+import org.scilab.forge.scirenderer.shapes.appearance.Color;
 import org.scilab.modules.commons.CommonFileUtils;
 import org.scilab.modules.graphic_objects.axes.Axes;
 import org.scilab.modules.graphic_objects.console.Console;
+import org.scilab.modules.graphic_objects.figure.ColorMap;
 import org.scilab.modules.graphic_objects.figure.Figure;
 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
 import org.scilab.modules.graphic_objects.graphicModel.GraphicModel;
@@ -127,7 +129,7 @@ public class XmlSaver {
         //immediate_drawing
         setAttribute(elemFig, "immediate_drawing", createAttribute(fig.getImmediateDrawing()), createAttribute(defaultFig.getImmediateDrawing()));
         //background
-        //setAttribute(elemFig, "background", createAttribute(fig.getBackground()), createAttribute(defaultFig.getBackground()));
+        setAttribute(elemFig, "background", createAttribute(fig.getBackground()), createAttribute(defaultFig.getBackground()));
         //visible
         setAttribute(elemFig, "visible", createAttribute(fig.getVisible()), createAttribute(defaultFig.getVisible()));
         //rotation_style
@@ -156,14 +158,12 @@ public class XmlSaver {
         setAttribute(elemFig, "dockable", createAttribute(fig.getDockable()), createAttribute(defaultFig.getDockable()));
         //layout
         setAttribute(elemFig, "layout", createAttribute(LayoutType.enumToString(fig.getLayoutAsEnum())), createAttribute(LayoutType.enumToString(defaultFig.getLayoutAsEnum())));
-
         //default_axes
         setAttribute(elemFig, "default_axes", createAttribute(fig.hasDefaultAxes()), createAttribute(defaultFig.hasDefaultAxes()));
         //icon
         setAttribute(elemFig, "icon", createAttribute(fig.getIcon()), createAttribute(defaultFig.getIcon()));
         //tag
         setAttribute(elemFig, "tag", createAttribute(fig.getTag()), createAttribute(defaultFig.getTag()));
-
         //layout_options
         if (fig.getLayoutAsEnum() == LayoutType.GRID) {
             setAttribute(elemFig, "grid_opt_grid", createAttribute(fig.getGridOptGrid()), createAttribute(defaultFig.getGridOptGrid()));
@@ -177,6 +177,15 @@ public class XmlSaver {
         for (int i = 0; i < children.length; i++) {
             appendChild(elemFig, createElement(doc, children[i], reverseChildren));
         }
+
+        //colormap
+        Double[] cm = fig.getColorMap().getData();
+        Double[] modelcm = defaultFig.getColorMap().getData();
+
+        if (Arrays.deepEquals(cm, modelcm) == false) {
+            createColorMap(doc, elemFig, fig.getColorMap().getData());
+        }
+
         return elemFig;
     }
 
@@ -635,4 +644,22 @@ public class XmlSaver {
         parent.appendChild(elemString);
     }
 
+    private static void createColorMap(Document doc, Element parent, Double[] value) {
+        //create a new Node to store string
+        Element elemString = doc.createElement("colormap");
+        Integer rows = value.length / 3;
+
+        setAttribute(elemString, "size", rows.toString(), "");
+
+        for (int i = 0 ; i < rows ; i++) {
+            Element elemSub = doc.createElement("colormapitem");
+            Color c = new Color(value[i].floatValue(), value[i + rows].floatValue(), value[i + 2 * rows].floatValue());
+            Integer color = c.getRGB();
+            setAttribute(elemSub, "color", color.toString(), null);
+            elemString.appendChild(elemSub);
+        }
+
+        parent.appendChild(elemString);
+    }
+
 }