Enable flat object view. Update LogView. Register debug view when starting MVC.
[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.HashMap;
17
18 import javax.swing.JEditorPane;
19 import javax.swing.JFrame;
20 import javax.swing.JPanel;
21 import javax.swing.JScrollPane;
22 import javax.swing.JSplitPane;
23 import javax.swing.JTree;
24 import javax.swing.event.TreeSelectionEvent;
25 import javax.swing.event.TreeSelectionListener;
26 import javax.swing.tree.DefaultMutableTreeNode;
27 import javax.swing.tree.DefaultTreeModel;
28 import javax.swing.tree.TreeSelectionModel;
29
30 import org.scilab.modules.graphic_objects.contouredObject.ContouredObject;
31 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
32 import org.scilab.modules.graphic_objects.graphicObject.GraphicObject;
33
34 public class TreeView implements GraphicView, TreeSelectionListener{
35
36     private static TreeView me;
37     private HashMap<String, DefaultMutableTreeNode> fObjects = new HashMap<String, DefaultMutableTreeNode>();
38     private DefaultTreeModel topFModel = null;
39     private DefaultMutableTreeNode topF = new DefaultMutableTreeNode("Graphic Objects Flatten");
40     private JTree flatTree;
41     private JEditorPane htmlDetailPane;
42     private JFrame frame;
43     
44     public static TreeView createTreeView() {
45         if (me == null) {
46             me = new TreeView();
47         }
48         return me;
49     }
50
51     public void show() {
52         frame.setVisible(true);
53     }
54     
55     private TreeView() {
56         frame = new JFrame("TreeView");
57         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
58
59         JPanel panel = new JPanel(new GridLayout(1,0));
60
61         //Create a tree that allows one selection at a time.
62         //topHModel = new DefaultTreeModel(topH);
63         //hierarchicalTree = new JTree(topHModel);
64         //hierarchicalTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
65         //hierarchicalTree.addTreeSelectionListener(this);
66         //hierarchicalTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
67
68         //Create a tree that allows one selection at a time.
69         topFModel = new DefaultTreeModel(topF);
70         flatTree = new JTree(topFModel);
71         flatTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
72         flatTree.addTreeSelectionListener(this);
73         flatTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
74
75         //Listen for when the selection changes.
76         //tree.addTreeSelectionListener(this);
77
78
79         Dimension minDims = new Dimension(400,300);
80         //Create the scroll pane and add the tree to it. 
81         //JScrollPane hTreeView = new JScrollPane(hierarchicalTree);
82         //hTreeView.setMinimumSize(minDims);
83         JScrollPane fTreeView = new JScrollPane(flatTree);
84         fTreeView.setMinimumSize(minDims);
85
86                 //Create the HTML detail viewing pane.
87         htmlDetailPane = new JEditorPane();
88         htmlDetailPane.setEditable(false);
89         htmlDetailPane.setContentType("text/html");
90         JScrollPane htmlView = new JScrollPane(htmlDetailPane);
91
92         //Add the scroll panes to a split pane.
93         JSplitPane treeDetailPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
94         treeDetailPane.setLeftComponent(fTreeView);
95         treeDetailPane.setRightComponent(htmlView);
96
97         treeDetailPane.setDividerLocation(100); 
98         treeDetailPane.setPreferredSize(new Dimension(1200, 600));
99
100         //Add the split pane to this panel.
101         panel.add(treeDetailPane);
102         frame.add(panel);
103         frame.setVisible(false);
104         frame.setSize(1200, 600);
105     }
106
107     public void createObject(String id) {
108         try {
109             DefaultMutableTreeNode node = new DefaultMutableTreeNode(new GraphicObjectNode(GraphicController.getController().getObjectFromId(id)));
110            
111             node = new DefaultMutableTreeNode(new GraphicObjectNode(GraphicController.getController().getObjectFromId(id)));
112             fObjects.put(id, node);
113             topF.add(node);
114             topFModel.nodeStructureChanged(topF);
115         }
116         catch (Exception e) {
117             e.printStackTrace();
118         }
119     }
120
121     public void deleteObject(String id) {
122         try {
123             DefaultMutableTreeNode node = fObjects.get(id);
124             topF.remove(node);
125             fObjects.remove(id);
126             topFModel.nodeStructureChanged(topF);
127         }
128         catch (Exception e) {
129             e.printStackTrace();
130         }
131     }
132
133     public void updateObject(String id, String property) {
134     }
135
136     /*
137      * Inner class to wrap GraphicObject in a Node.
138      */
139     private class GraphicObjectNode {
140         private GraphicObject graphicObject;
141
142         public GraphicObjectNode(GraphicObject object) {
143             this.graphicObject = object;
144         }
145
146         public String toString() {
147             return graphicObject.getType() + " : @" + graphicObject.getIdentifier();
148         }
149
150         public String toHTML() {
151             StringBuffer strBuff = new StringBuffer();
152             strBuff.append("<html><body>");
153             strBuff.append("<h1>Graphic Object of type: "+graphicObject.getType()+"</h1>");
154             strBuff.append("<h2> Id : "+graphicObject.getIdentifier()+"</h2>");
155             strBuff.append("<table border=\"1\">");
156             strBuff.append("<tr><th>Property Name</th><th>Value</th></tr>");
157             // Graphic Object
158             for (GraphicObject.GraphicObjectPropertyType goProperty : GraphicObject.GraphicObjectPropertyType.values()) {
159                 strBuff.append("<tr><td>");
160                 strBuff.append(goProperty);
161                 strBuff.append("</td><td>");
162                 Object values = graphicObject.getProperty(goProperty);
163                 if (values instanceof String[]) {
164                     String[] strings = (String []) values;
165                     strBuff.append("{");
166                     for (int i = 0; i < strings.length ; ++i) {
167                         strBuff.append(strings[i]+",");
168                     }
169                     strBuff.append("}");
170                 }
171                 else {
172                     if (values != null) {
173                         strBuff.append(values.toString());
174                     }
175                     else
176                     {
177                         strBuff.append("??? NULL ???");
178                     }
179                 }
180                 strBuff.append("</td></tr>");
181             }
182
183             // ContouredObject
184             if (graphicObject instanceof ContouredObject) {
185                 for (ContouredObject.ContouredObjectPropertyType goProperty : ContouredObject.ContouredObjectPropertyType.values()) {
186                     strBuff.append("<tr><td>");
187                     strBuff.append(goProperty);
188                     strBuff.append("</td><td>");
189                     strBuff.append(graphicObject.getProperty(goProperty));
190                     strBuff.append("</td></tr>");
191                 }
192             }
193             strBuff.append("</table>");
194             strBuff.append("</body></html>");
195             return strBuff.toString();
196         }
197     }
198     /** Required by TreeSelectionListener interface. */
199     public void valueChanged(TreeSelectionEvent e) {
200         Object node = flatTree.getLastSelectedPathComponent();
201
202         if (node == null || !(node instanceof DefaultMutableTreeNode)) {
203             htmlDetailPane.setText("");
204             return;
205         }
206
207         Object nodeInfo = ((DefaultMutableTreeNode) node).getUserObject();
208         if (nodeInfo == null || !(nodeInfo instanceof GraphicObjectNode)) {
209             htmlDetailPane.setText("");
210             return;
211         }
212         GraphicObjectNode graphicObject = (GraphicObjectNode) nodeInfo;
213         htmlDetailPane.setText(graphicObject.toHTML());
214     }
215
216 }