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