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