Remove Objects children before deleting it. 32/6332/1
Bruno JOFRET [Fri, 17 Feb 2012 14:13:23 +0000 (15:13 +0100)]
Enable TreeView:
* FlattenTreeView: shows all objects with properties
* GedTreeView: show hierarchical tree view

/!\ If a top hierarchical level object is removed, some inner may remain.

Change-Id: I64634eb264662d0cca0b98f7a6703bf586a60253

scilab/modules/graphic_objects/src/java/org/scilab/modules/graphic_objects/MVCDebugView.java
scilab/modules/graphic_objects/src/java/org/scilab/modules/graphic_objects/graphicController/GraphicController.java
scilab/modules/graphic_objects/src/java/org/scilab/modules/graphic_objects/graphicView/FlattenTreeView.java [new file with mode: 0644]
scilab/modules/graphic_objects/src/java/org/scilab/modules/graphic_objects/graphicView/GedTreeView.java [new file with mode: 0644]
scilab/modules/graphic_objects/src/java/org/scilab/modules/graphic_objects/graphicView/TreeView.java
scilab/modules/gui/etc/graphics_menubar.xml

index bafb575..033bed4 100644 (file)
@@ -11,8 +11,9 @@
  */
 package org.scilab.modules.graphic_objects;
 
+import org.scilab.modules.graphic_objects.graphicView.FlattenTreeView;
+import org.scilab.modules.graphic_objects.graphicView.GedTreeView;
 import org.scilab.modules.graphic_objects.graphicView.GuiLogView;
-import org.scilab.modules.graphic_objects.graphicView.TreeView;
 
 public class MVCDebugView {
 
@@ -21,6 +22,10 @@ public class MVCDebugView {
     }
    
     public static void showAllObjectsView() {
-        TreeView.createTreeView().show();
-    } 
+        FlattenTreeView.create().show();
+    }
+    
+    public static void showGedView() {
+        GedTreeView.create().show();
+    }
 }
index 24b7c55..991b772 100644 (file)
@@ -24,10 +24,11 @@ import org.scilab.modules.graphic_objects.graphicModel.GraphicModel;
 import org.scilab.modules.graphic_objects.graphicObject.GraphicObject;
 import org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties;
 import org.scilab.modules.graphic_objects.graphicObject.GraphicObject.Type;
+import org.scilab.modules.graphic_objects.graphicView.FlattenTreeView;
+import org.scilab.modules.graphic_objects.graphicView.GedTreeView;
 import org.scilab.modules.graphic_objects.graphicView.GraphicView;
 import org.scilab.modules.graphic_objects.graphicView.GuiLogView;
 import org.scilab.modules.graphic_objects.graphicView.LogView;
-import org.scilab.modules.graphic_objects.graphicView.TreeView;
 
 /**
  * GraphicController class
@@ -35,6 +36,7 @@ import org.scilab.modules.graphic_objects.graphicView.TreeView;
  */
 public class GraphicController {
 
+    private static boolean MVCViewEnable = false;
     private static boolean debugEnable = true;
     private static boolean infoEnable = false;
 
@@ -67,9 +69,10 @@ public class GraphicController {
      * Default constructor
      */
     private GraphicController() {
-        if (!GraphicsEnvironment.isHeadless() && debugEnable && infoEnable) {
+        if (!GraphicsEnvironment.isHeadless() && MVCViewEnable) {
             register(GuiLogView.createGuiLogView());
-            register(TreeView.createTreeView());
+            register(GedTreeView.create());
+            register(FlattenTreeView.create());
         }
         if (infoEnable) {
             register(LogView.createLogView());
@@ -362,18 +365,30 @@ public class GraphicController {
      * @param id deleted object identifier.
      */
     public void removeRelationShipAndDelete(String id) {
-        Object parent = getProperty(id, GraphicObjectProperties.__GO_PARENT__);
-        if ((parent != null) && (parent instanceof String)) {
-            String parentId = (String) parent;
-            if (!parentId.equals("")) {
-                getObjectFromId(parentId).removeChild(id);
-                //setProperty(id, GraphicObjectProperties.__GO_PARENT__, "");
-
-                objectUpdate(parentId, GraphicObjectProperties.__GO_CHILDREN__);
-                //objectUpdate(id, GraphicObjectProperties.__GO_PARENT__);
-            }
+        GraphicObject killMe = getObjectFromId(id);
+        String parentUID = killMe.getParent();
+
+        
+        /* Remove object from Parent's Children list */
+        if (parentUID != null && !parentUID.equals("")) {
+            getObjectFromId(parentUID).removeChild(id);
+            //setProperty(id, GraphicObjectProperties.__GO_PARENT__, "");
+
+            objectUpdate(parentUID, GraphicObjectProperties.__GO_CHILDREN__);
+            //objectUpdate(id, GraphicObjectProperties.__GO_PARENT__);
         }
 
+        recursiveDeleteChildren(killMe);
+        
         deleteObject(id);
     }
+    
+    private void recursiveDeleteChildren(GraphicObject killMe) {
+        String children[] = killMe.getChildren();
+        
+        for (int i = 0 ; i < children.length ; ++i) {
+            recursiveDeleteChildren(getObjectFromId(children[i]));
+            deleteObject(children[i]);
+        }
+    }
 }
diff --git a/scilab/modules/graphic_objects/src/java/org/scilab/modules/graphic_objects/graphicView/FlattenTreeView.java b/scilab/modules/graphic_objects/src/java/org/scilab/modules/graphic_objects/graphicView/FlattenTreeView.java
new file mode 100644 (file)
index 0000000..20c8f2a
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) 2012 - Scilab Enterprises - Bruno JOFRET
+ *
+ * This file must be used under the terms of the CeCILL.
+ * This source file is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution.  The terms
+ * are also available at
+ * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+ *
+ */
+package org.scilab.modules.graphic_objects.graphicView;
+
+import javax.swing.tree.DefaultMutableTreeNode;
+
+import org.scilab.modules.graphic_objects.graphicController.GraphicController;
+import org.scilab.modules.graphic_objects.graphicObject.GraphicObject;
+import org.scilab.modules.graphic_objects.uimenu.Uimenu;
+
+public class FlattenTreeView extends TreeView {
+    private static FlattenTreeView me;
+    
+    private FlattenTreeView() {
+        super();
+    }
+    
+    public static FlattenTreeView create() {
+        if (me == null) {
+            me = new FlattenTreeView();
+        }
+        return me;
+    }
+    
+    public void createObject(String id) {
+        try {
+            GraphicObject graphiObject = GraphicController.getController().getObjectFromId(id);
+            //if (!(graphiObject instanceof Uimenu)) {
+                DefaultMutableTreeNode node = new DefaultMutableTreeNode(new GraphicObjectNode(graphiObject));
+
+                allObjects.put(id, node);
+                top.add(node);
+                topModel.nodeStructureChanged(top);
+            //}
+        }
+        catch (Exception e) {
+            e.printStackTrace();
+        }       
+    }
+    
+    public void deleteObject(String id) {
+        try {
+            DefaultMutableTreeNode objectNode = allObjects.get(id);
+            if (objectNode != null) {
+                DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) objectNode.getParent();
+                objectNode.removeFromParent();
+                allObjects.remove(id);
+                topModel.nodeStructureChanged(parentNode);
+            }
+        }
+        catch (Exception e) {
+            e.printStackTrace();
+        }        
+    }
+    
+    public void updateObject(String id, String property) {
+        
+    }
+}
diff --git a/scilab/modules/graphic_objects/src/java/org/scilab/modules/graphic_objects/graphicView/GedTreeView.java b/scilab/modules/graphic_objects/src/java/org/scilab/modules/graphic_objects/graphicView/GedTreeView.java
new file mode 100644 (file)
index 0000000..42101d1
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) 2012 - Scilab Enterprises - Bruno JOFRET
+ *
+ * This file must be used under the terms of the CeCILL.
+ * This source file is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution.  The terms
+ * are also available at
+ * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+ *
+ */
+package org.scilab.modules.graphic_objects.graphicView;
+
+import javax.swing.tree.DefaultMutableTreeNode;
+
+import org.scilab.modules.graphic_objects.graphicController.GraphicController;
+import org.scilab.modules.graphic_objects.graphicObject.GraphicObject;
+import org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties;
+
+public class GedTreeView extends TreeView {
+    
+    private static GedTreeView me;
+    
+    private GedTreeView() {
+        super();
+    }
+    
+    public static GedTreeView create() {
+        if (me == null) {
+            me = new GedTreeView();
+        }
+        return me;
+    }
+    
+    public void createObject(String id) {
+        try {
+            GraphicObject graphiObject = GraphicController.getController().getObjectFromId(id);
+            DefaultMutableTreeNode node = new DefaultMutableTreeNode(new GraphicObjectNode(graphiObject));
+
+            allObjects.put(id, node);
+            top.add(node);
+            topModel.nodeStructureChanged(top);
+        }
+        catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    public void deleteObject(String id) {
+        try {
+            DefaultMutableTreeNode objectNode = allObjects.get(id);
+            if (objectNode != null) {
+                DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) objectNode.getParent();
+                objectNode.removeFromParent();
+                allObjects.remove(id);
+                topModel.nodeStructureChanged(parentNode);
+            }
+        }
+        catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+    
+    public void updateObject(String id, String property) {
+        
+        DefaultMutableTreeNode objectNode = allObjects.get(id);
+        /*
+         * Update parent property
+         */
+        if (objectNode != null && property.equals(GraphicObjectProperties.__GO_PARENT__))
+        {
+            DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) objectNode.getParent();
+            
+            if (parentNode != null) {
+                // Remove from old parent
+                objectNode.removeFromParent();
+                topModel.nodeStructureChanged(parentNode);
+                
+                String parentUID = (String) GraphicController.getController().getProperty(id, GraphicObjectProperties.__GO_PARENT__);
+                // Retrieve new Parent
+                DefaultMutableTreeNode newParentNode = allObjects.get(parentUID);
+
+                // If there is no more parent, leave object at top level
+                if (newParentNode == null) {
+                    newParentNode = top;
+                }
+                
+                newParentNode.add(objectNode);
+                topModel.nodeStructureChanged(newParentNode);               
+            }
+        }
+    }
+
+}
index 22b612a..e3bfb44 100644 (file)
@@ -13,7 +13,8 @@ package org.scilab.modules.graphic_objects.graphicView;
 
 import java.awt.Dimension;
 import java.awt.GridLayout;
-import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 import javax.swing.JEditorPane;
 import javax.swing.JFrame;
@@ -28,62 +29,44 @@ import javax.swing.tree.DefaultTreeModel;
 import javax.swing.tree.TreeSelectionModel;
 
 import org.scilab.modules.graphic_objects.contouredObject.ContouredObject;
-import org.scilab.modules.graphic_objects.graphicController.GraphicController;
 import org.scilab.modules.graphic_objects.graphicObject.GraphicObject;
 
-public class TreeView implements GraphicView, TreeSelectionListener{
+public abstract class TreeView implements GraphicView, TreeSelectionListener{
 
-    private static TreeView me;
-    private HashMap<String, DefaultMutableTreeNode> fObjects = new HashMap<String, DefaultMutableTreeNode>();
-    private DefaultTreeModel topFModel = null;
-    private DefaultMutableTreeNode topF = new DefaultMutableTreeNode("Graphic Objects Flatten");
-    private JTree flatTree;
+    protected Map<String, DefaultMutableTreeNode> allObjects = new ConcurrentHashMap<String, DefaultMutableTreeNode>();
+    protected DefaultTreeModel topModel = null;
+    protected DefaultMutableTreeNode top = new DefaultMutableTreeNode("Graphic Objects");
+    private JTree tree;
     private JEditorPane htmlDetailPane;
     private JFrame frame;
-    
-    public static TreeView createTreeView() {
-        if (me == null) {
-            me = new TreeView();
-        }
-        return me;
-    }
 
     public void show() {
         frame.setVisible(true);
     }
-    
-    private TreeView() {
+
+    protected TreeView() {
         frame = new JFrame("TreeView");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
         JPanel panel = new JPanel(new GridLayout(1,0));
 
-        //Create a tree that allows one selection at a time.
-        //topHModel = new DefaultTreeModel(topH);
-        //hierarchicalTree = new JTree(topHModel);
-        //hierarchicalTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
-        //hierarchicalTree.addTreeSelectionListener(this);
-        //hierarchicalTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
+        topModel = new DefaultTreeModel(top);
+        tree = new JTree(topModel);
+        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
+        tree.addTreeSelectionListener(this);
+        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
 
-        //Create a tree that allows one selection at a time.
-        topFModel = new DefaultTreeModel(topF);
-        flatTree = new JTree(topFModel);
-        flatTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
-        flatTree.addTreeSelectionListener(this);
-        flatTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
 
         //Listen for when the selection changes.
-        //tree.addTreeSelectionListener(this);
+        tree.addTreeSelectionListener(this);
 
 
         Dimension minDims = new Dimension(400,300);
         //Create the scroll pane and add the tree to it. 
-        //JScrollPane hTreeView = new JScrollPane(hierarchicalTree);
-        //hTreeView.setMinimumSize(minDims);
-        JScrollPane fTreeView = new JScrollPane(flatTree);
-        fTreeView.setMinimumSize(minDims);
+        JScrollPane treeView = new JScrollPane(tree);
+        treeView.setMinimumSize(minDims);
 
-                //Create the HTML detail viewing pane.
+        //Create the HTML detail viewing pane.
         htmlDetailPane = new JEditorPane();
         htmlDetailPane.setEditable(false);
         htmlDetailPane.setContentType("text/html");
@@ -91,7 +74,8 @@ public class TreeView implements GraphicView, TreeSelectionListener{
 
         //Add the scroll panes to a split pane.
         JSplitPane treeDetailPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
-        treeDetailPane.setLeftComponent(fTreeView);
+        //treeDetailPane.setLeftComponent(fTreeView);
+        treeDetailPane.setLeftComponent(treeView);
         treeDetailPane.setRightComponent(htmlView);
 
         treeDetailPane.setDividerLocation(100); 
@@ -104,39 +88,14 @@ public class TreeView implements GraphicView, TreeSelectionListener{
         frame.setSize(1200, 600);
     }
 
-    public void createObject(String id) {
-        try {
-            DefaultMutableTreeNode node = new DefaultMutableTreeNode(new GraphicObjectNode(GraphicController.getController().getObjectFromId(id)));
-           
-            node = new DefaultMutableTreeNode(new GraphicObjectNode(GraphicController.getController().getObjectFromId(id)));
-            fObjects.put(id, node);
-            topF.add(node);
-            topFModel.nodeStructureChanged(topF);
-        }
-        catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
-
-    public void deleteObject(String id) {
-        try {
-            DefaultMutableTreeNode node = fObjects.get(id);
-            topF.remove(node);
-            fObjects.remove(id);
-            topFModel.nodeStructureChanged(topF);
-        }
-        catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
-
-    public void updateObject(String id, String property) {
-    }
-
+    public abstract void createObject(String id);
+    public abstract void deleteObject(String id) ;
+    public abstract void updateObject(String id, String property);
+    
     /*
      * Inner class to wrap GraphicObject in a Node.
      */
-    private class GraphicObjectNode {
+    protected class GraphicObjectNode {
         private GraphicObject graphicObject;
 
         public GraphicObjectNode(GraphicObject object) {
@@ -197,7 +156,7 @@ public class TreeView implements GraphicView, TreeSelectionListener{
     }
     /** Required by TreeSelectionListener interface. */
     public void valueChanged(TreeSelectionEvent e) {
-        Object node = flatTree.getLastSelectedPathComponent();
+        Object node = tree.getLastSelectedPathComponent();
 
         if (node == null || !(node instanceof DefaultMutableTreeNode)) {
             htmlDetailPane.setText("");
index 44294ad..c0753d2 100644 (file)
         </submenu>
         <separator/>
         <submenu label="&amp;Ged-like View (Future ged)">
-             <callback instruction='org.scilab.modules.graphic_objects.MVCDebugView.showGedView([SCILAB_FIGURE_ID])' type="3"/>
+             <callback instruction='org.scilab.modules.graphic_objects.MVCDebugView.showGedView' type="3"/>
         </submenu>
         <separator/>
     </menu>