a922b086aaa1ce735f1a0e21a3fa3332e3b29845
[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 USER_ICON = new ImageIcon(FindIconHelper.findIcon("user"));
72     private static final ImageIcon FPTR_ICON = new ImageIcon(FindIconHelper.findIcon("fptr"));
73
74     /**
75      * Default private constructor for utility class
76      */
77     private BrowseVar() { }
78
79     /**
80      * Get ImageIcon instance from Scilab type (as int)
81      * @param type : scilab type as integer
82      * @return instance of type Icon
83      */
84     private static ImageIcon getIconFromType(int type) {
85         switch (type) {
86             case 1:
87                 return DOUBLE_ICON;
88             case 2:
89                 return POLYNOMIAL_ICON;
90             case 4:
91                 return BOOLEAN_ICON;
92             case 5:
93             case 6:
94             case 7:
95                 return SPARSE_ICON;
96             case 8:
97                 return INT_ICON;
98             case 9:
99                 return HANDLE_ICON;
100             case 10:
101                 return STRING_ICON;
102             case 11:
103             case 13:
104                 return FUNCTION_ICON;
105                 /*case 14:
106                   return LIBRARY_ICON;*/
107             case 15:
108                 return LIST_ICON;
109             case 16:
110                 return TLIST_ICON;
111             case 17:
112                 return MLIST_ICON;
113             case 128:
114                 return USER_ICON;
115             case 130:
116                 return FPTR_ICON;
117             default:
118                 return NO_ICON;
119         }
120     }
121
122     /**
123      * Open Variable Browser
124      */
125     public static void openVariableBrowser() {
126         ScilabVariableBrowser.openVariableBrowser();
127     }
128
129     /**
130      * Set the Variable Browser data given by Scilab
131      * @param dataNames : scilab variable name
132      * @param dataBytes : scilab variable size in bytes
133      * @param dataTypes : scilab variable type (as integer)
134      * @param dataIntegerTypes : Type of int (-1 if not int)
135      * @param dataSizes : scilab variable size under the form "XxX"
136      * @param dataVisibility : local or global variable
137      * @param dataFromUser : Scilab data or user data
138      */
139     public static void setVariableBrowserData(String[] dataNames, int[] dataBytes, int[] dataTypes, int[] dataIntegerTypes, String[] variableListTypes, String[] dataSizes, int[] dataNbRows, int[] dataNbCols, String[] dataVisibility, boolean[] dataFromUser) {
140         Object[][] data = new Object[dataNames.length][COLUMNNAMES.length];
141         for (int i = 0; i < dataNames.length; ++i) {
142             data[i][ICON_COLUMN_INDEX] = getIconFromType(dataTypes[i]);
143             data[i][NAME_COLUMN_INDEX] = dataNames[i];
144             data[i][SIZE_COLUMN_INDEX] = dataSizes[i];
145             data[i][TYPE_DESC_COLUMN_INDEX] = ScilabTypeEnumDescription.getTypeDescriptionFromId(dataTypes[i]);
146
147             if (dataTypes[i] == ScilabTypeEnum.sci_ints.swigValue() && dataIntegerTypes[i] != 0) {
148                 // It is an integer. We want to detail the precision of the int
149                 data[i][TYPE_DESC_COLUMN_INDEX] = data[i][TYPE_DESC_COLUMN_INDEX] + " " + dataIntegerTypes[i];
150             }
151
152             if ((dataTypes[i] == ScilabTypeEnum.sci_tlist.swigValue() || dataTypes[i] == ScilabTypeEnum.sci_mlist.swigValue()) && !variableListTypes[i].equals("")) {
153                 // Improve the display of the list
154                 String varType = ScilabTypeEnumDescription.getListTypeDescription(variableListTypes[i]);
155
156                 // It is a tlist and we want to display the user datatype
157                 data[i][TYPE_DESC_COLUMN_INDEX] = varType + " (" + data[i][TYPE_DESC_COLUMN_INDEX] + ")";
158             }
159             data[i][VISIBILITY_COLUMN_INDEX] = dataVisibility[i];
160             data[i][BYTES_COLUMN_INDEX] = dataBytes[i];
161             data[i][FROM_SCILAB_COLUMN_INDEX] = dataFromUser[i]; /* Tag if it is a variable from the user or from Scilab (%pi, %eps, etc) */
162             data[i][TYPE_COLUMN_INDEX] = dataTypes[i];
163             data[i][NB_ROWS_INDEX] = dataNbRows[i];
164             data[i][NB_COLS_INDEX] = dataNbCols[i];
165         }
166         ScilabVariableBrowser.setVariableBrowserData(data);
167     }
168
169     /**
170      * Update Variable Browser
171      */
172     public static void updateVariableBrowserData() {
173         ScilabVariableBrowser.updateVariableBrowser();
174     }
175
176     /**
177      * @return true if an instance of BrowseVar already exists.
178      */
179     public static boolean isVariableBrowserOpened() {
180         return ScilabVariableBrowser.isBrowseVarOpened();
181     }
182
183     /**
184      * Close Variable Browser
185      */
186     public static void closeVariableBrowser() {
187         ScilabVariableBrowser.closeVariableBrowser();
188     }
189 }