Coverity fixed: #1351035
[scilab.git] / scilab / modules / gui / src / java / org / scilab / modules / gui / SwingViewWidget.java
1 /*
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
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
17 package org.scilab.modules.gui;
18
19 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_CALLBACKTYPE__;
20 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_CALLBACK__;
21 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_PARENT__;
22 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_POSITION__;
23 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_BACKGROUNDCOLOR__;
24 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_ENABLE__;
25 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTANGLE__;
26 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTNAME__;
27 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTSIZE__;
28 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTUNITS__;
29 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTWEIGHT__;
30 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FOREGROUNDCOLOR__;
31 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_HORIZONTALALIGNMENT__;
32 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_RELIEF__;
33 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_STRING__;
34 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_TOOLTIPSTRING__;
35 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_UNITS__;
36 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_VERTICALALIGNMENT__;
37 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_VISIBLE__;
38
39 import java.awt.Color;
40 import java.awt.Component;
41 import java.awt.Font;
42 import java.lang.StringBuilder;
43
44 import javax.swing.JComponent;
45
46 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
47 import org.scilab.modules.graphic_objects.graphicObject.CallBack;
48 import org.scilab.modules.gui.bridge.editbox.SwingScilabEditBox;
49 import org.scilab.modules.gui.events.callback.CommonCallBack;
50 import org.scilab.modules.gui.utils.Position;
51 import org.scilab.modules.gui.utils.Size;
52 import org.scilab.modules.gui.utils.UnitsConverter;
53 import org.scilab.modules.gui.utils.UnitsConverter.UicontrolUnits;
54 import org.scilab.modules.gui.widget.Widget;
55
56 /**
57  * @author Bruno JOFRET
58  * @author Vincent COUVERT
59  */
60 public final class SwingViewWidget {
61
62     private static final String OBLIQUEFONT = "oblique";
63     private static final String ITALICFONT = "italic";
64     private static final String BOLDFONT = "bold";
65     private static final int COLORS_COEFF = 255;
66     private static final int X_INDEX = 0;
67     private static final int Y_INDEX = 1;
68     private static final int WIDTH_INDEX = 2;
69     private static final int HEIGHT_INDEX = 3;
70
71     /**
72      * Constructor
73      */
74     private SwingViewWidget() {
75         throw new UnsupportedOperationException();
76     }
77
78     /**
79      * Update the component in the view
80      * @param uiControl the component
81      * @param property the property name
82      * @param value the property value
83      */
84     public static void update(Widget uiControl, int property, Object value) {
85         Integer uid = ((SwingViewObject) uiControl).getId();
86
87         GraphicController controller = GraphicController.getController();
88         switch (property) {
89             case __GO_UI_BACKGROUNDCOLOR__: {
90                 Double[] allColors = ((Double[]) value);
91                 if (allColors[0] == -1) {
92                     // Do not set BackgroundColor for widgets
93                     // rely on Look and Feel
94                     ((JComponent) uiControl).setOpaque(true);
95                     uiControl.resetBackground();
96                 } else if (allColors[0] == -2) {
97                     ((JComponent) uiControl).setOpaque(false);
98                 } else {
99                     ((JComponent) uiControl).setOpaque(true);
100                     uiControl.setBackground(new Color(
101                                                 (int) (allColors[0] * COLORS_COEFF),
102                                                 (int) (allColors[1] * COLORS_COEFF),
103                                                 (int) (allColors[2] * COLORS_COEFF)));
104                 }
105                 break;
106             }
107             case __GO_CALLBACK__: {
108                 int cbType = (Integer) controller.getProperty(uid, __GO_CALLBACKTYPE__);
109                 uiControl.setCallback(CommonCallBack.createCallback((String) value, cbType, uid));
110                 break;
111             }
112             case __GO_CALLBACKTYPE__: {
113                 String cbString = (String) controller.getProperty(uid, __GO_CALLBACK__);
114                 if ((Integer) value == CallBack.UNTYPED) {
115                     /*
116                      * Deactivate
117                      * callback
118                      */
119                     uiControl.setCallback(null);
120                 } else {
121                     uiControl.setCallback(CommonCallBack.createCallback(cbString, (Integer) value, uid));
122                 }
123                 break;
124             }
125             case __GO_UI_ENABLE__: {
126                 uiControl.setEnabled(((Boolean) value).booleanValue());
127                 break;
128             }
129             case __GO_UI_FONTANGLE__ : {
130                 String angle = (String) value;
131                 if (angle.equals("") == false) {
132                     Font font = uiControl.getFont();
133                     if (font != null) {
134                         if (angle.equalsIgnoreCase(ITALICFONT) || angle.equalsIgnoreCase(OBLIQUEFONT)) {
135                             if (font.isBold()) {
136                                 font = new Font(font.getName(), Font.ITALIC + Font.BOLD, font.getSize());
137                             } else {
138                                 font = new Font(font.getName(), Font.ITALIC, font.getSize());
139                             }
140                         } else {
141                             if (font.isBold()) {
142                                 font = new Font(font.getName(), Font.BOLD, font.getSize());
143                             } else {
144                                 font = new Font(font.getName(), Font.PLAIN, font.getSize());
145                             }
146                         }
147
148                         uiControl.setFont(font);
149                     }
150                 }
151                 break;
152             }
153             case __GO_UI_FONTNAME__ : {
154                 String name = (String) value;
155                 if (name.equals("") == false) {
156                     Font font = uiControl.getFont();
157                     if (font != null) {
158
159                         font = new Font(name, font.getStyle(), font.getSize());
160
161                         if (uiControl instanceof SwingScilabEditBox) {
162                             ((SwingScilabEditBox)uiControl).setEditFont(font);
163                         } else {
164                             uiControl.setFont(font);
165                         }
166                     }
167                 }
168                 break;
169             }
170             case __GO_UI_FONTSIZE__ : {
171                 if ((Double)value != 0.0) {
172                     UicontrolUnits fontUnitsProperty = UnitsConverter.stringToUnitsEnum((String) controller.getProperty(uid, __GO_UI_FONTUNITS__));
173                     Double dblFontSize = UnitsConverter.convertToPoint((Double) value, fontUnitsProperty, uiControl, false);
174                     Font font = uiControl.getFont();
175                     if (font != null) {
176                         int size = dblFontSize.intValue();
177                         font = new Font(font.getName(), font.getStyle(), size);
178                         uiControl.setFont(font);
179                     }
180                 }
181                 break;
182             }
183             case __GO_UI_FONTWEIGHT__ : {
184                 String weight = (String) value;
185                 if (weight.equals("") == false) {
186                     Font font = uiControl.getFont();
187                     if (font != null) {
188                         if (weight.equalsIgnoreCase(BOLDFONT)) {
189                             if (font.isItalic()) {
190                                 font = new Font(font.getName(), Font.ITALIC + Font.BOLD, font.getSize());
191                             } else {
192                                 font = new Font(font.getName(), Font.BOLD, font.getSize());
193                             }
194                         } else {
195                             if (font.isItalic()) {
196                                 font = new Font(font.getName(), Font.ITALIC, font.getSize());
197                             } else {
198                                 font = new Font(font.getName(), Font.PLAIN, font.getSize());
199                             }
200                         }
201
202                         uiControl.setFont(font);
203                     }
204                 }
205                 break;
206             }
207             case __GO_UI_FOREGROUNDCOLOR__ : {
208                 Double[] allColors = ((Double[]) value);
209                 if (allColors[0] != -1) {
210                     uiControl.setForeground(new Color(
211                                                 (int) (allColors[0] * COLORS_COEFF),
212                                                 (int) (allColors[1] * COLORS_COEFF),
213                                                 (int) (allColors[2] * COLORS_COEFF)));
214                 } else {
215                     // Do not set ForegroundColor for widgets
216                     // rely on Look and Feel
217                     uiControl.resetForeground();
218                 }
219                 break;
220             }
221             case __GO_UI_HORIZONTALALIGNMENT__ : {
222                 String align = (String)value;
223                 if (align.equals("") == false) {
224                     uiControl.setHorizontalAlignment((String) value);
225                 }
226                 break;
227             }
228             case __GO_POSITION__: {
229                 /* Convert value according to units */
230                 updatePosition(uiControl, uid, value);
231                 break;
232             }
233             case __GO_UI_UNITS__: {
234                 /* Convert value according to units */
235                 Double[] p = (Double[]) GraphicController.getController().getProperty(uid, __GO_POSITION__);
236                 updatePosition(uiControl, uid, p);
237                 break;
238             }
239             case __GO_UI_RELIEF__: {
240                 uiControl.setRelief((String) value);
241                 break;
242             }
243             case __GO_UI_STRING__: {
244                 if (((String[])value).length == 0) {
245                     uiControl.setEmptyText();
246                 } else {
247                     uiControl.setText(((String[]) value)[0]);
248                 }
249                 break;
250             }
251             case __GO_UI_TOOLTIPSTRING__:
252                 String[] tooltipString = ((String[]) value);
253                 String tooltipText = tooltipString[0];
254                 if (tooltipString.length > 1) {
255                     StringBuilder sb = new StringBuilder();
256                     sb.append("<html>" + tooltipText);
257                     for (int kString = 1; kString < tooltipString.length; kString++) {
258                         sb.append("<br>").append(tooltipString[kString]);
259                     }
260                     sb.append("</html>");
261                     tooltipText = sb.toString();
262                 }
263                 if (tooltipText.equals("")) {
264                     uiControl.setToolTipText(null);
265                 } else {
266                     uiControl.setToolTipText(tooltipText);
267                 }
268                 break;
269
270             case __GO_UI_VERTICALALIGNMENT__ : {
271                 String align = (String)value;
272                 if (align.equals("") == false) {
273                     uiControl.setVerticalAlignment((String) value);
274                 }
275                 break;
276             }
277             case __GO_VISIBLE__ : {
278                 uiControl.setVisible(((Boolean) value).booleanValue());
279                 if (((Component) uiControl) != null && ((Component) uiControl).getParent() != null) {
280                     ((Component) uiControl).getParent().invalidate();
281                     ((Component) uiControl).getParent().doLayout();
282                 }
283                 break;
284             }
285             case __GO_PARENT__:
286                 /* Update position */
287                 ((SwingViewObject)uiControl).update(__GO_POSITION__, controller.getProperty(uid, __GO_POSITION__));
288                 break;
289             default:
290                 break;
291         }
292     }
293
294     public static Double[] updatePosition(Widget uiControl, Integer uid, Object value) {
295         /* Convert value according to units */
296         UicontrolUnits unitsProperty = UnitsConverter.stringToUnitsEnum((String) GraphicController.getController().getProperty(uid, __GO_UI_UNITS__));
297         Double[] dblValues = UnitsConverter.convertPositionToPixels((Double[]) value, unitsProperty, uiControl);
298         // Set dimensions before position because position is adjusted according to size
299         uiControl.setDims(new Size(dblValues[WIDTH_INDEX].intValue(), dblValues[HEIGHT_INDEX].intValue()));
300         uiControl.setPosition(new Position(dblValues[X_INDEX].intValue(), dblValues[Y_INDEX].intValue()));
301         return dblValues;
302     }
303 }