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
6 * Copyright (C) 2012 - 2016 - Scilab Enterprises
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.
16 package org.scilab.modules.ui_data;
18 import javax.swing.ImageIcon;
19 import javax.swing.JLabel;
20 import org.scilab.modules.commons.gui.FindIconHelper;
21 import org.scilab.modules.localization.Messages;
22 import org.scilab.modules.types.ScilabTypeEnum;
23 import org.scilab.modules.types.ScilabTypeEnumDescription;
24 import org.scilab.modules.ui_data.variablebrowser.ScilabVariableBrowser;
26 /** Static class to open/close Scilab Variable browser */
27 public class BrowseVar {
29 public static final int ICON_COLUMN_INDEX = 0;
30 public static final int NAME_COLUMN_INDEX = 1;
31 public static final int SIZE_COLUMN_INDEX = 2;
32 public static final int TYPE_DESC_COLUMN_INDEX = 3;
33 public static final int VISIBILITY_COLUMN_INDEX = 4;
34 public static final int BYTES_COLUMN_INDEX = 5;
35 public static final int FROM_SCILAB_COLUMN_INDEX = 6;
36 public static final int TYPE_COLUMN_INDEX = 7;
37 public static final int NB_ROWS_INDEX = 8;
38 public static final int NB_COLS_INDEX = 9;
40 public static final String[] COLUMNNAMES =
43 Messages.gettext("Name"),
44 Messages.gettext("Value"),
45 Messages.gettext("Type"),
46 Messages.gettext("Visibility"),
47 Messages.gettext("Memory"),
48 Messages.gettext("User"),
49 Messages.gettext("Type int value"),
54 public static final int[] COLUMNSALIGNMENT =
66 private static final ImageIcon NO_ICON = new ImageIcon(FindIconHelper.findIcon("noicon"));
67 private static final ImageIcon DOUBLE_ICON = new ImageIcon(FindIconHelper.findIcon("double"));
68 private static final ImageIcon POLYNOMIAL_ICON =
69 new ImageIcon(FindIconHelper.findIcon("polynomial"));
70 private static final ImageIcon BOOLEAN_ICON = new ImageIcon(FindIconHelper.findIcon("boolean"));
71 private static final ImageIcon SPARSE_ICON = new ImageIcon(FindIconHelper.findIcon("sparse"));
72 private static final ImageIcon INT_ICON = new ImageIcon(FindIconHelper.findIcon("int"));
73 private static final ImageIcon HANDLE_ICON = new ImageIcon(FindIconHelper.findIcon("handle"));
74 private static final ImageIcon STRING_ICON = new ImageIcon(FindIconHelper.findIcon("string"));
75 private static final ImageIcon FUNCTION_ICON = new ImageIcon(FindIconHelper.findIcon("function"));
76 private static final ImageIcon LIST_ICON = new ImageIcon(FindIconHelper.findIcon("list"));
77 private static final ImageIcon TLIST_ICON = new ImageIcon(FindIconHelper.findIcon("tlist"));
78 private static final ImageIcon MLIST_ICON = new ImageIcon(FindIconHelper.findIcon("mlist"));
79 private static final ImageIcon CELL_ICON = new ImageIcon(FindIconHelper.findIcon("cell"));
80 private static final ImageIcon USER_ICON = new ImageIcon(FindIconHelper.findIcon("user"));
81 private static final ImageIcon FPTR_ICON = new ImageIcon(FindIconHelper.findIcon("fptr"));
83 /** Default private constructor for utility class */
84 private BrowseVar() {}
87 * Get ImageIcon instance from Scilab type (as int)
89 * @param type : scilab type as integer
90 * @return instance of type Icon
92 private static ImageIcon getIconFromType(int type) {
97 return POLYNOMIAL_ICON;
112 return FUNCTION_ICON;
114 return LIBRARY_ICON;*/
133 /** Open Variable Browser */
134 public static void openVariableBrowser() {
135 ScilabVariableBrowser.openVariableBrowser();
139 * Set the Variable Browser data given by Scilab
141 * @param dataNames : scilab variable name
142 * @param dataBytes : scilab variable size in bytes
143 * @param dataTypes : scilab variable type (as integer)
144 * @param dataIntegerTypes : Type of int (-1 if not int)
145 * @param dataSizes : scilab variable size under the form "XxX"
146 * @param dataVisibility : local or global variable
147 * @param dataFromUser : Scilab data or user data
149 public static void setVariableBrowserData(
153 int[] dataIntegerTypes,
154 String[] variableListTypes,
158 String[] dataVisibility,
159 boolean[] dataFromUser) {
160 Object[][] data = new Object[dataNames.length][COLUMNNAMES.length];
161 for (int i = 0; i < dataNames.length; ++i) {
162 data[i][ICON_COLUMN_INDEX] = getIconFromType(dataTypes[i]);
163 data[i][NAME_COLUMN_INDEX] = dataNames[i];
164 data[i][SIZE_COLUMN_INDEX] = dataSizes[i];
165 data[i][TYPE_DESC_COLUMN_INDEX] =
166 ScilabTypeEnumDescription.getTypeDescriptionFromId(dataTypes[i]);
168 if (dataTypes[i] == ScilabTypeEnum.sci_ints.swigValue() && dataIntegerTypes[i] != 0) {
169 // It is an integer. We want to detail the precision of the int
170 data[i][TYPE_DESC_COLUMN_INDEX] =
171 data[i][TYPE_DESC_COLUMN_INDEX] + " " + dataIntegerTypes[i];
174 if ((dataTypes[i] == ScilabTypeEnum.sci_tlist.swigValue()
175 || dataTypes[i] == ScilabTypeEnum.sci_mlist.swigValue())
176 && !variableListTypes[i].equals("")) {
177 // Improve the display of the list
178 String varType = ScilabTypeEnumDescription.getListTypeDescription(variableListTypes[i]);
180 // It is a tlist and we want to display the user datatype
181 data[i][TYPE_DESC_COLUMN_INDEX] = varType + " (" + data[i][TYPE_DESC_COLUMN_INDEX] + ")";
183 data[i][VISIBILITY_COLUMN_INDEX] = dataVisibility[i];
184 data[i][BYTES_COLUMN_INDEX] = humanReadableByteCount(dataBytes[i], true);
185 data[i][FROM_SCILAB_COLUMN_INDEX] =
187 i]; /* Tag if it is a variable from the user or from Scilab (%pi, %eps, etc) */
188 data[i][TYPE_COLUMN_INDEX] = dataTypes[i];
189 data[i][NB_ROWS_INDEX] = dataNbRows[i];
190 data[i][NB_COLS_INDEX] = dataNbCols[i];
192 ScilabVariableBrowser.setVariableBrowserData(data);
196 * Convert a byte-count into a human readable string
199 * https://stackoverflow.com/questions/3758606/how-to-convert-byte-size-into-human-readable-format-in-java/3758880#3758880
200 * @param bytes the number of bytes
201 * @param si true if you wish to format as International System, false for Binary System
202 * @return a formatted string
204 public static String humanReadableByteCount(long bytes, boolean si) {
205 int unit = si ? 1000 : 1024;
210 int exp = (int) (Math.log(bytes) / Math.log(unit));
211 String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
212 return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
215 /** Update Variable Browser */
216 public static void updateVariableBrowserData() {
217 ScilabVariableBrowser.updateVariableBrowser();
220 /** @return true if an instance of BrowseVar already exists. */
221 public static boolean isVariableBrowserOpened() {
222 return ScilabVariableBrowser.isBrowseVarOpened();
225 /** Close Variable Browser */
226 public static void closeVariableBrowser() {
227 ScilabVariableBrowser.closeVariableBrowser();