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.graphic_objects.uicontrol;
16 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_POSITION__;
17 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_STYLE__;
18 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UICONTROL__;
19 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_BACKGROUNDCOLOR__;
20 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_CHECKBOX__;
21 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_EDIT__;
22 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME__;
23 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_ENABLE__;
24 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTANGLE__;
25 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTNAME__;
26 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTSIZE__;
27 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTUNITS__;
28 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTWEIGHT__;
29 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FOREGROUNDCOLOR__;
30 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_HORIZONTALALIGNMENT__;
31 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_IMAGE__;
32 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_LISTBOX__;
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_LISTBOXTOP_SIZE__;
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_MAX__;
37 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_POPUPMENU__;
38 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_PUSHBUTTON__;
39 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_RADIOBUTTON__;
40 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_RELIEF__;
41 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_SLIDER__;
42 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_SLIDERSTEP__;
43 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_STRING__;
44 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_STRING_SIZE__;
45 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_STRING_COLNB__;
46 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_TOOLTIPSTRING__;
47 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_TOOLTIPSTRING_SIZE__;
48 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_TABLE__;
49 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_TEXT__;
50 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_UNITS__;
51 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_VALUE__;
52 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_VALUE_SIZE__;
53 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_VERTICALALIGNMENT__;
55 import java.util.StringTokenizer;
57 import org.scilab.modules.graphic_objects.graphicObject.GraphicObject;
58 import org.scilab.modules.graphic_objects.graphicObject.Visitor;
61 * @author Bruno JOFRET
62 * @author Vincent COUVERT
64 public class Uicontrol extends GraphicObject {
66 protected static final String FLAT_RELIEF = "flat";
67 protected static final String RAISED_RELIEF = "raised";
68 protected static final String SUNKEN_RELIEF = "sunken";
69 protected static final String GROOVE_RELIEF = "groove";
70 protected static final String RIDGE_RELIEF = "ridge";
71 protected static final String SOLID_RELIEF = "solid";
73 private static final double DEFAULT_RED_BACKGROUND = 0.8;
74 private static final double DEFAULT_GREEN_BACKGROUND = 0.8;
75 private static final double DEFAULT_BLUE_BACKGROUND = 0.8;
77 private static final double DEFAULT_X = 20.0;
78 private static final double DEFAULT_Y = 40.0;
79 private static final double DEFAULT_WIDTH = 40.0;
80 private static final double DEFAULT_HEIGHT = 20.0;
82 private static final double DEFAULTFONTSIZE = 10;
83 private static final String DEFAULTFONTNAME = "helvetica";
84 private static final String DEFAULTFONTWEIGHT = "normal";
85 private static final String DEFAULTFONTANGLE = DEFAULTFONTWEIGHT;
86 private static final String STRING_SEPARATOR = "|";
88 private UicontrolStyle style;
89 private Double[] backgroundColor = {DEFAULT_RED_BACKGROUND, DEFAULT_GREEN_BACKGROUND, DEFAULT_BLUE_BACKGROUND};
90 private boolean enable = true;
91 private String fontAngle = DEFAULTFONTANGLE;
92 private String fontName = DEFAULTFONTNAME;
93 private double fontSize = DEFAULTFONTSIZE;
94 private String fontUnits = "points";
95 private String fontWeight = DEFAULTFONTWEIGHT;
96 private Double[] foregroundColor = {0.0, 0.0, 0.0};
97 private String horizontalAlignment = "center";
98 private Integer[] listboxTop;
99 private double max = 1.0;
101 private Double[] position = {DEFAULT_X, DEFAULT_Y, DEFAULT_WIDTH, DEFAULT_HEIGHT};
102 private String relief = RAISED_RELIEF;
103 private Double[] sliderStep = {0.01, 0.1};
104 private String[] string = {""};
105 private int stringColNb = 1; // Used for tables
106 private String[] tooltipString = {""};
107 private String units = "pixels";
108 private Double[] value;
109 private String verticalAlignment = "middle";
112 * All uicontrol properties
114 private enum UicontrolProperty {
146 * All uicontrol styles
148 private enum UicontrolStyle {
167 setVisible(false); /* To avoid to see the object rendered before all its properties to be set (See bug #10346) */
171 * Get style as a string
172 * @param style the uicontrol style
173 * @return the uicontrol style as a string
175 private int styleEnumToInt(UicontrolStyle style) {
178 return __GO_UI_CHECKBOX__;
180 return __GO_UI_EDIT__;
182 return __GO_UI_FRAME__;
184 return __GO_UI_IMAGE__;
186 return __GO_UI_LISTBOX__;
188 return __GO_UI_POPUPMENU__;
190 return __GO_UI_PUSHBUTTON__;
192 return __GO_UI_RADIOBUTTON__;
194 return __GO_UI_SLIDER__;
196 return __GO_UI_TABLE__;
198 return __GO_UI_TEXT__;
205 * Get style as an enum element
206 * @param style the uicontrol style
207 * @return the uicontrol style as an enum element
209 private UicontrolStyle intToStyleEnum(int style) {
211 case __GO_UI_CHECKBOX__ :
212 return UicontrolStyle.CHECKBOX;
213 case __GO_UI_EDIT__ :
214 return UicontrolStyle.EDIT;
215 case __GO_UI_FRAME__ :
216 return UicontrolStyle.FRAME;
217 case __GO_UI_IMAGE__ :
218 return UicontrolStyle.IMAGE;
219 case __GO_UI_LISTBOX__ :
220 return UicontrolStyle.LISTBOX;
221 case __GO_UI_POPUPMENU__ :
222 return UicontrolStyle.POPUPMENU;
223 case __GO_UI_PUSHBUTTON__ :
224 return UicontrolStyle.PUSHBUTTON;
225 case __GO_UI_RADIOBUTTON__ :
226 return UicontrolStyle.RADIOBUTTON;
227 case __GO_UI_SLIDER__ :
228 return UicontrolStyle.SLIDER;
229 case __GO_UI_TABLE__ :
230 return UicontrolStyle.TABLE;
231 case __GO_UI_TEXT__ :
232 return UicontrolStyle.TEXT;
239 * Get this object type
241 * @see org.scilab.modules.graphic_objects.graphicObject.GraphicObject#getType()
243 public Integer getType() {
244 return __GO_UICONTROL__;
248 * Returns the enum associated to a property name
249 * @param propertyName the property name
250 * @return the property enum
252 public Object getPropertyFromName(int propertyName) {
253 switch (propertyName) {
255 return UicontrolProperty.STYLE;
256 case __GO_UI_BACKGROUNDCOLOR__ :
257 return UicontrolProperty.BACKGROUNDCOLOR;
258 case __GO_UI_ENABLE__ :
259 return UicontrolProperty.ENABLE;
260 case __GO_UI_FONTANGLE__ :
261 return UicontrolProperty.FONTANGLE;
262 case __GO_UI_FONTNAME__ :
263 return UicontrolProperty.FONTNAME;
264 case __GO_UI_FONTSIZE__ :
265 return UicontrolProperty.FONTSIZE;
266 case __GO_UI_FONTUNITS__ :
267 return UicontrolProperty.FONTUNITS;
268 case __GO_UI_FONTWEIGHT__ :
269 return UicontrolProperty.FONTWEIGHT;
270 case __GO_UI_FOREGROUNDCOLOR__ :
271 return UicontrolProperty.FOREGROUNDCOLOR;
272 case __GO_UI_HORIZONTALALIGNMENT__ :
273 return UicontrolProperty.HORIZONTALALIGNMENT;
274 case __GO_UI_LISTBOXTOP__ :
275 return UicontrolProperty.LISTBOXTOP;
276 case __GO_UI_LISTBOXTOP_SIZE__ :
277 return UicontrolProperty.LISTBOXTOP_SIZE;
279 return UicontrolProperty.MAX;
281 return UicontrolProperty.MIN;
282 case __GO_POSITION__ :
283 return UicontrolProperty.POSITION;
284 case __GO_UI_RELIEF__ :
285 return UicontrolProperty.RELIEF;
286 case __GO_UI_SLIDERSTEP__ :
287 return UicontrolProperty.SLIDERSTEP;
288 case __GO_UI_STRING__ :
289 return UicontrolProperty.STRING;
290 case __GO_UI_STRING_SIZE__ :
291 return UicontrolProperty.STRING_SIZE;
292 case __GO_UI_STRING_COLNB__ :
293 return UicontrolProperty.STRING_COLNB;
294 case __GO_UI_TOOLTIPSTRING__ :
295 return UicontrolProperty.TOOLTIPSTRING;
296 case __GO_UI_TOOLTIPSTRING_SIZE__ :
297 return UicontrolProperty.TOOLTIPSTRING_SIZE;
298 case __GO_UI_UNITS__ :
299 return UicontrolProperty.UNITS;
300 case __GO_UI_VALUE__ :
301 return UicontrolProperty.VALUE;
302 case __GO_UI_VALUE_SIZE__ :
303 return UicontrolProperty.VALUE_SIZE;
304 case __GO_UI_VERTICALALIGNMENT__ :
305 return UicontrolProperty.VERTICALALIGNMENT;
307 return super.getPropertyFromName(propertyName);
312 * Fast property get method
313 * @param property the property to get
314 * @return the property value
316 public Object getProperty(Object property) {
317 if (property == UicontrolProperty.STYLE) {
319 } else if (property == UicontrolProperty.BACKGROUNDCOLOR) {
320 return getBackgroundColor();
321 } else if (property == UicontrolProperty.ENABLE) {
323 } else if (property == UicontrolProperty.FONTANGLE) {
324 return getFontAngle();
325 } else if (property == UicontrolProperty.FONTNAME) {
326 return getFontName();
327 } else if (property == UicontrolProperty.FONTSIZE) {
328 return getFontSize();
329 } else if (property == UicontrolProperty.FONTUNITS) {
330 return getFontUnits();
331 } else if (property == UicontrolProperty.FONTWEIGHT) {
332 return getFontWeight();
333 } else if (property == UicontrolProperty.FOREGROUNDCOLOR) {
334 return getForegroundColor();
335 } else if (property == UicontrolProperty.HORIZONTALALIGNMENT) {
336 return getHorizontalAlignment();
337 } else if (property == UicontrolProperty.LISTBOXTOP) {
338 return getListboxTop();
339 } else if (property == UicontrolProperty.LISTBOXTOP_SIZE) {
340 return getListboxTopSize();
341 } else if (property == UicontrolProperty.MAX) {
343 } else if (property == UicontrolProperty.MIN) {
345 } else if (property == UicontrolProperty.POSITION) {
346 return getUiPosition();
347 } else if (property == UicontrolProperty.RELIEF) {
349 } else if (property == UicontrolProperty.SLIDERSTEP) {
350 return getSliderStep();
351 } else if (property == UicontrolProperty.STRING) {
353 } else if (property == UicontrolProperty.STRING_SIZE) {
354 return getString().length;
355 } else if (property == UicontrolProperty.STRING_COLNB) {
356 return getStringColNb();
357 } else if (property == UicontrolProperty.TOOLTIPSTRING) {
358 return getTooltipString();
359 } else if (property == UicontrolProperty.TOOLTIPSTRING_SIZE) {
360 return getTooltipString().length;
361 } else if (property == UicontrolProperty.UNITS) {
363 } else if (property == UicontrolProperty.VALUE) {
365 } else if (property == UicontrolProperty.VALUE_SIZE) {
366 return getUiValueSize();
367 } else if (property == UicontrolProperty.VERTICALALIGNMENT) {
368 return getVerticalAlignment();
370 return super.getProperty(property);
375 * Fast property set method
376 * @param property the property to set
377 * @param value the property value
378 * @return true if the property has been set, false otherwise
380 public UpdateStatus setProperty(Object property, Object value) {
381 if (property == UicontrolProperty.STYLE) {
382 setStyle((Integer) value);
383 } else if (property == UicontrolProperty.BACKGROUNDCOLOR) {
384 setBackgroundColor((Double[]) value);
385 } else if (property == UicontrolProperty.ENABLE) {
386 setEnable((Boolean) value);
387 } else if (property == UicontrolProperty.FONTANGLE) {
388 setFontAngle((String) value);
389 } else if (property == UicontrolProperty.FONTNAME) {
390 setFontName((String) value);
391 } else if (property == UicontrolProperty.FONTSIZE) {
392 setFontSize((Double) value);
393 } else if (property == UicontrolProperty.FONTUNITS) {
394 setFontUnits((String) value);
395 } else if (property == UicontrolProperty.FONTWEIGHT) {
396 setFontWeight((String) value);
397 } else if (property == UicontrolProperty.FOREGROUNDCOLOR) {
398 setForegroundColor((Double[]) value);
399 } else if (property == UicontrolProperty.HORIZONTALALIGNMENT) {
400 setHorizontalAlignment((String) value);
401 } else if (property == UicontrolProperty.LISTBOXTOP) {
402 setListboxTop((Integer[]) value);
403 } else if (property == UicontrolProperty.MAX) {
404 setMax((Double) value);
405 } else if (property == UicontrolProperty.MIN) {
406 setMin((Double) value);
407 } else if (property == UicontrolProperty.POSITION) {
408 setUiPosition((Double[]) value);
409 } else if (property == UicontrolProperty.RELIEF) {
410 setRelief((String) value);
411 } else if (property == UicontrolProperty.SLIDERSTEP) {
412 setSliderStep((Double[]) value);
413 } else if (property == UicontrolProperty.STRING) {
414 setString((String[]) value);
415 } else if (property == UicontrolProperty.STRING_COLNB) {
416 setStringColNb((Integer) value);
417 } else if (property == UicontrolProperty.TOOLTIPSTRING) {
418 setTooltipString((String[]) value);
419 } else if (property == UicontrolProperty.UNITS) {
420 setUnits((String) value);
421 } else if (property == UicontrolProperty.VALUE) {
422 setUiValue((Double[]) value);
423 } else if (property == UicontrolProperty.VERTICALALIGNMENT) {
424 setVerticalAlignment((String) value);
426 return super.setProperty(property, value);
429 return UpdateStatus.Success;
436 public Integer getStyle() {
437 return styleEnumToInt(this.style);
442 * @param style the style
444 public void setStyle(int style) {
445 this.style = intToStyleEnum(style);
448 /* Background Color */
449 public Double[] getBackgroundColor() {
450 return this.backgroundColor;
453 public void setBackgroundColor(Double[] colors) {
454 this.backgroundColor = colors;
458 public boolean getEnable() {
462 public void setEnable(boolean status) {
463 this.enable = status;
467 public String getFontAngle() {
468 return this.fontAngle;
471 public void setFontAngle(String fontAngle) {
472 this.fontAngle = fontAngle;
476 public String getFontName() {
477 return this.fontName;
480 public void setFontName(String fontName) {
481 this.fontName = fontName;
485 public double getFontSize() {
486 return this.fontSize;
489 public void setFontSize(double fontSize) {
490 this.fontSize = fontSize;
494 public String getFontUnits() {
495 return this.fontUnits;
498 public void setFontUnits(String fontUnits) {
499 this.fontUnits = fontUnits;
503 public String getFontWeight() {
504 return this.fontWeight;
507 public void setFontWeight(String fontWeight) {
508 this.fontWeight = fontWeight;
511 /* Foreground Color */
512 public Double[] getForegroundColor() {
513 return this.foregroundColor;
516 public void setForegroundColor(Double[] colors) {
517 this.foregroundColor = colors;
520 /* Horizontal Alignment */
521 public String getHorizontalAlignment() {
522 return this.horizontalAlignment;
525 public void setHorizontalAlignment(String alignment) {
526 this.horizontalAlignment = alignment;
530 public Integer getListboxTopSize() {
531 return (listboxTop != null ? listboxTop.length : 0);
534 public Integer[] getListboxTop() {
535 return this.listboxTop;
538 public void setListboxTop(Integer[] listboxTop) {
539 this.listboxTop = listboxTop;
543 public Double getMax() {
547 public void setMax(double max) {
552 public Double getMin() {
556 public void setMin(double min) {
561 public Double[] getUiPosition() {
562 return this.position;
565 public void setUiPosition(Double[] position) {
566 this.position = position;
570 public String getRelief() {
574 public void setRelief(String relief) {
575 this.relief = relief;
582 public String[] getString() {
588 * @param string the string
590 public void setString(String[] string) {
591 if (this.style == UicontrolStyle.LISTBOX || this.style == UicontrolStyle.POPUPMENU) {
592 /* String can be set using a|b|c|d */
593 if (string.length == 1 & string[0].contains(STRING_SEPARATOR)) {
594 StringTokenizer strTok = new StringTokenizer(string[0], STRING_SEPARATOR);
595 String[] stringTab = new String[strTok.countTokens()];
596 while (strTok.hasMoreTokens()) {
597 stringTab[stringTab.length - strTok.countTokens()] = strTok.nextToken();
599 this.string = stringTab;
603 this.string = string;
607 * Get the string column number
608 * @return the number of columns
610 public int getStringColNb() {
615 * Set the string column number
616 * @param stringColNb the number of columns
618 public void setStringColNb(Integer stringColNb) {
619 this.stringColNb = stringColNb;
624 * Get the tooltip string
625 * @return the tooltip string
627 public String[] getTooltipString() {
628 return this.tooltipString;
632 * Set the tooltip string
633 * @param tooltipString the tooltip string
635 public void setTooltipString(String[] tooltipString) {
636 this.tooltipString = tooltipString;
640 public Double[] getSliderStep() {
641 return this.sliderStep;
644 public void setSliderStep(Double[] sliderStep) {
645 this.sliderStep = sliderStep;
649 public String getUnits() {
653 public void setUnits(String units) {
658 public Integer getUiValueSize() {
659 return (value != null ? value.length : 0);
662 public Double[] getUiValue() {
666 public void setUiValue(Double[] value) {
670 /* Vertical Alignment */
671 public String getVerticalAlignment() {
672 return this.verticalAlignment;
675 public void setVerticalAlignment(String alignment) {
676 this.verticalAlignment = alignment;
679 public void accept(Visitor visitor) {