* Bug 15876 fixed: now struct and cells are reported native in browsevar
[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  * Copyright (C) 2012 - 2016 - Scilab Enterprises
7  *
8  * This file is hereby licensed under the terms of the GNU GPL v2.0,
9  * pursuant to article 5.3.4 of the CeCILL v.2.1.
10  * This file was originally licensed under the terms of the CeCILL v2.1,
11  * and continues to be available under such terms.
12  * For more information, see the COPYING file which you should have received
13  * along with this program.
14  *
15  */
16 package org.scilab.modules.ui_data;
17
18 import javax.swing.ImageIcon;
19 import javax.swing.JLabel;
20
21 import org.scilab.modules.commons.gui.FindIconHelper;
22 import org.scilab.modules.localization.Messages;
23 import org.scilab.modules.types.ScilabTypeEnum;
24 import org.scilab.modules.types.ScilabTypeEnumDescription;
25 import org.scilab.modules.ui_data.variablebrowser.ScilabVariableBrowser;
26
27 /**
28  *
29  * Static class to open/close Scilab Variable browser
30  *
31  */
32 public class BrowseVar {
33
34     public static final int ICON_COLUMN_INDEX = 0;
35     public static final int NAME_COLUMN_INDEX = 1;
36     public static final int SIZE_COLUMN_INDEX = 2;
37     public static final int TYPE_DESC_COLUMN_INDEX = 3;
38     public static final int VISIBILITY_COLUMN_INDEX = 4;
39     public static final int BYTES_COLUMN_INDEX = 5;
40     public static final int FROM_SCILAB_COLUMN_INDEX = 6;
41     public static final int TYPE_COLUMN_INDEX = 7;
42     public static final int NB_ROWS_INDEX = 8;
43     public static final int NB_COLS_INDEX = 9;
44
45     public static final String[] COLUMNNAMES = new String[] {"", /* Icon */
46             Messages.gettext("Name"),
47             Messages.gettext("Value"),
48             Messages.gettext("Type"),
49             Messages.gettext("Visibility"),
50             Messages.gettext("Bytes"),
51             Messages.gettext("User"),
52             Messages.gettext("Type int value"),
53             "", /* nbrows */
54             "" /* nbcols */
55                                                             };
56
57     public static final int[] COLUMNSALIGNMENT = new int[] { -1, JLabel.LEFT, JLabel.RIGHT, JLabel.RIGHT, JLabel.RIGHT, JLabel.RIGHT, JLabel.RIGHT, JLabel.RIGHT};
58
59     private static final ImageIcon NO_ICON = new ImageIcon(FindIconHelper.findIcon("noicon"));
60     private static final ImageIcon DOUBLE_ICON = new ImageIcon(FindIconHelper.findIcon("double"));
61     private static final ImageIcon POLYNOMIAL_ICON = new ImageIcon(FindIconHelper.findIcon("polynomial"));
62     private static final ImageIcon BOOLEAN_ICON = new ImageIcon(FindIconHelper.findIcon("boolean"));
63     private static final ImageIcon SPARSE_ICON = new ImageIcon(FindIconHelper.findIcon("sparse"));
64     private static final ImageIcon INT_ICON = new ImageIcon(FindIconHelper.findIcon("int"));
65     private static final ImageIcon HANDLE_ICON = new ImageIcon(FindIconHelper.findIcon("handle"));
66     private static final ImageIcon STRING_ICON = new ImageIcon(FindIconHelper.findIcon("string"));
67     private static final ImageIcon FUNCTION_ICON = new ImageIcon(FindIconHelper.findIcon("function"));
68     private static final ImageIcon LIST_ICON = new ImageIcon(FindIconHelper.findIcon("list"));
69     private static final ImageIcon TLIST_ICON = new ImageIcon(FindIconHelper.findIcon("tlist"));
70     private static final ImageIcon MLIST_ICON = new ImageIcon(FindIconHelper.findIcon("mlist"));
71     private static final ImageIcon CELL_ICON = new ImageIcon(FindIconHelper.findIcon("cell"));
72     private static final ImageIcon USER_ICON = new ImageIcon(FindIconHelper.findIcon("user"));
73     private static final ImageIcon FPTR_ICON = new ImageIcon(FindIconHelper.findIcon("fptr"));
74
75     /**
76      * Default private constructor for utility class
77      */
78     private BrowseVar() { }
79
80     /**
81      * Get ImageIcon instance from Scilab type (as int)
82      * @param type : scilab type as integer
83      * @return instance of type Icon
84      */
85     private static ImageIcon getIconFromType(int type) {
86         switch (type) {
87             case 1:
88                 return DOUBLE_ICON;
89             case 2:
90                 return POLYNOMIAL_ICON;
91             case 4:
92                 return BOOLEAN_ICON;
93             case 5:
94             case 6:
95             case 7:
96                 return SPARSE_ICON;
97             case 8:
98                 return INT_ICON;
99             case 9:
100                 return HANDLE_ICON;
101             case 10:
102                 return STRING_ICON;
103             case 11:
104             case 13:
105                 return FUNCTION_ICON;
106                 /*case 14:
107                   return LIBRARY_ICON;*/
108             case 15:
109                 return LIST_ICON;
110             case 16:
111                 return TLIST_ICON;
112             case 17:
113             case 18:
114                 return MLIST_ICON;
115             case 19:
116                 return CELL_ICON;
117             case 128:
118                 return USER_ICON;
119             case 130:
120                 return FPTR_ICON;
121             default:
122                 return NO_ICON;
123         }
124     }
125
126     /**
127      * Open Variable Browser
128      */
129     public static void openVariableBrowser() {
130         ScilabVariableBrowser.openVariableBrowser();
131     }
132
133     /**
134      * Set the Variable Browser data given by Scilab
135      * @param dataNames : scilab variable name
136      * @param dataBytes : scilab variable size in bytes
137      * @param dataTypes : scilab variable type (as integer)
138      * @param dataIntegerTypes : Type of int (-1 if not int)
139      * @param dataSizes : scilab variable size under the form "XxX"
140      * @param dataVisibility : local or global variable
141      * @param dataFromUser : Scilab data or user data
142      */
143     public static void setVariableBrowserData(String[] dataNames, int[] dataBytes, int[] dataTypes, int[] dataIntegerTypes, String[] variableListTypes, String[] dataSizes, int[] dataNbRows, int[] dataNbCols, String[] dataVisibility, boolean[] dataFromUser) {
144         Object[][] data = new Object[dataNames.length][COLUMNNAMES.length];
145         for (int i = 0; i < dataNames.length; ++i) {
146             data[i][ICON_COLUMN_INDEX] = getIconFromType(dataTypes[i]);
147             data[i][NAME_COLUMN_INDEX] = dataNames[i];
148             data[i][SIZE_COLUMN_INDEX] = dataSizes[i];
149             data[i][TYPE_DESC_COLUMN_INDEX] = ScilabTypeEnumDescription.getTypeDescriptionFromId(dataTypes[i]);
150
151             if (dataTypes[i] == ScilabTypeEnum.sci_ints.swigValue() && dataIntegerTypes[i] != 0) {
152                 // It is an integer. We want to detail the precision of the int
153                 data[i][TYPE_DESC_COLUMN_INDEX] = data[i][TYPE_DESC_COLUMN_INDEX] + " " + dataIntegerTypes[i];
154             }
155
156             if ((dataTypes[i] == ScilabTypeEnum.sci_tlist.swigValue() || dataTypes[i] == ScilabTypeEnum.sci_mlist.swigValue()) && !variableListTypes[i].equals("")) {
157                 // Improve the display of the list
158                 String varType = ScilabTypeEnumDescription.getListTypeDescription(variableListTypes[i]);
159
160                 // It is a tlist and we want to display the user datatype
161                 data[i][TYPE_DESC_COLUMN_INDEX] = varType + " (" + data[i][TYPE_DESC_COLUMN_INDEX] + ")";
162             }
163             data[i][VISIBILITY_COLUMN_INDEX] = dataVisibility[i];
164             data[i][BYTES_COLUMN_INDEX] = dataBytes[i];
165             data[i][FROM_SCILAB_COLUMN_INDEX] = dataFromUser[i]; /* Tag if it is a variable from the user or from Scilab (%pi, %eps, etc) */
166             data[i][TYPE_COLUMN_INDEX] = dataTypes[i];
167             data[i][NB_ROWS_INDEX] = dataNbRows[i];
168             data[i][NB_COLS_INDEX] = dataNbCols[i];
169         }
170         ScilabVariableBrowser.setVariableBrowserData(data);
171     }
172
173     /**
174      * Update Variable Browser
175      */
176     public static void updateVariableBrowserData() {
177         ScilabVariableBrowser.updateVariableBrowser();
178     }
179
180     /**
181      * @return true if an instance of BrowseVar already exists.
182      */
183     public static boolean isVariableBrowserOpened() {
184         return ScilabVariableBrowser.isBrowseVarOpened();
185     }
186
187     /**
188      * Close Variable Browser
189      */
190     public static void closeVariableBrowser() {
191         ScilabVariableBrowser.closeVariableBrowser();
192     }
193 }