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