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 final UielementType enumType = StyleToEnum(type);
320 final SwingViewObject newSVObject[] = new SwingViewObject[1];
321 if (SwingUtilities.isEventDispatchThread()) {
322 newSVObject[0] = CreateObjectFromType(enumType, id);
325 SwingUtilities.invokeAndWait(new Runnable() {
329 newSVObject[0] = CreateObjectFromType(enumType, id);
333 } catch (InterruptedException e) {
334 // TODO Auto-generated catch block
336 } catch (InvocationTargetException e) {
337 // TODO Auto-generated catch block
341 return new TypedObject(enumType, newSVObject[0]);
344 private SwingViewObject CreateObjectFromType(UielementType type, String id) {
347 SwingScilabCheckBox checkBox = new SwingScilabCheckBox();
349 setDefaultProperties(checkBox, id);
352 Console console = (Console) GraphicController.getController().getObjectFromId(id);
353 if (console.getScilabMode() == Console.ScilabMode.STD) {
354 WindowsConfigurationManager.restoreUUID(NULLUUID);
355 SwingScilabConsole sciConsole = ((SwingScilabConsole) ScilabConsole.getConsole().getAsSimpleConsole());
356 SwingScilabTab consoleTab = (SwingScilabTab) sciConsole.getParent();
357 consoleTab.setId(id);
363 SwingScilabEditBox edit = new SwingScilabEditBox();
365 setDefaultProperties(edit, id);
368 Figure figure = (Figure) GraphicController.getController().getObjectFromId(id);
369 String figureTitle = figure.getName();
370 Integer figureId = figure.getId();
371 if ((figureTitle != null) && (figureId != null)) {
372 figureTitle = figureTitle.replaceFirst("%d", figureId.toString());
375 SwingScilabWindow window = new SwingScilabWindow();
377 window.setTitle(figureTitle);
379 ToolBar toolBar = ToolBarBuilder.buildToolBar(SwingScilabTab.GRAPHICS_TOOLBAR_DESCRIPTOR, figureId);
381 TextBox infoBar = ScilabTextBox.createTextBox();
383 SwingScilabTab tab = new SwingScilabTab(figureTitle, figureId, figure);
386 tab.setMenuBar(ScilabMenuBar.createMenuBar());
387 tab.setToolBar(toolBar);
388 tab.setInfoBar(ScilabTextBox.createTextBox());
389 window.addMenuBar(tab.getMenuBar());
390 window.addToolBar(tab.getToolBar());
391 window.addInfoBar(tab.getInfoBar());
393 tab.setWindowIcon("graphic-window");
395 tab.setParentWindowId(window.getId());
397 tab.setEventHandler(figure.getEventHandlerString());
398 tab.setEventHandlerEnabled(figure.getEventHandlerEnable());
400 DockingManager.dock(tab, window.getDockingPort());
401 ActiveDockableTracker.requestDockableActivation(tab);
403 window.setVisible(true);
404 tab.setVisible(true);
405 tab.setName(figureTitle);
407 String infoMessage = figure.getInfoMessage();
408 if ((infoMessage == null) || (infoMessage.length() == 0)) {
411 infoBar.setText(infoMessage);
413 tab.update(__GO_SIZE__, (Integer[]) GraphicController.getController().getProperty(id, __GO_SIZE__));
414 tab.update(__GO_POSITION__, (Integer[]) GraphicController.getController().getProperty(id, __GO_POSITION__));
415 // TODO set other default properties
418 SwingScilabFrame frame = new SwingScilabFrame();
420 setDefaultProperties(frame, id);
423 SwingScilabUiImage image = new SwingScilabUiImage();
425 setDefaultProperties(image, id);
428 SwingScilabListBox listBox = new SwingScilabListBox();
430 setDefaultProperties(listBox, id);
433 SwingScilabPopupMenu popupMenu = new SwingScilabPopupMenu();
435 setDefaultProperties(popupMenu, id);
438 SwingScilabWaitBar progressbar = new SwingScilabWaitBar();
439 progressbar.setIndeterminateMode(true);
440 progressbar.setId(id);
443 SwingScilabPushButton pushButton = new SwingScilabPushButton();
444 pushButton.setId(id);
445 setDefaultProperties(pushButton, id);
448 SwingScilabRadioButton radioButton = new SwingScilabRadioButton();
449 radioButton.setId(id);
450 setDefaultProperties(radioButton, id);
453 SwingScilabSlider slider = new SwingScilabSlider();
455 setDefaultProperties(slider, id);
458 SwingScilabUiTable table = new SwingScilabUiTable();
460 setDefaultProperties(table, id);
463 SwingScilabLabel text = new SwingScilabLabel();
465 setDefaultProperties(text, id);
468 throw new UnsupportedOperationException();
469 case UiParentMenu: /* SwingView internal type */
470 SwingScilabMenu parentMenu = new SwingScilabMenu();
471 parentMenu.setId(id);
472 setMenuDefaultProperties(parentMenu, id);
474 case UiChildMenu: /* SwingView internal type */
475 SwingScilabMenuItem childMenu = new SwingScilabMenuItem();
477 setMenuDefaultProperties(childMenu, id);
479 case UiCheckedMenu: /* SwingView internal type */
480 SwingScilabCheckBoxMenuItem checkedMenu = new SwingScilabCheckBoxMenuItem(false);
481 checkedMenu.setId(id);
482 setMenuDefaultProperties(checkedMenu, id);
485 SwingScilabContextMenu contextMenu = new SwingScilabContextMenu();
486 contextMenu.setId(id);
489 SwingScilabWaitBar waitbar = new SwingScilabWaitBar();
490 waitbar.setIndeterminateMode(false);
499 * Initialize all poperties according to model
500 * @param uiMenuObject the uimenu
501 * @param id the uimenu id
503 private void setMenuDefaultProperties(Widget uiMenuObject, String id) {
504 SwingViewMenu.update(uiMenuObject, __GO_CHILDREN__,
505 (String[]) GraphicController.getController().getProperty(id, __GO_CHILDREN__));
506 SwingViewMenu.update(uiMenuObject, __GO_CALLBACK__,
507 (String) GraphicController.getController().getProperty(id, __GO_CALLBACK__));
508 SwingViewMenu.update(uiMenuObject, __GO_CALLBACKTYPE__,
509 (Integer) GraphicController.getController().getProperty(id, __GO_CALLBACKTYPE__));
510 SwingViewMenu.update(uiMenuObject, __GO_UI_CHECKED__,
511 (Boolean) GraphicController.getController().getProperty(id, __GO_UI_CHECKED__));
512 SwingViewMenu.update(uiMenuObject, __GO_UI_ENABLE__,
513 (Boolean) GraphicController.getController().getProperty(id, __GO_UI_ENABLE__));
514 SwingViewMenu.update(uiMenuObject, __GO_UI_FOREGROUNDCOLOR__,
515 (Double[]) GraphicController.getController().getProperty(id, __GO_UI_FOREGROUNDCOLOR__));
516 SwingViewMenu.update(uiMenuObject, __GO_UI_LABEL__,
517 (String) GraphicController.getController().getProperty(id, __GO_UI_LABEL__));
518 SwingViewMenu.update(uiMenuObject, __GO_UI_ICON__,
519 (String) GraphicController.getController().getProperty(id, __GO_UI_ICON__));
523 * Initialize all poperties according to model
524 * @param uiControlObject the uicontrol
525 * @param id the uicontrol id
527 private void setDefaultProperties(Widget uiControlObject, String id) {
528 /* Visible property is set first to avoid to see the object rendered before all its properties to be set (See bug #10346) */
529 SwingViewWidget.update(uiControlObject, __GO_VISIBLE__,
530 (Boolean) GraphicController.getController().getProperty(id, __GO_VISIBLE__));
531 SwingViewWidget.update(uiControlObject, __GO_UI_BACKGROUNDCOLOR__,
532 (Double[]) GraphicController.getController().getProperty(id, __GO_UI_BACKGROUNDCOLOR__));
533 SwingViewWidget.update(uiControlObject, __GO_UI_ENABLE__,
534 (Boolean) GraphicController.getController().getProperty(id, __GO_UI_ENABLE__));
535 SwingViewWidget.update(uiControlObject, __GO_UI_ENABLE__,
536 (Boolean) GraphicController.getController().getProperty(id, __GO_UI_ENABLE__));
537 SwingViewWidget.update(uiControlObject, __GO_UI_FONTANGLE__,
538 (String) GraphicController.getController().getProperty(id, __GO_UI_FONTANGLE__));
539 SwingViewWidget.update(uiControlObject, __GO_UI_FONTNAME__,
540 (String) GraphicController.getController().getProperty(id, __GO_UI_FONTNAME__));
541 SwingViewWidget.update(uiControlObject, __GO_UI_FONTUNITS__,
542 (String) GraphicController.getController().getProperty(id, __GO_UI_FONTUNITS__));
543 SwingViewWidget.update(uiControlObject, __GO_UI_FONTSIZE__,
544 (Double) GraphicController.getController().getProperty(id, __GO_UI_FONTSIZE__));
545 SwingViewWidget.update(uiControlObject, __GO_UI_FONTWEIGHT__,
546 (String) GraphicController.getController().getProperty(id, __GO_UI_FONTWEIGHT__));
547 SwingViewWidget.update(uiControlObject, __GO_UI_FOREGROUNDCOLOR__,
548 (Double[]) GraphicController.getController().getProperty(id, __GO_UI_FOREGROUNDCOLOR__));
549 SwingViewWidget.update(uiControlObject, __GO_UI_HORIZONTALALIGNMENT__,
550 (String) GraphicController.getController().getProperty(id, __GO_UI_HORIZONTALALIGNMENT__));
551 SwingViewWidget.update(uiControlObject, __GO_UI_RELIEF__,
552 (String) GraphicController.getController().getProperty(id, __GO_UI_RELIEF__));
553 SwingViewWidget.update(uiControlObject, __GO_UI_STRING__,
554 (String[]) GraphicController.getController().getProperty(id, __GO_UI_STRING__));
555 SwingViewWidget.update(uiControlObject, __GO_UI_VERTICALALIGNMENT__,
556 (String) GraphicController.getController().getProperty(id, __GO_UI_VERTICALALIGNMENT__));
557 SwingViewWidget.update(uiControlObject, __GO_POSITION__,
558 (Double[]) GraphicController.getController().getProperty(id, __GO_POSITION__));
561 public void deleteObject(String id) {
562 final TypedObject requestedObject = allObjects.get(id);
563 if (requestedObject != null) {
564 switch (requestedObject.getType()) {
566 final SwingScilabTab tab = (SwingScilabTab) requestedObject.getValue();
568 DockingManager.close(tab);
569 DockingManager.unregisterDockable((Dockable) tab);
570 ClosingOperationsManager.unregisterClosingOperation(tab);
571 ClosingOperationsManager.removeDependency(tab);
572 ClosingOperationsManager.checkTabForClosing(tab);
577 SwingScilabWaitBar bar = (SwingScilabWaitBar) requestedObject.getValue();
582 // uicontrol case: the object is destroyed when its parent updates its children
585 allObjects.remove(id);
590 public void updateObject(final String id, final int property) {
591 final TypedObject registeredObject = allObjects.get(id);
592 if (registeredObject == null && property != __GO_STYLE__) {
596 /* On uicontrol style is set after object creation */
597 if (registeredObject == null && property == __GO_STYLE__) {
598 int style = (Integer) GraphicController.getController().getProperty(id, __GO_STYLE__);
599 allObjects.put(id, CreateObjectFromType(style, id));
603 if (SwingUtilities.isEventDispatchThread()) {
604 updateObjectOnEDT(registeredObject, id, property);
607 SwingUtilities.invokeAndWait(new Runnable() {
610 updateObjectOnEDT(registeredObject, id, property);
613 } catch (InterruptedException e) {
614 // TODO Auto-generated catch block
616 } catch (InvocationTargetException e) {
617 // TODO Auto-generated catch block
623 public void updateObjectOnEDT(TypedObject registeredObject, final String id, final int property) {
624 /* Removes the swing object if its parent is not display */
625 if (registeredObject != null && property == __GO_PARENT__) {
626 String parentId = (String) GraphicController.getController().getProperty(id, __GO_PARENT__);
627 TypedObject registeredParent = allObjects.get(parentId);
628 if (registeredParent == null) {
629 allObjects.remove(id);
633 int type = (Integer) GraphicController.getController().getProperty(id, __GO_TYPE__);
634 /* Children list update */
635 if (registeredObject != null && property == __GO_CHILDREN__) {
636 final String[] newChildren = (String[]) GraphicController.getController().getProperty(id, __GO_CHILDREN__);
640 * FIGURE CHILDREN UPDATE
643 updateFigureChildren(registeredObject, newChildren);
646 * CONSOLE CHILDREN UPDATE
648 case __GO_CONSOLE__ :
649 updateConsoleChildren(registeredObject, newChildren);
652 * MENU CHILDREN UPDATE
655 updateMenuChildren(registeredObject, id, newChildren);
658 * CONTEXTMENU CHILDREN UPDATE
660 case __GO_UICONTEXTMENU__ :
661 updateContextMenuChildren(registeredObject, newChildren);
664 * UICONTROL "FRAME" CHILDREN UPDATE
666 case __GO_UICONTROL__ :
667 int style = (Integer) GraphicController.getController().getProperty(id, __GO_STYLE__);
668 if (style == __GO_UI_FRAME__) {
669 updateFrameChildren(registeredObject, newChildren);
676 * When the CHECKED property is updated for a UIMENU,
677 * the object is converted to a SwingScilabCheckBoxMenuItem is not already of this type
679 if (registeredObject != null && property == __GO_UI_CHECKED__) {
680 if (type == __GO_UIMENU__) {
681 switch (registeredObject.getType()) {
683 SwingScilabMenu meAsAMenu = (SwingScilabMenu) registeredObject.getValue();
684 Container parent = meAsAMenu.getParent();
685 parent.remove(meAsAMenu);
686 registeredObject = CreateObjectFromType(__GO_UICHECKEDMENU__, id);
687 allObjects.put(id, registeredObject);
688 SwingScilabCheckBoxMenuItem meAsAMenuItem = (SwingScilabCheckBoxMenuItem) registeredObject.getValue();
689 parent.add(meAsAMenuItem);
692 String parentId = (String) GraphicController.getController().getProperty(id, __GO_PARENT__);
693 SwingScilabMenuItem childMenu = (SwingScilabMenuItem) allObjects.get(id).getValue();
694 SwingScilabMenu parentMenu = (SwingScilabMenu) allObjects.get(parentId).getValue();
696 int index = parentMenu.getComponentZOrder(childMenu);
697 parentMenu.remove(childMenu);
698 registeredObject = CreateObjectFromType(__GO_UICHECKEDMENU__, id);
699 allObjects.put(id, registeredObject);
700 registeredObject = allObjects.get(id);
701 parentMenu.add((SwingScilabCheckBoxMenuItem) registeredObject.getValue(), index);
710 * When the SEPARATOR property is updated for a UIMENU,
711 * When the property is set to TRUE: A separator is added if not already done
712 * When the property is set to FALSE: The previous separator is removed if it exists
714 if (registeredObject != null && property == __GO_UI_SEPARATOR__) {
715 if (type == __GO_UIMENU__) {
716 String parentId = (String) GraphicController.getController().getProperty(id, __GO_PARENT__);
717 int menuPosition = -1;
718 Component currentComponent = (Component) registeredObject.getValue();
719 Component[] allChildren = ((SwingScilabMenu) allObjects.get(parentId).getValue()).getMenuComponents();
720 for (int kChild = 0; kChild < allChildren.length; kChild++) {
721 if (allChildren[kChild] == currentComponent) {
722 menuPosition = kChild;
726 if (menuPosition < 0) {
729 boolean newSeparatorMode = (Boolean) GraphicController.getController().getProperty(id, __GO_UI_SEPARATOR__);
730 if (newSeparatorMode) { // Add a separator above the menu
731 ((SwingScilabMenu) allObjects.get(parentId).getValue()).insertSeparator(menuPosition);
732 } else { // Remove the separator above the menu (if there is one)
733 if (menuPosition == 0) { // There is no object above the menu
736 if (((SwingScilabMenu) allObjects.get(parentId).getValue()).getComponent(menuPosition - 1) instanceof JSeparator) {
737 ((SwingScilabMenu) allObjects.get(parentId).getValue()).remove(menuPosition - 1);
743 if (registeredObject != null) {
744 final SwingViewObject swingObject = registeredObject.getValue();
745 if (swingObject != null) {
746 swingObject.update(property, GraphicController.getController().getProperty(id, property));
752 * Update the figure children (called by generic updateObject method)
753 * @param id the id of the figure
754 * @param newChildren the new children IDs list
756 private void updateFigureChildren(TypedObject updatedObject, String[] newChildren) {
757 Container updatedComponent = (SwingScilabTab) updatedObject.getValue();
758 boolean needRevalidate = false;
761 for (String childId : newChildren) {
762 if (!updatedObject.hasChild(childId)) {
765 updatedObject.addChild(childId);
767 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
769 /* Add an uicontrol */
770 if (childType == __GO_UICONTROL__) {
771 ((SwingScilabTab) updatedComponent).addMember(allObjects.get(childId).getValue());
772 needRevalidate = true;
776 if (childType == __GO_UIMENU__) {
777 TypedObject childAsTypedObject = allObjects.get(childId);
778 switch (childAsTypedObject.getType()) {
781 allObjects.remove(childId);
782 allObjects.put(childId, CreateObjectFromType(__GO_UIPARENTMENU__, childId));
783 ((Container) ((SwingScilabTab) updatedComponent).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) allObjects.get(childId).getValue());
785 default: /* UiParentMenu */
786 ((Container) ((SwingScilabTab) updatedComponent).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) allObjects.get(childId).getValue());
789 needRevalidate = true;
794 // Remove children which have been deleted
795 Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
796 // Clone the children set to avoid concurrent accesses
797 String[] oldChildrenSet = updatedObject.getChildren().toArray(new String[updatedObject.getChildren().size()]);
798 for (String childId : oldChildrenSet) {
799 if (!newChildrenSet.contains(childId)) {
802 updatedObject.removeChild(childId);
804 final Object childTypeObject = GraphicController.getController().getProperty(childId, __GO_TYPE__);
805 if (childTypeObject == null) {
806 // the child has already been removed from the model
809 int childType = (Integer) childTypeObject;
811 /* Remove an uicontrol */
812 if (childType == __GO_UICONTROL__) {
813 ((SwingScilabTab) updatedComponent).removeMember(allObjects.get(childId).getValue());
814 needRevalidate = true;
817 /* Remove an uimenu */
818 if (childType == __GO_UIMENU__) {
819 TypedObject childAsTypedObject = allObjects.get(childId);
820 switch (childAsTypedObject.getType()) {
822 ((Container) ((SwingScilabTab) updatedComponent).getMenuBar().getAsSimpleMenuBar()).remove((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
824 default: /* UiParentMenu */
825 ((Container) ((SwingScilabTab) updatedComponent).getMenuBar().getAsSimpleMenuBar()).remove((SwingScilabMenu) allObjects.get(childId).getValue());
828 needRevalidate = true;
832 if (needRevalidate && updatedComponent != null) {
833 ((View) updatedComponent).revalidate();
838 * Update the frame children (called by generic updateObject method)
839 * @param id the id of the figure
840 * @param newChildren the new children IDs list
842 private void updateFrameChildren(TypedObject updatedObject, String[] newChildren) {
843 Container updatedComponent = (SwingScilabFrame) updatedObject.getValue();
844 boolean needRevalidate = false;
847 for (String childId : newChildren) {
848 if (!updatedObject.hasChild(childId)) {
851 updatedObject.addChild(childId);
853 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
855 /* Add an uicontrol */
856 if (childType == __GO_UICONTROL__) {
857 ((SwingScilabFrame) updatedComponent).addMember(allObjects.get(childId).getValue());
858 needRevalidate = true;
863 // Remove children which have been deleted
864 Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
865 // Clone the children set to avoid concurrent accesses
866 String[] oldChildrenSet = updatedObject.getChildren().toArray(new String[updatedObject.getChildren().size()]);
867 for (String childId : oldChildrenSet) {
868 if (!newChildrenSet.contains(childId)) {
871 updatedObject.removeChild(childId);
873 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
875 /* Remove an uicontrol */
876 if (childType == __GO_UICONTROL__) {
877 updatedComponent.remove((Component) allObjects.get(childId).getValue());
878 needRevalidate = true;
882 if (needRevalidate && updatedComponent != null) {
883 ((JPanel) updatedComponent).revalidate();
888 * Update the Console menus (called by generic updateObject method)
889 * @param id the id of the console object
890 * @param newChildren the new children IDs list
892 private void updateConsoleChildren(TypedObject updatedObject, String[] newChildren) {
893 Container updatedComponent = (SwingScilabTab) updatedObject.getValue();
894 boolean needRevalidate = false;
897 for (String childId : newChildren) {
898 if (!updatedObject.hasChild(childId)) {
901 updatedObject.addChild(childId);
903 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
905 if (childType == __GO_UIMENU__) {
906 TypedObject childAsTypedObject = allObjects.get(childId);
907 switch (childAsTypedObject.getType()) {
910 TypedObject newUiParentMenu = CreateObjectFromType(__GO_UIPARENTMENU__, childId);
911 allObjects.put(childId, newUiParentMenu);
912 ((Container) ((SwingScilabTab) updatedObject.getValue()).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) newUiParentMenu.getValue());
914 default: /* UiParentMenu */
915 ((Container) ((SwingScilabTab) updatedObject.getValue()).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) childAsTypedObject.getValue());
918 needRevalidate = true;
923 // Remove children which have been deleted
924 Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
925 Object[] updatedObjectChildren = updatedObject.getChildren().toArray();
926 for (int i = 0 ; i < updatedObjectChildren.length ; ++i) {
927 String childId = (String) updatedObjectChildren[i];
928 if (!newChildrenSet.contains(childId)) {
931 updatedObject.removeChild(childId);
933 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
935 if (childType == __GO_UIMENU__) {
936 ((Container) ((SwingScilabTab) updatedObject.getValue()).getMenuBar().getAsSimpleMenuBar()).remove((SwingScilabMenu) allObjects.get(childId).getValue());
937 needRevalidate = true;
941 if (needRevalidate && updatedComponent != null) {
942 updatedComponent.validate();
948 * Update the children of a menu menus (called by generic updateObject method)
949 * @param id the id of the menu object
950 * @param newChildren the new children IDs list
952 private void updateMenuChildren(TypedObject updatedObject, String id, String[] newChildren) {
953 Container updatedComponent = null;
954 boolean needRevalidate = false;
955 int updatedObjectPosition = 0;
956 TypedObject newParent = null;
959 for (String childId : newChildren) {
960 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
961 if (childType == __GO_UIMENU__) {
962 if (!updatedObject.hasChild(childId)) {
964 updatedObject.addChild(childId);
966 TypedObject childAsTypedObject = allObjects.get(childId);
967 Object addedChild = allObjects.get(childId).getValue();
968 JComponent parent = null;
969 switch (updatedObject.getType()) {
971 updatedComponent = (SwingScilabMenuItem) updatedObject.getValue();
972 parent = (JComponent) updatedComponent.getParent();
973 switch (childAsTypedObject.getType()) {
975 /* Replace the item by a parent menu */
976 updatedObjectPosition = parent.getComponentZOrder((SwingScilabMenuItem) updatedObject.getValue());
977 parent.remove((SwingScilabMenuItem) updatedObject.getValue());
978 newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
979 allObjects.put(id, newParent);
980 newParent.addChild(childId);
981 parent.add((SwingScilabMenu) newParent.getValue(), updatedObjectPosition);
982 /* Update the created menu */
983 ((SwingScilabMenu) newParent.getValue()).add((SwingScilabMenuItem) addedChild);
986 /* Replace the item by a parent menu */
987 updatedObjectPosition = parent.getComponentZOrder((SwingScilabCheckBoxMenuItem) updatedObject.getValue());
988 parent.remove((SwingScilabCheckBoxMenuItem) updatedObject.getValue());
989 newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
990 allObjects.put(id, newParent);
991 newParent.addChild(childId);
992 parent.add((SwingScilabMenu) newParent.getValue(), updatedObjectPosition);
993 /* Update the created menu */
994 ((SwingScilabMenu) newParent.getValue()).add((SwingScilabCheckBoxMenuItem) addedChild);
996 default: /* UiParentMenu */
997 ((SwingScilabMenu) updatedObject.getValue()).add((SwingScilabMenu) addedChild);
1002 updatedComponent = (SwingScilabCheckBoxMenuItem) updatedObject.getValue();
1003 parent = (JComponent) updatedComponent.getParent();
1004 switch (childAsTypedObject.getType()) {
1006 /* Replace the item by a parent menu */
1007 updatedObjectPosition = parent.getComponentZOrder((SwingScilabCheckBoxMenuItem) updatedObject.getValue());
1008 parent.remove((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
1009 newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
1010 allObjects.put(id, newParent);
1011 newParent.addChild(childId);
1012 parent.add((SwingScilabMenu) newParent.getValue(), updatedObjectPosition);
1013 /* Update the created menu */
1014 ((SwingScilabMenu) newParent.getValue()).add((SwingScilabMenuItem) allObjects.get(childId).getValue());
1017 /* Replace the item by a parent menu */
1018 updatedObjectPosition = parent.getComponentZOrder((SwingScilabCheckBoxMenuItem) updatedObject.getValue());
1019 parent.remove((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
1020 newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
1021 allObjects.put(id, newParent);
1022 newParent.addChild(childId);
1023 parent.add((SwingScilabMenu) newParent.getValue(), updatedObjectPosition);
1024 /* Update the created menu */
1025 ((SwingScilabMenu) newParent.getValue()).add((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
1027 default: /* UiParentMenu */
1028 System.out.println("childAsTypedObject.getType() = UiParentMenu");
1029 ((SwingScilabMenu) updatedObject.getValue()).add((SwingScilabMenu) allObjects.get(childId).getValue());
1033 default: /* UiParentMenu */
1034 SwingScilabMenu updatedMenu = (SwingScilabMenu) updatedObject.getValue();
1035 updatedComponent = updatedMenu;
1036 switch (childAsTypedObject.getType()) {
1038 updatedMenu.add((SwingScilabMenuItem) allObjects.get(childId).getValue());
1041 updatedMenu.add((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
1043 default: /* UiParentMenu */
1044 /* Java can not add a JMenu in a JMenu */
1045 /* We need to convert the child into a MenuItem */
1046 TypedObject newMenu = CreateObjectFromType(__GO_UIMENU__, childId);
1047 allObjects.put(childId, newMenu);
1048 updatedMenu.add((SwingScilabMenuItem) newMenu.getValue());
1053 needRevalidate = true;
1057 // Remove children which have been deleted
1058 Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
1059 String[] oldChildren = updatedObject.getChildren().toArray(new String[updatedObject.getChildren().size()]);
1060 for (int childIndex = 0; childIndex < oldChildren.length; childIndex++) {
1061 String childId = oldChildren[childIndex];
1062 if (!newChildrenSet.contains(childId)) {
1064 updatedObject.removeChild(childId);
1065 updatedComponent = (SwingScilabMenu) updatedObject.getValue();
1067 TypedObject childAsTypedObject = allObjects.get(childId);
1068 Object removedMenu = childAsTypedObject.getValue();
1070 switch (childAsTypedObject.getType()) {
1072 updatedComponent.remove((SwingScilabMenuItem) removedMenu);
1075 updatedComponent.remove((SwingScilabCheckBoxMenuItem) removedMenu);
1078 updatedComponent.remove((SwingScilabMenu) removedMenu);
1081 needRevalidate = true;
1084 if (needRevalidate && updatedComponent != null) {
1085 updatedComponent.validate();
1089 * Update the children of a contextmenu (called by generic updateObject method)
1090 * @param id the id of the contextmenu object
1091 * @param newChildren the new children IDs list
1093 private void updateContextMenuChildren(TypedObject updatedObject, String[] newChildren) {
1094 Container updatedComponent = null;
1095 boolean needRevalidate = false;
1098 for (String childId : newChildren) {
1099 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1100 if (childType == __GO_UIMENU__) {
1101 if (!updatedObject.hasChild(childId)) {
1104 updatedObject.addChild(childId);
1106 TypedObject childAsTypedObject = allObjects.get(childId);
1107 SwingScilabContextMenu updatedMenu = (SwingScilabContextMenu) updatedObject.getValue();
1108 updatedComponent = updatedMenu;
1109 switch (childAsTypedObject.getType()) {
1111 updatedMenu.add((SwingScilabMenuItem) allObjects.get(childId).getValue());
1114 updatedMenu.add((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
1116 default: /* UiParentMenu */
1117 updatedMenu.add((SwingScilabMenu) allObjects.get(childId).getValue());
1120 needRevalidate = true;
1124 // Remove children which have been deleted
1125 Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
1126 for (String childId : updatedObject.getChildren()) {
1127 if (!newChildrenSet.contains(childId)) {
1129 updatedObject.removeChild(childId);
1130 updatedComponent = (SwingScilabMenu) updatedObject.getValue();
1132 TypedObject childAsTypedObject = allObjects.get(childId);
1133 SwingScilabContextMenu updatedMenu = (SwingScilabContextMenu) updatedObject.getValue();
1134 Object removedMenu = allObjects.get(childId).getValue();
1136 switch (childAsTypedObject.getType()) {
1138 updatedMenu.remove((SwingScilabMenuItem) removedMenu);
1141 updatedMenu.remove((SwingScilabCheckBoxMenuItem) removedMenu);
1144 updatedMenu.remove((SwingScilabMenu) removedMenu);
1147 needRevalidate = true;
1150 if (needRevalidate && updatedComponent != null) {
1151 updatedComponent.validate();