The variable browser also shows the user type of a mlist
[scilab.git] / scilab / modules / ui_data / src / java / org / scilab / modules / ui_data / BrowseVar.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2010 - DIGITEO - Bruno JOFRET
4  * Copyright (C) 2010 - DIGITEO - Sylvestre KOUMAR
5  *
6  * This file must be used under the terms of the CeCILL.
7  * This source file is licensed as described in the file COPYING, which
8  * you should have received as part of this distribution.  The terms
9  * are also available at
10  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
11  *
12  */
13 package org.scilab.modules.ui_data;
14
15 import javax.swing.ImageIcon;
16 import javax.swing.JLabel;
17
18 import org.scilab.modules.gui.utils.ScilabSwingUtilities;
19 import org.scilab.modules.localization.Messages;
20 import org.scilab.modules.types.ScilabTypeEnum;
21 import org.scilab.modules.types.ScilabTypeEnumDescription;
22 import org.scilab.modules.ui_data.variablebrowser.ScilabVariableBrowser;
23
24 /**
25  *
26  * Static class to open/close Scilab Variable browser
27  *
28  */
29 public class BrowseVar {
30
31     public static final int ICON_COLUMN_INDEX = 0;
32     public static final int NAME_COLUMN_INDEX = 1;
33     public static final int SIZE_COLUMN_INDEX = 2;
34     public static final int TYPE_DESC_COLUMN_INDEX = 3;
35     public static final int VISIBILITY_COLUMN_INDEX = 4;
36     public static final int BYTES_COLUMN_INDEX = 5;
37     public static final int FROM_SCILAB_COLUMN_INDEX = 6;
38     public static final int TYPE_COLUMN_INDEX = 7;
39
40     public static final String[] COLUMNNAMES = new String[] {"", /* Icon */
41             Messages.gettext("Name"),
42             Messages.gettext("Dimension"),
43             Messages.gettext("Type"),
44             Messages.gettext("Visibility"),
45             Messages.gettext("Bytes"),
46             Messages.gettext("User"),
47             Messages.gettext("Type int value"),
48                                                             };
49
50     public static final int[] COLUMNSALIGNMENT = new int[] { -1, JLabel.LEFT, JLabel.RIGHT, JLabel.RIGHT, JLabel.RIGHT, JLabel.RIGHT, JLabel.RIGHT, JLabel.RIGHT};
51
52     private static final ImageIcon NO_ICON = new ImageIcon(ScilabSwingUtilities.findIcon("noicon"));
53     private static final ImageIcon DOUBLE_ICON = new ImageIcon(ScilabSwingUtilities.findIcon("double"));
54     private static final ImageIcon POLYNOMIAL_ICON = new ImageIcon(ScilabSwingUtilities.findIcon("polynomial"));
55     private static final ImageIcon BOOLEAN_ICON = new ImageIcon(ScilabSwingUtilities.findIcon("boolean"));
56     private static final ImageIcon SPARSE_ICON = new ImageIcon(ScilabSwingUtilities.findIcon("sparse"));
57     private static final ImageIcon INT_ICON = new ImageIcon(ScilabSwingUtilities.findIcon("int"));
58     private static final ImageIcon HANDLE_ICON = new ImageIcon(ScilabSwingUtilities.findIcon("handle"));
59     private static final ImageIcon STRING_ICON = new ImageIcon(ScilabSwingUtilities.findIcon("string"));
60     private static final ImageIcon FUNCTION_ICON = new ImageIcon(ScilabSwingUtilities.findIcon("function"));
61     private static final ImageIcon LIST_ICON = new ImageIcon(ScilabSwingUtilities.findIcon("list"));
62     private static final ImageIcon TLIST_ICON = new ImageIcon(ScilabSwingUtilities.findIcon("tlist"));
63     private static final ImageIcon MLIST_ICON = new ImageIcon(ScilabSwingUtilities.findIcon("mlist"));
64     private static final ImageIcon USER_ICON = new ImageIcon(ScilabSwingUtilities.findIcon("user"));
65     private static final ImageIcon FPTR_ICON = new ImageIcon(ScilabSwingUtilities.findIcon("fptr"));
66
67     /**
68      * Default private constructor for utility class
69      */
70     private BrowseVar() { }
71
72     /**
73      * Get ImageIcon instance from Scilab type (as int)
74      * @param type : scilab type as integer
75      * @return instance of type Icon
76      */
77     private static ImageIcon getIconFromType(int type) {
78         switch (type) {
79             case 1:
80                 return DOUBLE_ICON;
81             case 2:
82                 return POLYNOMIAL_ICON;
83             case 4:
84                 return BOOLEAN_ICON;
85             case 5:
86             case 6:
87             case 7:
88                 return SPARSE_ICON;
89             case 8:
90                 return INT_ICON;
91             case 9:
92                 return HANDLE_ICON;
93             case 10:
94                 return STRING_ICON;
95             case 11:
96             case 13:
97                 return FUNCTION_ICON;
98                 /*case 14:
99                   return LIBRARY_ICON;*/
100             case 15:
101                 return LIST_ICON;
102             case 16:
103                 return TLIST_ICON;
104             case 17:
105                 return MLIST_ICON;
106             case 128:
107                 return USER_ICON;
108             case 130:
109                 return FPTR_ICON;
110             default:
111                 return NO_ICON;
112         }
113     }
114
115     /**
116      * Open variable Browser
117      */
118     public static void openVariableBrowser(boolean update) {
119         ScilabVariableBrowser.getVariableBrowser(update);
120     }
121
122     /**
123      * Open variable Browser with information given by Scilab
124      * @param dataNames : scilab variable name
125      * @param dataBytes : scilab variable size in bytes
126      * @param dataTypes : scilab variable type (as integer)
127      * @param dataIntegerTypes : Type of int (-1 if not int)
128      * @param dataSizes : scilab variable size under the form "XxX"
129      * @param dataVisibility : local or global variable
130      * @param dataFromUser : Scilab data or user data
131      */
132     public static void openVariableBrowser(boolean update, String[] dataNames, int[] dataBytes, int[] dataTypes, int[] dataIntegerTypes, String[] variableListTypes, String[] dataSizes, String[] dataVisibility, boolean[] dataFromUser) {
133         Object[][] data = new Object[dataNames.length][COLUMNNAMES.length];
134         for (int i = 0; i < dataNames.length; ++i) {
135             data[i][ICON_COLUMN_INDEX] = getIconFromType(dataTypes[i]);
136             data[i][NAME_COLUMN_INDEX] = dataNames[i];
137             data[i][SIZE_COLUMN_INDEX] = dataSizes[i];
138             data[i][TYPE_DESC_COLUMN_INDEX] = ScilabTypeEnumDescription.getTypeDescriptionFromId(dataTypes[i]);
139             if (dataTypes[i] == ScilabTypeEnum.sci_ints.swigValue() && dataIntegerTypes[i] != 0) {
140                 // It is an integer. We want to detail the precision of the int
141                 data[i][TYPE_DESC_COLUMN_INDEX] = data[i][TYPE_DESC_COLUMN_INDEX] + " " + dataIntegerTypes[i];
142             }
143             if ((dataTypes[i] == ScilabTypeEnum.sci_tlist.swigValue() || dataTypes[i] == ScilabTypeEnum.sci_mlist.swigValue()) && !variableListTypes[i].equals("")) {
144                 // It is a tlist and we want to display the user datatype
145                 data[i][TYPE_DESC_COLUMN_INDEX] = variableListTypes[i] + " (" + data[i][TYPE_DESC_COLUMN_INDEX] + ")";
146             }
147             data[i][VISIBILITY_COLUMN_INDEX] = dataVisibility[i];
148             data[i][BYTES_COLUMN_INDEX] = dataBytes[i];
149             data[i][FROM_SCILAB_COLUMN_INDEX] = dataFromUser[i]; /* Tag if it is a variable from the user or from Scilab (%pi, %eps, etc) */
150             data[i][TYPE_COLUMN_INDEX] = dataTypes[i];
151         }
152         ScilabVariableBrowser.getVariableBrowser(update, data);
153     }
154
155     /**
156      * @return true if an instance of BrowseVar already exists.
157      */
158     public static boolean isVariableBrowserOpened() {
159         return ScilabVariableBrowser.isBrowseVarOpened();
160     }
161
162     /**
163      * Close Variable Browser
164      */
165     public static void closeVariableBrowser() {
166         ScilabVariableBrowser.closeVariableBrowser();
167     }
168 }