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