Update debug view
[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.1-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 import org.scilab.modules.graphic_objects.uicontrol.Uicontrol;
34
35 public abstract class TreeView implements GraphicView, TreeSelectionListener {
36
37     protected Map<Integer, DefaultMutableTreeNode> allObjects = new ConcurrentHashMap<Integer, DefaultMutableTreeNode>();
38     protected DefaultTreeModel topModel = null;
39     protected DefaultMutableTreeNode top = new DefaultMutableTreeNode("Graphic Objects");
40     private JTree tree;
41     private JEditorPane htmlDetailPane;
42     private JFrame frame;
43
44     public void show() {
45         frame.setVisible(true);
46     }
47
48     protected TreeView() {
49         frame = new JFrame("TreeView");
50         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
51
52         JPanel panel = new JPanel(new GridLayout(1, 0));
53
54         topModel = new DefaultTreeModel(top);
55         tree = new JTree(topModel);
56         tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
57         tree.addTreeSelectionListener(this);
58         tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
59
60
61         //Listen for when the selection changes.
62         tree.addTreeSelectionListener(this);
63
64
65         Dimension minDims = new Dimension(400, 300);
66         //Create the scroll pane and add the tree to it.
67         JScrollPane treeView = new JScrollPane(tree);
68         treeView.setMinimumSize(minDims);
69
70         //Create the HTML detail viewing pane.
71         htmlDetailPane = new JEditorPane();
72         htmlDetailPane.setEditable(false);
73         htmlDetailPane.setContentType("text/html");
74         JScrollPane htmlView = new JScrollPane(htmlDetailPane);
75
76         //Add the scroll panes to a split pane.
77         JSplitPane treeDetailPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
78         //treeDetailPane.setLeftComponent(fTreeView);
79         treeDetailPane.setLeftComponent(treeView);
80         treeDetailPane.setRightComponent(htmlView);
81
82         treeDetailPane.setDividerLocation(100);
83         treeDetailPane.setPreferredSize(new Dimension(1200, 600));
84
85         //Add the split pane to this panel.
86         panel.add(treeDetailPane);
87         frame.add(panel);
88         frame.setVisible(false);
89         frame.setSize(1200, 600);
90     }
91
92     public abstract void createObject(Integer id);
93     public abstract void deleteObject(Integer id) ;
94     public abstract void updateObject(Integer id, int property);
95
96     /*
97      * Inner class to wrap GraphicObject in a Node.
98      */
99     protected class GraphicObjectNode {
100         private GraphicObject graphicObject;
101
102         public GraphicObjectNode(GraphicObject object) {
103             this.graphicObject = object;
104         }
105
106         public String toString() {
107             return typeToString(graphicObject.getType()) + " : @" + graphicObject.getIdentifier().toString();
108         }
109
110         public String toHTML() {
111             StringBuffer strBuff = new StringBuffer();
112             strBuff.append("<html><body>");
113             strBuff.append("<h1>Graphic Object of type: " + typeToString(graphicObject.getType()) + "</h1>");
114             strBuff.append("<h2> Id : " + graphicObject.getIdentifier() + "</h2>");
115             strBuff.append("<table border=\"1\">");
116             strBuff.append("<tr><th>Property Name</th><th>Value</th></tr>");
117             // Graphic Object
118             for (GraphicObject.GraphicObjectPropertyType goProperty : GraphicObject.GraphicObjectPropertyType.values()) {
119                 strBuff.append("<tr><td>");
120                 strBuff.append(goProperty);
121                 strBuff.append("</td><td>");
122                 Object values = graphicObject.getProperty(goProperty);
123                 if (values instanceof String[]) {
124                     String[] strings = (String []) values;
125                     strBuff.append("{");
126                     for (int i = 0; i < strings.length ; ++i) {
127                         strBuff.append(strings[i] + ",");
128                     }
129                     strBuff.append("}");
130                 } else if (values instanceof Integer[]) {
131                     Integer[] ints = (Integer []) values;
132                     strBuff.append("{");
133                     for (int i = 0; i < ints.length ; ++i) {
134                         strBuff.append(ints[i] + ",");
135                     }
136                     strBuff.append("}");
137                 } else if (values instanceof Double[]) {
138                     Double[] vals = (Double []) values;
139                     strBuff.append("{");
140                     for (int i = 0; i < vals.length ; ++i) {
141                         strBuff.append(vals[i] + ",");
142                     }
143                     strBuff.append("}");
144                 } else {
145                     if (values != null) {
146                         if (values.getClass().getPackage().getName().contains("scilab")) {
147                             strBuff.append(values.getClass().getCanonicalName());
148                         } else {
149                             strBuff.append(values.toString());
150                         }
151                     } else {
152                         strBuff.append("??? NULL ???");
153                     }
154                 }
155                 strBuff.append("</td></tr>");
156             }
157
158             // ContouredObject
159             if (graphicObject instanceof ContouredObject) {
160                 for (ContouredObject.ContouredObjectPropertyType goProperty : ContouredObject.ContouredObjectPropertyType.values()) {
161                     strBuff.append("<tr><td>");
162                     strBuff.append(goProperty);
163                     strBuff.append("</td><td>");
164                     strBuff.append(graphicObject.getProperty(goProperty));
165                     strBuff.append("</td></tr>");
166                 }
167             }
168
169             // UiControl
170             if (graphicObject instanceof Uicontrol) {
171                 Uicontrol uiControl = (Uicontrol) graphicObject;
172                 strBuff.append("<tr><td>STYLE</td><td>" + typeToString(uiControl.getStyle()) + "</td>");
173                 strBuff.append("<tr><td>LAYOUT</td><td>" + uiControl.getLayoutAsEnum() + "</td>");
174                 strBuff.append("<tr><td>GROUP NAME</td><td>" + uiControl.getGroupName() + "</td>");
175             }
176
177             strBuff.append("</table>");
178             strBuff.append("</body></html>");
179             return strBuff.toString();
180         }
181
182         private String typeToString(Integer key) {
183             return LogView.pierreDeRosette.get(key);
184         }
185     }
186
187
188
189     /** Required by TreeSelectionListener interface. */
190     public void valueChanged(TreeSelectionEvent e) {
191         Object node = tree.getLastSelectedPathComponent();
192
193         if (node == null || !(node instanceof DefaultMutableTreeNode)) {
194             htmlDetailPane.setText("");
195             return;
196         }
197
198         Object nodeInfo = ((DefaultMutableTreeNode) node).getUserObject();
199         if (nodeInfo == null || !(nodeInfo instanceof GraphicObjectNode)) {
200             htmlDetailPane.setText("");
201             return;
202         }
203         GraphicObjectNode graphicObject = (GraphicObjectNode) nodeInfo;
204         htmlDetailPane.setText(graphicObject.toHTML());
205     }
206
207 }