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