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