d3f79ac2c5359ef41ea05e52f5bff881d2c45da5
[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                 }
430                 tab.setId(id);
431
432                 /* MENUBAR */
433                 MenuBar menuBar = ScilabMenuBar.createMenuBar();
434                 menuBar.setVisible(figure.getMenubarVisible());
435                 tab.setMenuBar(menuBar);
436                 window.addMenuBar(menuBar);
437
438                 /* TOOLBAR */
439                 ToolBar toolbar = ScilabToolBar.createToolBar();
440                 toolbar.setVisible(figure.getToolbarVisible());
441                 tab.setToolBar(toolbar);
442                 window.addToolBar(toolbar);
443
444                 /* INFOBAR */
445                 TextBox infoBar = ScilabTextBox.createTextBox();
446                 infoBar.setVisible(figure.getInfobarVisible());
447                 tab.setInfoBar(infoBar);
448                 window.addInfoBar(infoBar);
449
450                 if (figure.getDockable()) {
451                     DockingManager.dock((SwingScilabDockablePanel) tab, window.getDockingPort());
452                     ActiveDockableTracker.requestDockableActivation((SwingScilabDockablePanel) tab);
453                 } else {
454                     window.addTab(tab);
455                 }
456
457                 tab.setWindowIcon("graphic-window");
458
459                 tab.setParentWindowId(window.getId());
460
461                 tab.setEventHandler(figure.getEventHandlerString());
462                 tab.setEventHandlerEnabled(figure.getEventHandlerEnable());
463                 tab.setVisible(true);
464                 tab.setName(figureTitle);
465
466                 String infoMessage = figure.getInfoMessage();
467                 if ((infoMessage == null) || (infoMessage.length() == 0)) {
468                     infoBar.setText("");
469                 } else {
470                     infoBar.setText(infoMessage);
471                 }
472
473                 tab.update(__GO_POSITION__, GraphicController.getController().getProperty(id, __GO_POSITION__));
474
475                 String icon = (String)GraphicController.getController().getProperty(id, __GO_UI_ICON__);
476                 if (icon != null && icon.equals("") == false) {
477                     tab.update(__GO_UI_ICON__, icon);
478                 }
479
480                 // TODO set other default properties
481
482                 window.setVisible(figure.getVisible());
483                 window.pack();
484                 return tab;
485             }
486             case Axes:
487                 SwingScilabAxes axes = new SwingScilabAxes();
488                 return axes;
489             case Frame: {
490                 Boolean scrollable = (Boolean) GraphicController.getController().getProperty(id, __GO_UI_SCROLLABLE__);
491                 if (scrollable) {
492                     SwingScilabScrollableFrame frame = new SwingScilabScrollableFrame();
493                     frame.setId(id);
494                     setDefaultProperties(frame, id);
495                     Integer borderId = (Integer) GraphicController.getController().getProperty(id, __GO_UI_FRAME_BORDER__);
496                     if (borderId != 0) {
497                         frame.setBorder(BorderConvertor.getBorder(borderId));
498                     }
499                     return frame;
500                 } else {
501                     SwingScilabFrame frame = new SwingScilabFrame();
502                     frame.setId(id);
503                     setDefaultProperties(frame, id);
504                     Integer borderId = (Integer) GraphicController.getController().getProperty(id, __GO_UI_FRAME_BORDER__);
505                     if (borderId != 0) {
506                         frame.setBorder(BorderConvertor.getBorder(borderId));
507                     }
508                     return frame;
509                 }
510             }
511             case Image:
512                 SwingScilabUiImage image = new SwingScilabUiImage();
513                 image.setId(id);
514                 setDefaultProperties(image, id);
515                 return image;
516             case ListBox:
517                 SwingScilabListBox listBox = new SwingScilabListBox();
518                 listBox.setId(id);
519                 setDefaultProperties(listBox, id);
520                 return listBox;
521             case PopupMenu:
522                 SwingScilabPopupMenu popupMenu = new SwingScilabPopupMenu();
523                 popupMenu.setId(id);
524                 setDefaultProperties(popupMenu, id);
525                 return popupMenu;
526             case Progressbar:
527                 SwingScilabWaitBar progressbar = new SwingScilabWaitBar();
528                 progressbar.setIndeterminateMode(true);
529                 progressbar.setId(id);
530                 return progressbar;
531             case PushButton: {
532                 SwingScilabPushButton pushButton = new SwingScilabPushButton();
533                 pushButton.setId(id);
534                 setDefaultProperties(pushButton, id);
535                 String icon = (String)GraphicController.getController().getProperty(id, __GO_UI_ICON__);
536                 if (icon != null && icon.equals("") == false) {
537                     pushButton.update(__GO_UI_ICON__, icon);
538                 }
539
540                 return pushButton;
541             }
542             case RadioButton:
543                 SwingScilabRadioButton radioButton = new SwingScilabRadioButton();
544                 radioButton.setId(id);
545                 setDefaultProperties(radioButton, id);
546                 return radioButton;
547             case Slider:
548                 if (Console.getConsole().getUseDeprecatedLF()) {
549                     SwingScilabScroll slider = new SwingScilabScroll();
550                     slider.setId(id);
551                     setDefaultProperties(slider, id);
552                     return slider;
553                 } else {
554                     SwingScilabSlider slider = new SwingScilabSlider();
555                     slider.setId(id);
556                     setDefaultProperties(slider, id);
557                     return slider;
558                 }
559             case Table:
560                 SwingScilabUiTable table = new SwingScilabUiTable();
561                 table.setId(id);
562                 setDefaultProperties(table, id);
563                 return table;
564             case Text: {
565                 SwingScilabLabel text = new SwingScilabLabel();
566                 text.setId(id);
567                 setDefaultProperties(text, id);
568                 String icon = (String)GraphicController.getController().getProperty(id, __GO_UI_ICON__);
569                 if (icon != null && icon.equals("") == false) {
570                     text.update(__GO_UI_ICON__, icon);
571                 }
572                 return text;
573             }
574             case Uimenu:
575                 throw new UnsupportedOperationException();
576             case UiParentMenu: /* SwingView internal type */
577                 SwingScilabMenu parentMenu = new SwingScilabMenu();
578                 parentMenu.setId(id);
579                 setMenuDefaultProperties(parentMenu, id);
580                 return parentMenu;
581             case UiChildMenu: /* SwingView internal type */
582                 SwingScilabMenuItem childMenu = new SwingScilabMenuItem();
583                 childMenu.setId(id);
584                 setMenuDefaultProperties(childMenu, id);
585                 return childMenu;
586             case UiCheckedMenu: /* SwingView internal type */
587                 SwingScilabCheckBoxMenuItem checkedMenu = new SwingScilabCheckBoxMenuItem(false);
588                 checkedMenu.setId(id);
589                 setMenuDefaultProperties(checkedMenu, id);
590                 return checkedMenu;
591             case UiContextMenu:
592                 SwingScilabContextMenu contextMenu = new SwingScilabContextMenu();
593                 contextMenu.setId(id);
594                 return contextMenu;
595             case Waitbar:
596                 SwingScilabWaitBar waitbar = new SwingScilabWaitBar();
597                 waitbar.setIndeterminateMode(false);
598                 waitbar.setId(id);
599                 return waitbar;
600             case Tab:
601                 SwingScilabTabGroup TabGroup = new SwingScilabTabGroup();
602                 TabGroup.setId(id);
603                 //setDefaultProperties(TabGroup, id);
604                 //TabGroup.update(property, value);
605                 return TabGroup;
606             case Layer:
607                 SwingScilabLayer layer = new SwingScilabLayer();
608                 layer.setId(id);
609                 return layer;
610             default:
611                 System.err.println("[SwingView] Unmanaged Creation type !!!");
612                 return null;
613         }
614     }
615
616     /**
617      * Initialize all poperties according to model
618      * @param uiMenuObject the uimenu
619      * @param id the uimenu id
620      */
621     private void setMenuDefaultProperties(Widget uiMenuObject, Integer id) {
622         SwingViewMenu.update(uiMenuObject, __GO_CHILDREN__, GraphicController.getController().getProperty(id, __GO_CHILDREN__));
623         SwingViewMenu.update(uiMenuObject, __GO_CALLBACK__, GraphicController.getController().getProperty(id, __GO_CALLBACK__));
624         SwingViewMenu.update(uiMenuObject, __GO_CALLBACKTYPE__, GraphicController.getController().getProperty(id, __GO_CALLBACKTYPE__));
625         SwingViewMenu.update(uiMenuObject, __GO_UI_CHECKED__, GraphicController.getController().getProperty(id, __GO_UI_CHECKED__));
626         SwingViewMenu.update(uiMenuObject, __GO_UI_ENABLE__, GraphicController.getController().getProperty(id, __GO_UI_ENABLE__));
627         SwingViewMenu.update(uiMenuObject, __GO_UI_FOREGROUNDCOLOR__, GraphicController.getController().getProperty(id, __GO_UI_FOREGROUNDCOLOR__));
628         SwingViewMenu.update(uiMenuObject, __GO_UI_LABEL__, GraphicController.getController().getProperty(id, __GO_UI_LABEL__));
629         SwingViewMenu.update(uiMenuObject, __GO_UI_ICON__, GraphicController.getController().getProperty(id, __GO_UI_ICON__));
630     }
631
632     /**
633      * Initialize all poperties according to model
634      * @param uiControlObject the uicontrol
635      * @param id the uicontrol id
636      */
637     private void setDefaultProperties(SwingViewObject uiControlObject, Integer id) {
638
639         /*
640          * Visible property is set first to avoid to see the object rendered
641          * before all its properties to be set (See bug #10346)
642          */
643         uiControlObject.update(__GO_VISIBLE__, GraphicController.getController().getProperty(id, __GO_VISIBLE__));
644         uiControlObject.update(__GO_UI_BACKGROUNDCOLOR__, GraphicController.getController().getProperty(id, __GO_UI_BACKGROUNDCOLOR__));
645         uiControlObject.update(__GO_UI_ENABLE__, GraphicController.getController().getProperty(id, __GO_UI_ENABLE__));
646         uiControlObject.update(__GO_UI_FONTANGLE__, GraphicController.getController().getProperty(id, __GO_UI_FONTANGLE__));
647         uiControlObject.update(__GO_UI_FONTNAME__, GraphicController.getController().getProperty(id, __GO_UI_FONTNAME__));
648         uiControlObject.update(__GO_UI_FONTUNITS__, GraphicController.getController().getProperty(id, __GO_UI_FONTUNITS__));
649         uiControlObject.update(__GO_UI_FONTSIZE__, GraphicController.getController().getProperty(id, __GO_UI_FONTSIZE__));
650         uiControlObject.update(__GO_UI_FONTWEIGHT__, GraphicController.getController().getProperty(id, __GO_UI_FONTWEIGHT__));
651         uiControlObject.update(__GO_UI_FOREGROUNDCOLOR__, GraphicController.getController().getProperty(id, __GO_UI_FOREGROUNDCOLOR__));
652         uiControlObject.update(__GO_UI_HORIZONTALALIGNMENT__, GraphicController.getController().getProperty(id, __GO_UI_HORIZONTALALIGNMENT__));
653         uiControlObject.update(__GO_UI_RELIEF__, GraphicController.getController().getProperty(id, __GO_UI_RELIEF__));
654         uiControlObject.update(__GO_UI_STRING__, GraphicController.getController().getProperty(id, __GO_UI_STRING__));
655         uiControlObject.update(__GO_UI_VERTICALALIGNMENT__, GraphicController.getController().getProperty(id, __GO_UI_VERTICALALIGNMENT__));
656         uiControlObject.update(__GO_POSITION__, GraphicController.getController().getProperty(id, __GO_POSITION__));
657         uiControlObject.update(__GO_LAYOUT__, GraphicController.getController().getProperty(id, __GO_LAYOUT__));
658         uiControlObject.update(__GO_UI_MIN__, GraphicController.getController().getProperty(id, __GO_UI_MIN__));
659         uiControlObject.update(__GO_UI_MAX__, GraphicController.getController().getProperty(id, __GO_UI_MAX__));
660     }
661
662     public void deleteObject(Integer id) {
663         final TypedObject requestedObject = allObjects.get(id);
664         if (requestedObject != null) {
665             switch (requestedObject.getType()) {
666                 case Figure:
667                     if (SwingUtilities.isEventDispatchThread()) {
668                         final SwingScilabPanel tab = (SwingScilabPanel) requestedObject.getValue();
669                         if (requestedObject.getValue() instanceof SwingScilabDockablePanel) {
670                             final SwingScilabDockablePanel dockableTab = (SwingScilabDockablePanel) tab;
671                             dockableTab.disablePaint();
672                             DockingManager.close(dockableTab);
673                             DockingManager.unregisterDockable((Dockable) dockableTab);
674                             ClosingOperationsManager.unregisterClosingOperation(dockableTab);
675                             ClosingOperationsManager.removeDependency(dockableTab);
676                             ClosingOperationsManager.checkTabForClosing(dockableTab);
677                             tab.close();
678                         } else {
679                             tab.close();
680                         }
681                     } else {
682                         try {
683                             SwingUtilities.invokeAndWait(new Runnable() {
684
685                                 @Override
686                                 public void run() {
687                                     final SwingScilabPanel tab = (SwingScilabPanel) requestedObject.getValue();
688                                     if (tab instanceof SwingScilabDockablePanel) {
689                                         final SwingScilabDockablePanel dockableTab = (SwingScilabDockablePanel) tab;
690                                         dockableTab.disablePaint();
691                                         DockingManager.close(dockableTab);
692                                         DockingManager.unregisterDockable((Dockable) dockableTab);
693                                         ClosingOperationsManager.unregisterClosingOperation(dockableTab);
694                                         ClosingOperationsManager.removeDependency(dockableTab);
695                                         ClosingOperationsManager.checkTabForClosing(dockableTab);
696                                         tab.close();
697                                     } else {
698                                         tab.close();
699                                     }
700                                 }
701
702                             });
703                         } catch (InterruptedException e) {
704                             // TODO Auto-generated catch block
705                             e.printStackTrace();
706                         } catch (InvocationTargetException e) {
707                             // TODO Auto-generated catch block
708                             e.printStackTrace();
709                         }
710                     }
711
712                     //try close the Property List (GED)
713                     try {
714                         Inspector.getInspector().close();
715                     } catch (NullPointerException e) {
716                     }
717                     break;
718                 case Progressbar:
719                 case Waitbar:
720                     SwingScilabWaitBar bar = (SwingScilabWaitBar) requestedObject.getValue();
721                     bar.close();
722                     break;
723                 default:
724                     // Nothing to do
725                     // uicontrol case: the object is destroyed when its parent updates its children
726                     break;
727             }
728             allObjects.remove(id);
729         }
730     }
731
732     @Override
733     public void updateObject(final Integer id, final int property) {
734         final TypedObject registeredObject = allObjects.get(id);
735
736         if (property == __GO_SIZE__ && (headless || GraphicsEnvironment.isHeadless())) {
737             int objectType = (Integer) GraphicController.getController().getProperty(id, __GO_TYPE__);
738             if (objectType == __GO_FIGURE__) {
739                 Integer[] dims = (Integer[]) GraphicController.getController().getProperty(id, __GO_SIZE__);
740                 GraphicController.getController().setProperty(id, __GO_AXES_SIZE__, dims);
741             }
742         }
743
744         if (registeredObject != null && property == __GO_VALID__ && ((Boolean) GraphicController.getController().getProperty(id, __GO_VALID__))) {
745             if (registeredObject.getValue() instanceof SwingScilabDockablePanel) {
746                 final Runnable r = new Runnable() {
747                     @Override
748                     public void run() {
749                         ((SwingScilabDockablePanel) registeredObject.getValue()).getParentWindow().setVisible(true);
750                         ((SwingScilabDockablePanel) registeredObject.getValue()).setVisible(true);
751                         Integer[] figureSize = (Integer[]) GraphicController.getController().getProperty(id, __GO_SIZE__);
752                         ((SwingScilabDockablePanel) registeredObject.getValue()).getParentWindow().setDims(new Size(figureSize[0], figureSize[1]));
753                     }
754                 };
755
756                 if (SwingUtilities.isEventDispatchThread()) {
757                     r.run();
758                 } else {
759                     SwingUtilities.invokeLater(r);
760                 }
761             }
762         }
763
764         if (property == __GO_IMMEDIATE_DRAWING__) {
765             return;
766         }
767
768         if (registeredObject == null && property != __GO_STYLE__) {
769             return;
770         }
771
772         /* On uicontrol style is set after object creation */
773         if (registeredObject == null && property == __GO_STYLE__) {
774             int style = (Integer) GraphicController.getController().getProperty(id, __GO_STYLE__);
775             allObjects.put(id, CreateObjectFromType(style, id));
776             return;
777         }
778
779         /* Do not update axes on EDT to avoid dead locks between SwingView & DrawerVisitor */
780         /* Test which freezes without this condition: bug_1257.tst in graphics */
781         int type = (Integer) GraphicController.getController().getProperty(id, __GO_TYPE__);
782         if (type == __GO_AXES__) {
783             if (property == __GO_PARENT__) {
784                 Integer parentId = (Integer) GraphicController.getController().getProperty(id, __GO_PARENT__);
785                 TypedObject registeredParent = allObjects.get(parentId);
786                 if (registeredParent == null) {
787                     allObjects.remove(id);
788                 } else {
789                     ((SwingScilabAxes) allObjects.get(id).getValue()).update(property, parentId);
790                 }
791             }
792             return;
793         }
794
795         if (SwingUtilities.isEventDispatchThread()) {
796             updateObjectOnEDT(registeredObject, id, property);
797         } else {
798             try {
799                 SwingUtilities.invokeAndWait(new Runnable() {
800                     public void run() {
801                         updateObjectOnEDT(registeredObject, id, property);
802                     }
803                 });
804             } catch (InterruptedException e) {
805                 e.printStackTrace();
806             } catch (InvocationTargetException e) {
807                 e.printStackTrace();
808             }
809         }
810     }
811
812     public void updateObjectOnEDT(TypedObject registeredObject, final Integer id, final int property) {
813         /* Removes the swing object if its parent is not display */
814         if (registeredObject != null && property == __GO_PARENT__) {
815             Integer parentId = (Integer) GraphicController.getController().getProperty(id, __GO_PARENT__);
816             TypedObject registeredParent = allObjects.get(parentId);
817             if (registeredParent == null) {
818                 allObjects.remove(id);
819             }
820         }
821
822         int type = (Integer) GraphicController.getController().getProperty(id, __GO_TYPE__);
823         /* Children list update */
824         if (registeredObject != null && property == __GO_CHILDREN__) {
825             final Integer[] newChildren = (Integer[]) GraphicController.getController().getProperty(id, __GO_CHILDREN__);
826
827             switch (type) {
828                     /*
829                      * FIGURE CHILDREN UPDATE
830                      */
831                 case __GO_FIGURE__:
832                     updateFigureChildren(registeredObject, newChildren);
833                     break;
834                     /*
835                      * CONSOLE CHILDREN UPDATE
836                      */
837                 case __GO_CONSOLE__:
838                     updateConsoleChildren(registeredObject, newChildren);
839                     break;
840                     /*
841                      * MENU CHILDREN UPDATE
842                      */
843                 case __GO_UIMENU__:
844                     updateMenuChildren(registeredObject, id, newChildren);
845                     break;
846                     /*
847                      * CONTEXTMENU CHILDREN UPDATE
848                      */
849                 case __GO_UICONTEXTMENU__:
850                     updateContextMenuChildren(registeredObject, newChildren);
851                     break;
852                     /*
853                      * UICONTROL "FRAME" CHILDREN UPDATE
854                      */
855                 case __GO_UICONTROL__:
856
857                     int style = (Integer) GraphicController.getController().getProperty(id, __GO_STYLE__);
858                     switch (style) {
859                         case __GO_UI_LAYER__:
860                             updateLayerChildren(registeredObject, newChildren);
861                             break;
862                         case __GO_UI_FRAME__:
863                             if (registeredObject.getValue() instanceof SwingScilabFrame) {
864                                 updateFrameChildren(registeredObject, newChildren);
865                             } else if (registeredObject.getValue() instanceof SwingScilabScrollableFrame) {
866                                 updateScrollableFrameChildren(registeredObject, newChildren);
867                             }
868                             break;
869                         case __GO_UI_TAB__:
870                             updateTabGroupChildren(registeredObject, newChildren);
871                             break;
872                     }
873                     break;
874             }
875         }
876
877         /*
878          * When the CHECKED property is updated for a UIMENU, the object is
879          * converted to a SwingScilabCheckBoxMenuItem is not already of this
880          * type
881          */
882         if (registeredObject != null && property == __GO_UI_CHECKED__) {
883             if (type == __GO_UIMENU__) {
884                 switch (registeredObject.getType()) {
885                     case UiParentMenu: {
886                         SwingScilabMenu meAsAMenu = (SwingScilabMenu) registeredObject.getValue();
887                         Container parent = meAsAMenu.getParent();
888                         if (parent != null) {
889                             parent.remove(meAsAMenu);
890                         }
891
892                         registeredObject = CreateObjectFromType(__GO_UICHECKEDMENU__, id);
893                         allObjects.put(id, registeredObject);
894                         SwingScilabCheckBoxMenuItem meAsAMenuItem = (SwingScilabCheckBoxMenuItem) registeredObject.getValue();
895                         if (parent != null) {
896                             parent.add(meAsAMenuItem);
897                         }
898                         break;
899                     }
900                     case UiChildMenu: {
901                         SwingScilabMenuItem childMenu = (SwingScilabMenuItem) allObjects.get(id).getValue();
902                         JComponent parentMenu = (JComponent) childMenu.getParent();
903                         int index = 0;
904                         if (parentMenu != null) {
905                             index = parentMenu.getComponentZOrder(childMenu);
906                             parentMenu.remove(childMenu);
907                         }
908                         registeredObject = CreateObjectFromType(__GO_UICHECKEDMENU__, id);
909                         allObjects.put(id, registeredObject);
910                         registeredObject = allObjects.get(id);
911                         if (parentMenu != null) {
912                             parentMenu.add((SwingScilabCheckBoxMenuItem) registeredObject.getValue(), index);
913                         }
914                         return;
915                     }
916                     default:
917                         break;
918                 }
919             }
920         }
921
922         /*
923          * When the SEPARATOR property is updated for a UIMENU, When the
924          * property is set to TRUE: A separator is added if not already done
925          * When the property is set to FALSE: The previous separator is removed
926          * if it exists
927          */
928         if (registeredObject != null && property == __GO_UI_SEPARATOR__) {
929             if (type == __GO_UIMENU__) {
930                 Integer parentId = (Integer) GraphicController.getController().getProperty(id, __GO_PARENT__);
931                 int menuPosition = -1;
932                 Component currentComponent = (Component) registeredObject.getValue();
933                 Component[] allChildren = ((SwingScilabMenu) allObjects.get(parentId).getValue()).getMenuComponents();
934                 for (int kChild = 0; kChild < allChildren.length; kChild++) {
935                     if (allChildren[kChild] == currentComponent) {
936                         menuPosition = kChild;
937                         break;
938                     }
939                 }
940                 if (menuPosition < 0) {
941                     return;
942                 }
943                 boolean newSeparatorMode = (Boolean) GraphicController.getController().getProperty(id, __GO_UI_SEPARATOR__);
944                 if (newSeparatorMode) { // Add a separator above the menu
945                     ((SwingScilabMenu) allObjects.get(parentId).getValue()).insertSeparator(menuPosition);
946                 } else { // Remove the separator above the menu (if there is one)
947                     if (menuPosition == 0) { // There is no object above the menu
948                         return;
949                     }
950                     if (((SwingScilabMenu) allObjects.get(parentId).getValue()).getComponent(menuPosition - 1) instanceof JSeparator) {
951                         ((SwingScilabMenu) allObjects.get(parentId).getValue()).remove(menuPosition - 1);
952                     }
953                 }
954             }
955         }
956
957         if (registeredObject != null) {
958             final SwingViewObject swingObject = registeredObject.getValue();
959             if (swingObject != null) {
960                 swingObject.update(property, GraphicController.getController().getProperty(id, property));
961             }
962         }
963     }
964
965     /**
966      * Update the figure children (called by generic updateObject method)
967      * @param id the id of the figure
968      * @param newChildren the new children IDs list
969      */
970     private void updateFigureChildren(TypedObject updatedObject, Integer[] newChildren) {
971         Container updatedComponent = (Container) updatedObject.getValue();
972         boolean needRevalidate = false;
973         boolean needRevalidateMenubar = false;
974
975         // Add new children
976         for (Integer childId : newChildren) {
977             if (!updatedObject.hasChild(childId)) {
978
979                 // Add the child
980                 updatedObject.addChild(childId);
981
982                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
983
984                 /* Add an Axes */
985                 if (childType == __GO_AXES__) {
986                     SwingViewObject canvas = allObjects.get(childId).getValue();
987                     ((SwingScilabPanel) updatedComponent).addMember(canvas);
988                 }
989
990                 /* Add an uicontrol */
991                 if (childType == __GO_UICONTROL__) {
992                     SwingViewObject uiContol = allObjects.get(childId).getValue();
993                     ((SwingScilabPanel) updatedComponent).addMember(uiContol);
994
995                     // Force Position
996                     uiContol.update(__GO_POSITION__, GraphicController.getController().getProperty(childId, __GO_POSITION__));
997                     needRevalidate = true;
998                 }
999
1000                 /* Add an uimenu */
1001                 if (childType == __GO_UIMENU__) {
1002                     TypedObject childAsTypedObject = allObjects.get(childId);
1003                     switch (childAsTypedObject.getType()) {
1004                         case UiChildMenu:
1005                         case UiCheckedMenu:
1006                             allObjects.remove(childId);
1007                             allObjects.put(childId, CreateObjectFromType(__GO_UIPARENTMENU__, childId));
1008                             ((Container) ((SwingScilabPanel) updatedComponent).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) allObjects.get(childId).getValue());
1009                             break;
1010                         default: /* UiParentMenu */
1011                             ((Container) ((SwingScilabPanel) updatedComponent).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) allObjects.get(childId).getValue());
1012                             break;
1013                     }
1014                     needRevalidateMenubar = true;
1015                 }
1016             }
1017         }
1018
1019         // Remove children which have been deleted
1020         Set<Integer> newChildrenSet = new HashSet<Integer>(Arrays.asList(newChildren));
1021         // Clone the children set to avoid concurrent accesses
1022         Integer[] oldChildrenSet = updatedObject.getChildren().toArray(new Integer[updatedObject.getChildren().size()]);
1023         for (Integer childId : oldChildrenSet) {
1024             if (!newChildrenSet.contains(childId)) {
1025
1026                 // Remove the child
1027                 updatedObject.removeChild(childId);
1028
1029                 final Object childTypeObject = GraphicController.getController().getProperty(childId, __GO_TYPE__);
1030                 if (childTypeObject == null) {
1031                     // the child has already been removed from the model
1032                     continue;
1033                 }
1034                 int childType = (Integer) childTypeObject;
1035
1036                 /* Remove an uicontrol */
1037                 if (childType == __GO_UICONTROL__) {
1038                     ((SwingScilabPanel) updatedComponent).removeMember(allObjects.get(childId).getValue());
1039                     needRevalidate = true;
1040                 }
1041
1042                 /* Remove an uimenu */
1043                 if (childType == __GO_UIMENU__) {
1044                     TypedObject childAsTypedObject = allObjects.get(childId);
1045                     switch (childAsTypedObject.getType()) {
1046                         case UiCheckedMenu:
1047                             ((Container) ((SwingScilabPanel) updatedComponent).getMenuBar().getAsSimpleMenuBar()).remove((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
1048                             break;
1049                         default: /* UiParentMenu */
1050                             ((Container) ((SwingScilabPanel) updatedComponent).getMenuBar().getAsSimpleMenuBar()).remove((SwingScilabMenu) allObjects.get(childId).getValue());
1051                             break;
1052                     }
1053                     needRevalidateMenubar = true;
1054                 }
1055             }
1056         }
1057
1058         if (needRevalidateMenubar) {
1059             ((JComponent) ((SwingScilabPanel) updatedComponent).getMenuBar().getAsSimpleMenuBar()).revalidate();
1060             ((JComponent) ((SwingScilabPanel) updatedComponent).getMenuBar().getAsSimpleMenuBar()).repaint();
1061         }
1062
1063         if (needRevalidate && updatedComponent != null) {
1064             ((JComponent) updatedComponent).revalidate();
1065         }
1066     }
1067
1068     /**
1069      * Update the frame children (called by generic updateObject method)
1070      * @param id the id of the figure
1071      * @param newChildren the new children IDs list
1072      */
1073     private void updateFrameChildren(TypedObject updatedObject, Integer[] newChildren) {
1074         SwingScilabFrame updatedComponent = (SwingScilabFrame) updatedObject.getValue();
1075         boolean needRevalidate = false;
1076
1077         // Add new children
1078         for (Integer childId : newChildren) {
1079             if (!updatedObject.hasChild(childId)) {
1080
1081                 // Add the child
1082                 updatedObject.addChild(childId);
1083
1084                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1085
1086                 /* Add an uicontrol */
1087                 if (childType == __GO_UICONTROL__ || childType == __GO_AXES__) {
1088                     updatedComponent.addMember(allObjects.get(childId).getValue());
1089                     needRevalidate = true;
1090                 }
1091             }
1092         }
1093
1094         // Remove children which have been deleted
1095         Set<Integer> newChildrenSet = new HashSet<Integer>(Arrays.asList(newChildren));
1096         // Clone the children set to avoid concurrent accesses
1097         Integer[] oldChildrenSet = updatedObject.getChildren().toArray(new Integer[updatedObject.getChildren().size()]);
1098         for (Integer childId : oldChildrenSet) {
1099             if (!newChildrenSet.contains(childId)) {
1100
1101                 // Remove the child
1102                 updatedObject.removeChild(childId);
1103
1104                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1105
1106                 /* Remove an uicontrol */
1107                 if (childType == __GO_UICONTROL__) {
1108                     updatedComponent.remove((Component) allObjects.get(childId).getValue());
1109                     needRevalidate = true;
1110                 }
1111                 
1112                 if (childType == __GO_AXES__) {
1113                     updatedComponent.removeAxes();
1114                     needRevalidate = true;
1115                 }
1116                 
1117             }
1118         }
1119         if (needRevalidate && updatedComponent != null) {
1120             updatedComponent.revalidate();
1121         }
1122     }
1123
1124     private void updateScrollableFrameChildren(TypedObject updatedObject, Integer[] newChildren) {
1125         SwingScilabScrollableFrame updatedComponent = (SwingScilabScrollableFrame) updatedObject.getValue();
1126         boolean needRevalidate = false;
1127
1128         // Add new children
1129         for (Integer childId : newChildren) {
1130             if (!updatedObject.hasChild(childId)) {
1131
1132                 // Add the child
1133                 updatedObject.addChild(childId);
1134
1135                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1136
1137                 /* Add an uicontrol */
1138                 if (childType == __GO_UICONTROL__) {
1139                     updatedComponent.addMember(allObjects.get(childId).getValue());
1140                     needRevalidate = true;
1141                 }
1142             }
1143         }
1144
1145         // Remove children which have been deleted
1146         Set<Integer> newChildrenSet = new HashSet<Integer>(Arrays.asList(newChildren));
1147         // Clone the children set to avoid concurrent accesses
1148         Integer[] oldChildrenSet = updatedObject.getChildren().toArray(new Integer[updatedObject.getChildren().size()]);
1149         for (Integer childId : oldChildrenSet) {
1150             if (!newChildrenSet.contains(childId)) {
1151
1152                 // Remove the child
1153                 updatedObject.removeChild(childId);
1154
1155                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1156
1157                 /* Remove an uicontrol */
1158                 if (childType == __GO_UICONTROL__) {
1159                     updatedComponent.remove((Component) allObjects.get(childId).getValue());
1160                     needRevalidate = true;
1161                 }
1162             }
1163         }
1164         if (needRevalidate && updatedComponent != null) {
1165             updatedComponent.revalidate();
1166         }
1167     }
1168
1169     /**
1170      * Update the frame children (called by generic updateObject method)
1171      * @param id the id of the figure
1172      * @param newChildren the new children IDs list
1173      */
1174     private void updateLayerChildren(TypedObject updatedObject, Integer[] newChildren) {
1175         SwingScilabLayer updatedComponent = (SwingScilabLayer) updatedObject.getValue();
1176
1177         // Add new children
1178         for (Integer childId : newChildren) {
1179             if (!updatedObject.hasChild(childId)) {
1180
1181                 // Add the child
1182                 updatedObject.addChild(childId);
1183
1184                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1185
1186                 /* Add an uicontrol */
1187                 if (childType == __GO_UICONTROL__) {
1188                     int childStyle = (Integer) GraphicController.getController().getProperty(childId, __GO_STYLE__);
1189                     if (childStyle == __GO_UI_FRAME__) {
1190                         SwingViewObject obj = allObjects.get(childId).getValue();
1191                         if (obj instanceof SwingScilabScrollableFrame) {
1192                             updatedComponent.addMember((SwingScilabScrollableFrame)obj);
1193                         } else {
1194                             updatedComponent.addMember((SwingScilabFrame)obj);
1195                         }
1196                     }
1197                 }
1198             }
1199         }
1200
1201         // Remove children which have been deleted
1202         Set<Integer> newChildrenSet = new HashSet<Integer>(Arrays.asList(newChildren));
1203         // Clone the children set to avoid concurrent accesses
1204         Integer[] oldChildrenSet = updatedObject.getChildren().toArray(new Integer[updatedObject.getChildren().size()]);
1205         for (Integer childId : oldChildrenSet) {
1206             if (!newChildrenSet.contains(childId)) {
1207
1208                 // Remove the child
1209                 updatedObject.removeChild(childId);
1210
1211                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1212
1213                 /* Remove an uicontrol */
1214                 if (childType == __GO_UICONTROL__) {
1215                     int childStyle = (Integer) GraphicController.getController().getProperty(childId, __GO_STYLE__);
1216                     if (childStyle == __GO_UI_FRAME__) {
1217                         SwingViewObject obj = allObjects.get(childId).getValue();
1218                         if (obj instanceof SwingScilabScrollableFrame) {
1219                             updatedComponent.removeMember((SwingScilabScrollableFrame)obj);
1220                         } else {
1221                             updatedComponent.removeMember((SwingScilabFrame)obj);
1222                         }
1223                     }
1224                 }
1225             }
1226         }
1227     }
1228
1229     /**
1230      * Update the frame children (called by generic updateObject method)
1231      * @param id the id of the figure
1232      * @param newChildren the new children IDs list
1233      */
1234     private void updateTabGroupChildren(TypedObject updatedObject, Integer[] newChildren) {
1235         SwingScilabTabGroup updatedComponent = (SwingScilabTabGroup) updatedObject.getValue();
1236         boolean needRevalidate = false;
1237
1238         // Add new children
1239         for (Integer childId : newChildren) {
1240             if (!updatedObject.hasChild(childId)) {
1241
1242                 // Add the child
1243                 updatedObject.addChild(childId);
1244
1245                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1246
1247                 /* Add an uicontrol */
1248                 if (childType == __GO_UICONTROL__) {
1249                     String str = "Tab";
1250                     String[] text = (String[]) GraphicController.getController().getProperty(childId, __GO_UI_STRING__);
1251                     if (text != null && text[0] != null) {
1252                         str = text[0];
1253                     }
1254
1255                     updatedComponent.insertTab(str, null, (Component) allObjects.get(childId).getValue(), "", 0);
1256                     needRevalidate = true;
1257                 }
1258             }
1259         }
1260
1261         // Remove children which have been deleted
1262         Set<Integer> newChildrenSet = new HashSet<Integer>(Arrays.asList(newChildren));
1263         // Clone the children set to avoid concurrent accesses
1264         Integer[] oldChildrenSet = updatedObject.getChildren().toArray(new Integer[updatedObject.getChildren().size()]);
1265         for (Integer childId : oldChildrenSet) {
1266             if (!newChildrenSet.contains(childId)) {
1267
1268                 // Remove the child
1269                 updatedObject.removeChild(childId);
1270
1271                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1272
1273                 /* Remove an uicontrol */
1274                 if (childType == __GO_UICONTROL__) {
1275                     updatedComponent.remove((Component) allObjects.get(childId).getValue());
1276                     needRevalidate = true;
1277                 }
1278             }
1279         }
1280         if (needRevalidate && updatedComponent != null) {
1281             updatedComponent.revalidate();
1282         }
1283     }
1284
1285     /**
1286      * Update the Console menus (called by generic updateObject method)
1287      * @param id the id of the console object
1288      * @param newChildren the new children IDs list
1289      */
1290     private void updateConsoleChildren(TypedObject updatedObject, Integer[] newChildren) {
1291         Container updatedComponent = (SwingScilabDockablePanel) updatedObject.getValue();
1292         boolean needRevalidate = false;
1293
1294         // Add new children
1295         for (Integer childId : newChildren) {
1296             if (!updatedObject.hasChild(childId)) {
1297
1298                 // Add the child
1299                 updatedObject.addChild(childId);
1300
1301                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1302
1303                 if (childType == __GO_UIMENU__) {
1304                     TypedObject childAsTypedObject = allObjects.get(childId);
1305                     switch (childAsTypedObject.getType()) {
1306                         case UiChildMenu:
1307                         case UiCheckedMenu:
1308                             TypedObject newUiParentMenu = CreateObjectFromType(__GO_UIPARENTMENU__, childId);
1309                             allObjects.put(childId, newUiParentMenu);
1310                             ((Container) ((SwingScilabDockablePanel) updatedObject.getValue()).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) newUiParentMenu.getValue());
1311                             break;
1312                         default: /* UiParentMenu */
1313                             ((Container) ((SwingScilabDockablePanel) updatedObject.getValue()).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) childAsTypedObject.getValue());
1314                             break;
1315                     }
1316                     needRevalidate = true;
1317                 }
1318             }
1319         }
1320
1321         // Remove children which have been deleted
1322         Set<Integer> newChildrenSet = new HashSet<Integer>(Arrays.asList(newChildren));
1323         Object[] updatedObjectChildren = updatedObject.getChildren().toArray();
1324         for (int i = 0; i < updatedObjectChildren.length; ++i) {
1325             Integer childId = (Integer) updatedObjectChildren[i];
1326             if (!newChildrenSet.contains(childId)) {
1327
1328                 // Remove the child
1329                 updatedObject.removeChild(childId);
1330
1331                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1332
1333                 if (childType == __GO_UIMENU__) {
1334                     ((Container) ((SwingScilabDockablePanel) updatedObject.getValue()).getMenuBar().getAsSimpleMenuBar()).remove((SwingScilabMenu) allObjects.get(childId).getValue());
1335                     needRevalidate = true;
1336                 }
1337             }
1338         }
1339         if (needRevalidate && updatedComponent != null) {
1340             updatedComponent.validate();
1341         }
1342
1343     }
1344
1345     /**
1346      * Update the children of a menu menus (called by generic updateObject
1347      * method)
1348      * @param id the id of the menu object
1349      * @param newChildren the new children IDs list
1350      */
1351     private void updateMenuChildren(TypedObject updatedObject, Integer id, Integer[] newChildren) {
1352         Container updatedComponent = null;
1353         boolean needRevalidate = false;
1354         int updatedObjectPosition = 0;
1355         TypedObject newParent = null;
1356
1357         // Add new children
1358         for (Integer childId : newChildren) {
1359             int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1360             if (childType == __GO_UIMENU__) {
1361                 if (!updatedObject.hasChild(childId)) {
1362                     // Add the child
1363                     updatedObject.addChild(childId);
1364
1365                     TypedObject childAsTypedObject = allObjects.get(childId);
1366                     Object addedChild = allObjects.get(childId).getValue();
1367                     JComponent parent = null;
1368                     switch (updatedObject.getType()) {
1369                         case UiChildMenu:
1370                             updatedComponent = (SwingScilabMenuItem) updatedObject.getValue();
1371                             parent = (JComponent) updatedComponent.getParent();
1372                             switch (childAsTypedObject.getType()) {
1373                                 case UiChildMenu:
1374                                     /* Replace the item by a parent menu */
1375                                     if (parent != null) {
1376                                         updatedObjectPosition = parent.getComponentZOrder((SwingScilabMenuItem) updatedObject.getValue());
1377                                         parent.remove((SwingScilabMenuItem) updatedObject.getValue());
1378                                     }
1379                                     newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
1380                                     allObjects.put(id, newParent);
1381                                     newParent.addChild(childId);
1382                                     if (parent != null) {
1383                                         parent.add((SwingScilabMenu) newParent.getValue(), updatedObjectPosition);
1384                                     }
1385                                     /* Update the created menu */
1386                                     ((SwingScilabMenu) newParent.getValue()).add((SwingScilabMenuItem) addedChild);
1387                                     break;
1388                                 case UiCheckedMenu:
1389                                     /* Replace the item by a parent menu */
1390                                     if (parent != null) {
1391                                         updatedObjectPosition = parent.getComponentZOrder((SwingScilabCheckBoxMenuItem) updatedObject.getValue());
1392                                         parent.remove((SwingScilabCheckBoxMenuItem) updatedObject.getValue());
1393                                     }
1394                                     newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
1395                                     allObjects.put(id, newParent);
1396                                     newParent.addChild(childId);
1397                                     if (parent != null) {
1398                                         parent.add((SwingScilabMenu) newParent.getValue(), updatedObjectPosition);
1399                                     }
1400                                     /* Update the created menu */
1401                                     ((SwingScilabMenu) newParent.getValue()).add((SwingScilabCheckBoxMenuItem) addedChild);
1402                                     break;
1403                                 default: /* UiParentMenu */
1404                                     ((SwingScilabMenu) updatedObject.getValue()).add((SwingScilabMenu) addedChild);
1405                                     break;
1406                             }
1407                             break;
1408                         case UiCheckedMenu:
1409                             updatedComponent = (SwingScilabCheckBoxMenuItem) updatedObject.getValue();
1410                             parent = (JComponent) updatedComponent.getParent();
1411                             switch (childAsTypedObject.getType()) {
1412                                 case UiChildMenu:
1413                                     /* Replace the item by a parent menu */
1414                                     if (parent != null) {
1415                                         updatedObjectPosition = parent.getComponentZOrder((SwingScilabCheckBoxMenuItem) updatedObject.getValue());
1416                                         parent.remove((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
1417                                     }
1418                                     newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
1419                                     allObjects.put(id, newParent);
1420                                     newParent.addChild(childId);
1421                                     if (parent != null) {
1422                                         parent.add((SwingScilabMenu) newParent.getValue(), updatedObjectPosition);
1423                                     }
1424                                     /* Update the created menu */
1425                                     ((SwingScilabMenu) newParent.getValue()).add((SwingScilabMenuItem) allObjects.get(childId).getValue());
1426                                     break;
1427                                 case UiCheckedMenu:
1428                                     /* Replace the item by a parent menu */
1429                                     if (parent != null) {
1430                                         updatedObjectPosition = parent.getComponentZOrder((SwingScilabCheckBoxMenuItem) updatedObject.getValue());
1431                                         parent.remove((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
1432                                     }
1433                                     newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
1434                                     allObjects.put(id, newParent);
1435                                     newParent.addChild(childId);
1436                                     if (parent != null) {
1437                                         parent.add((SwingScilabMenu) newParent.getValue(), updatedObjectPosition);
1438                                     }
1439                                     /* Update the created menu */
1440                                     ((SwingScilabMenu) newParent.getValue()).add((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
1441                                     break;
1442                                 default: /* UiParentMenu */
1443                                     ((SwingScilabMenu) updatedObject.getValue()).add((SwingScilabMenu) allObjects.get(childId).getValue());
1444                                     break;
1445                             }
1446                             break;
1447                         default: /* UiParentMenu */
1448                             SwingScilabMenu updatedMenu = (SwingScilabMenu) updatedObject.getValue();
1449                             updatedComponent = updatedMenu;
1450                             switch (childAsTypedObject.getType()) {
1451                                 case UiChildMenu:
1452                                     updatedMenu.add((SwingScilabMenuItem) allObjects.get(childId).getValue());
1453                                     break;
1454                                 case UiCheckedMenu:
1455                                     updatedMenu.add((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
1456                                     break;
1457                                 default: /* UiParentMenu */
1458                                     /* Java can not add a JMenu in a JMenu */
1459                                     /*
1460                                      * We need to convert the child into a
1461                                      * MenuItem
1462                                      */
1463                                     TypedObject newMenu = CreateObjectFromType(__GO_UIMENU__, childId);
1464                                     allObjects.put(childId, newMenu);
1465                                     updatedMenu.add((SwingScilabMenuItem) newMenu.getValue());
1466                                     break;
1467                             }
1468                             break;
1469                     }
1470                     needRevalidate = true;
1471                 }
1472             }
1473         }
1474         // Remove children which have been deleted
1475         Set<Integer> newChildrenSet = new HashSet<Integer>(Arrays.asList(newChildren));
1476         Integer[] oldChildren = updatedObject.getChildren().toArray(new Integer[updatedObject.getChildren().size()]);
1477         for (int childIndex = 0; childIndex < oldChildren.length; childIndex++) {
1478             Integer childId = oldChildren[childIndex];
1479             if (!newChildrenSet.contains(childId)) {
1480                 // Remove the child
1481                 updatedObject.removeChild(childId);
1482                 updatedComponent = (SwingScilabMenu) updatedObject.getValue();
1483
1484                 TypedObject childAsTypedObject = allObjects.get(childId);
1485                 Object removedMenu = childAsTypedObject.getValue();
1486
1487                 switch (childAsTypedObject.getType()) {
1488                     case UiChildMenu:
1489                         updatedComponent.remove((SwingScilabMenuItem) removedMenu);
1490                         break;
1491                     case UiCheckedMenu:
1492                         updatedComponent.remove((SwingScilabCheckBoxMenuItem) removedMenu);
1493                         break;
1494                     default:
1495                         updatedComponent.remove((SwingScilabMenu) removedMenu);
1496                         break;
1497                 }
1498                 needRevalidate = true;
1499             }
1500         }
1501         if (needRevalidate && updatedComponent != null) {
1502             updatedComponent.validate();
1503         }
1504     }
1505
1506     /**
1507      * Update the children of a contextmenu (called by generic updateObject
1508      * method)
1509      * @param id the id of the contextmenu object
1510      * @param newChildren the new children IDs list
1511      */
1512     private void updateContextMenuChildren(TypedObject updatedObject, Integer[] newChildren) {
1513         Container updatedComponent = null;
1514         boolean needRevalidate = false;
1515
1516         // Add new children
1517         for (Integer childId : newChildren) {
1518             int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1519             if (childType == __GO_UIMENU__) {
1520                 if (!updatedObject.hasChild(childId)) {
1521
1522                     // Add the child
1523                     updatedObject.addChild(childId);
1524
1525                     TypedObject childAsTypedObject = allObjects.get(childId);
1526                     SwingScilabContextMenu updatedMenu = (SwingScilabContextMenu) updatedObject.getValue();
1527                     updatedComponent = updatedMenu;
1528                     switch (childAsTypedObject.getType()) {
1529                         case UiChildMenu:
1530                             updatedMenu.add((SwingScilabMenuItem) allObjects.get(childId).getValue());
1531                             break;
1532                         case UiCheckedMenu:
1533                             updatedMenu.add((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
1534                             break;
1535                         default: /* UiParentMenu */
1536                             updatedMenu.add((SwingScilabMenu) allObjects.get(childId).getValue());
1537                             break;
1538                     }
1539                     needRevalidate = true;
1540                 }
1541             }
1542         }
1543         // Remove children which have been deleted
1544         Set<Integer> newChildrenSet = new HashSet<Integer>(Arrays.asList(newChildren));
1545         for (Integer childId : updatedObject.getChildren()) {
1546             if (!newChildrenSet.contains(childId)) {
1547                 // Remove the child
1548                 updatedObject.removeChild(childId);
1549                 updatedComponent = (SwingScilabMenu) updatedObject.getValue();
1550
1551                 TypedObject childAsTypedObject = allObjects.get(childId);
1552                 SwingScilabContextMenu updatedMenu = (SwingScilabContextMenu) updatedObject.getValue();
1553                 Object removedMenu = allObjects.get(childId).getValue();
1554
1555                 switch (childAsTypedObject.getType()) {
1556                     case UiChildMenu:
1557                         updatedMenu.remove((SwingScilabMenuItem) removedMenu);
1558                         break;
1559                     case UiCheckedMenu:
1560                         updatedMenu.remove((SwingScilabCheckBoxMenuItem) removedMenu);
1561                         break;
1562                     default:
1563                         updatedMenu.remove((SwingScilabMenu) removedMenu);
1564                         break;
1565                 }
1566                 needRevalidate = true;
1567             }
1568         }
1569         if (needRevalidate && updatedComponent != null) {
1570             updatedComponent.validate();
1571         }
1572     }
1573 }