Allow mutiplie axes within frames.
[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         boolean hasOpenGLAxes = false;
1077         
1078         // Add new children
1079         for (Integer childId : newChildren) {
1080             int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1081             if (!updatedObject.hasChild(childId)) {
1082
1083                 // Add the child
1084                 updatedObject.addChild(childId);
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             if (childType == __GO_AXES__) {
1094                 hasOpenGLAxes = true;
1095             }
1096             
1097         }
1098
1099         // Remove children which have been deleted
1100         Set<Integer> newChildrenSet = new HashSet<Integer>(Arrays.asList(newChildren));
1101         // Clone the children set to avoid concurrent accesses
1102         Integer[] oldChildrenSet = updatedObject.getChildren().toArray(new Integer[updatedObject.getChildren().size()]);
1103         for (Integer childId : oldChildrenSet) {
1104             if (!newChildrenSet.contains(childId)) {
1105
1106                 // Remove the child
1107                 updatedObject.removeChild(childId);
1108
1109                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1110
1111                 /* Remove an uicontrol */
1112                 if (childType == __GO_UICONTROL__) {
1113                     updatedComponent.remove((Component) allObjects.get(childId).getValue());
1114                     needRevalidate = true;
1115                 }
1116             }
1117         }
1118         
1119         if (!hasOpenGLAxes) {
1120             updatedComponent.removeAxes();
1121             needRevalidate = true;
1122         }
1123         
1124         if (needRevalidate && updatedComponent != null) {
1125             updatedComponent.revalidate();
1126         }
1127     }
1128
1129     private void updateScrollableFrameChildren(TypedObject updatedObject, Integer[] newChildren) {
1130         SwingScilabScrollableFrame updatedComponent = (SwingScilabScrollableFrame) updatedObject.getValue();
1131         boolean needRevalidate = false;
1132
1133         // Add new children
1134         for (Integer childId : newChildren) {
1135             if (!updatedObject.hasChild(childId)) {
1136
1137                 // Add the child
1138                 updatedObject.addChild(childId);
1139
1140                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1141
1142                 /* Add an uicontrol */
1143                 if (childType == __GO_UICONTROL__) {
1144                     updatedComponent.addMember(allObjects.get(childId).getValue());
1145                     needRevalidate = true;
1146                 }
1147             }
1148         }
1149
1150         // Remove children which have been deleted
1151         Set<Integer> newChildrenSet = new HashSet<Integer>(Arrays.asList(newChildren));
1152         // Clone the children set to avoid concurrent accesses
1153         Integer[] oldChildrenSet = updatedObject.getChildren().toArray(new Integer[updatedObject.getChildren().size()]);
1154         for (Integer childId : oldChildrenSet) {
1155             if (!newChildrenSet.contains(childId)) {
1156
1157                 // Remove the child
1158                 updatedObject.removeChild(childId);
1159
1160                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1161
1162                 /* Remove an uicontrol */
1163                 if (childType == __GO_UICONTROL__) {
1164                     updatedComponent.remove((Component) allObjects.get(childId).getValue());
1165                     needRevalidate = true;
1166                 }
1167             }
1168         }
1169         if (needRevalidate && updatedComponent != null) {
1170             updatedComponent.revalidate();
1171         }
1172     }
1173
1174     /**
1175      * Update the frame children (called by generic updateObject method)
1176      * @param id the id of the figure
1177      * @param newChildren the new children IDs list
1178      */
1179     private void updateLayerChildren(TypedObject updatedObject, Integer[] newChildren) {
1180         SwingScilabLayer updatedComponent = (SwingScilabLayer) updatedObject.getValue();
1181
1182         // Add new children
1183         for (Integer childId : newChildren) {
1184             if (!updatedObject.hasChild(childId)) {
1185
1186                 // Add the child
1187                 updatedObject.addChild(childId);
1188
1189                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1190
1191                 /* Add an uicontrol */
1192                 if (childType == __GO_UICONTROL__) {
1193                     int childStyle = (Integer) GraphicController.getController().getProperty(childId, __GO_STYLE__);
1194                     if (childStyle == __GO_UI_FRAME__) {
1195                         SwingViewObject obj = allObjects.get(childId).getValue();
1196                         if (obj instanceof SwingScilabScrollableFrame) {
1197                             updatedComponent.addMember((SwingScilabScrollableFrame)obj);
1198                         } else {
1199                             updatedComponent.addMember((SwingScilabFrame)obj);
1200                         }
1201                     }
1202                 }
1203             }
1204         }
1205
1206         // Remove children which have been deleted
1207         Set<Integer> newChildrenSet = new HashSet<Integer>(Arrays.asList(newChildren));
1208         // Clone the children set to avoid concurrent accesses
1209         Integer[] oldChildrenSet = updatedObject.getChildren().toArray(new Integer[updatedObject.getChildren().size()]);
1210         for (Integer childId : oldChildrenSet) {
1211             if (!newChildrenSet.contains(childId)) {
1212
1213                 // Remove the child
1214                 updatedObject.removeChild(childId);
1215
1216                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1217
1218                 /* Remove an uicontrol */
1219                 if (childType == __GO_UICONTROL__) {
1220                     int childStyle = (Integer) GraphicController.getController().getProperty(childId, __GO_STYLE__);
1221                     if (childStyle == __GO_UI_FRAME__) {
1222                         SwingViewObject obj = allObjects.get(childId).getValue();
1223                         if (obj instanceof SwingScilabScrollableFrame) {
1224                             updatedComponent.removeMember((SwingScilabScrollableFrame)obj);
1225                         } else {
1226                             updatedComponent.removeMember((SwingScilabFrame)obj);
1227                         }
1228                     }
1229                 }
1230             }
1231         }
1232     }
1233
1234     /**
1235      * Update the frame children (called by generic updateObject method)
1236      * @param id the id of the figure
1237      * @param newChildren the new children IDs list
1238      */
1239     private void updateTabGroupChildren(TypedObject updatedObject, Integer[] newChildren) {
1240         SwingScilabTabGroup updatedComponent = (SwingScilabTabGroup) updatedObject.getValue();
1241         boolean needRevalidate = false;
1242
1243         // Add new children
1244         for (Integer childId : newChildren) {
1245             if (!updatedObject.hasChild(childId)) {
1246
1247                 // Add the child
1248                 updatedObject.addChild(childId);
1249
1250                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1251
1252                 /* Add an uicontrol */
1253                 if (childType == __GO_UICONTROL__) {
1254                     String str = "Tab";
1255                     String[] text = (String[]) GraphicController.getController().getProperty(childId, __GO_UI_STRING__);
1256                     if (text != null && text[0] != null) {
1257                         str = text[0];
1258                     }
1259
1260                     updatedComponent.insertTab(str, null, (Component) allObjects.get(childId).getValue(), "", 0);
1261                     needRevalidate = true;
1262                 }
1263             }
1264         }
1265
1266         // Remove children which have been deleted
1267         Set<Integer> newChildrenSet = new HashSet<Integer>(Arrays.asList(newChildren));
1268         // Clone the children set to avoid concurrent accesses
1269         Integer[] oldChildrenSet = updatedObject.getChildren().toArray(new Integer[updatedObject.getChildren().size()]);
1270         for (Integer childId : oldChildrenSet) {
1271             if (!newChildrenSet.contains(childId)) {
1272
1273                 // Remove the child
1274                 updatedObject.removeChild(childId);
1275
1276                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1277
1278                 /* Remove an uicontrol */
1279                 if (childType == __GO_UICONTROL__) {
1280                     updatedComponent.remove((Component) allObjects.get(childId).getValue());
1281                     needRevalidate = true;
1282                 }
1283             }
1284         }
1285         if (needRevalidate && updatedComponent != null) {
1286             updatedComponent.revalidate();
1287         }
1288     }
1289
1290     /**
1291      * Update the Console menus (called by generic updateObject method)
1292      * @param id the id of the console object
1293      * @param newChildren the new children IDs list
1294      */
1295     private void updateConsoleChildren(TypedObject updatedObject, Integer[] newChildren) {
1296         Container updatedComponent = (SwingScilabDockablePanel) updatedObject.getValue();
1297         boolean needRevalidate = false;
1298
1299         // Add new children
1300         for (Integer childId : newChildren) {
1301             if (!updatedObject.hasChild(childId)) {
1302
1303                 // Add the child
1304                 updatedObject.addChild(childId);
1305
1306                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1307
1308                 if (childType == __GO_UIMENU__) {
1309                     TypedObject childAsTypedObject = allObjects.get(childId);
1310                     switch (childAsTypedObject.getType()) {
1311                         case UiChildMenu:
1312                         case UiCheckedMenu:
1313                             TypedObject newUiParentMenu = CreateObjectFromType(__GO_UIPARENTMENU__, childId);
1314                             allObjects.put(childId, newUiParentMenu);
1315                             ((Container) ((SwingScilabDockablePanel) updatedObject.getValue()).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) newUiParentMenu.getValue());
1316                             break;
1317                         default: /* UiParentMenu */
1318                             ((Container) ((SwingScilabDockablePanel) updatedObject.getValue()).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) childAsTypedObject.getValue());
1319                             break;
1320                     }
1321                     needRevalidate = true;
1322                 }
1323             }
1324         }
1325
1326         // Remove children which have been deleted
1327         Set<Integer> newChildrenSet = new HashSet<Integer>(Arrays.asList(newChildren));
1328         Object[] updatedObjectChildren = updatedObject.getChildren().toArray();
1329         for (int i = 0; i < updatedObjectChildren.length; ++i) {
1330             Integer childId = (Integer) updatedObjectChildren[i];
1331             if (!newChildrenSet.contains(childId)) {
1332
1333                 // Remove the child
1334                 updatedObject.removeChild(childId);
1335
1336                 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1337
1338                 if (childType == __GO_UIMENU__) {
1339                     ((Container) ((SwingScilabDockablePanel) updatedObject.getValue()).getMenuBar().getAsSimpleMenuBar()).remove((SwingScilabMenu) allObjects.get(childId).getValue());
1340                     needRevalidate = true;
1341                 }
1342             }
1343         }
1344         if (needRevalidate && updatedComponent != null) {
1345             updatedComponent.validate();
1346         }
1347
1348     }
1349
1350     /**
1351      * Update the children of a menu menus (called by generic updateObject
1352      * method)
1353      * @param id the id of the menu object
1354      * @param newChildren the new children IDs list
1355      */
1356     private void updateMenuChildren(TypedObject updatedObject, Integer id, Integer[] newChildren) {
1357         Container updatedComponent = null;
1358         boolean needRevalidate = false;
1359         int updatedObjectPosition = 0;
1360         TypedObject newParent = null;
1361
1362         // Add new children
1363         for (Integer childId : newChildren) {
1364             int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1365             if (childType == __GO_UIMENU__) {
1366                 if (!updatedObject.hasChild(childId)) {
1367                     // Add the child
1368                     updatedObject.addChild(childId);
1369
1370                     TypedObject childAsTypedObject = allObjects.get(childId);
1371                     Object addedChild = allObjects.get(childId).getValue();
1372                     JComponent parent = null;
1373                     switch (updatedObject.getType()) {
1374                         case UiChildMenu:
1375                             updatedComponent = (SwingScilabMenuItem) updatedObject.getValue();
1376                             parent = (JComponent) updatedComponent.getParent();
1377                             switch (childAsTypedObject.getType()) {
1378                                 case UiChildMenu:
1379                                     /* Replace the item by a parent menu */
1380                                     if (parent != null) {
1381                                         updatedObjectPosition = parent.getComponentZOrder((SwingScilabMenuItem) updatedObject.getValue());
1382                                         parent.remove((SwingScilabMenuItem) updatedObject.getValue());
1383                                     }
1384                                     newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
1385                                     allObjects.put(id, newParent);
1386                                     newParent.addChild(childId);
1387                                     if (parent != null) {
1388                                         parent.add((SwingScilabMenu) newParent.getValue(), updatedObjectPosition);
1389                                     }
1390                                     /* Update the created menu */
1391                                     ((SwingScilabMenu) newParent.getValue()).add((SwingScilabMenuItem) addedChild);
1392                                     break;
1393                                 case UiCheckedMenu:
1394                                     /* Replace the item by a parent menu */
1395                                     if (parent != null) {
1396                                         updatedObjectPosition = parent.getComponentZOrder((SwingScilabCheckBoxMenuItem) updatedObject.getValue());
1397                                         parent.remove((SwingScilabCheckBoxMenuItem) updatedObject.getValue());
1398                                     }
1399                                     newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
1400                                     allObjects.put(id, newParent);
1401                                     newParent.addChild(childId);
1402                                     if (parent != null) {
1403                                         parent.add((SwingScilabMenu) newParent.getValue(), updatedObjectPosition);
1404                                     }
1405                                     /* Update the created menu */
1406                                     ((SwingScilabMenu) newParent.getValue()).add((SwingScilabCheckBoxMenuItem) addedChild);
1407                                     break;
1408                                 default: /* UiParentMenu */
1409                                     ((SwingScilabMenu) updatedObject.getValue()).add((SwingScilabMenu) addedChild);
1410                                     break;
1411                             }
1412                             break;
1413                         case UiCheckedMenu:
1414                             updatedComponent = (SwingScilabCheckBoxMenuItem) updatedObject.getValue();
1415                             parent = (JComponent) updatedComponent.getParent();
1416                             switch (childAsTypedObject.getType()) {
1417                                 case UiChildMenu:
1418                                     /* Replace the item by a parent menu */
1419                                     if (parent != null) {
1420                                         updatedObjectPosition = parent.getComponentZOrder((SwingScilabCheckBoxMenuItem) updatedObject.getValue());
1421                                         parent.remove((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
1422                                     }
1423                                     newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
1424                                     allObjects.put(id, newParent);
1425                                     newParent.addChild(childId);
1426                                     if (parent != null) {
1427                                         parent.add((SwingScilabMenu) newParent.getValue(), updatedObjectPosition);
1428                                     }
1429                                     /* Update the created menu */
1430                                     ((SwingScilabMenu) newParent.getValue()).add((SwingScilabMenuItem) allObjects.get(childId).getValue());
1431                                     break;
1432                                 case UiCheckedMenu:
1433                                     /* Replace the item by a parent menu */
1434                                     if (parent != null) {
1435                                         updatedObjectPosition = parent.getComponentZOrder((SwingScilabCheckBoxMenuItem) updatedObject.getValue());
1436                                         parent.remove((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
1437                                     }
1438                                     newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
1439                                     allObjects.put(id, newParent);
1440                                     newParent.addChild(childId);
1441                                     if (parent != null) {
1442                                         parent.add((SwingScilabMenu) newParent.getValue(), updatedObjectPosition);
1443                                     }
1444                                     /* Update the created menu */
1445                                     ((SwingScilabMenu) newParent.getValue()).add((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
1446                                     break;
1447                                 default: /* UiParentMenu */
1448                                     ((SwingScilabMenu) updatedObject.getValue()).add((SwingScilabMenu) allObjects.get(childId).getValue());
1449                                     break;
1450                             }
1451                             break;
1452                         default: /* UiParentMenu */
1453                             SwingScilabMenu updatedMenu = (SwingScilabMenu) updatedObject.getValue();
1454                             updatedComponent = updatedMenu;
1455                             switch (childAsTypedObject.getType()) {
1456                                 case UiChildMenu:
1457                                     updatedMenu.add((SwingScilabMenuItem) allObjects.get(childId).getValue());
1458                                     break;
1459                                 case UiCheckedMenu:
1460                                     updatedMenu.add((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
1461                                     break;
1462                                 default: /* UiParentMenu */
1463                                     /* Java can not add a JMenu in a JMenu */
1464                                     /*
1465                                      * We need to convert the child into a
1466                                      * MenuItem
1467                                      */
1468                                     TypedObject newMenu = CreateObjectFromType(__GO_UIMENU__, childId);
1469                                     allObjects.put(childId, newMenu);
1470                                     updatedMenu.add((SwingScilabMenuItem) newMenu.getValue());
1471                                     break;
1472                             }
1473                             break;
1474                     }
1475                     needRevalidate = true;
1476                 }
1477             }
1478         }
1479         // Remove children which have been deleted
1480         Set<Integer> newChildrenSet = new HashSet<Integer>(Arrays.asList(newChildren));
1481         Integer[] oldChildren = updatedObject.getChildren().toArray(new Integer[updatedObject.getChildren().size()]);
1482         for (int childIndex = 0; childIndex < oldChildren.length; childIndex++) {
1483             Integer childId = oldChildren[childIndex];
1484             if (!newChildrenSet.contains(childId)) {
1485                 // Remove the child
1486                 updatedObject.removeChild(childId);
1487                 updatedComponent = (SwingScilabMenu) updatedObject.getValue();
1488
1489                 TypedObject childAsTypedObject = allObjects.get(childId);
1490                 Object removedMenu = childAsTypedObject.getValue();
1491
1492                 switch (childAsTypedObject.getType()) {
1493                     case UiChildMenu:
1494                         updatedComponent.remove((SwingScilabMenuItem) removedMenu);
1495                         break;
1496                     case UiCheckedMenu:
1497                         updatedComponent.remove((SwingScilabCheckBoxMenuItem) removedMenu);
1498                         break;
1499                     default:
1500                         updatedComponent.remove((SwingScilabMenu) removedMenu);
1501                         break;
1502                 }
1503                 needRevalidate = true;
1504             }
1505         }
1506         if (needRevalidate && updatedComponent != null) {
1507             updatedComponent.validate();
1508         }
1509     }
1510
1511     /**
1512      * Update the children of a contextmenu (called by generic updateObject
1513      * method)
1514      * @param id the id of the contextmenu object
1515      * @param newChildren the new children IDs list
1516      */
1517     private void updateContextMenuChildren(TypedObject updatedObject, Integer[] newChildren) {
1518         Container updatedComponent = null;
1519         boolean needRevalidate = false;
1520
1521         // Add new children
1522         for (Integer childId : newChildren) {
1523             int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1524             if (childType == __GO_UIMENU__) {
1525                 if (!updatedObject.hasChild(childId)) {
1526
1527                     // Add the child
1528                     updatedObject.addChild(childId);
1529
1530                     TypedObject childAsTypedObject = allObjects.get(childId);
1531                     SwingScilabContextMenu updatedMenu = (SwingScilabContextMenu) updatedObject.getValue();
1532                     updatedComponent = updatedMenu;
1533                     switch (childAsTypedObject.getType()) {
1534                         case UiChildMenu:
1535                             updatedMenu.add((SwingScilabMenuItem) allObjects.get(childId).getValue());
1536                             break;
1537                         case UiCheckedMenu:
1538                             updatedMenu.add((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
1539                             break;
1540                         default: /* UiParentMenu */
1541                             updatedMenu.add((SwingScilabMenu) allObjects.get(childId).getValue());
1542                             break;
1543                     }
1544                     needRevalidate = true;
1545                 }
1546             }
1547         }
1548         // Remove children which have been deleted
1549         Set<Integer> newChildrenSet = new HashSet<Integer>(Arrays.asList(newChildren));
1550         for (Integer childId : updatedObject.getChildren()) {
1551             if (!newChildrenSet.contains(childId)) {
1552                 // Remove the child
1553                 updatedObject.removeChild(childId);
1554                 updatedComponent = (SwingScilabMenu) updatedObject.getValue();
1555
1556                 TypedObject childAsTypedObject = allObjects.get(childId);
1557                 SwingScilabContextMenu updatedMenu = (SwingScilabContextMenu) updatedObject.getValue();
1558                 Object removedMenu = allObjects.get(childId).getValue();
1559
1560                 switch (childAsTypedObject.getType()) {
1561                     case UiChildMenu:
1562                         updatedMenu.remove((SwingScilabMenuItem) removedMenu);
1563                         break;
1564                     case UiCheckedMenu:
1565                         updatedMenu.remove((SwingScilabCheckBoxMenuItem) removedMenu);
1566                         break;
1567                     default:
1568                         updatedMenu.remove((SwingScilabMenu) removedMenu);
1569                         break;
1570                 }
1571                 needRevalidate = true;
1572             }
1573         }
1574         if (needRevalidate && updatedComponent != null) {
1575             updatedComponent.validate();
1576         }
1577     }
1578 }