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
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-en.txt
13 package org.scilab.modules.gui;
15 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_CALLBACKTYPE__;
16 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_CALLBACK__;
17 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_CHILDREN__;
18 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_CONSOLE__;
19 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_FIGURE__;
20 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_PARENT__;
21 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_POSITION__;
22 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_PROGRESSIONBAR__;
23 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_SIZE__;
24 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_STYLE__;
25 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_TYPE__;
26 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UICHECKEDMENU__;
27 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UICHILDMENU__;
28 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UICONTEXTMENU__;
29 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UICONTROL__;
30 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UIMENU__;
31 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UIPARENTMENU__;
32 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_BACKGROUNDCOLOR__;
33 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_CHECKBOX__;
34 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_CHECKED__;
35 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_EDIT__;
36 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_ENABLE__;
37 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTANGLE__;
38 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTNAME__;
39 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTSIZE__;
40 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTUNITS__;
41 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTWEIGHT__;
42 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FOREGROUNDCOLOR__;
43 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME__;
44 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_HORIZONTALALIGNMENT__;
45 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_ICON__;
46 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_IMAGE__;
47 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_LABEL__;
48 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_LISTBOX__;
49 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_POPUPMENU__;
50 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_PUSHBUTTON__;
51 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_RADIOBUTTON__;
52 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_RELIEF__;
53 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_SEPARATOR__;
54 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_SLIDER__;
55 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_STRING__;
56 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_TABLE__;
57 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_TEXT__;
58 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_VERTICALALIGNMENT__;
59 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_VALID__;
60 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_VISIBLE__;
61 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_WAITBAR__;
62 import static org.scilab.modules.gui.utils.Debug.DEBUG;
64 import java.awt.Component;
65 import java.awt.Container;
66 import java.awt.GraphicsEnvironment;
67 import java.lang.reflect.InvocationTargetException;
68 import java.util.Arrays;
69 import java.util.Collections;
70 import java.util.HashMap;
71 import java.util.HashSet;
74 import java.util.UUID;
76 import javax.swing.JComponent;
77 import javax.swing.JPanel;
78 import javax.swing.JSeparator;
79 import javax.swing.SwingUtilities;
81 import org.flexdock.docking.Dockable;
82 import org.flexdock.docking.DockingManager;
83 import org.flexdock.docking.activation.ActiveDockableTracker;
84 import org.flexdock.view.View;
85 import org.scilab.modules.graphic_export.Driver;
86 import org.scilab.modules.graphic_objects.console.Console;
87 import org.scilab.modules.graphic_objects.figure.Figure;
88 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
89 import org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties;
90 import org.scilab.modules.graphic_objects.graphicView.GraphicView;
91 import org.scilab.modules.gui.bridge.checkbox.SwingScilabCheckBox;
92 import org.scilab.modules.gui.bridge.checkboxmenuitem.SwingScilabCheckBoxMenuItem;
93 import org.scilab.modules.gui.bridge.console.SwingScilabConsole;
94 import org.scilab.modules.gui.bridge.contextmenu.SwingScilabContextMenu;
95 import org.scilab.modules.gui.bridge.editbox.SwingScilabEditBox;
96 import org.scilab.modules.gui.bridge.frame.SwingScilabFrame;
97 import org.scilab.modules.gui.bridge.label.SwingScilabLabel;
98 import org.scilab.modules.gui.bridge.listbox.SwingScilabListBox;
99 import org.scilab.modules.gui.bridge.menu.SwingScilabMenu;
100 import org.scilab.modules.gui.bridge.menuitem.SwingScilabMenuItem;
101 import org.scilab.modules.gui.bridge.popupmenu.SwingScilabPopupMenu;
102 import org.scilab.modules.gui.bridge.pushbutton.SwingScilabPushButton;
103 import org.scilab.modules.gui.bridge.radiobutton.SwingScilabRadioButton;
104 import org.scilab.modules.gui.bridge.slider.SwingScilabSlider;
105 import org.scilab.modules.gui.bridge.tab.SwingScilabTab;
106 import org.scilab.modules.gui.bridge.uiimage.SwingScilabUiImage;
107 import org.scilab.modules.gui.bridge.uitable.SwingScilabUiTable;
108 import org.scilab.modules.gui.bridge.waitbar.SwingScilabWaitBar;
109 import org.scilab.modules.gui.bridge.window.SwingScilabWindow;
110 import org.scilab.modules.gui.console.ScilabConsole;
111 import org.scilab.modules.gui.menubar.ScilabMenuBar;
112 import org.scilab.modules.gui.textbox.ScilabTextBox;
113 import org.scilab.modules.gui.textbox.TextBox;
114 import org.scilab.modules.gui.toolbar.ToolBar;
115 import org.scilab.modules.gui.utils.ClosingOperationsManager;
116 import org.scilab.modules.gui.utils.ToolBarBuilder;
117 import org.scilab.modules.gui.utils.WindowsConfigurationManager;
118 import org.scilab.modules.gui.widget.Widget;
121 * @author Bruno JOFRET
122 * @author Vincent COUVERT
124 public final class SwingView implements GraphicView {
126 public static final String NULLUUID = new UUID(0L, 0L).toString();
127 private static SwingView me;
128 private static boolean headless;
129 private Map<String, TypedObject> allObjects;
134 private SwingView() {
135 GraphicController.getController().register(this);
136 allObjects = Collections.synchronizedMap(new HashMap<String, TypedObject>());
139 public static void registerSwingView() {
140 DEBUG("SwingView", "calling registerSwingView()");
142 me = new SwingView();
146 public static void setHeadless(boolean headless) {
147 SwingView.headless = headless;
150 public static boolean isHeadless() {
151 return SwingView.headless;
154 public static SwingViewObject getFromId(String id) {
155 TypedObject typedObject = me.allObjects.get(id);
157 if (typedObject == null) {
161 return typedObject.getValue();
164 private enum UielementType {
187 private class TypedObject {
188 private UielementType _type;
189 private SwingViewObject _value;
190 private Set<String> _children;
192 public TypedObject(UielementType _type, SwingViewObject _value) {
194 this._value = _value;
195 this._children = Collections.synchronizedSet(new HashSet<String>());
198 public UielementType getType() {
202 public SwingViewObject getValue() {
206 public Set<String> getChildren() {
210 public void addChild(String childUID) {
211 _children.add(childUID);
214 public void removeChild(String childUID) {
215 _children.remove(childUID);
218 public boolean hasChild(String childUID) {
219 return _children.contains(childUID);
223 private static final Set<Integer> managedTypes = new HashSet<Integer>(Arrays.asList(
224 GraphicObjectProperties.__GO_FIGURE__,
225 GraphicObjectProperties.__GO_UICONTEXTMENU__,
226 GraphicObjectProperties.__GO_UIMENU__,
227 GraphicObjectProperties.__GO_CONSOLE__,
228 GraphicObjectProperties.__GO_PROGRESSIONBAR__,
229 GraphicObjectProperties.__GO_WAITBAR__,
230 GraphicObjectProperties.__GO_UICONTROL__
234 public void createObject(String id) {
236 int objectType = (Integer) GraphicController.getController().getProperty(id, __GO_TYPE__);
238 if (managedTypes.contains(objectType) == false) {
241 //System.err.println("[SwingWiew] Object Created : " + id + " with type : " + objectType);
242 boolean isValid = (Boolean) GraphicController.getController().getProperty(id, __GO_VALID__);
247 if (!headless && !GraphicsEnvironment.isHeadless()) {
248 DEBUG("SwingWiew", "Object Created : " + id + "with type : " + objectType);
249 if (objectType == __GO_FIGURE__
250 || objectType == __GO_UICONTEXTMENU__
251 || objectType == __GO_UIMENU__
252 || objectType == __GO_CONSOLE__
253 || objectType == __GO_PROGRESSIONBAR__
254 || objectType == __GO_WAITBAR__) {
255 allObjects.put(id, CreateObjectFromType(objectType, id));
259 if (objectType == __GO_UICONTROL__) {
260 int style = (Integer) GraphicController.getController().getProperty(id, __GO_STYLE__);
261 DEBUG("SwingView", "__GO_STYLE__(" + style + ")");
262 allObjects.put(id, CreateObjectFromType(style, id));
265 if (objectType == __GO_FIGURE__) {
266 Driver.setDefaultVisitor(id);
271 private UielementType StyleToEnum(int style) {
272 DEBUG("SwingView", "StyleToEnum(" + style + ")");
275 return UielementType.Figure;
276 case __GO_CONSOLE__ :
277 return UielementType.Console;
278 case __GO_UI_CHECKBOX__ :
279 return UielementType.CheckBox;
280 case __GO_UI_EDIT__ :
281 return UielementType.Edit;
282 case __GO_UI_FRAME__ :
283 return UielementType.Frame;
284 case __GO_UI_IMAGE__ :
285 return UielementType.Image;
286 case __GO_UI_LISTBOX__ :
287 return UielementType.ListBox;
288 case __GO_UI_POPUPMENU__ :
289 return UielementType.PopupMenu;
290 case __GO_UI_PUSHBUTTON__ :
291 return UielementType.PushButton;
292 case __GO_UI_RADIOBUTTON__ :
293 return UielementType.RadioButton;
294 case __GO_UI_SLIDER__ :
295 return UielementType.Slider;
296 case __GO_UI_TABLE__ :
297 return UielementType.Table;
298 case __GO_UI_TEXT__ :
299 return UielementType.Text;
301 return UielementType.UiChildMenu;
302 case __GO_UIPARENTMENU__ :
303 return UielementType.UiParentMenu;
304 case __GO_UICHILDMENU__ :
305 return UielementType.UiChildMenu;
306 case __GO_UICHECKEDMENU__ :
307 return UielementType.UiCheckedMenu;
308 case __GO_PROGRESSIONBAR__ :
309 return UielementType.Progressbar;
310 case __GO_WAITBAR__ :
311 return UielementType.Waitbar;
312 case __GO_UICONTEXTMENU__ :
313 return UielementType.UiContextMenu;
318 private TypedObject CreateObjectFromType(final int type, final String id) {
319 UielementType enumType = StyleToEnum(type);
320 return new TypedObject(enumType, CreateObjectFromType(enumType, id));
323 private SwingViewObject CreateObjectFromType(UielementType type, String id) {
326 SwingScilabCheckBox checkBox = new SwingScilabCheckBox();
328 setDefaultProperties(checkBox, id);
331 Console console = (Console) GraphicController.getController().getObjectFromId(id);
332 if (console.getScilabMode() == Console.ScilabMode.STD) {
333 WindowsConfigurationManager.restoreUUID(NULLUUID);
334 SwingScilabConsole sciConsole = ((SwingScilabConsole) ScilabConsole.getConsole().getAsSimpleConsole());
335 SwingScilabTab consoleTab = (SwingScilabTab) sciConsole.getParent();
336 consoleTab.setId(id);
342 SwingScilabEditBox edit = new SwingScilabEditBox();
344 setDefaultProperties(edit, id);
347 Figure figure = (Figure) GraphicController.getController().getObjectFromId(id);
348 String figureTitle = figure.getName();
349 Integer figureId = figure.getId();
350 if ((figureTitle != null) && (figureId != null)) {
351 figureTitle = figureTitle.replaceFirst("%d", figureId.toString());
354 SwingScilabWindow window = new SwingScilabWindow();
356 window.setTitle(figureTitle);
358 ToolBar toolBar = ToolBarBuilder.buildToolBar(SwingScilabTab.GRAPHICS_TOOLBAR_DESCRIPTOR, figureId);
360 TextBox infoBar = ScilabTextBox.createTextBox();
362 SwingScilabTab tab = new SwingScilabTab(figureTitle, figureId, figure);
365 tab.setMenuBar(ScilabMenuBar.createMenuBar());
366 tab.setToolBar(toolBar);
367 tab.setInfoBar(ScilabTextBox.createTextBox());
368 window.addMenuBar(tab.getMenuBar());
369 window.addToolBar(tab.getToolBar());
370 window.addInfoBar(tab.getInfoBar());
372 tab.setWindowIcon("graphic-window");
374 tab.setParentWindowId(window.getId());
376 tab.setEventHandler(figure.getEventHandlerString());
377 tab.setEventHandlerEnabled(figure.getEventHandlerEnable());
379 DockingManager.dock(tab, window.getDockingPort());
380 ActiveDockableTracker.requestDockableActivation(tab);
382 window.setVisible(true);
383 tab.setVisible(true);
384 tab.setName(figureTitle);
386 String infoMessage = figure.getInfoMessage();
387 if ((infoMessage == null) || (infoMessage.length() == 0)) {
390 infoBar.setText(infoMessage);
392 tab.update(__GO_SIZE__, (Integer[]) GraphicController.getController().getProperty(id, __GO_SIZE__));
393 tab.update(__GO_POSITION__, (Integer[]) GraphicController.getController().getProperty(id, __GO_POSITION__));
394 // TODO set other default properties
397 SwingScilabFrame frame = new SwingScilabFrame();
399 setDefaultProperties(frame, id);
402 SwingScilabUiImage image = new SwingScilabUiImage();
404 setDefaultProperties(image, id);
407 SwingScilabListBox listBox = new SwingScilabListBox();
409 setDefaultProperties(listBox, id);
412 SwingScilabPopupMenu popupMenu = new SwingScilabPopupMenu();
414 setDefaultProperties(popupMenu, id);
417 SwingScilabWaitBar progressbar = new SwingScilabWaitBar();
418 progressbar.setIndeterminateMode(true);
419 progressbar.setId(id);
422 SwingScilabPushButton pushButton = new SwingScilabPushButton();
423 pushButton.setId(id);
424 setDefaultProperties(pushButton, id);
427 SwingScilabRadioButton radioButton = new SwingScilabRadioButton();
428 radioButton.setId(id);
429 setDefaultProperties(radioButton, id);
432 SwingScilabSlider slider = new SwingScilabSlider();
434 setDefaultProperties(slider, id);
437 SwingScilabUiTable table = new SwingScilabUiTable();
439 setDefaultProperties(table, id);
442 SwingScilabLabel text = new SwingScilabLabel();
444 setDefaultProperties(text, id);
447 throw new UnsupportedOperationException();
448 case UiParentMenu: /* SwingView internal type */
449 SwingScilabMenu parentMenu = new SwingScilabMenu();
450 parentMenu.setId(id);
451 setMenuDefaultProperties(parentMenu, id);
453 case UiChildMenu: /* SwingView internal type */
454 SwingScilabMenuItem childMenu = new SwingScilabMenuItem();
456 setMenuDefaultProperties(childMenu, id);
458 case UiCheckedMenu: /* SwingView internal type */
459 SwingScilabCheckBoxMenuItem checkedMenu = new SwingScilabCheckBoxMenuItem(false);
460 checkedMenu.setId(id);
461 setMenuDefaultProperties(checkedMenu, id);
464 SwingScilabContextMenu contextMenu = new SwingScilabContextMenu();
465 contextMenu.setId(id);
468 SwingScilabWaitBar waitbar = new SwingScilabWaitBar();
469 waitbar.setIndeterminateMode(false);
478 * Initialize all poperties according to model
479 * @param uiMenuObject the uimenu
480 * @param id the uimenu id
482 private void setMenuDefaultProperties(Widget uiMenuObject, String id) {
483 SwingViewMenu.update(uiMenuObject, __GO_CHILDREN__,
484 (String[]) GraphicController.getController().getProperty(id, __GO_CHILDREN__));
485 SwingViewMenu.update(uiMenuObject, __GO_CALLBACK__,
486 (String) GraphicController.getController().getProperty(id, __GO_CALLBACK__));
487 SwingViewMenu.update(uiMenuObject, __GO_CALLBACKTYPE__,
488 (Integer) GraphicController.getController().getProperty(id, __GO_CALLBACKTYPE__));
489 SwingViewMenu.update(uiMenuObject, __GO_UI_CHECKED__,
490 (Boolean) GraphicController.getController().getProperty(id, __GO_UI_CHECKED__));
491 SwingViewMenu.update(uiMenuObject, __GO_UI_ENABLE__,
492 (Boolean) GraphicController.getController().getProperty(id, __GO_UI_ENABLE__));
493 SwingViewMenu.update(uiMenuObject, __GO_UI_FOREGROUNDCOLOR__,
494 (Double[]) GraphicController.getController().getProperty(id, __GO_UI_FOREGROUNDCOLOR__));
495 SwingViewMenu.update(uiMenuObject, __GO_UI_LABEL__,
496 (String) GraphicController.getController().getProperty(id, __GO_UI_LABEL__));
497 SwingViewMenu.update(uiMenuObject, __GO_UI_ICON__,
498 (String) GraphicController.getController().getProperty(id, __GO_UI_ICON__));
502 * Initialize all poperties according to model
503 * @param uiControlObject the uicontrol
504 * @param id the uicontrol id
506 private void setDefaultProperties(Widget uiControlObject, String id) {
507 /* Visible property is set first to avoid to see the object rendered before all its properties to be set (See bug #10346) */
508 SwingViewWidget.update(uiControlObject, __GO_VISIBLE__,
509 (Boolean) GraphicController.getController().getProperty(id, __GO_VISIBLE__));
510 SwingViewWidget.update(uiControlObject, __GO_UI_BACKGROUNDCOLOR__,
511 (Double[]) GraphicController.getController().getProperty(id, __GO_UI_BACKGROUNDCOLOR__));
512 SwingViewWidget.update(uiControlObject, __GO_UI_ENABLE__,
513 (Boolean) GraphicController.getController().getProperty(id, __GO_UI_ENABLE__));
514 SwingViewWidget.update(uiControlObject, __GO_UI_ENABLE__,
515 (Boolean) GraphicController.getController().getProperty(id, __GO_UI_ENABLE__));
516 SwingViewWidget.update(uiControlObject, __GO_UI_FONTANGLE__,
517 (String) GraphicController.getController().getProperty(id, __GO_UI_FONTANGLE__));
518 SwingViewWidget.update(uiControlObject, __GO_UI_FONTNAME__,
519 (String) GraphicController.getController().getProperty(id, __GO_UI_FONTNAME__));
520 SwingViewWidget.update(uiControlObject, __GO_UI_FONTUNITS__,
521 (String) GraphicController.getController().getProperty(id, __GO_UI_FONTUNITS__));
522 SwingViewWidget.update(uiControlObject, __GO_UI_FONTSIZE__,
523 (Double) GraphicController.getController().getProperty(id, __GO_UI_FONTSIZE__));
524 SwingViewWidget.update(uiControlObject, __GO_UI_FONTWEIGHT__,
525 (String) GraphicController.getController().getProperty(id, __GO_UI_FONTWEIGHT__));
526 SwingViewWidget.update(uiControlObject, __GO_UI_FOREGROUNDCOLOR__,
527 (Double[]) GraphicController.getController().getProperty(id, __GO_UI_FOREGROUNDCOLOR__));
528 SwingViewWidget.update(uiControlObject, __GO_UI_HORIZONTALALIGNMENT__,
529 (String) GraphicController.getController().getProperty(id, __GO_UI_HORIZONTALALIGNMENT__));
530 SwingViewWidget.update(uiControlObject, __GO_UI_RELIEF__,
531 (String) GraphicController.getController().getProperty(id, __GO_UI_RELIEF__));
532 SwingViewWidget.update(uiControlObject, __GO_UI_STRING__,
533 (String[]) GraphicController.getController().getProperty(id, __GO_UI_STRING__));
534 SwingViewWidget.update(uiControlObject, __GO_UI_VERTICALALIGNMENT__,
535 (String) GraphicController.getController().getProperty(id, __GO_UI_VERTICALALIGNMENT__));
536 SwingViewWidget.update(uiControlObject, __GO_POSITION__,
537 (Double[]) GraphicController.getController().getProperty(id, __GO_POSITION__));
540 public void deleteObject(String id) {
541 final TypedObject requestedObject = allObjects.get(id);
542 if (requestedObject != null) {
543 switch (requestedObject.getType()) {
545 final SwingScilabTab tab = (SwingScilabTab) requestedObject.getValue();
547 DockingManager.close(tab);
548 DockingManager.unregisterDockable((Dockable) tab);
549 ClosingOperationsManager.unregisterClosingOperation(tab);
550 ClosingOperationsManager.removeDependency(tab);
551 ClosingOperationsManager.checkTabForClosing(tab);
556 SwingScilabWaitBar bar = (SwingScilabWaitBar) requestedObject.getValue();
561 // uicontrol case: the object is destroyed when its parent updates its children
564 allObjects.remove(id);
569 public void updateObject(final String id, final int property) {
570 final TypedObject registeredObject = allObjects.get(id);
571 if (registeredObject == null && property != __GO_STYLE__) {
575 /* On uicontrol style is set after object creation */
576 if (registeredObject == null && property == __GO_STYLE__) {
577 int style = (Integer) GraphicController.getController().getProperty(id, __GO_STYLE__);
578 allObjects.put(id, CreateObjectFromType(style, id));
582 if (SwingUtilities.isEventDispatchThread()) {
583 updateObjectOnEDT(registeredObject, id, property);
586 SwingUtilities.invokeAndWait(new Runnable() {
589 updateObjectOnEDT(registeredObject, id, property);
592 } catch (InterruptedException e) {
593 // TODO Auto-generated catch block
595 } catch (InvocationTargetException e) {
596 // TODO Auto-generated catch block
602 public void updateObjectOnEDT(TypedObject registeredObject, final String id, final int property) {
603 /* Removes the swing object if its parent is not display */
604 if (registeredObject != null && property == __GO_PARENT__) {
605 String parentId = (String) GraphicController.getController().getProperty(id, __GO_PARENT__);
606 TypedObject registeredParent = allObjects.get(parentId);
607 if (registeredParent == null) {
608 allObjects.remove(id);
612 int type = (Integer) GraphicController.getController().getProperty(id, __GO_TYPE__);
613 /* Children list update */
614 if (registeredObject != null && property == __GO_CHILDREN__) {
615 final String[] newChildren = (String[]) GraphicController.getController().getProperty(id, __GO_CHILDREN__);
619 * FIGURE CHILDREN UPDATE
622 updateFigureChildren(registeredObject, newChildren);
625 * CONSOLE CHILDREN UPDATE
627 case __GO_CONSOLE__ :
628 updateConsoleChildren(registeredObject, newChildren);
631 * MENU CHILDREN UPDATE
634 updateMenuChildren(registeredObject, id, newChildren);
637 * CONTEXTMENU CHILDREN UPDATE
639 case __GO_UICONTEXTMENU__ :
640 updateContextMenuChildren(registeredObject, newChildren);
643 * UICONTROL "FRAME" CHILDREN UPDATE
645 case __GO_UICONTROL__ :
646 int style = (Integer) GraphicController.getController().getProperty(id, __GO_STYLE__);
647 if (style == __GO_UI_FRAME__) {
648 updateFrameChildren(registeredObject, newChildren);
655 * When the CHECKED property is updated for a UIMENU,
656 * the object is converted to a SwingScilabCheckBoxMenuItem is not already of this type
658 if (registeredObject != null && property == __GO_UI_CHECKED__) {
659 if (type == __GO_UIMENU__) {
660 switch (registeredObject.getType()) {
662 SwingScilabMenu meAsAMenu = (SwingScilabMenu) registeredObject.getValue();
663 Container parent = meAsAMenu.getParent();
664 parent.remove(meAsAMenu);
665 registeredObject = CreateObjectFromType(__GO_UICHECKEDMENU__, id);
666 allObjects.put(id, registeredObject);
667 SwingScilabCheckBoxMenuItem meAsAMenuItem = (SwingScilabCheckBoxMenuItem) registeredObject.getValue();
668 parent.add(meAsAMenuItem);
671 String parentId = (String) GraphicController.getController().getProperty(id, __GO_PARENT__);
672 SwingScilabMenuItem childMenu = (SwingScilabMenuItem) allObjects.get(id).getValue();
673 SwingScilabMenu parentMenu = (SwingScilabMenu) allObjects.get(parentId).getValue();
675 int index = parentMenu.getComponentZOrder(childMenu);
676 parentMenu.remove(childMenu);
677 registeredObject = CreateObjectFromType(__GO_UICHECKEDMENU__, id);
678 allObjects.put(id, registeredObject);
679 registeredObject = allObjects.get(id);
680 parentMenu.add((SwingScilabCheckBoxMenuItem) registeredObject.getValue(), index);
689 * When the SEPARATOR property is updated for a UIMENU,
690 * When the property is set to TRUE: A separator is added if not already done
691 * When the property is set to FALSE: The previous separator is removed if it exists
693 if (registeredObject != null && property == __GO_UI_SEPARATOR__) {
694 if (type == __GO_UIMENU__) {
695 String parentId = (String) GraphicController.getController().getProperty(id, __GO_PARENT__);
696 int menuPosition = -1;
697 Component currentComponent = (Component) registeredObject.getValue();
698 Component[] allChildren = ((SwingScilabMenu) allObjects.get(parentId).getValue()).getMenuComponents();
699 for (int kChild = 0; kChild < allChildren.length; kChild++) {
700 if (allChildren[kChild] == currentComponent) {
701 menuPosition = kChild;
705 if (menuPosition < 0) {
708 boolean newSeparatorMode = (Boolean) GraphicController.getController().getProperty(id, __GO_UI_SEPARATOR__);
709 if (newSeparatorMode) { // Add a separator above the menu
710 ((SwingScilabMenu) allObjects.get(parentId).getValue()).insertSeparator(menuPosition);
711 } else { // Remove the separator above the menu (if there is one)
712 if (menuPosition == 0) { // There is no object above the menu
715 if (((SwingScilabMenu) allObjects.get(parentId).getValue()).getComponent(menuPosition - 1) instanceof JSeparator) {
716 ((SwingScilabMenu) allObjects.get(parentId).getValue()).remove(menuPosition - 1);
722 if (registeredObject != null) {
723 final SwingViewObject swingObject = registeredObject.getValue();
724 if (swingObject != null) {
725 swingObject.update(property, GraphicController.getController().getProperty(id, property));
731 * Update the figure children (called by generic updateObject method)
732 * @param id the id of the figure
733 * @param newChildren the new children IDs list
735 private void updateFigureChildren(TypedObject updatedObject, String[] newChildren) {
736 Container updatedComponent = (SwingScilabTab) updatedObject.getValue();
737 boolean needRevalidate = false;
740 for (String childId : newChildren) {
741 if (!updatedObject.hasChild(childId)) {
744 updatedObject.addChild(childId);
746 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
748 /* Add an uicontrol */
749 if (childType == __GO_UICONTROL__) {
750 ((SwingScilabTab) updatedComponent).addMember(allObjects.get(childId).getValue());
751 needRevalidate = true;
755 if (childType == __GO_UIMENU__) {
756 TypedObject childAsTypedObject = allObjects.get(childId);
757 switch (childAsTypedObject.getType()) {
760 allObjects.remove(childId);
761 allObjects.put(childId, CreateObjectFromType(__GO_UIPARENTMENU__, childId));
762 ((Container) ((SwingScilabTab) updatedComponent).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) allObjects.get(childId).getValue());
764 default: /* UiParentMenu */
765 ((Container) ((SwingScilabTab) updatedComponent).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) allObjects.get(childId).getValue());
768 needRevalidate = true;
773 // Remove children which have been deleted
774 Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
775 // Clone the children set to avoid concurrent accesses
776 String[] oldChildrenSet = updatedObject.getChildren().toArray(new String[updatedObject.getChildren().size()]);
777 for (String childId : oldChildrenSet) {
778 if (!newChildrenSet.contains(childId)) {
781 updatedObject.removeChild(childId);
783 final Object childTypeObject = GraphicController.getController().getProperty(childId, __GO_TYPE__);
784 if (childTypeObject == null) {
785 // the child has already been removed from the model
788 int childType = (Integer) childTypeObject;
790 /* Remove an uicontrol */
791 if (childType == __GO_UICONTROL__) {
792 ((SwingScilabTab) updatedComponent).removeMember(allObjects.get(childId).getValue());
793 needRevalidate = true;
796 /* Remove an uimenu */
797 if (childType == __GO_UIMENU__) {
798 TypedObject childAsTypedObject = allObjects.get(childId);
799 switch (childAsTypedObject.getType()) {
801 ((Container) ((SwingScilabTab) updatedComponent).getMenuBar().getAsSimpleMenuBar()).remove((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
803 default: /* UiParentMenu */
804 ((Container) ((SwingScilabTab) updatedComponent).getMenuBar().getAsSimpleMenuBar()).remove((SwingScilabMenu) allObjects.get(childId).getValue());
807 needRevalidate = true;
811 if (needRevalidate && updatedComponent != null) {
812 ((View) updatedComponent).revalidate();
817 * Update the frame children (called by generic updateObject method)
818 * @param id the id of the figure
819 * @param newChildren the new children IDs list
821 private void updateFrameChildren(TypedObject updatedObject, String[] newChildren) {
822 Container updatedComponent = (SwingScilabFrame) updatedObject.getValue();
823 boolean needRevalidate = false;
826 for (String childId : newChildren) {
827 if (!updatedObject.hasChild(childId)) {
830 updatedObject.addChild(childId);
832 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
834 /* Add an uicontrol */
835 if (childType == __GO_UICONTROL__) {
836 ((SwingScilabFrame) updatedComponent).addMember(allObjects.get(childId).getValue());
837 needRevalidate = true;
842 // Remove children which have been deleted
843 Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
844 // Clone the children set to avoid concurrent accesses
845 String[] oldChildrenSet = updatedObject.getChildren().toArray(new String[updatedObject.getChildren().size()]);
846 for (String childId : oldChildrenSet) {
847 if (!newChildrenSet.contains(childId)) {
850 updatedObject.removeChild(childId);
852 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
854 /* Remove an uicontrol */
855 if (childType == __GO_UICONTROL__) {
856 updatedComponent.remove((Component) allObjects.get(childId).getValue());
857 needRevalidate = true;
861 if (needRevalidate && updatedComponent != null) {
862 ((JPanel) updatedComponent).revalidate();
867 * Update the Console menus (called by generic updateObject method)
868 * @param id the id of the console object
869 * @param newChildren the new children IDs list
871 private void updateConsoleChildren(TypedObject updatedObject, String[] newChildren) {
872 Container updatedComponent = (SwingScilabTab) updatedObject.getValue();
873 boolean needRevalidate = false;
876 for (String childId : newChildren) {
877 if (!updatedObject.hasChild(childId)) {
880 updatedObject.addChild(childId);
882 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
884 if (childType == __GO_UIMENU__) {
885 TypedObject childAsTypedObject = allObjects.get(childId);
886 switch (childAsTypedObject.getType()) {
889 TypedObject newUiParentMenu = CreateObjectFromType(__GO_UIPARENTMENU__, childId);
890 allObjects.put(childId, newUiParentMenu);
891 ((Container) ((SwingScilabTab) updatedObject.getValue()).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) newUiParentMenu.getValue());
893 default: /* UiParentMenu */
894 ((Container) ((SwingScilabTab) updatedObject.getValue()).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) childAsTypedObject.getValue());
897 needRevalidate = true;
902 // Remove children which have been deleted
903 Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
904 Object[] updatedObjectChildren = updatedObject.getChildren().toArray();
905 for (int i = 0 ; i < updatedObjectChildren.length ; ++i) {
906 String childId = (String) updatedObjectChildren[i];
907 if (!newChildrenSet.contains(childId)) {
910 updatedObject.removeChild(childId);
912 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
914 if (childType == __GO_UIMENU__) {
915 ((Container) ((SwingScilabTab) updatedObject.getValue()).getMenuBar().getAsSimpleMenuBar()).remove((SwingScilabMenu) allObjects.get(childId).getValue());
916 needRevalidate = true;
920 if (needRevalidate && updatedComponent != null) {
921 updatedComponent.validate();
927 * Update the children of a menu menus (called by generic updateObject method)
928 * @param id the id of the menu object
929 * @param newChildren the new children IDs list
931 private void updateMenuChildren(TypedObject updatedObject, String id, String[] newChildren) {
932 Container updatedComponent = null;
933 boolean needRevalidate = false;
934 int updatedObjectPosition = 0;
935 TypedObject newParent = null;
938 for (String childId : newChildren) {
939 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
940 if (childType == __GO_UIMENU__) {
941 if (!updatedObject.hasChild(childId)) {
943 updatedObject.addChild(childId);
945 TypedObject childAsTypedObject = allObjects.get(childId);
946 Object addedChild = allObjects.get(childId).getValue();
947 JComponent parent = null;
948 switch (updatedObject.getType()) {
950 updatedComponent = (SwingScilabMenuItem) updatedObject.getValue();
951 parent = (JComponent) updatedComponent.getParent();
952 switch (childAsTypedObject.getType()) {
954 /* Replace the item by a parent menu */
955 updatedObjectPosition = parent.getComponentZOrder((SwingScilabMenuItem) updatedObject.getValue());
956 parent.remove((SwingScilabMenuItem) updatedObject.getValue());
957 newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
958 allObjects.put(id, newParent);
959 newParent.addChild(childId);
960 parent.add((SwingScilabMenu) newParent.getValue(), updatedObjectPosition);
961 /* Update the created menu */
962 ((SwingScilabMenu) newParent.getValue()).add((SwingScilabMenuItem) addedChild);
965 /* Replace the item by a parent menu */
966 updatedObjectPosition = parent.getComponentZOrder((SwingScilabCheckBoxMenuItem) updatedObject.getValue());
967 parent.remove((SwingScilabCheckBoxMenuItem) updatedObject.getValue());
968 newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
969 allObjects.put(id, newParent);
970 newParent.addChild(childId);
971 parent.add((SwingScilabMenu) newParent.getValue(), updatedObjectPosition);
972 /* Update the created menu */
973 ((SwingScilabMenu) newParent.getValue()).add((SwingScilabCheckBoxMenuItem) addedChild);
975 default: /* UiParentMenu */
976 ((SwingScilabMenu) updatedObject.getValue()).add((SwingScilabMenu) addedChild);
981 updatedComponent = (SwingScilabCheckBoxMenuItem) updatedObject.getValue();
982 parent = (JComponent) updatedComponent.getParent();
983 switch (childAsTypedObject.getType()) {
985 /* Replace the item by a parent menu */
986 updatedObjectPosition = parent.getComponentZOrder((SwingScilabCheckBoxMenuItem) updatedObject.getValue());
987 parent.remove((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
988 newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
989 allObjects.put(id, newParent);
990 newParent.addChild(childId);
991 parent.add((SwingScilabMenu) newParent.getValue(), updatedObjectPosition);
992 /* Update the created menu */
993 ((SwingScilabMenu) newParent.getValue()).add((SwingScilabMenuItem) allObjects.get(childId).getValue());
996 /* Replace the item by a parent menu */
997 updatedObjectPosition = parent.getComponentZOrder((SwingScilabCheckBoxMenuItem) updatedObject.getValue());
998 parent.remove((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
999 newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
1000 allObjects.put(id, newParent);
1001 newParent.addChild(childId);
1002 parent.add((SwingScilabMenu) newParent.getValue(), updatedObjectPosition);
1003 /* Update the created menu */
1004 ((SwingScilabMenu) newParent.getValue()).add((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
1006 default: /* UiParentMenu */
1007 System.out.println("childAsTypedObject.getType() = UiParentMenu");
1008 ((SwingScilabMenu) updatedObject.getValue()).add((SwingScilabMenu) allObjects.get(childId).getValue());
1012 default: /* UiParentMenu */
1013 SwingScilabMenu updatedMenu = (SwingScilabMenu) updatedObject.getValue();
1014 updatedComponent = updatedMenu;
1015 switch (childAsTypedObject.getType()) {
1017 updatedMenu.add((SwingScilabMenuItem) allObjects.get(childId).getValue());
1020 updatedMenu.add((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
1022 default: /* UiParentMenu */
1023 /* Java can not add a JMenu in a JMenu */
1024 /* We need to convert the child into a MenuItem */
1025 TypedObject newMenu = CreateObjectFromType(__GO_UIMENU__, childId);
1026 allObjects.put(childId, newMenu);
1027 updatedMenu.add((SwingScilabMenuItem) newMenu.getValue());
1032 needRevalidate = true;
1036 // Remove children which have been deleted
1037 Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
1038 String[] oldChildren = updatedObject.getChildren().toArray(new String[updatedObject.getChildren().size()]);
1039 for (int childIndex = 0; childIndex < oldChildren.length; childIndex++) {
1040 String childId = oldChildren[childIndex];
1041 if (!newChildrenSet.contains(childId)) {
1043 updatedObject.removeChild(childId);
1044 updatedComponent = (SwingScilabMenu) updatedObject.getValue();
1046 TypedObject childAsTypedObject = allObjects.get(childId);
1047 Object removedMenu = childAsTypedObject.getValue();
1049 switch (childAsTypedObject.getType()) {
1051 updatedComponent.remove((SwingScilabMenuItem) removedMenu);
1054 updatedComponent.remove((SwingScilabCheckBoxMenuItem) removedMenu);
1057 updatedComponent.remove((SwingScilabMenu) removedMenu);
1060 needRevalidate = true;
1063 if (needRevalidate && updatedComponent != null) {
1064 updatedComponent.validate();
1068 * Update the children of a contextmenu (called by generic updateObject method)
1069 * @param id the id of the contextmenu object
1070 * @param newChildren the new children IDs list
1072 private void updateContextMenuChildren(TypedObject updatedObject, String[] newChildren) {
1073 Container updatedComponent = null;
1074 boolean needRevalidate = false;
1077 for (String childId : newChildren) {
1078 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1079 if (childType == __GO_UIMENU__) {
1080 if (!updatedObject.hasChild(childId)) {
1083 updatedObject.addChild(childId);
1085 TypedObject childAsTypedObject = allObjects.get(childId);
1086 SwingScilabContextMenu updatedMenu = (SwingScilabContextMenu) updatedObject.getValue();
1087 updatedComponent = updatedMenu;
1088 switch (childAsTypedObject.getType()) {
1090 updatedMenu.add((SwingScilabMenuItem) allObjects.get(childId).getValue());
1093 updatedMenu.add((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
1095 default: /* UiParentMenu */
1096 updatedMenu.add((SwingScilabMenu) allObjects.get(childId).getValue());
1099 needRevalidate = true;
1103 // Remove children which have been deleted
1104 Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
1105 for (String childId : updatedObject.getChildren()) {
1106 if (!newChildrenSet.contains(childId)) {
1108 updatedObject.removeChild(childId);
1109 updatedComponent = (SwingScilabMenu) updatedObject.getValue();
1111 TypedObject childAsTypedObject = allObjects.get(childId);
1112 SwingScilabContextMenu updatedMenu = (SwingScilabContextMenu) updatedObject.getValue();
1113 Object removedMenu = allObjects.get(childId).getValue();
1115 switch (childAsTypedObject.getType()) {
1117 updatedMenu.remove((SwingScilabMenuItem) removedMenu);
1120 updatedMenu.remove((SwingScilabCheckBoxMenuItem) removedMenu);
1123 updatedMenu.remove((SwingScilabMenu) removedMenu);
1126 needRevalidate = true;
1129 if (needRevalidate && updatedComponent != null) {
1130 updatedComponent.validate();