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