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