* Bug #11489 fixed - 'SliderStep' property was ignored for uicontrols.
[scilab.git] / scilab / modules / gui / src / java / org / scilab / modules / gui / SwingView.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-en.txt
11  *
12  */
13 package org.scilab.modules.gui;
14
15 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_CALLBACKTYPE__;
16 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_CALLBACK__;
17 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_CHILDREN__;
18 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_CONSOLE__;
19 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_FIGURE__;
20 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_PARENT__;
21 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_POSITION__;
22 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_PROGRESSIONBAR__;
23 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_SIZE__;
24 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_STYLE__;
25 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_TYPE__;
26 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UICHECKEDMENU__;
27 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UICHILDMENU__;
28 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UICONTEXTMENU__;
29 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UICONTROL__;
30 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UIMENU__;
31 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UIPARENTMENU__;
32 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_BACKGROUNDCOLOR__;
33 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_CHECKBOX__;
34 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_CHECKED__;
35 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_EDIT__;
36 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_ENABLE__;
37 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTANGLE__;
38 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTNAME__;
39 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTUNITS__;
40 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTSIZE__;
41 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTWEIGHT__;
42 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FOREGROUNDCOLOR__;
43 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME__;
44 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_HORIZONTALALIGNMENT__;
45 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_ICON__;
46 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_IMAGE__;
47 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_LABEL__;
48 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_LISTBOX__;
49 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_POPUPMENU__;
50 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_PUSHBUTTON__;
51 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_RADIOBUTTON__;
52 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_RELIEF__;
53 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_SEPARATOR__;
54 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_SLIDER__;
55 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_STRING__;
56 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_TABLE__;
57 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_TEXT__;
58 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_VERTICALALIGNMENT__;
59 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_VALID__;
60 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_VISIBLE__;
61 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_WAITBAR__;
62 import static org.scilab.modules.gui.utils.Debug.DEBUG;
63
64 import java.awt.Component;
65 import java.awt.Container;
66 import java.awt.GraphicsEnvironment;
67 import java.util.Arrays;
68 import java.util.Collections;
69 import java.util.HashMap;
70 import java.util.HashSet;
71 import java.util.Map;
72 import java.util.Set;
73 import java.util.UUID;
74
75 import javax.swing.JComponent;
76 import javax.swing.JPanel;
77 import javax.swing.JSeparator;
78 import javax.swing.SwingUtilities;
79
80 import org.flexdock.docking.Dockable;
81 import org.flexdock.docking.DockingManager;
82 import org.flexdock.docking.activation.ActiveDockableTracker;
83 import org.flexdock.view.View;
84 import org.scilab.modules.graphic_export.Driver;
85 import org.scilab.modules.graphic_objects.console.Console;
86 import org.scilab.modules.graphic_objects.figure.Figure;
87 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
88 import org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties;
89 import org.scilab.modules.graphic_objects.graphicView.GraphicView;
90 import org.scilab.modules.gui.bridge.checkbox.SwingScilabCheckBox;
91 import org.scilab.modules.gui.bridge.checkboxmenuitem.SwingScilabCheckBoxMenuItem;
92 import org.scilab.modules.gui.bridge.console.SwingScilabConsole;
93 import org.scilab.modules.gui.bridge.contextmenu.SwingScilabContextMenu;
94 import org.scilab.modules.gui.bridge.editbox.SwingScilabEditBox;
95 import org.scilab.modules.gui.bridge.frame.SwingScilabFrame;
96 import org.scilab.modules.gui.bridge.label.SwingScilabLabel;
97 import org.scilab.modules.gui.bridge.listbox.SwingScilabListBox;
98 import org.scilab.modules.gui.bridge.menu.SwingScilabMenu;
99 import org.scilab.modules.gui.bridge.menuitem.SwingScilabMenuItem;
100 import org.scilab.modules.gui.bridge.popupmenu.SwingScilabPopupMenu;
101 import org.scilab.modules.gui.bridge.pushbutton.SwingScilabPushButton;
102 import org.scilab.modules.gui.bridge.radiobutton.SwingScilabRadioButton;
103 import org.scilab.modules.gui.bridge.slider.SwingScilabSlider;
104 import org.scilab.modules.gui.bridge.tab.SwingScilabTab;
105 import org.scilab.modules.gui.bridge.uiimage.SwingScilabUiImage;
106 import org.scilab.modules.gui.bridge.uitable.SwingScilabUiTable;
107 import org.scilab.modules.gui.bridge.waitbar.SwingScilabWaitBar;
108 import org.scilab.modules.gui.bridge.window.SwingScilabWindow;
109 import org.scilab.modules.gui.console.ScilabConsole;
110 import org.scilab.modules.gui.menubar.ScilabMenuBar;
111 import org.scilab.modules.gui.textbox.ScilabTextBox;
112 import org.scilab.modules.gui.textbox.TextBox;
113 import org.scilab.modules.gui.toolbar.ToolBar;
114 import org.scilab.modules.gui.utils.ClosingOperationsManager;
115 import org.scilab.modules.gui.utils.ToolBarBuilder;
116 import org.scilab.modules.gui.utils.WindowsConfigurationManager;
117 import org.scilab.modules.gui.widget.Widget;
118
119 /**
120  * @author Bruno JOFRET
121  * @author Vincent COUVERT
122  */
123 public final class SwingView implements GraphicView {
124
125     public static final String NULLUUID = new UUID(0L, 0L).toString();
126
127     private static SwingView me;
128
129     private static boolean headless;
130
131     private Map<String, TypedObject> allObjects;
132
133     /**
134      * Constructor
135      */
136     private SwingView() {
137         GraphicController.getController().register(this);
138         allObjects = Collections.synchronizedMap(new HashMap<String, TypedObject>());
139     }
140
141     public static void registerSwingView() {
142         DEBUG("SwingView", "calling registerSwingView()");
143         if (me == null) {
144             me = new SwingView();
145         }
146     }
147
148     public static void setHeadless(boolean headless) {
149         SwingView.headless = headless;
150     }
151
152     public static boolean isHeadless() {
153         return SwingView.headless;
154     }
155
156     public static SwingViewObject getFromId(String id) {
157         TypedObject typedObject = me.allObjects.get(id);
158
159         if (typedObject == null) {
160             return null;
161         }
162
163         return typedObject.getValue();
164     }
165
166     private enum UielementType {
167         Console,
168         CheckBox,
169         Edit,
170         Frame,
171         Figure,
172         Image,
173         ListBox,
174         PopupMenu,
175         Progressbar,
176         PushButton,
177         RadioButton,
178         Slider,
179         Table,
180         Text,
181         Uimenu,
182         UiParentMenu,
183         UiChildMenu,
184         UiCheckedMenu,
185         UiContextMenu,
186         Waitbar
187     }
188
189     private class TypedObject {
190         private UielementType   _type;
191         private SwingViewObject _value;
192         private Set<String> _children;
193
194         public TypedObject(UielementType _type, SwingViewObject _value) {
195             this._type = _type;
196             this._value = _value;
197             this._children = Collections.synchronizedSet(new HashSet<String>());
198         }
199
200         public UielementType getType() {
201             return _type;
202         }
203
204         public SwingViewObject getValue() {
205             return _value;
206         }
207
208         public Set<String> getChildren() {
209             return _children;
210         }
211
212         public void addChild(String childUID) {
213             _children.add(childUID);
214         }
215
216         public void removeChild(String childUID) {
217             _children.remove(childUID);
218         }
219
220         public boolean hasChild(String childUID) {
221             return _children.contains(childUID);
222         }
223     };
224
225     private static final Set<Integer> managedTypes = new HashSet<Integer>(Arrays.asList(
226                 GraphicObjectProperties.__GO_FIGURE__,
227                 GraphicObjectProperties.__GO_UICONTEXTMENU__,
228                 GraphicObjectProperties.__GO_UIMENU__,
229                 GraphicObjectProperties.__GO_CONSOLE__,
230                 GraphicObjectProperties.__GO_PROGRESSIONBAR__,
231                 GraphicObjectProperties.__GO_WAITBAR__,
232                 GraphicObjectProperties.__GO_UICONTROL__
233             ));
234     @Override
235     public void createObject(String id) {
236
237         int objectType = (Integer) GraphicController.getController().getProperty(id, __GO_TYPE__);
238
239         if (managedTypes.contains(objectType) == false) {
240             return;
241         }
242         //System.err.println("[SwingWiew] Object Created : " + id + " with type : " + objectType);
243         boolean isValid = (Boolean) GraphicController.getController().getProperty(id, __GO_VALID__);
244         if (!isValid) {
245             return;
246         }
247
248         if (!headless && !GraphicsEnvironment.isHeadless()) {
249             DEBUG("SwingWiew", "Object Created : " + id + "with type : " + objectType);
250             if (objectType == __GO_FIGURE__
251                     || objectType == __GO_UICONTEXTMENU__
252                     || objectType == __GO_UIMENU__
253                     || objectType == __GO_CONSOLE__
254                     || objectType == __GO_PROGRESSIONBAR__
255                     || objectType == __GO_WAITBAR__) {
256                 allObjects.put(id, CreateObjectFromType(objectType, id));
257                 return;
258             }
259
260             if (objectType == __GO_UICONTROL__) {
261                 int style = (Integer) GraphicController.getController().getProperty(id, __GO_STYLE__);
262                 DEBUG("SwingView", "__GO_STYLE__(" + style + ")");
263                 allObjects.put(id, CreateObjectFromType(style, id));
264             }
265         } else {
266             if (objectType == __GO_FIGURE__) {
267                 Driver.setDefaultVisitor(id);
268             }
269         }
270     }
271
272     private UielementType StyleToEnum(int style) {
273         DEBUG("SwingView", "StyleToEnum(" + style + ")");
274         switch (style) {
275             case __GO_FIGURE__ :
276                 return UielementType.Figure;
277             case __GO_CONSOLE__ :
278                 return UielementType.Console;
279             case __GO_UI_CHECKBOX__ :
280                 return UielementType.CheckBox;
281             case __GO_UI_EDIT__ :
282                 return UielementType.Edit;
283             case __GO_UI_FRAME__ :
284                 return UielementType.Frame;
285             case __GO_UI_IMAGE__ :
286                 return UielementType.Image;
287             case __GO_UI_LISTBOX__ :
288                 return UielementType.ListBox;
289             case __GO_UI_POPUPMENU__ :
290                 return UielementType.PopupMenu;
291             case __GO_UI_PUSHBUTTON__ :
292                 return UielementType.PushButton;
293             case __GO_UI_RADIOBUTTON__ :
294                 return UielementType.RadioButton;
295             case __GO_UI_SLIDER__ :
296                 return UielementType.Slider;
297             case __GO_UI_TABLE__ :
298                 return UielementType.Table;
299             case __GO_UI_TEXT__ :
300                 return UielementType.Text;
301             case __GO_UIMENU__ :
302                 return UielementType.UiChildMenu;
303             case __GO_UIPARENTMENU__ :
304                 return UielementType.UiParentMenu;
305             case __GO_UICHILDMENU__ :
306                 return UielementType.UiChildMenu;
307             case __GO_UICHECKEDMENU__ :
308                 return UielementType.UiCheckedMenu;
309             case __GO_PROGRESSIONBAR__ :
310                 return UielementType.Progressbar;
311             case __GO_WAITBAR__ :
312                 return UielementType.Waitbar;
313             case __GO_UICONTEXTMENU__ :
314                 return UielementType.UiContextMenu;
315         }
316         return null;
317     }
318
319     private TypedObject CreateObjectFromType(int type, String id) {
320         UielementType enumType = StyleToEnum(type);
321         return new TypedObject(enumType, CreateObjectFromType(enumType, id));
322     }
323
324     private SwingViewObject CreateObjectFromType(UielementType type, String id) {
325         switch (type) {
326             case CheckBox:
327                 SwingScilabCheckBox checkBox = new SwingScilabCheckBox();
328                 checkBox.setId(id);
329                 setDefaultProperties(checkBox, id);
330                 return checkBox;
331             case Console:
332                 Console console = (Console) GraphicController.getController().getObjectFromId(id);
333                 if (console.getScilabMode() == Console.ScilabMode.STD) {
334                     WindowsConfigurationManager.restoreUUID(NULLUUID);
335                     SwingScilabConsole sciConsole = ((SwingScilabConsole) ScilabConsole.getConsole().getAsSimpleConsole());
336                     SwingScilabTab consoleTab = (SwingScilabTab) sciConsole.getParent();
337                     consoleTab.setId(id);
338                     return consoleTab;
339                 } else {
340                     return null;
341                 }
342             case Edit:
343                 SwingScilabEditBox edit = new SwingScilabEditBox();
344                 edit.setId(id);
345                 setDefaultProperties(edit, id);
346                 return edit;
347             case Figure:
348                 Figure figure = (Figure) GraphicController.getController().getObjectFromId(id);
349                 String figureTitle = figure.getName();
350                 Integer figureId = figure.getId();
351                 if ((figureTitle != null) && (figureId != null)) {
352                     figureTitle = figureTitle.replaceFirst("%d", figureId.toString());
353                 }
354
355                 SwingScilabWindow window = new SwingScilabWindow();
356
357                 window.setTitle(figureTitle);
358                 /* TOOLBAR */
359                 ToolBar toolBar = ToolBarBuilder.buildToolBar(SwingScilabTab.GRAPHICS_TOOLBAR_DESCRIPTOR, figureId);
360                 /* INFOBAR */
361                 TextBox infoBar = ScilabTextBox.createTextBox();
362
363                 SwingScilabTab tab = new SwingScilabTab(figureTitle, figureId, figure);
364                 tab.setId(id);
365
366                 tab.setMenuBar(ScilabMenuBar.createMenuBar());
367                 tab.setToolBar(toolBar);
368                 tab.setInfoBar(ScilabTextBox.createTextBox());
369                 window.addMenuBar(tab.getMenuBar());
370                 window.addToolBar(tab.getToolBar());
371                 window.addInfoBar(tab.getInfoBar());
372
373                 tab.setWindowIcon("graphic-window");
374
375                 tab.setParentWindowId(window.getId());
376
377                 tab.setEventHandler(figure.getEventHandlerString());
378                 tab.setEventHandlerEnabled(figure.getEventHandlerEnable());
379
380                 DockingManager.dock(tab, window.getDockingPort());
381                 ActiveDockableTracker.requestDockableActivation(tab);
382
383                 window.setVisible(true);
384                 tab.setVisible(true);
385                 tab.setName(figureTitle);
386
387                 String infoMessage = figure.getInfoMessage();
388                 if ((infoMessage == null) || (infoMessage.length() == 0)) {
389                     infoBar.setText("");
390                 } else {
391                     infoBar.setText(infoMessage);
392                 }
393                 tab.update(__GO_SIZE__, (Integer[]) GraphicController.getController().getProperty(id, __GO_SIZE__));
394                 tab.update(__GO_POSITION__, (Integer[]) GraphicController.getController().getProperty(id, __GO_POSITION__));
395                 // TODO set other default properties
396                 return tab;
397             case Frame:
398                 SwingScilabFrame frame = new SwingScilabFrame();
399                 frame.setId(id);
400                 setDefaultProperties(frame, id);
401                 return frame;
402             case Image:
403                 SwingScilabUiImage image = new SwingScilabUiImage();
404                 image.setId(id);
405                 setDefaultProperties(image, id);
406                 return image;
407             case ListBox:
408                 SwingScilabListBox listBox = new SwingScilabListBox();
409                 listBox.setId(id);
410                 setDefaultProperties(listBox, id);
411                 return listBox;
412             case PopupMenu:
413                 SwingScilabPopupMenu popupMenu = new SwingScilabPopupMenu();
414                 popupMenu.setId(id);
415                 setDefaultProperties(popupMenu, id);
416                 return popupMenu;
417             case Progressbar:
418                 SwingScilabWaitBar progressbar = new SwingScilabWaitBar();
419                 progressbar.setIndeterminateMode(true);
420                 progressbar.setId(id);
421                 return progressbar;
422             case PushButton:
423                 SwingScilabPushButton pushButton = new SwingScilabPushButton();
424                 pushButton.setId(id);
425                 setDefaultProperties(pushButton, id);
426                 return pushButton;
427             case RadioButton:
428                 SwingScilabRadioButton radioButton = new SwingScilabRadioButton();
429                 radioButton.setId(id);
430                 setDefaultProperties(radioButton, id);
431                 return radioButton;
432             case Slider:
433                 SwingScilabSlider slider = new SwingScilabSlider();
434                 slider.setId(id);
435                 setDefaultProperties(slider, id);
436                 return slider;
437             case Table:
438                 SwingScilabUiTable table = new SwingScilabUiTable();
439                 table.setId(id);
440                 setDefaultProperties(table, id);
441                 return table;
442             case Text:
443                 SwingScilabLabel text = new SwingScilabLabel();
444                 text.setId(id);
445                 setDefaultProperties(text, id);
446                 return text;
447             case Uimenu:
448                 throw new UnsupportedOperationException();
449             case UiParentMenu: /* SwingView internal type */
450                 SwingScilabMenu parentMenu = new SwingScilabMenu();
451                 parentMenu.setId(id);
452                 setMenuDefaultProperties(parentMenu, id);
453                 return parentMenu;
454             case UiChildMenu: /* SwingView internal type */
455                 SwingScilabMenuItem childMenu = new SwingScilabMenuItem();
456                 childMenu.setId(id);
457                 setMenuDefaultProperties(childMenu, id);
458                 return childMenu;
459             case UiCheckedMenu: /* SwingView internal type */
460                 SwingScilabCheckBoxMenuItem checkedMenu = new SwingScilabCheckBoxMenuItem(false);
461                 checkedMenu.setId(id);
462                 setMenuDefaultProperties(checkedMenu, id);
463                 return checkedMenu;
464             case UiContextMenu:
465                 SwingScilabContextMenu contextMenu = new SwingScilabContextMenu();
466                 contextMenu.setId(id);
467                 return contextMenu;
468             case Waitbar:
469                 SwingScilabWaitBar waitbar = new SwingScilabWaitBar();
470                 waitbar.setIndeterminateMode(false);
471                 waitbar.setId(id);
472                 return waitbar;
473             default:
474                 return null;
475         }
476     }
477
478     /**
479      * Initialize all poperties according to model
480      * @param uiMenuObject the uimenu
481      * @param id the uimenu id
482      */
483     private void setMenuDefaultProperties(Widget uiMenuObject, String id) {
484         SwingViewMenu.update(uiMenuObject, __GO_CHILDREN__,
485                              (String[]) GraphicController.getController().getProperty(id, __GO_CHILDREN__));
486         SwingViewMenu.update(uiMenuObject, __GO_CALLBACK__,
487                              (String) GraphicController.getController().getProperty(id, __GO_CALLBACK__));
488         SwingViewMenu.update(uiMenuObject, __GO_CALLBACKTYPE__,
489                              (Integer) GraphicController.getController().getProperty(id, __GO_CALLBACKTYPE__));
490         SwingViewMenu.update(uiMenuObject, __GO_UI_CHECKED__,
491                              (Boolean) GraphicController.getController().getProperty(id, __GO_UI_CHECKED__));
492         SwingViewMenu.update(uiMenuObject, __GO_UI_ENABLE__,
493                              (Boolean) GraphicController.getController().getProperty(id, __GO_UI_ENABLE__));
494         SwingViewMenu.update(uiMenuObject, __GO_UI_FOREGROUNDCOLOR__,
495                              (Double[]) GraphicController.getController().getProperty(id, __GO_UI_FOREGROUNDCOLOR__));
496         SwingViewMenu.update(uiMenuObject, __GO_UI_LABEL__,
497                              (String) GraphicController.getController().getProperty(id, __GO_UI_LABEL__));
498         SwingViewMenu.update(uiMenuObject, __GO_UI_ICON__,
499                              (String) GraphicController.getController().getProperty(id, __GO_UI_ICON__));
500     }
501
502     /**
503      * Initialize all poperties according to model
504      * @param uiControlObject the uicontrol
505      * @param id the uicontrol id
506      */
507     private void setDefaultProperties(Widget uiControlObject, String id) {
508         /* Visible property is set first to avoid to see the object rendered before all its properties to be set (See bug #10346) */
509         SwingViewWidget.update(uiControlObject, __GO_VISIBLE__,
510                                (Boolean) GraphicController.getController().getProperty(id, __GO_VISIBLE__));
511         SwingViewWidget.update(uiControlObject, __GO_UI_BACKGROUNDCOLOR__,
512                                (Double[]) GraphicController.getController().getProperty(id, __GO_UI_BACKGROUNDCOLOR__));
513         SwingViewWidget.update(uiControlObject, __GO_UI_ENABLE__,
514                                (Boolean) GraphicController.getController().getProperty(id, __GO_UI_ENABLE__));
515         SwingViewWidget.update(uiControlObject, __GO_UI_ENABLE__,
516                                (Boolean) GraphicController.getController().getProperty(id, __GO_UI_ENABLE__));
517         SwingViewWidget.update(uiControlObject, __GO_UI_FONTANGLE__,
518                                (String) GraphicController.getController().getProperty(id, __GO_UI_FONTANGLE__));
519         SwingViewWidget.update(uiControlObject, __GO_UI_FONTNAME__,
520                                (String) GraphicController.getController().getProperty(id, __GO_UI_FONTNAME__));
521         SwingViewWidget.update(uiControlObject, __GO_UI_FONTUNITS__,
522                                (String) GraphicController.getController().getProperty(id, __GO_UI_FONTUNITS__));
523         SwingViewWidget.update(uiControlObject, __GO_UI_FONTSIZE__,
524                                (Double) GraphicController.getController().getProperty(id, __GO_UI_FONTSIZE__));
525         SwingViewWidget.update(uiControlObject, __GO_UI_FONTWEIGHT__,
526                                (String) GraphicController.getController().getProperty(id, __GO_UI_FONTWEIGHT__));
527         SwingViewWidget.update(uiControlObject, __GO_UI_FOREGROUNDCOLOR__,
528                                (Double[]) GraphicController.getController().getProperty(id, __GO_UI_FOREGROUNDCOLOR__));
529         SwingViewWidget.update(uiControlObject, __GO_UI_HORIZONTALALIGNMENT__,
530                                (String) GraphicController.getController().getProperty(id, __GO_UI_HORIZONTALALIGNMENT__));
531         SwingViewWidget.update(uiControlObject, __GO_UI_RELIEF__,
532                                (String) GraphicController.getController().getProperty(id, __GO_UI_RELIEF__));
533         SwingViewWidget.update(uiControlObject, __GO_UI_STRING__,
534                                (String[]) GraphicController.getController().getProperty(id, __GO_UI_STRING__));
535         SwingViewWidget.update(uiControlObject, __GO_UI_VERTICALALIGNMENT__,
536                                (String) GraphicController.getController().getProperty(id, __GO_UI_VERTICALALIGNMENT__));
537         SwingViewWidget.update(uiControlObject, __GO_POSITION__,
538                                (Double[]) GraphicController.getController().getProperty(id, __GO_POSITION__));
539     }
540
541     @Override
542     public void deleteObject(String id) {
543         final TypedObject requestedObject = allObjects.get(id);
544         if (requestedObject != null) {
545             switch (requestedObject.getType()) {
546                 case Figure:
547                     final SwingScilabTab tab = (SwingScilabTab) requestedObject.getValue();
548                     tab.disablePaint();
549                     SwingUtilities.invokeLater(new Runnable() {
550                         public void run() {
551                             DockingManager.close(tab);
552                             DockingManager.unregisterDockable((Dockable) tab);
553                             ClosingOperationsManager.unregisterClosingOperation(tab);
554                             ClosingOperationsManager.removeDependency(tab);
555                             ClosingOperationsManager.checkTabForClosing(tab);
556                             tab.close();
557                         }
558                     });
559                     break;
560                 case Progressbar:
561                 case Waitbar:
562                     SwingScilabWaitBar bar = (SwingScilabWaitBar) requestedObject.getValue();
563                     bar.close();
564                     break;
565                 default:
566                     // Nothing to do
567                     // uicontrol case: the object is destroyed when its parent updates its children
568                     break;
569             }
570             allObjects.remove(id);
571         }
572     }
573
574     @Override
575     public void updateObject(String id, int property) {
576         TypedObject registeredObject = allObjects.get(id);
577         DEBUG("SwingView", "Update" + property);
578
579         /* On uicontrol style is set after object creation */
580         if (registeredObject == null && property == __GO_STYLE__) {
581             int style = (Integer) GraphicController.getController().getProperty(id, __GO_STYLE__);
582             allObjects.put(id, CreateObjectFromType(style, id));
583         }
584
585         /* Removes the swing object if its parent is not display */
586         if (registeredObject != null && property == __GO_PARENT__) {
587             String parentId = (String) GraphicController.getController().getProperty(id, __GO_PARENT__);
588             TypedObject registeredParent = allObjects.get(parentId);
589             if (registeredParent == null) {
590                 allObjects.remove(id);
591             }
592         }
593         int type = (Integer) GraphicController.getController().getProperty(id, __GO_TYPE__);
594         /* Children list update */
595         if (registeredObject != null && property == __GO_CHILDREN__) {
596             String[] newChildren = (String[]) GraphicController.getController().getProperty(id, __GO_CHILDREN__);
597
598             switch (type) {
599                     /*
600                      * FIGURE CHILDREN UPDATE
601                      */
602                 case __GO_FIGURE__ :
603                     updateFigureChildren(id, newChildren);
604                     break;
605                     /*
606                      * CONSOLE CHILDREN UPDATE
607                      */
608                 case __GO_CONSOLE__ :
609                     updateConsoleChildren(id, newChildren);
610                     break;
611                     /*
612                      * MENU CHILDREN UPDATE
613                      */
614                 case __GO_UIMENU__ :
615                     updateMenuChildren(id, newChildren);
616                     break;
617                     /*
618                      * CONTEXTMENU CHILDREN UPDATE
619                      */
620                 case __GO_UICONTEXTMENU__ :
621                     updateContextMenuChildren(id, newChildren);
622                     break;
623                     /*
624                      * UICONTROL "FRAME" CHILDREN UPDATE
625                      */
626                 case __GO_UICONTROL__ :
627                     int style = (Integer) GraphicController.getController().getProperty(id, __GO_STYLE__);
628                     if (style == __GO_UI_FRAME__) {
629                         updateFrameChildren(id, newChildren);
630                     }
631                     break;
632             }
633         }
634
635         /*
636          * When the CHECKED property is updated for a UIMENU,
637          * the object is converted to a SwingScilabCheckBoxMenuItem is not already of this type
638          */
639         if (registeredObject != null && property == __GO_UI_CHECKED__) {
640             if (type == __GO_UIMENU__) {
641                 TypedObject updatedObject = allObjects.get(id);
642                 switch (updatedObject.getType()) {
643                     case UiParentMenu:
644                         allObjects.put(id, CreateObjectFromType(__GO_UICHECKEDMENU__, id));
645                         registeredObject = allObjects.get(id);
646                         break;
647                     case UiChildMenu:
648                         String parentId = (String) GraphicController.getController().getProperty(id, __GO_PARENT__);
649                         int index = ((SwingScilabMenu) allObjects.get(parentId).getValue())
650                                     .getComponentZOrder((SwingScilabMenuItem) allObjects.get(id).getValue());
651                         ((SwingScilabMenu) allObjects.get(parentId).getValue()).remove((SwingScilabMenuItem) allObjects.get(id).getValue());
652                         allObjects.put(id, CreateObjectFromType(__GO_UICHECKEDMENU__, id));
653                         registeredObject = allObjects.get(id);
654                         ((SwingScilabMenu) allObjects.get(parentId).getValue()).add((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue(), index);
655                         return;
656                     default:
657                         break;
658                 }
659             }
660         }
661
662         /*
663          * When the SEPARATOR property is updated for a UIMENU,
664          * When the property is set to TRUE: A separator is added if not already done
665          * When the property is set to FALSE: The previous separator is removed if it exists
666          */
667         if (registeredObject != null && property == __GO_UI_SEPARATOR__) {
668             if (type == __GO_UIMENU__) {
669                 String parentId = (String) GraphicController.getController().getProperty(id, __GO_PARENT__);
670                 int menuPosition = -1;
671                 Component[] allChildren =  ((SwingScilabMenu) allObjects.get(parentId).getValue()).getMenuComponents();
672                 for (int kChild = 0; kChild < allChildren.length; kChild++) {
673                     if (allChildren[kChild] == (Component) allObjects.get(id).getValue()) {
674                         menuPosition = kChild;
675                         break;
676                     }
677                 }
678                 if (menuPosition < 0) {
679                     return;
680                 }
681                 boolean newSeparatorMode = (Boolean) GraphicController.getController().getProperty(id, __GO_UI_SEPARATOR__);
682                 if (newSeparatorMode) { // Add a separator above the menu
683                     ((SwingScilabMenu) allObjects.get(parentId).getValue()).insertSeparator(menuPosition);
684                 } else { // Remove the separator above the menu (if there is one)
685                     if (menuPosition == 0) { // There is no object above the menu
686                         return;
687                     }
688                     if (((SwingScilabMenu) allObjects.get(parentId).getValue()).getComponent(menuPosition - 1) instanceof JSeparator) {
689                         ((SwingScilabMenu) allObjects.get(parentId).getValue()).remove(menuPosition - 1);
690                     }
691                 }
692             }
693         }
694
695         if (registeredObject != null) {
696             SwingViewObject swingObject = registeredObject.getValue();
697             if (swingObject != null) {
698                 swingObject.update(property, GraphicController.getController().getProperty(id, property));
699             }
700         }
701     }
702
703     /**
704      * Update the figure children (called by generic updateObject method)
705      * @param id the id of the figure
706      * @param newChildren the new children IDs list
707      */
708     private void updateFigureChildren(String id, String[] newChildren) {
709         TypedObject updatedObject = allObjects.get(id);
710         Container updatedComponent = (SwingScilabTab) updatedObject.getValue();
711         boolean needRevalidate = false;
712
713         // Add new children
714         for (String childId : newChildren) {
715             if (!updatedObject.hasChild(childId)) {
716
717                 // Add the child
718                 updatedObject.addChild(childId);
719
720                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
721
722                 /* Add an uicontrol */
723                 if (childType == __GO_UICONTROL__) {
724                     ((SwingScilabTab) updatedComponent).addMember(allObjects.get(childId).getValue());
725                     needRevalidate = true;
726                 }
727
728                 /* Add an uimenu */
729                 if (childType == __GO_UIMENU__) {
730                     TypedObject childAsTypedObject = allObjects.get(childId);
731                     switch (childAsTypedObject.getType()) {
732                         case UiChildMenu:
733                         case UiCheckedMenu:
734                             allObjects.put(childId, CreateObjectFromType(__GO_UIPARENTMENU__, childId));
735                             ((Container) ((SwingScilabTab) updatedComponent).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) allObjects.get(childId).getValue());
736                             break;
737                         default: /* UiParentMenu */
738                             ((Container) ((SwingScilabTab) updatedComponent).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) allObjects.get(childId).getValue());
739                             break;
740                     }
741                     needRevalidate = true;
742                 }
743             }
744         }
745
746         // Remove children which have been deleted
747         Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
748         // Clone the children set to avoid concurrent accesses
749         String[] oldChildrenSet = updatedObject.getChildren().toArray(new String[updatedObject.getChildren().size()]);
750         for (String childId : oldChildrenSet) {
751             if (!newChildrenSet.contains(childId)) {
752
753                 // Remove the child
754                 updatedObject.removeChild(childId);
755
756                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
757
758                 /* Remove an uicontrol */
759                 if (childType == __GO_UICONTROL__) {
760                     ((SwingScilabTab) updatedComponent).removeMember(allObjects.get(childId).getValue());
761                     needRevalidate = true;
762                 }
763
764                 /* Remove an uimenu */
765                 if (childType == __GO_UIMENU__) {
766                     TypedObject childAsTypedObject = allObjects.get(childId);
767                     switch (childAsTypedObject.getType()) {
768                         case UiCheckedMenu:
769                             ((Container) ((SwingScilabTab) updatedComponent).getMenuBar().getAsSimpleMenuBar()).remove((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
770                             break;
771                         default: /* UiParentMenu */
772                             ((Container) ((SwingScilabTab) updatedComponent).getMenuBar().getAsSimpleMenuBar()).remove((SwingScilabMenu) allObjects.get(childId).getValue());
773                             break;
774                     }
775                     needRevalidate = true;
776                 }
777             }
778         }
779         if (needRevalidate && updatedComponent != null) {
780             ((View) updatedComponent).revalidate();
781         }
782     }
783
784     /**
785      * Update the frame children (called by generic updateObject method)
786      * @param id the id of the figure
787      * @param newChildren the new children IDs list
788      */
789     private void updateFrameChildren(String id, String[] newChildren) {
790         TypedObject updatedObject = allObjects.get(id);
791         Container updatedComponent = (SwingScilabFrame) updatedObject.getValue();
792         boolean needRevalidate = false;
793
794         // Add new children
795         for (String childId : newChildren) {
796             if (!updatedObject.hasChild(childId)) {
797
798                 // Add the child
799                 updatedObject.addChild(childId);
800
801                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
802
803                 /* Add an uicontrol */
804                 if (childType == __GO_UICONTROL__) {
805                     ((SwingScilabFrame) updatedComponent).addMember(allObjects.get(childId).getValue());
806                     needRevalidate = true;
807                 }
808             }
809         }
810
811         // Remove children which have been deleted
812         Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
813         // Clone the children set to avoid concurrent accesses
814         String[] oldChildrenSet = updatedObject.getChildren().toArray(new String[updatedObject.getChildren().size()]);
815         for (String childId : oldChildrenSet) {
816             if (!newChildrenSet.contains(childId)) {
817
818                 // Remove the child
819                 updatedObject.removeChild(childId);
820
821                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
822
823                 /* Remove an uicontrol */
824                 if (childType == __GO_UICONTROL__) {
825                     updatedComponent.remove((Component) allObjects.get(childId).getValue());
826                     needRevalidate = true;
827                 }
828             }
829         }
830         if (needRevalidate && updatedComponent != null) {
831             ((JPanel) updatedComponent).revalidate();
832         }
833     }
834
835     /**
836      * Update the Console menus (called by generic updateObject method)
837      * @param id the id of the console object
838      * @param newChildren the new children IDs list
839      */
840     private void updateConsoleChildren(String id, String[] newChildren) {
841         TypedObject updatedObject = allObjects.get(id);
842         Container updatedComponent = (SwingScilabTab) updatedObject.getValue();
843         boolean needRevalidate = false;
844
845         // Add new children
846         for (String childId : newChildren) {
847             if (!updatedObject.hasChild(childId)) {
848
849                 // Add the child
850                 updatedObject.addChild(childId);
851
852                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
853
854                 if (childType == __GO_UIMENU__) {
855                     TypedObject childAsTypedObject = allObjects.get(childId);
856                     switch (childAsTypedObject.getType()) {
857                         case UiChildMenu:
858                         case UiCheckedMenu:
859                             allObjects.put(childId, CreateObjectFromType(__GO_UIPARENTMENU__, childId));
860                             ((Container) ((SwingScilabTab) updatedObject.getValue()).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) allObjects.get(childId).getValue());
861                             break;
862                         default: /* UiParentMenu */
863                             ((Container) ((SwingScilabTab) updatedObject.getValue()).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) allObjects.get(childId).getValue());
864                             break;
865                     }
866                     needRevalidate = true;
867                 }
868             }
869         }
870
871         // Remove children which have been deleted
872         Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
873         Object[] updatedObjectChildren = updatedObject.getChildren().toArray();
874         for (int i = 0 ; i < updatedObjectChildren.length ; ++i) {
875             String childId = (String) updatedObjectChildren[i];
876             if (!newChildrenSet.contains(childId)) {
877
878                 // Remove the child
879                 updatedObject.removeChild(childId);
880
881                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
882
883                 if (childType == __GO_UIMENU__) {
884                     ((Container) ((SwingScilabTab) allObjects.get(id).getValue()).getMenuBar().getAsSimpleMenuBar()).remove((SwingScilabMenu) allObjects.get(childId).getValue());
885                     needRevalidate = true;
886                 }
887             }
888         }
889         if (needRevalidate && updatedComponent != null) {
890             updatedComponent.validate();
891         }
892
893     }
894
895     /**
896      * Update the children of a menu menus (called by generic updateObject method)
897      * @param id the id of the menu object
898      * @param newChildren the new children IDs list
899      */
900     private void updateMenuChildren(String id, String[] newChildren) {
901         TypedObject updatedObject = allObjects.get(id);
902         Container updatedComponent = null;
903         boolean needRevalidate = false;
904         int updatedObjectPosition = 0;
905         TypedObject newParent = null;
906
907         // Add new children
908         for (String childId : newChildren) {
909             int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
910             if (childType == __GO_UIMENU__) {
911                 if (!updatedObject.hasChild(childId)) {
912                     // Add the child
913                     updatedObject.addChild(childId);
914
915                     TypedObject childAsTypedObject = allObjects.get(childId);
916                     Object addedChild = allObjects.get(childId).getValue();
917                     JComponent parent = null;
918                     switch (updatedObject.getType()) {
919                         case UiChildMenu:
920                             updatedComponent = (SwingScilabMenuItem) updatedObject.getValue();
921                             parent = (JComponent) updatedComponent.getParent();
922                             switch (childAsTypedObject.getType()) {
923                                 case UiChildMenu:
924                                     /* Replace the item by a parent menu */
925                                     updatedObjectPosition = parent.getComponentZOrder((SwingScilabMenuItem) allObjects.get(id).getValue());
926                                     parent.remove((SwingScilabMenuItem) allObjects.get(id).getValue());
927                                     newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
928                                     allObjects.put(id, newParent);
929                                     newParent.addChild(childId);
930                                     parent.add((SwingScilabMenu) allObjects.get(id).getValue(), updatedObjectPosition);
931                                     /* Update the created menu */
932                                     ((SwingScilabMenu) allObjects.get(id).getValue()).add((SwingScilabMenuItem) addedChild);
933                                     break;
934                                 case UiCheckedMenu:
935                                     /* Replace the item by a parent menu */
936                                     updatedObjectPosition = parent.getComponentZOrder((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
937                                     parent.remove((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
938                                     newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
939                                     allObjects.put(id, newParent);
940                                     newParent.addChild(childId);
941                                     parent.add((SwingScilabMenu) allObjects.get(id).getValue(), updatedObjectPosition);
942                                     /* Update the created menu */
943                                     ((SwingScilabMenu) allObjects.get(id).getValue()).add((SwingScilabCheckBoxMenuItem) addedChild);
944                                     break;
945                                 default: /* UiParentMenu */
946                                     ((SwingScilabMenu) allObjects.get(id).getValue()).add((SwingScilabMenu) addedChild);
947                                     break;
948                             }
949                             break;
950                         case UiCheckedMenu:
951                             updatedComponent = (SwingScilabCheckBoxMenuItem) updatedObject.getValue();
952                             parent = (JComponent) updatedComponent.getParent();
953                             switch (childAsTypedObject.getType()) {
954                                 case UiChildMenu:
955                                     /* Replace the item by a parent menu */
956                                     updatedObjectPosition = parent.getComponentZOrder((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
957                                     parent.remove((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
958                                     newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
959                                     allObjects.put(id, newParent);
960                                     newParent.addChild(childId);
961                                     parent.add((SwingScilabMenu) allObjects.get(id).getValue(), updatedObjectPosition);
962                                     /* Update the created menu */
963                                     ((SwingScilabMenu) allObjects.get(id).getValue()).add((SwingScilabMenuItem) allObjects.get(childId).getValue());
964                                     break;
965                                 case UiCheckedMenu:
966                                     /* Replace the item by a parent menu */
967                                     updatedObjectPosition = parent.getComponentZOrder((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
968                                     parent.remove((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
969                                     newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
970                                     allObjects.put(id, newParent);
971                                     newParent.addChild(childId);
972                                     parent.add((SwingScilabMenu) allObjects.get(id).getValue(), updatedObjectPosition);
973                                     /* Update the created menu */
974                                     ((SwingScilabMenu) allObjects.get(id).getValue()).add((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
975                                     break;
976                                 default: /* UiParentMenu */
977                                     ((SwingScilabMenu) allObjects.get(id).getValue()).add((SwingScilabMenu) allObjects.get(childId).getValue());
978                                     break;
979                             }
980                             break;
981                         default: /* UiParentMenu */
982                             SwingScilabMenu updatedMenu = (SwingScilabMenu) updatedObject.getValue();
983                             updatedComponent = updatedMenu;
984                             switch (childAsTypedObject.getType()) {
985                                 case UiChildMenu:
986                                     updatedMenu.add((SwingScilabMenuItem) allObjects.get(childId).getValue());
987                                     break;
988                                 case UiCheckedMenu:
989                                     updatedMenu.add((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
990                                     break;
991                                 default: /* UiParentMenu */
992                                     updatedMenu.add((SwingScilabMenu) allObjects.get(childId).getValue());
993                                     break;
994                             }
995                             break;
996                     }
997                     needRevalidate = true;
998                 }
999             }
1000         }
1001         // Remove children which have been deleted
1002         Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
1003         String[] oldChildren = updatedObject.getChildren().toArray(new String[updatedObject.getChildren().size()]);
1004         for (int childIndex = 0; childIndex < oldChildren.length; childIndex++) {
1005             String childId = oldChildren[childIndex];
1006             if (!newChildrenSet.contains(childId)) {
1007                 // Remove the child
1008                 updatedObject.removeChild(childId);
1009                 updatedComponent = (SwingScilabMenu) updatedObject.getValue();
1010
1011                 TypedObject childAsTypedObject = allObjects.get(childId);
1012                 SwingScilabMenu updatedMenu = (SwingScilabMenu) allObjects.get(id).getValue();
1013                 Object removedMenu = allObjects.get(childId).getValue();
1014
1015                 switch (childAsTypedObject.getType()) {
1016                     case UiChildMenu:
1017                         updatedMenu.remove((SwingScilabMenuItem) removedMenu);
1018                         break;
1019                     case UiCheckedMenu:
1020                         updatedMenu.remove((SwingScilabCheckBoxMenuItem) removedMenu);
1021                         break;
1022                     default:
1023                         updatedMenu.remove((SwingScilabMenu) removedMenu);
1024                         break;
1025                 }
1026                 needRevalidate = true;
1027             }
1028         }
1029         if (needRevalidate && updatedComponent != null) {
1030             updatedComponent.validate();
1031         }
1032     }
1033     /**
1034      * Update the children of a contextmenu (called by generic updateObject method)
1035      * @param id the id of the contextmenu object
1036      * @param newChildren the new children IDs list
1037      */
1038     private void updateContextMenuChildren(String id, String[] newChildren) {
1039         TypedObject updatedObject = allObjects.get(id);
1040         Container updatedComponent = null;
1041         boolean needRevalidate = false;
1042
1043         // Add new children
1044         for (String childId : newChildren) {
1045             int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1046             if (childType == __GO_UIMENU__) {
1047                 if (!updatedObject.hasChild(childId)) {
1048
1049                     // Add the child
1050                     updatedObject.addChild(childId);
1051
1052                     TypedObject childAsTypedObject = allObjects.get(childId);
1053                     SwingScilabContextMenu updatedMenu = (SwingScilabContextMenu) updatedObject.getValue();
1054                     updatedComponent = updatedMenu;
1055                     switch (childAsTypedObject.getType()) {
1056                         case UiChildMenu:
1057                             updatedMenu.add((SwingScilabMenuItem) allObjects.get(childId).getValue());
1058                             break;
1059                         case UiCheckedMenu:
1060                             updatedMenu.add((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
1061                             break;
1062                         default: /* UiParentMenu */
1063                             updatedMenu.add((SwingScilabMenu) allObjects.get(childId).getValue());
1064                             break;
1065                     }
1066                     needRevalidate = true;
1067                 }
1068             }
1069         }
1070         // Remove children which have been deleted
1071         Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
1072         for (String childId : updatedObject.getChildren()) {
1073             if (!newChildrenSet.contains(childId)) {
1074                 // Remove the child
1075                 updatedObject.removeChild(childId);
1076                 updatedComponent = (SwingScilabMenu) updatedObject.getValue();
1077
1078                 TypedObject childAsTypedObject = allObjects.get(childId);
1079                 SwingScilabContextMenu updatedMenu = (SwingScilabContextMenu) allObjects.get(id).getValue();
1080                 Object removedMenu = allObjects.get(childId).getValue();
1081
1082                 switch (childAsTypedObject.getType()) {
1083                     case UiChildMenu:
1084                         updatedMenu.remove((SwingScilabMenuItem) removedMenu);
1085                         break;
1086                     case UiCheckedMenu:
1087                         updatedMenu.remove((SwingScilabCheckBoxMenuItem) removedMenu);
1088                         break;
1089                     default:
1090                         updatedMenu.remove((SwingScilabMenu) removedMenu);
1091                         break;
1092                 }
1093                 needRevalidate = true;
1094             }
1095         }
1096         if (needRevalidate && updatedComponent != null) {
1097             updatedComponent.validate();
1098         }
1099     }
1100 }