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