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