5e855217b184346189178b54156d971c7fd5c8a4
[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_FONTSIZE__;
40 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTUNITS__;
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                         SwingScilabMenu meAsAMenu = (SwingScilabMenu) updatedObject.getValue();
645                         Container parent = meAsAMenu.getParent();
646                         parent.remove(meAsAMenu);
647                         allObjects.put(id, CreateObjectFromType(__GO_UICHECKEDMENU__, id));
648                         SwingScilabCheckBoxMenuItem meAsAMenuItem = (SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue();
649                         parent.add(meAsAMenuItem);
650                         registeredObject = allObjects.get(id);
651                         break;
652                     case UiChildMenu:
653                         String parentId = (String) GraphicController.getController().getProperty(id, __GO_PARENT__);
654                         int index = ((SwingScilabMenu) allObjects.get(parentId).getValue())
655                                     .getComponentZOrder((SwingScilabMenuItem) allObjects.get(id).getValue());
656                         ((SwingScilabMenu) allObjects.get(parentId).getValue()).remove((SwingScilabMenuItem) allObjects.get(id).getValue());
657                         allObjects.put(id, CreateObjectFromType(__GO_UICHECKEDMENU__, id));
658                         registeredObject = allObjects.get(id);
659                         ((SwingScilabMenu) allObjects.get(parentId).getValue()).add((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue(), index);
660                         return;
661                     default:
662                         break;
663                 }
664             }
665         }
666
667         /*
668          * When the SEPARATOR property is updated for a UIMENU,
669          * When the property is set to TRUE: A separator is added if not already done
670          * When the property is set to FALSE: The previous separator is removed if it exists
671          */
672         if (registeredObject != null && property == __GO_UI_SEPARATOR__) {
673             if (type == __GO_UIMENU__) {
674                 String parentId = (String) GraphicController.getController().getProperty(id, __GO_PARENT__);
675                 int menuPosition = -1;
676                 Component[] allChildren =  ((SwingScilabMenu) allObjects.get(parentId).getValue()).getMenuComponents();
677                 for (int kChild = 0; kChild < allChildren.length; kChild++) {
678                     if (allChildren[kChild] == (Component) allObjects.get(id).getValue()) {
679                         menuPosition = kChild;
680                         break;
681                     }
682                 }
683                 if (menuPosition < 0) {
684                     return;
685                 }
686                 boolean newSeparatorMode = (Boolean) GraphicController.getController().getProperty(id, __GO_UI_SEPARATOR__);
687                 if (newSeparatorMode) { // Add a separator above the menu
688                     ((SwingScilabMenu) allObjects.get(parentId).getValue()).insertSeparator(menuPosition);
689                 } else { // Remove the separator above the menu (if there is one)
690                     if (menuPosition == 0) { // There is no object above the menu
691                         return;
692                     }
693                     if (((SwingScilabMenu) allObjects.get(parentId).getValue()).getComponent(menuPosition - 1) instanceof JSeparator) {
694                         ((SwingScilabMenu) allObjects.get(parentId).getValue()).remove(menuPosition - 1);
695                     }
696                 }
697             }
698         }
699
700         if (registeredObject != null) {
701             SwingViewObject swingObject = registeredObject.getValue();
702             if (swingObject != null) {
703                 swingObject.update(property, GraphicController.getController().getProperty(id, property));
704             }
705         }
706     }
707
708     /**
709      * Update the figure children (called by generic updateObject method)
710      * @param id the id of the figure
711      * @param newChildren the new children IDs list
712      */
713     private void updateFigureChildren(String id, String[] newChildren) {
714         TypedObject updatedObject = allObjects.get(id);
715         Container updatedComponent = (SwingScilabTab) updatedObject.getValue();
716         boolean needRevalidate = false;
717
718         // Add new children
719         for (String childId : newChildren) {
720             if (!updatedObject.hasChild(childId)) {
721
722                 // Add the child
723                 updatedObject.addChild(childId);
724
725                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
726
727                 /* Add an uicontrol */
728                 if (childType == __GO_UICONTROL__) {
729                     ((SwingScilabTab) updatedComponent).addMember(allObjects.get(childId).getValue());
730                     needRevalidate = true;
731                 }
732
733                 /* Add an uimenu */
734                 if (childType == __GO_UIMENU__) {
735                     TypedObject childAsTypedObject = allObjects.get(childId);
736                     switch (childAsTypedObject.getType()) {
737                         case UiChildMenu:
738                         case UiCheckedMenu:
739                             allObjects.remove(childId);
740                             allObjects.put(childId, CreateObjectFromType(__GO_UIPARENTMENU__, childId));
741                             ((Container) ((SwingScilabTab) updatedComponent).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) allObjects.get(childId).getValue());
742                             break;
743                         default: /* UiParentMenu */
744                             ((Container) ((SwingScilabTab) updatedComponent).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) allObjects.get(childId).getValue());
745                             break;
746                     }
747                     needRevalidate = true;
748                 }
749             }
750         }
751
752         // Remove children which have been deleted
753         Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
754         // Clone the children set to avoid concurrent accesses
755         String[] oldChildrenSet = updatedObject.getChildren().toArray(new String[updatedObject.getChildren().size()]);
756         for (String childId : oldChildrenSet) {
757             if (!newChildrenSet.contains(childId)) {
758
759                 // Remove the child
760                 updatedObject.removeChild(childId);
761
762                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
763
764                 /* Remove an uicontrol */
765                 if (childType == __GO_UICONTROL__) {
766                     ((SwingScilabTab) updatedComponent).removeMember(allObjects.get(childId).getValue());
767                     needRevalidate = true;
768                 }
769
770                 /* Remove an uimenu */
771                 if (childType == __GO_UIMENU__) {
772                     TypedObject childAsTypedObject = allObjects.get(childId);
773                     switch (childAsTypedObject.getType()) {
774                         case UiCheckedMenu:
775                             ((Container) ((SwingScilabTab) updatedComponent).getMenuBar().getAsSimpleMenuBar()).remove((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
776                             break;
777                         default: /* UiParentMenu */
778                             ((Container) ((SwingScilabTab) updatedComponent).getMenuBar().getAsSimpleMenuBar()).remove((SwingScilabMenu) allObjects.get(childId).getValue());
779                             break;
780                     }
781                     needRevalidate = true;
782                 }
783             }
784         }
785         if (needRevalidate && updatedComponent != null) {
786             ((View) updatedComponent).revalidate();
787         }
788     }
789
790     /**
791      * Update the frame children (called by generic updateObject method)
792      * @param id the id of the figure
793      * @param newChildren the new children IDs list
794      */
795     private void updateFrameChildren(String id, String[] newChildren) {
796         TypedObject updatedObject = allObjects.get(id);
797         Container updatedComponent = (SwingScilabFrame) updatedObject.getValue();
798         boolean needRevalidate = false;
799
800         // Add new children
801         for (String childId : newChildren) {
802             if (!updatedObject.hasChild(childId)) {
803
804                 // Add the child
805                 updatedObject.addChild(childId);
806
807                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
808
809                 /* Add an uicontrol */
810                 if (childType == __GO_UICONTROL__) {
811                     ((SwingScilabFrame) updatedComponent).addMember(allObjects.get(childId).getValue());
812                     needRevalidate = true;
813                 }
814             }
815         }
816
817         // Remove children which have been deleted
818         Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
819         // Clone the children set to avoid concurrent accesses
820         String[] oldChildrenSet = updatedObject.getChildren().toArray(new String[updatedObject.getChildren().size()]);
821         for (String childId : oldChildrenSet) {
822             if (!newChildrenSet.contains(childId)) {
823
824                 // Remove the child
825                 updatedObject.removeChild(childId);
826
827                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
828
829                 /* Remove an uicontrol */
830                 if (childType == __GO_UICONTROL__) {
831                     updatedComponent.remove((Component) allObjects.get(childId).getValue());
832                     needRevalidate = true;
833                 }
834             }
835         }
836         if (needRevalidate && updatedComponent != null) {
837             ((JPanel) updatedComponent).revalidate();
838         }
839     }
840
841     /**
842      * Update the Console menus (called by generic updateObject method)
843      * @param id the id of the console object
844      * @param newChildren the new children IDs list
845      */
846     private void updateConsoleChildren(String id, String[] newChildren) {
847         TypedObject updatedObject = allObjects.get(id);
848         Container updatedComponent = (SwingScilabTab) updatedObject.getValue();
849         boolean needRevalidate = false;
850
851         // Add new children
852         for (String childId : newChildren) {
853             if (!updatedObject.hasChild(childId)) {
854
855                 // Add the child
856                 updatedObject.addChild(childId);
857
858                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
859
860                 if (childType == __GO_UIMENU__) {
861                     TypedObject childAsTypedObject = allObjects.get(childId);
862                     switch (childAsTypedObject.getType()) {
863                         case UiChildMenu:
864                         case UiCheckedMenu:
865                             allObjects.put(childId, CreateObjectFromType(__GO_UIPARENTMENU__, childId));
866                             ((Container) ((SwingScilabTab) updatedObject.getValue()).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) allObjects.get(childId).getValue());
867                             break;
868                         default: /* UiParentMenu */
869                             ((Container) ((SwingScilabTab) updatedObject.getValue()).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) allObjects.get(childId).getValue());
870                             break;
871                     }
872                     needRevalidate = true;
873                 }
874             }
875         }
876
877         // Remove children which have been deleted
878         Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
879         Object[] updatedObjectChildren = updatedObject.getChildren().toArray();
880         for (int i = 0 ; i < updatedObjectChildren.length ; ++i) {
881             String childId = (String) updatedObjectChildren[i];
882             if (!newChildrenSet.contains(childId)) {
883
884                 // Remove the child
885                 updatedObject.removeChild(childId);
886
887                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
888
889                 if (childType == __GO_UIMENU__) {
890                     ((Container) ((SwingScilabTab) allObjects.get(id).getValue()).getMenuBar().getAsSimpleMenuBar()).remove((SwingScilabMenu) allObjects.get(childId).getValue());
891                     needRevalidate = true;
892                 }
893             }
894         }
895         if (needRevalidate && updatedComponent != null) {
896             updatedComponent.validate();
897         }
898
899     }
900
901     /**
902      * Update the children of a menu menus (called by generic updateObject method)
903      * @param id the id of the menu object
904      * @param newChildren the new children IDs list
905      */
906     private void updateMenuChildren(String id, String[] newChildren) {
907         TypedObject updatedObject = allObjects.get(id);
908         Container updatedComponent = null;
909         boolean needRevalidate = false;
910         int updatedObjectPosition = 0;
911         TypedObject newParent = null;
912
913         // Add new children
914         for (String childId : newChildren) {
915             int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
916             if (childType == __GO_UIMENU__) {
917                 if (!updatedObject.hasChild(childId)) {
918                     // Add the child
919                     updatedObject.addChild(childId);
920
921                     TypedObject childAsTypedObject = allObjects.get(childId);
922                     Object addedChild = allObjects.get(childId).getValue();
923                     JComponent parent = null;
924                     switch (updatedObject.getType()) {
925                         case UiChildMenu:
926                             updatedComponent = (SwingScilabMenuItem) updatedObject.getValue();
927                             parent = (JComponent) updatedComponent.getParent();
928                             switch (childAsTypedObject.getType()) {
929                                 case UiChildMenu:
930                                     /* Replace the item by a parent menu */
931                                     updatedObjectPosition = parent.getComponentZOrder((SwingScilabMenuItem) allObjects.get(id).getValue());
932                                     parent.remove((SwingScilabMenuItem) allObjects.get(id).getValue());
933                                     newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
934                                     allObjects.put(id, newParent);
935                                     newParent.addChild(childId);
936                                     parent.add((SwingScilabMenu) allObjects.get(id).getValue(), updatedObjectPosition);
937                                     /* Update the created menu */
938                                     ((SwingScilabMenu) allObjects.get(id).getValue()).add((SwingScilabMenuItem) addedChild);
939                                     break;
940                                 case UiCheckedMenu:
941                                     /* Replace the item by a parent menu */
942                                     updatedObjectPosition = parent.getComponentZOrder((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
943                                     parent.remove((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
944                                     newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
945                                     allObjects.put(id, newParent);
946                                     newParent.addChild(childId);
947                                     parent.add((SwingScilabMenu) allObjects.get(id).getValue(), updatedObjectPosition);
948                                     /* Update the created menu */
949                                     ((SwingScilabMenu) allObjects.get(id).getValue()).add((SwingScilabCheckBoxMenuItem) addedChild);
950                                     break;
951                                 default: /* UiParentMenu */
952                                     ((SwingScilabMenu) allObjects.get(id).getValue()).add((SwingScilabMenu) addedChild);
953                                     break;
954                             }
955                             break;
956                         case UiCheckedMenu:
957                             updatedComponent = (SwingScilabCheckBoxMenuItem) updatedObject.getValue();
958                             parent = (JComponent) updatedComponent.getParent();
959                             switch (childAsTypedObject.getType()) {
960                                 case UiChildMenu:
961                                     /* Replace the item by a parent menu */
962                                     updatedObjectPosition = parent.getComponentZOrder((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
963                                     parent.remove((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
964                                     newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
965                                     allObjects.put(id, newParent);
966                                     newParent.addChild(childId);
967                                     parent.add((SwingScilabMenu) allObjects.get(id).getValue(), updatedObjectPosition);
968                                     /* Update the created menu */
969                                     ((SwingScilabMenu) allObjects.get(id).getValue()).add((SwingScilabMenuItem) allObjects.get(childId).getValue());
970                                     break;
971                                 case UiCheckedMenu:
972                                     /* Replace the item by a parent menu */
973                                     updatedObjectPosition = parent.getComponentZOrder((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
974                                     parent.remove((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
975                                     newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
976                                     allObjects.put(id, newParent);
977                                     newParent.addChild(childId);
978                                     parent.add((SwingScilabMenu) allObjects.get(id).getValue(), updatedObjectPosition);
979                                     /* Update the created menu */
980                                     ((SwingScilabMenu) allObjects.get(id).getValue()).add((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
981                                     break;
982                                 default: /* UiParentMenu */
983                                     System.out.println("childAsTypedObject.getType() = UiParentMenu");
984                                     ((SwingScilabMenu) allObjects.get(id).getValue()).add((SwingScilabMenu) allObjects.get(childId).getValue());
985                                     break;
986                             }
987                             break;
988                         default: /* UiParentMenu */
989                             SwingScilabMenu updatedMenu = (SwingScilabMenu) updatedObject.getValue();
990                             updatedComponent = updatedMenu;
991                             switch (childAsTypedObject.getType()) {
992                                 case UiChildMenu:
993                                     updatedMenu.add((SwingScilabMenuItem) allObjects.get(childId).getValue());
994                                     break;
995                                 case UiCheckedMenu:
996                                     updatedMenu.add((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
997                                     break;
998                                 default: /* UiParentMenu */
999                                     /* Java can not add a JMenu in a JMenu */
1000                                     /* We need to convert the child into a MenuItem */
1001                                     allObjects.put(childId, CreateObjectFromType(__GO_UIMENU__, childId));
1002                                     updatedMenu.add((SwingScilabMenuItem) allObjects.get(childId).getValue());
1003                                     break;
1004                             }
1005                             break;
1006                     }
1007                     needRevalidate = true;
1008                 }
1009             }
1010         }
1011         // Remove children which have been deleted
1012         Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
1013         String[] oldChildren = updatedObject.getChildren().toArray(new String[updatedObject.getChildren().size()]);
1014         for (int childIndex = 0; childIndex < oldChildren.length; childIndex++) {
1015             String childId = oldChildren[childIndex];
1016             if (!newChildrenSet.contains(childId)) {
1017                 // Remove the child
1018                 updatedObject.removeChild(childId);
1019                 updatedComponent = (SwingScilabMenu) updatedObject.getValue();
1020
1021                 TypedObject childAsTypedObject = allObjects.get(childId);
1022                 SwingScilabMenu updatedMenu = (SwingScilabMenu) allObjects.get(id).getValue();
1023                 Object removedMenu = allObjects.get(childId).getValue();
1024
1025                 switch (childAsTypedObject.getType()) {
1026                     case UiChildMenu:
1027                         updatedMenu.remove((SwingScilabMenuItem) removedMenu);
1028                         break;
1029                     case UiCheckedMenu:
1030                         updatedMenu.remove((SwingScilabCheckBoxMenuItem) removedMenu);
1031                         break;
1032                     default:
1033                         updatedMenu.remove((SwingScilabMenu) removedMenu);
1034                         break;
1035                 }
1036                 needRevalidate = true;
1037             }
1038         }
1039         if (needRevalidate && updatedComponent != null) {
1040             updatedComponent.validate();
1041         }
1042     }
1043     /**
1044      * Update the children of a contextmenu (called by generic updateObject method)
1045      * @param id the id of the contextmenu object
1046      * @param newChildren the new children IDs list
1047      */
1048     private void updateContextMenuChildren(String id, String[] newChildren) {
1049         TypedObject updatedObject = allObjects.get(id);
1050         Container updatedComponent = null;
1051         boolean needRevalidate = false;
1052
1053         // Add new children
1054         for (String childId : newChildren) {
1055             int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1056             if (childType == __GO_UIMENU__) {
1057                 if (!updatedObject.hasChild(childId)) {
1058
1059                     // Add the child
1060                     updatedObject.addChild(childId);
1061
1062                     TypedObject childAsTypedObject = allObjects.get(childId);
1063                     SwingScilabContextMenu updatedMenu = (SwingScilabContextMenu) updatedObject.getValue();
1064                     updatedComponent = updatedMenu;
1065                     switch (childAsTypedObject.getType()) {
1066                         case UiChildMenu:
1067                             updatedMenu.add((SwingScilabMenuItem) allObjects.get(childId).getValue());
1068                             break;
1069                         case UiCheckedMenu:
1070                             updatedMenu.add((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
1071                             break;
1072                         default: /* UiParentMenu */
1073                             updatedMenu.add((SwingScilabMenu) allObjects.get(childId).getValue());
1074                             break;
1075                     }
1076                     needRevalidate = true;
1077                 }
1078             }
1079         }
1080         // Remove children which have been deleted
1081         Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
1082         for (String childId : updatedObject.getChildren()) {
1083             if (!newChildrenSet.contains(childId)) {
1084                 // Remove the child
1085                 updatedObject.removeChild(childId);
1086                 updatedComponent = (SwingScilabMenu) updatedObject.getValue();
1087
1088                 TypedObject childAsTypedObject = allObjects.get(childId);
1089                 SwingScilabContextMenu updatedMenu = (SwingScilabContextMenu) allObjects.get(id).getValue();
1090                 Object removedMenu = allObjects.get(childId).getValue();
1091
1092                 switch (childAsTypedObject.getType()) {
1093                     case UiChildMenu:
1094                         updatedMenu.remove((SwingScilabMenuItem) removedMenu);
1095                         break;
1096                     case UiCheckedMenu:
1097                         updatedMenu.remove((SwingScilabCheckBoxMenuItem) removedMenu);
1098                         break;
1099                     default:
1100                         updatedMenu.remove((SwingScilabMenu) removedMenu);
1101                         break;
1102                 }
1103                 needRevalidate = true;
1104             }
1105         }
1106         if (needRevalidate && updatedComponent != null) {
1107             updatedComponent.validate();
1108         }
1109     }
1110 }