Remove Objects children before deleting it.
[scilab.git] / scilab / modules / graphic_objects / src / java / org / scilab / modules / graphic_objects / graphicView / TreeView.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2010 - DIGITEO - Bruno JOFRET
4  *
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at
9  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12 package org.scilab.modules.graphic_objects.graphicView;
13
14 import java.awt.Dimension;
15 import java.awt.GridLayout;
16 import java.util.Map;
17 import java.util.concurrent.ConcurrentHashMap;
18
19 import javax.swing.JEditorPane;
20 import javax.swing.JFrame;
21 import javax.swing.JPanel;
22 import javax.swing.JScrollPane;
23 import javax.swing.JSplitPane;
24 import javax.swing.JTree;
25 import javax.swing.event.TreeSelectionEvent;
26 import javax.swing.event.TreeSelectionListener;
27 import javax.swing.tree.DefaultMutableTreeNode;
28 import javax.swing.tree.DefaultTreeModel;
29 import javax.swing.tree.TreeSelectionModel;
30
31 import org.scilab.modules.graphic_objects.contouredObject.ContouredObject;
32 import org.scilab.modules.graphic_objects.graphicObject.GraphicObject;
33
34 public abstract class TreeView implements GraphicView, TreeSelectionListener{
35
36     protected Map<String, DefaultMutableTreeNode> allObjects = new ConcurrentHashMap<String, DefaultMutableTreeNode>();
37     protected DefaultTreeModel topModel = null;
38     protected DefaultMutableTreeNode top = new DefaultMutableTreeNode("Graphic Objects");
39     private JTree tree;
40     private JEditorPane htmlDetailPane;
41     private JFrame frame;
42
43     public void show() {
44         frame.setVisible(true);
45     }
46
47     protected TreeView() {
48         frame = new JFrame("TreeView");
49         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
50
51         JPanel panel = new JPanel(new GridLayout(1,0));
52
53         topModel = new DefaultTreeModel(top);
54         tree = new JTree(topModel);
55         tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
56         tree.addTreeSelectionListener(this);
57         tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
58
59
60         //Listen for when the selection changes.
61         tree.addTreeSelectionListener(this);
62
63
64         Dimension minDims = new Dimension(400,300);
65         //Create the scroll pane and add the tree to it. 
66         JScrollPane treeView = new JScrollPane(tree);
67         treeView.setMinimumSize(minDims);
68
69         //Create the HTML detail viewing pane.
70         htmlDetailPane = new JEditorPane();
71         htmlDetailPane.setEditable(false);
72         htmlDetailPane.setContentType("text/html");
73         JScrollPane htmlView = new JScrollPane(htmlDetailPane);
74
75         //Add the scroll panes to a split pane.
76         JSplitPane treeDetailPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
77         //treeDetailPane.setLeftComponent(fTreeView);
78         treeDetailPane.setLeftComponent(treeView);
79         treeDetailPane.setRightComponent(htmlView);
80
81         treeDetailPane.setDividerLocation(100); 
82         treeDetailPane.setPreferredSize(new Dimension(1200, 600));
83
84         //Add the split pane to this panel.
85         panel.add(treeDetailPane);
86         frame.add(panel);
87         frame.setVisible(false);
88         frame.setSize(1200, 600);
89     }
90
91     public abstract void createObject(String id);
92     public abstract void deleteObject(String id) ;
93     public abstract void updateObject(String id, String property);
94     
95     /*
96      * Inner class to wrap GraphicObject in a Node.
97      */
98     protected class GraphicObjectNode {
99         private GraphicObject graphicObject;
100
101         public GraphicObjectNode(GraphicObject object) {
102             this.graphicObject = object;
103         }
104
105         public String toString() {
106             return graphicObject.getType() + " : @" + graphicObject.getIdentifier();
107         }
108
109         public String toHTML() {
110             StringBuffer strBuff = new StringBuffer();
111             strBuff.append("<html><body>");
112             strBuff.append("<h1>Graphic Object of type: "+graphicObject.getType()+"</h1>");
113             strBuff.append("<h2> Id : "+graphicObject.getIdentifier()+"</h2>");
114             strBuff.append("<table border=\"1\">");
115             strBuff.append("<tr><th>Property Name</th><th>Value</th></tr>");
116             // Graphic Object
117             for (GraphicObject.GraphicObjectPropertyType goProperty : GraphicObject.GraphicObjectPropertyType.values()) {
118                 strBuff.append("<tr><td>");
119                 strBuff.append(goProperty);
120                 strBuff.append("</td><td>");
121                 Object values = graphicObject.getProperty(goProperty);
122                 if (values instanceof String[]) {
123                     String[] strings = (String []) values;
124                     strBuff.append("{");
125                     for (int i = 0; i < strings.length ; ++i) {
126                         strBuff.append(strings[i]+",");
127                     }
128                     strBuff.append("}");
129                 }
130                 else {
131                     if (values != null) {
132                         strBuff.append(values.toString());
133                     }
134                     else
135                     {
136                         strBuff.append("??? NULL ???");
137                     }
138                 }
139                 strBuff.append("</td></tr>");
140             }
141
142             // ContouredObject
143             if (graphicObject instanceof ContouredObject) {
144                 for (ContouredObject.ContouredObjectPropertyType goProperty : ContouredObject.ContouredObjectPropertyType.values()) {
145                     strBuff.append("<tr><td>");
146                     strBuff.append(goProperty);
147                     strBuff.append("</td><td>");
148                     strBuff.append(graphicObject.getProperty(goProperty));
149                     strBuff.append("</td></tr>");
150                 }
151             }
152             strBuff.append("</table>");
153             strBuff.append("</body></html>");
154             return strBuff.toString();
155         }
156     }
157     /** Required by TreeSelectionListener interface. */
158     public void valueChanged(TreeSelectionEvent e) {
159         Object node = tree.getLastSelectedPathComponent();
160
161         if (node == null || !(node instanceof DefaultMutableTreeNode)) {
162             htmlDetailPane.setText("");
163             return;
164         }
165
166         Object nodeInfo = ((DefaultMutableTreeNode) node).getUserObject();
167         if (nodeInfo == null || !(nodeInfo instanceof GraphicObjectNode)) {
168             htmlDetailPane.setText("");
169             return;
170         }
171         GraphicObjectNode graphicObject = (GraphicObjectNode) nodeInfo;
172         htmlDetailPane.setText(graphicObject.toHTML());
173     }
174
175 }