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