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