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