xml load take care of scilab current directory
[scilab.git] / scilab / modules / graphic_objects / src / java / org / scilab / modules / graphic_objects / xmlloader / XMLDomLoader.java
index 0ae9c16..ba7ddf7 100644 (file)
@@ -9,6 +9,7 @@ import java.util.StringTokenizer;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 
+import org.scilab.modules.commons.CommonFileUtils;
 import org.scilab.modules.commons.gui.FindIconHelper;
 import org.scilab.modules.graphic_objects.builder.Builder;
 import org.scilab.modules.graphic_objects.console.Console;
@@ -35,6 +36,7 @@ public class XMLDomLoader {
 
     private static HashMap<String, Pair<Integer, ModelType>> figPropToGO = new HashMap<String, Pair<Integer, ModelType>>();
     private static HashMap<String, Pair<Integer, ModelType>> UiPropToGO = new HashMap<String, Pair<Integer, ModelType>>();
+    private static HashMap<String, Pair<Integer, ModelType>> MenuPropToGO = new HashMap<String, Pair<Integer, ModelType>>();
     private static HashMap<String, Pair<Integer, ModelType>> BorderPropToGO = new HashMap<String, Pair<Integer, ModelType>>();
 
 
@@ -169,6 +171,17 @@ public class XMLDomLoader {
         BorderPropToGO.put("fontname", new Pair<Integer, ModelType>(__GO_UI_FONTNAME__, ModelType.STRING));
         BorderPropToGO.put("fontsize", new Pair<Integer, ModelType>(__GO_UI_FONTSIZE__, ModelType.INTEGER));
         BorderPropToGO.put("fontweight", new Pair<Integer, ModelType>(__GO_UI_FONTWEIGHT__, ModelType.STRING));
+
+        MenuPropToGO.put("enable", new Pair<Integer, ModelType>(__GO_UI_ENABLE__, ModelType.BOOLEAN));
+        MenuPropToGO.put("foreground", new Pair<Integer, ModelType>(__GO_UI_FOREGROUNDCOLOR__, ModelType.DOUBLE_ARRAY));
+        MenuPropToGO.put("label", new Pair<Integer, ModelType>(__GO_UI_LABEL__, ModelType.STRING));
+        MenuPropToGO.put("hidden", new Pair<Integer, ModelType>(__GO_HIDDEN__, ModelType.BOOLEAN));
+        MenuPropToGO.put("visible", new Pair<Integer, ModelType>(__GO_VISIBLE__, ModelType.BOOLEAN));
+        MenuPropToGO.put("callback", new Pair<Integer, ModelType>(__GO_CALLBACK__, ModelType.STRING));
+        MenuPropToGO.put("callback_type", new Pair<Integer, ModelType>(__GO_CALLBACKTYPE__, ModelType.INTEGER));
+        MenuPropToGO.put("checked", new Pair<Integer, ModelType>(__GO_UI_CHECKED__, ModelType.BOOLEAN));
+        MenuPropToGO.put("icon", new Pair<Integer, ModelType>(__GO_UI_ICON__, ModelType.STRING));
+        MenuPropToGO.put("tag", new Pair<Integer, ModelType>(__GO_TAG__, ModelType.STRING));
     }
 
     private String filename = "";
@@ -186,19 +199,52 @@ public class XMLDomLoader {
     public int parse(String filename) {
         try {
             this.filename = filename;
-            File file = new File(filename);
+            File f = new File(filename);
+            String currentPath = "";
+
+            if (f.exists()) {
+                //add filename filepath in ScilabSwingUtilities paths
+                if (f.isAbsolute()) {
+                    String filePath = f.getAbsolutePath();
+                    currentPath = filePath.substring(0, filePath.lastIndexOf(File.separator));
+                    FindIconHelper.addThemePath(currentPath);
+                } else {
+                    String initialDirectoryPath = CommonFileUtils.getCWD();
+                    String filePath = "";
+                    Integer index = filename.lastIndexOf(File.separator);
+                    if (index != -1) {
+                        filePath = filename.substring(0, index);
+                    }
+
+                    currentPath = initialDirectoryPath + File.separator + filePath;
+                    FindIconHelper.addThemePath(currentPath);
 
-            if (file.exists()) {
-                //add filename filepath in FindIconHelper paths
-                String absoluteFilePath = file.getAbsolutePath();
-                String path = absoluteFilePath.substring(0, absoluteFilePath.lastIndexOf(File.separator));
-                FindIconHelper.addThemePath(path);
+                    f = new File(currentPath + File.separator + filename);
+                }
             } else {
-                return 1;
+                //try to find file in currentPath
+                if (f.isAbsolute()) {
+                    //failed
+                    return 1;
+                } else {
+                    String initialDirectoryPath = CommonFileUtils.getCWD();
+                    String filePath = "";
+                    Integer index = filename.lastIndexOf(File.separator);
+                    if (index != -1) {
+                        filePath = filename.substring(0, index);
+                    }
+
+                    currentPath = initialDirectoryPath + File.separator + filePath;
+                }
+
+                f = new File(currentPath + File.separator + filename);
+                if (f.exists() == false) {
+                    return 1;
+                }
             }
 
             DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
-            Document doc = dBuilder.parse(file);
+            Document doc = dBuilder.parse(f);
             doc.getDocumentElement().normalize();
 
             if (doc.hasChildNodes()) {
@@ -294,8 +340,11 @@ public class XMLDomLoader {
                         break;
                     }
 
-                    case __GO_UICONTEXTMENU__:
+                    case __GO_UICONTEXTMENU__: {
+                        break;
+                    }
                     case __GO_UIMENU__: {
+                        child = createUiMenu(parent, childNode);
                         break;
                     }
 
@@ -521,6 +570,52 @@ public class XMLDomLoader {
         return uic;
     }
 
+    private Integer createUiMenu(Integer parent, Node node) {
+        GraphicController controller = GraphicController.getController();
+        NamedNodeMap attr = node.getAttributes();
+
+        Integer uim = GraphicController.getController().askObject(GraphicObject.getTypeFromName(__GO_UIMENU__));
+        controller.setProperty(uim, __GO_VISIBLE__, true);
+
+        for (int i = 0 ; i < attr.getLength() ; i++) {
+            Node prop = attr.item(i);
+            Pair<Integer, ModelType> pair = MenuPropToGO.get(prop.getNodeName());
+            System.out.println("node : " + prop.getNodeName());
+            ModelType modelType = pair.getSecond();
+            switch (modelType) {
+                case BOOLEAN:
+                    controller.setProperty(uim, pair.getFirst(), getAttributeAsBoolean(prop.getNodeValue()));
+                    break;
+                case BOOLEAN_ARRAY :
+                    controller.setProperty(uim, pair.getFirst(), getAttributeAsBooleanArray(prop.getNodeValue()));
+                    break;
+                case DOUBLE:
+                    controller.setProperty(uim, pair.getFirst(), getAttributeAsDouble(prop.getNodeValue()));
+                    break;
+                case DOUBLE_ARRAY:
+                    controller.setProperty(uim, pair.getFirst(), getAttributeAsDoubleArray(prop.getNodeValue()));
+                    break;
+                case INTEGER:
+                    controller.setProperty(uim, pair.getFirst(), getAttributeAsInteger(prop.getNodeValue()));
+                    break;
+                case INTEGER_ARRAY:
+                    controller.setProperty(uim, pair.getFirst(), getAttributeAsIntegerArray(prop.getNodeValue()));
+                    break;
+                case STRING:
+                    controller.setProperty(uim, pair.getFirst(), getAttributeAsString(prop.getNodeValue()));
+                    break;
+                case STRING_ARRAY:
+                    controller.setProperty(uim, pair.getFirst(), getAttributeAsStringArray(prop.getNodeValue()));
+                    break;
+                default:
+                    System.out.println("missing type");
+                    break;
+            }
+        }
+
+        return uim;
+    }
+
     private Boolean getAttributeAsBoolean(String val) {
         if (val == null) {
             return null;