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