2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2011 - DIGITEO - Bruno JOFRET
4 * Copyright (C) 2011 - DIGITEO - Vincent COUVERT
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
14 package org.scilab.modules.gui;
16 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_CALLBACK__;
17 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_CALLBACKTYPE__;
18 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_CHILDREN__;
19 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_PARENT__;
20 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_POSITION__;
21 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_STYLE__;
22 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_TAG__;
23 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_USER_DATA__;
24 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_BACKGROUNDCOLOR__;
25 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_ENABLE__;
26 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTANGLE__;
27 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTNAME__;
28 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTSIZE__;
29 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTUNITS__;
30 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTWEIGHT__;
31 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FOREGROUNDCOLOR__;
32 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_HORIZONTALALIGNMENT__;
33 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_LISTBOXTOP__;
34 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_MAX__;
35 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_MIN__;
36 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_RELIEF__;
37 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_SLIDERSTEP__;
38 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_STRING__;
39 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_STRING_COLNB__;
40 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_TOOLTIPSTRING__;
41 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_UNITS__;
42 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_VALUE__;
43 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_VERTICALALIGNMENT__;
44 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_VALID__;
45 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_VISIBLE__;
47 import java.awt.Color;
50 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
51 import org.scilab.modules.graphic_objects.graphicObject.CallBack;
52 import org.scilab.modules.gui.bridge.checkbox.SwingScilabCheckBox;
53 import org.scilab.modules.gui.bridge.listbox.SwingScilabListBox;
54 import org.scilab.modules.gui.bridge.popupmenu.SwingScilabPopupMenu;
55 import org.scilab.modules.gui.bridge.radiobutton.SwingScilabRadioButton;
56 import org.scilab.modules.gui.bridge.slider.SwingScilabSlider;
57 import org.scilab.modules.gui.bridge.uiimage.SwingScilabUiImage;
58 import org.scilab.modules.gui.bridge.uitable.SwingScilabUiTable;
59 import org.scilab.modules.gui.events.callback.CommonCallBack;
60 import org.scilab.modules.gui.utils.Position;
61 import org.scilab.modules.gui.utils.Size;
62 import org.scilab.modules.gui.utils.UnitsConverter;
63 import org.scilab.modules.gui.utils.UnitsConverter.UicontrolUnits;
64 import org.scilab.modules.gui.widget.Widget;
67 * @author Bruno JOFRET
68 * @author Vincent COUVERT
70 public final class SwingViewWidget {
72 private static final String OBLIQUEFONT = "oblique";
73 private static final String ITALICFONT = "italic";
74 private static final String BOLDFONT = "bold";
75 private static final int COLORS_COEFF = 255;
76 private static final int X_INDEX = 0;
77 private static final int Y_INDEX = 1;
78 private static final int WIDTH_INDEX = 2;
79 private static final int HEIGHT_INDEX = 3;
84 private SwingViewWidget() {
85 throw new UnsupportedOperationException();
89 * Update the component in the view
90 * @param uiControl the component
91 * @param property the property name
92 * @param value the property value
94 public static void update(Widget uiControl, int property, Object value) {
95 String uid = ((SwingViewObject) uiControl).getId();
97 Double[] allColors = null;
101 case __GO_UI_BACKGROUNDCOLOR__ :
102 allColors = ((Double[]) value);
103 uiControl.setBackground(new Color((int) (allColors[0] * COLORS_COEFF),
104 (int) (allColors[1] * COLORS_COEFF),
105 (int) (allColors[2] * COLORS_COEFF)));
107 case __GO_CALLBACK__ :
108 int cbType = (Integer) GraphicController.getController().getProperty(uid, __GO_CALLBACKTYPE__);
109 uiControl.setCallback(CommonCallBack.createCallback((String) value, cbType, uid));
111 case __GO_CALLBACKTYPE__ :
112 String cbString = (String) GraphicController.getController().getProperty(uid, __GO_CALLBACK__);
113 if ((Integer) value == CallBack.UNTYPED) { /* Deactivate callback */
114 uiControl.setCallback(null);
116 uiControl.setCallback(CommonCallBack.createCallback(cbString, (Integer) value, uid));
119 case __GO_CHILDREN__ :
122 case __GO_UI_ENABLE__ :
123 uiControl.setEnabled(((Boolean) value).booleanValue());
125 case __GO_UI_FONTANGLE__ :
126 font = uiControl.getFont();
128 String angle = (String) value;
130 if (angle.equalsIgnoreCase(ITALICFONT) || angle.equalsIgnoreCase(OBLIQUEFONT)) {
132 font = new Font(font.getName(), Font.ITALIC + Font.BOLD, font.getSize());
134 font = new Font(font.getName(), Font.ITALIC, font.getSize());
138 font = new Font(font.getName(), Font.BOLD, font.getSize());
140 font = new Font(font.getName(), Font.PLAIN, font.getSize());
144 uiControl.setFont(font);
147 case __GO_UI_FONTNAME__ :
148 font = uiControl.getFont();
150 String name = (String) value;
152 font = new Font(name, font.getStyle(), font.getSize());
154 uiControl.setFont(font);
157 case __GO_UI_FONTSIZE__ :
158 UicontrolUnits fontUnitsProperty = UnitsConverter.stringToUnitsEnum((String) GraphicController.getController()
159 .getProperty(uid, __GO_UI_FONTUNITS__));
160 Double dblFontSize = UnitsConverter.convertToPoint((Double) value, fontUnitsProperty, uiControl, false);
161 font = uiControl.getFont();
163 int size = dblFontSize.intValue();
164 font = new Font(font.getName(), font.getStyle(), size);
165 uiControl.setFont(font);
168 case __GO_UI_FONTUNITS__ :
169 /* Nothing to do here, this property is used when setting position */
171 case __GO_UI_FONTWEIGHT__ :
172 font = uiControl.getFont();
174 String weight = (String) value;
176 if (weight.equalsIgnoreCase(BOLDFONT)) {
177 if (font.isItalic()) {
178 font = new Font(font.getName(), Font.ITALIC + Font.BOLD, font.getSize());
180 font = new Font(font.getName(), Font.BOLD, font.getSize());
183 if (font.isItalic()) {
184 font = new Font(font.getName(), Font.ITALIC, font.getSize());
186 font = new Font(font.getName(), Font.PLAIN, font.getSize());
190 uiControl.setFont(font);
193 case __GO_UI_FOREGROUNDCOLOR__ :
194 allColors = ((Double[]) value);
195 uiControl.setForeground(new Color((int) (allColors[0] * COLORS_COEFF),
196 (int) (allColors[1] * COLORS_COEFF),
197 (int) (allColors[2] * COLORS_COEFF)));
199 case __GO_UI_HORIZONTALALIGNMENT__ :
200 uiControl.setHorizontalAlignment((String) value);
202 case __GO_UI_LISTBOXTOP__ :
203 if (uiControl instanceof SwingScilabListBox) {
204 Integer[] listboxtopValue = ((Integer[]) value);
205 if (listboxtopValue.length > 0) {
206 ((SwingScilabListBox) uiControl).setListBoxTop(listboxtopValue[0]);
211 maxValue = ((Double) value);
212 Double[] allValues = (Double[]) GraphicController.getController().getProperty(uid, __GO_UI_VALUE__);
213 if ((allValues == null) || (allValues.length == 0)) {
216 double uicontrolValue = allValues[0];
217 if (uiControl instanceof SwingScilabSlider) {
218 // Update the slider properties
219 double minValue = (Double) GraphicController.getController().getProperty(uid, __GO_UI_MIN__);
220 ((SwingScilabSlider) uiControl).setMaximumValue(maxValue);
221 Double[] sliderStep = ((Double[]) GraphicController.getController().getProperty(uid, __GO_UI_SLIDERSTEP__));
222 double minorSliderStep = sliderStep[0].doubleValue();
223 double majorSliderStep = sliderStep[1].doubleValue();
224 if (minValue <= maxValue) {
225 ((SwingScilabSlider) uiControl).setMinorTickSpacing((int) (minorSliderStep * (maxValue - minValue)));
226 ((SwingScilabSlider) uiControl).setMajorTickSpacing((int) (majorSliderStep * (maxValue - minValue)));
228 } else if (uiControl instanceof SwingScilabListBox) {
229 // Enable/Disable multiple selection
230 double minValue = (Double) GraphicController.getController().getProperty(uid, __GO_UI_MIN__);
231 ((SwingScilabListBox) uiControl).setMultipleSelectionEnabled(maxValue - minValue > 1);
232 } else if (uiControl instanceof SwingScilabCheckBox) {
233 // Check/Uncheck the CheckBox
234 ((SwingScilabCheckBox) uiControl).setChecked(maxValue == uicontrolValue);
235 } else if (uiControl instanceof SwingScilabRadioButton) {
236 // Check/Uncheck the RadioButton
237 ((SwingScilabRadioButton) uiControl).setChecked(maxValue == uicontrolValue);
241 double minValue = ((Double) value);
242 if (uiControl instanceof SwingScilabSlider) {
243 // Update the slider properties
244 maxValue = (Double) GraphicController.getController().getProperty(uid, __GO_UI_MAX__);
245 ((SwingScilabSlider) uiControl).setMinimumValue(minValue);
246 Double[] sliderStep = ((Double[]) GraphicController.getController().getProperty(uid, __GO_UI_SLIDERSTEP__));
247 double minorSliderStep = sliderStep[0].doubleValue();
248 double majorSliderStep = sliderStep[1].doubleValue();
249 if (minValue <= maxValue) {
250 ((SwingScilabSlider) uiControl).setMinorTickSpacing((int) (minorSliderStep * (maxValue - minValue)));
251 ((SwingScilabSlider) uiControl).setMajorTickSpacing((int) (majorSliderStep * (maxValue - minValue)));
253 } else if (uiControl instanceof SwingScilabListBox) {
254 // Enable/Disable multiple selection
255 maxValue = (Double) GraphicController.getController().getProperty(uid, __GO_UI_MAX__);
256 ((SwingScilabListBox) uiControl).setMultipleSelectionEnabled(maxValue - minValue > 1);
259 case __GO_POSITION__ :
260 /* Convert value according to units */
261 UicontrolUnits unitsProperty = UnitsConverter.stringToUnitsEnum((String) GraphicController
262 .getController().getProperty(uid, __GO_UI_UNITS__));
263 Double[] dblValues = UnitsConverter.convertPositionToPixels((Double[]) value, unitsProperty, uiControl);
264 /* Set dimensions before position because position is adjusted according to size */
265 uiControl.setDims(new Size(dblValues[WIDTH_INDEX].intValue(), dblValues[HEIGHT_INDEX].intValue()));
266 uiControl.setPosition(new Position(dblValues[X_INDEX].intValue(), dblValues[Y_INDEX].intValue()));
267 /* Manage sliders orientation */
268 if (uiControl instanceof SwingScilabSlider) {
269 if (dblValues[WIDTH_INDEX].intValue() > dblValues[HEIGHT_INDEX].intValue()) {
270 ((SwingScilabSlider) uiControl).setHorizontal();
272 ((SwingScilabSlider) uiControl).setVertical();
276 case __GO_UI_RELIEF__ :
277 uiControl.setRelief((String) value);
279 case __GO_UI_SLIDERSTEP__ :
280 Double[] sliderStep = ((Double[]) value);
281 if (uiControl instanceof SwingScilabSlider) {
282 // Update the slider properties
283 double minorSliderStep = sliderStep[0].doubleValue();
284 double majorSliderStep = sliderStep[1].doubleValue();
285 ((SwingScilabSlider) uiControl).setMinorTickSpacing(minorSliderStep);
286 ((SwingScilabSlider) uiControl).setMajorTickSpacing(majorSliderStep);
290 /* Nothing to do unless we want to change style interactively */
292 case __GO_UI_STRING__ :
293 if (uiControl instanceof SwingScilabUiTable) {
294 // Update column names
295 String[] stringValue = (String[]) value;
296 int colNb = ((Integer) GraphicController.getController().getProperty(uid, __GO_UI_STRING_COLNB__));
297 String[] colNames = new String[colNb - 1];
298 for (int k = 1; k < colNb; k++) {
299 colNames[k - 1] = stringValue[k * (stringValue.length / colNb)];
301 ((SwingScilabUiTable) uiControl).setColumnNames(colNames);
303 String[] rowNames = new String[stringValue.length / colNb - 1];
304 for (int k = 1; k < stringValue.length / colNb; k++) {
305 rowNames[k - 1] = stringValue[k];
307 ((SwingScilabUiTable) uiControl).setRowNames(rowNames);
309 String[] tableData = new String[rowNames.length * colNames.length];
311 for (int kCol = 1; kCol <= colNames.length; kCol++) {
312 for (int kRow = 1; kRow <= rowNames.length; kRow++) {
313 tableData[kData++] = stringValue[kCol * (stringValue.length / colNb) + kRow];
316 if (tableData.length != 0) {
317 ((SwingScilabUiTable) uiControl).setData(tableData);
319 } else if (uiControl instanceof SwingScilabListBox) {
320 // Listboxes manage string vectors
321 ((SwingScilabListBox) uiControl).setText((String[]) value);
322 } else if (uiControl instanceof SwingScilabPopupMenu) {
323 // Popupmenus manage string vectors
324 ((SwingScilabPopupMenu) uiControl).setText((String[]) value);
326 uiControl.setText(((String[]) value)[0]);
329 case __GO_UI_TOOLTIPSTRING__ :
330 String[] tooltipString = ((String[]) value);
331 String tooltipText = tooltipString[0];
332 if (tooltipString.length > 1) {
333 tooltipText = "<html>" + tooltipText;
334 for (int kString = 1; kString < tooltipString.length; kString++) {
335 tooltipText = tooltipText + "<br>" + tooltipString[kString];
337 tooltipText = tooltipText + "</html>";
339 if (tooltipText.equals("")) {
340 uiControl.setToolTipText(null);
342 uiControl.setToolTipText(tooltipText);
345 case __GO_UI_STRING_COLNB__ :
349 case __GO_USER_DATA__ :
350 /* Nothing to do here */
351 case __GO_UI_UNITS__ :
352 /* Nothing to do here, this property is used when setting position */
356 case __GO_UI_VALUE__ :
358 Double[] doubleValue = ((Double[]) value);
359 if (doubleValue.length == 0) {
362 int[] intValue = new int[doubleValue.length];
363 for (int k = 0; k < doubleValue.length; k++) {
364 intValue[k] = doubleValue[k].intValue();
367 if (uiControl instanceof SwingScilabListBox) {
368 // Update selected items in the listbox
369 ((SwingScilabListBox) uiControl).setSelectedIndices(intValue);
370 } else if (uiControl instanceof SwingScilabPopupMenu) {
371 // Update selected items in the popupmenu
372 ((SwingScilabPopupMenu) uiControl).setUserSelectedIndex(intValue[0]);
373 } else if (uiControl instanceof SwingScilabCheckBox) {
374 // Check the checkbox if the value is equal to MAX property
375 maxValue = ((Double) GraphicController.getController().getProperty(uid, __GO_UI_MAX__)).intValue();
376 ((SwingScilabCheckBox) uiControl).setChecked(maxValue == intValue[0]);
377 } else if (uiControl instanceof SwingScilabRadioButton) {
378 // Check the radiobutton if the value is equal to MAX property
379 maxValue = ((Double) GraphicController.getController().getProperty(uid, __GO_UI_MAX__)).intValue();
380 ((SwingScilabRadioButton) uiControl).setChecked(maxValue == intValue[0]);
381 } else if (uiControl instanceof SwingScilabSlider) {
382 // Update the slider value
383 ((SwingScilabSlider) uiControl).setUserValue(doubleValue[0]);
384 } else if (uiControl instanceof SwingScilabUiImage) {
385 // Update the image parameters
386 double[] imageParams = new double[5];
387 if (doubleValue.length < 1) {
388 imageParams[0] = 1.0;
390 imageParams[0] = doubleValue[0];
392 if (doubleValue.length < 2) {
393 imageParams[1] = 1.0;
395 imageParams[1] = doubleValue[1];
397 if (doubleValue.length < 3) {
398 imageParams[2] = 0.0;
400 imageParams[2] = doubleValue[2];
402 if (doubleValue.length < 4) {
403 imageParams[3] = 0.0;
405 imageParams[3] = doubleValue[3];
407 if (doubleValue.length < 5) {
408 imageParams[4] = 0.0;
410 imageParams[4] = doubleValue[4];
412 double[] scale = new double[2];
413 scale[0] = imageParams[0];
414 scale[1] = imageParams[1];
415 ((SwingScilabUiImage) uiControl).setScale(scale);
416 double[] shear = new double[2];
417 shear[0] = imageParams[2];
418 shear[1] = imageParams[3];
419 ((SwingScilabUiImage) uiControl).setShear(shear);
420 ((SwingScilabUiImage) uiControl).setRotate(imageParams[4]);
423 case __GO_UI_VERTICALALIGNMENT__ :
424 uiControl.setVerticalAlignment((String) value);
426 case __GO_VISIBLE__ :
427 uiControl.setVisible(((Boolean) value).booleanValue());
430 /* Update position */
431 SwingViewWidget.update(uiControl, __GO_POSITION__,
432 (Double[]) GraphicController.getController()
433 .getProperty(uid, __GO_POSITION__));
436 System.err.println("[SwingScilabWidget.update] Property not mapped: " + property);