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.util.Arrays;
68 import java.util.Collections;
69 import java.util.HashMap;
70 import java.util.HashSet;
73 import java.util.UUID;
75 import javax.swing.JComponent;
76 import javax.swing.JPanel;
77 import javax.swing.JSeparator;
78 import javax.swing.SwingUtilities;
80 import org.flexdock.docking.Dockable;
81 import org.flexdock.docking.DockingManager;
82 import org.flexdock.docking.activation.ActiveDockableTracker;
83 import org.flexdock.view.View;
84 import org.scilab.modules.graphic_export.Driver;
85 import org.scilab.modules.graphic_objects.console.Console;
86 import org.scilab.modules.graphic_objects.figure.Figure;
87 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
88 import org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties;
89 import org.scilab.modules.graphic_objects.graphicView.GraphicView;
90 import org.scilab.modules.gui.bridge.checkbox.SwingScilabCheckBox;
91 import org.scilab.modules.gui.bridge.checkboxmenuitem.SwingScilabCheckBoxMenuItem;
92 import org.scilab.modules.gui.bridge.console.SwingScilabConsole;
93 import org.scilab.modules.gui.bridge.contextmenu.SwingScilabContextMenu;
94 import org.scilab.modules.gui.bridge.editbox.SwingScilabEditBox;
95 import org.scilab.modules.gui.bridge.frame.SwingScilabFrame;
96 import org.scilab.modules.gui.bridge.label.SwingScilabLabel;
97 import org.scilab.modules.gui.bridge.listbox.SwingScilabListBox;
98 import org.scilab.modules.gui.bridge.menu.SwingScilabMenu;
99 import org.scilab.modules.gui.bridge.menuitem.SwingScilabMenuItem;
100 import org.scilab.modules.gui.bridge.popupmenu.SwingScilabPopupMenu;
101 import org.scilab.modules.gui.bridge.pushbutton.SwingScilabPushButton;
102 import org.scilab.modules.gui.bridge.radiobutton.SwingScilabRadioButton;
103 import org.scilab.modules.gui.bridge.slider.SwingScilabSlider;
104 import org.scilab.modules.gui.bridge.tab.SwingScilabTab;
105 import org.scilab.modules.gui.bridge.uiimage.SwingScilabUiImage;
106 import org.scilab.modules.gui.bridge.uitable.SwingScilabUiTable;
107 import org.scilab.modules.gui.bridge.waitbar.SwingScilabWaitBar;
108 import org.scilab.modules.gui.bridge.window.SwingScilabWindow;
109 import org.scilab.modules.gui.console.ScilabConsole;
110 import org.scilab.modules.gui.menubar.ScilabMenuBar;
111 import org.scilab.modules.gui.textbox.ScilabTextBox;
112 import org.scilab.modules.gui.textbox.TextBox;
113 import org.scilab.modules.gui.toolbar.ToolBar;
114 import org.scilab.modules.gui.utils.ClosingOperationsManager;
115 import org.scilab.modules.gui.utils.ToolBarBuilder;
116 import org.scilab.modules.gui.utils.WindowsConfigurationManager;
117 import org.scilab.modules.gui.widget.Widget;
120 * @author Bruno JOFRET
121 * @author Vincent COUVERT
123 public final class SwingView implements GraphicView {
125 public static final String NULLUUID = new UUID(0L, 0L).toString();
127 private static SwingView me;
129 private static boolean headless;
131 private Map<String, TypedObject> allObjects;
136 private SwingView() {
137 GraphicController.getController().register(this);
138 allObjects = Collections.synchronizedMap(new HashMap<String, TypedObject>());
141 public static void registerSwingView() {
142 DEBUG("SwingView", "calling registerSwingView()");
144 me = new SwingView();
148 public static void setHeadless(boolean headless) {
149 SwingView.headless = headless;
152 public static boolean isHeadless() {
153 return SwingView.headless;
156 public static SwingViewObject getFromId(String id) {
157 TypedObject typedObject = me.allObjects.get(id);
159 if (typedObject == null) {
163 return typedObject.getValue();
166 private enum UielementType {
189 private class TypedObject {
190 private UielementType _type;
191 private SwingViewObject _value;
192 private Set<String> _children;
194 public TypedObject(UielementType _type, SwingViewObject _value) {
196 this._value = _value;
197 this._children = Collections.synchronizedSet(new HashSet<String>());
200 public UielementType getType() {
204 public SwingViewObject getValue() {
208 public Set<String> getChildren() {
212 public void addChild(String childUID) {
213 _children.add(childUID);
216 public void removeChild(String childUID) {
217 _children.remove(childUID);
220 public boolean hasChild(String childUID) {
221 return _children.contains(childUID);
225 private static final Set<Integer> managedTypes = new HashSet<Integer>(Arrays.asList(
226 GraphicObjectProperties.__GO_FIGURE__,
227 GraphicObjectProperties.__GO_UICONTEXTMENU__,
228 GraphicObjectProperties.__GO_UIMENU__,
229 GraphicObjectProperties.__GO_CONSOLE__,
230 GraphicObjectProperties.__GO_PROGRESSIONBAR__,
231 GraphicObjectProperties.__GO_WAITBAR__,
232 GraphicObjectProperties.__GO_UICONTROL__
235 public void createObject(String id) {
237 int objectType = (Integer) GraphicController.getController().getProperty(id, __GO_TYPE__);
239 if (managedTypes.contains(objectType) == false) {
242 //System.err.println("[SwingWiew] Object Created : " + id + " with type : " + objectType);
243 boolean isValid = (Boolean) GraphicController.getController().getProperty(id, __GO_VALID__);
248 if (!headless && !GraphicsEnvironment.isHeadless()) {
249 DEBUG("SwingWiew", "Object Created : " + id + "with type : " + objectType);
250 if (objectType == __GO_FIGURE__
251 || objectType == __GO_UICONTEXTMENU__
252 || objectType == __GO_UIMENU__
253 || objectType == __GO_CONSOLE__
254 || objectType == __GO_PROGRESSIONBAR__
255 || objectType == __GO_WAITBAR__) {
256 allObjects.put(id, CreateObjectFromType(objectType, id));
260 if (objectType == __GO_UICONTROL__) {
261 int style = (Integer) GraphicController.getController().getProperty(id, __GO_STYLE__);
262 DEBUG("SwingView", "__GO_STYLE__(" + style + ")");
263 allObjects.put(id, CreateObjectFromType(style, id));
266 if (objectType == __GO_FIGURE__) {
267 Driver.setDefaultVisitor(id);
272 private UielementType StyleToEnum(int style) {
273 DEBUG("SwingView", "StyleToEnum(" + style + ")");
276 return UielementType.Figure;
277 case __GO_CONSOLE__ :
278 return UielementType.Console;
279 case __GO_UI_CHECKBOX__ :
280 return UielementType.CheckBox;
281 case __GO_UI_EDIT__ :
282 return UielementType.Edit;
283 case __GO_UI_FRAME__ :
284 return UielementType.Frame;
285 case __GO_UI_IMAGE__ :
286 return UielementType.Image;
287 case __GO_UI_LISTBOX__ :
288 return UielementType.ListBox;
289 case __GO_UI_POPUPMENU__ :
290 return UielementType.PopupMenu;
291 case __GO_UI_PUSHBUTTON__ :
292 return UielementType.PushButton;
293 case __GO_UI_RADIOBUTTON__ :
294 return UielementType.RadioButton;
295 case __GO_UI_SLIDER__ :
296 return UielementType.Slider;
297 case __GO_UI_TABLE__ :
298 return UielementType.Table;
299 case __GO_UI_TEXT__ :
300 return UielementType.Text;
302 return UielementType.UiChildMenu;
303 case __GO_UIPARENTMENU__ :
304 return UielementType.UiParentMenu;
305 case __GO_UICHILDMENU__ :
306 return UielementType.UiChildMenu;
307 case __GO_UICHECKEDMENU__ :
308 return UielementType.UiCheckedMenu;
309 case __GO_PROGRESSIONBAR__ :
310 return UielementType.Progressbar;
311 case __GO_WAITBAR__ :
312 return UielementType.Waitbar;
313 case __GO_UICONTEXTMENU__ :
314 return UielementType.UiContextMenu;
319 private TypedObject CreateObjectFromType(int type, String id) {
320 UielementType enumType = StyleToEnum(type);
321 return new TypedObject(enumType, CreateObjectFromType(enumType, id));
324 private SwingViewObject CreateObjectFromType(UielementType type, String id) {
327 SwingScilabCheckBox checkBox = new SwingScilabCheckBox();
329 setDefaultProperties(checkBox, id);
332 Console console = (Console) GraphicController.getController().getObjectFromId(id);
333 if (console.getScilabMode() == Console.ScilabMode.STD) {
334 WindowsConfigurationManager.restoreUUID(NULLUUID);
335 SwingScilabConsole sciConsole = ((SwingScilabConsole) ScilabConsole.getConsole().getAsSimpleConsole());
336 SwingScilabTab consoleTab = (SwingScilabTab) sciConsole.getParent();
337 consoleTab.setId(id);
343 SwingScilabEditBox edit = new SwingScilabEditBox();
345 setDefaultProperties(edit, id);
348 Figure figure = (Figure) GraphicController.getController().getObjectFromId(id);
349 String figureTitle = figure.getName();
350 Integer figureId = figure.getId();
351 if ((figureTitle != null) && (figureId != null)) {
352 figureTitle = figureTitle.replaceFirst("%d", figureId.toString());
355 SwingScilabWindow window = new SwingScilabWindow();
357 window.setTitle(figureTitle);
359 ToolBar toolBar = ToolBarBuilder.buildToolBar(SwingScilabTab.GRAPHICS_TOOLBAR_DESCRIPTOR, figureId);
361 TextBox infoBar = ScilabTextBox.createTextBox();
363 SwingScilabTab tab = new SwingScilabTab(figureTitle, figureId, figure);
366 tab.setMenuBar(ScilabMenuBar.createMenuBar());
367 tab.setToolBar(toolBar);
368 tab.setInfoBar(ScilabTextBox.createTextBox());
369 window.addMenuBar(tab.getMenuBar());
370 window.addToolBar(tab.getToolBar());
371 window.addInfoBar(tab.getInfoBar());
373 tab.setWindowIcon("graphic-window");
375 tab.setParentWindowId(window.getId());
377 tab.setEventHandler(figure.getEventHandlerString());
378 tab.setEventHandlerEnabled(figure.getEventHandlerEnable());
380 DockingManager.dock(tab, window.getDockingPort());
381 ActiveDockableTracker.requestDockableActivation(tab);
383 window.setVisible(true);
384 tab.setVisible(true);
385 tab.setName(figureTitle);
387 String infoMessage = figure.getInfoMessage();
388 if ((infoMessage == null) || (infoMessage.length() == 0)) {
391 infoBar.setText(infoMessage);
393 tab.update(__GO_SIZE__, (Integer[]) GraphicController.getController().getProperty(id, __GO_SIZE__));
394 tab.update(__GO_POSITION__, (Integer[]) GraphicController.getController().getProperty(id, __GO_POSITION__));
395 // TODO set other default properties
398 SwingScilabFrame frame = new SwingScilabFrame();
400 setDefaultProperties(frame, id);
403 SwingScilabUiImage image = new SwingScilabUiImage();
405 setDefaultProperties(image, id);
408 SwingScilabListBox listBox = new SwingScilabListBox();
410 setDefaultProperties(listBox, id);
413 SwingScilabPopupMenu popupMenu = new SwingScilabPopupMenu();
415 setDefaultProperties(popupMenu, id);
418 SwingScilabWaitBar progressbar = new SwingScilabWaitBar();
419 progressbar.setIndeterminateMode(true);
420 progressbar.setId(id);
423 SwingScilabPushButton pushButton = new SwingScilabPushButton();
424 pushButton.setId(id);
425 setDefaultProperties(pushButton, id);
428 SwingScilabRadioButton radioButton = new SwingScilabRadioButton();
429 radioButton.setId(id);
430 setDefaultProperties(radioButton, id);
433 SwingScilabSlider slider = new SwingScilabSlider();
435 setDefaultProperties(slider, id);
438 SwingScilabUiTable table = new SwingScilabUiTable();
440 setDefaultProperties(table, id);
443 SwingScilabLabel text = new SwingScilabLabel();
445 setDefaultProperties(text, id);
448 throw new UnsupportedOperationException();
449 case UiParentMenu: /* SwingView internal type */
450 SwingScilabMenu parentMenu = new SwingScilabMenu();
451 parentMenu.setId(id);
452 setMenuDefaultProperties(parentMenu, id);
454 case UiChildMenu: /* SwingView internal type */
455 SwingScilabMenuItem childMenu = new SwingScilabMenuItem();
457 setMenuDefaultProperties(childMenu, id);
459 case UiCheckedMenu: /* SwingView internal type */
460 SwingScilabCheckBoxMenuItem checkedMenu = new SwingScilabCheckBoxMenuItem(false);
461 checkedMenu.setId(id);
462 setMenuDefaultProperties(checkedMenu, id);
465 SwingScilabContextMenu contextMenu = new SwingScilabContextMenu();
466 contextMenu.setId(id);
469 SwingScilabWaitBar waitbar = new SwingScilabWaitBar();
470 waitbar.setIndeterminateMode(false);
479 * Initialize all poperties according to model
480 * @param uiMenuObject the uimenu
481 * @param id the uimenu id
483 private void setMenuDefaultProperties(Widget uiMenuObject, String id) {
484 SwingViewMenu.update(uiMenuObject, __GO_CHILDREN__,
485 (String[]) GraphicController.getController().getProperty(id, __GO_CHILDREN__));
486 SwingViewMenu.update(uiMenuObject, __GO_CALLBACK__,
487 (String) GraphicController.getController().getProperty(id, __GO_CALLBACK__));
488 SwingViewMenu.update(uiMenuObject, __GO_CALLBACKTYPE__,
489 (Integer) GraphicController.getController().getProperty(id, __GO_CALLBACKTYPE__));
490 SwingViewMenu.update(uiMenuObject, __GO_UI_CHECKED__,
491 (Boolean) GraphicController.getController().getProperty(id, __GO_UI_CHECKED__));
492 SwingViewMenu.update(uiMenuObject, __GO_UI_ENABLE__,
493 (Boolean) GraphicController.getController().getProperty(id, __GO_UI_ENABLE__));
494 SwingViewMenu.update(uiMenuObject, __GO_UI_FOREGROUNDCOLOR__,
495 (Double[]) GraphicController.getController().getProperty(id, __GO_UI_FOREGROUNDCOLOR__));
496 SwingViewMenu.update(uiMenuObject, __GO_UI_LABEL__,
497 (String) GraphicController.getController().getProperty(id, __GO_UI_LABEL__));
498 SwingViewMenu.update(uiMenuObject, __GO_UI_ICON__,
499 (String) GraphicController.getController().getProperty(id, __GO_UI_ICON__));
503 * Initialize all poperties according to model
504 * @param uiControlObject the uicontrol
505 * @param id the uicontrol id
507 private void setDefaultProperties(Widget uiControlObject, String id) {
508 /* Visible property is set first to avoid to see the object rendered before all its properties to be set (See bug #10346) */
509 SwingViewWidget.update(uiControlObject, __GO_VISIBLE__,
510 (Boolean) GraphicController.getController().getProperty(id, __GO_VISIBLE__));
511 SwingViewWidget.update(uiControlObject, __GO_UI_BACKGROUNDCOLOR__,
512 (Double[]) GraphicController.getController().getProperty(id, __GO_UI_BACKGROUNDCOLOR__));
513 SwingViewWidget.update(uiControlObject, __GO_UI_ENABLE__,
514 (Boolean) GraphicController.getController().getProperty(id, __GO_UI_ENABLE__));
515 SwingViewWidget.update(uiControlObject, __GO_UI_ENABLE__,
516 (Boolean) GraphicController.getController().getProperty(id, __GO_UI_ENABLE__));
517 SwingViewWidget.update(uiControlObject, __GO_UI_FONTANGLE__,
518 (String) GraphicController.getController().getProperty(id, __GO_UI_FONTANGLE__));
519 SwingViewWidget.update(uiControlObject, __GO_UI_FONTNAME__,
520 (String) GraphicController.getController().getProperty(id, __GO_UI_FONTNAME__));
521 SwingViewWidget.update(uiControlObject, __GO_UI_FONTUNITS__,
522 (String) GraphicController.getController().getProperty(id, __GO_UI_FONTUNITS__));
523 SwingViewWidget.update(uiControlObject, __GO_UI_FONTSIZE__,
524 (Double) GraphicController.getController().getProperty(id, __GO_UI_FONTSIZE__));
525 SwingViewWidget.update(uiControlObject, __GO_UI_FONTWEIGHT__,
526 (String) GraphicController.getController().getProperty(id, __GO_UI_FONTWEIGHT__));
527 SwingViewWidget.update(uiControlObject, __GO_UI_FOREGROUNDCOLOR__,
528 (Double[]) GraphicController.getController().getProperty(id, __GO_UI_FOREGROUNDCOLOR__));
529 SwingViewWidget.update(uiControlObject, __GO_UI_HORIZONTALALIGNMENT__,
530 (String) GraphicController.getController().getProperty(id, __GO_UI_HORIZONTALALIGNMENT__));
531 SwingViewWidget.update(uiControlObject, __GO_UI_RELIEF__,
532 (String) GraphicController.getController().getProperty(id, __GO_UI_RELIEF__));
533 SwingViewWidget.update(uiControlObject, __GO_UI_STRING__,
534 (String[]) GraphicController.getController().getProperty(id, __GO_UI_STRING__));
535 SwingViewWidget.update(uiControlObject, __GO_UI_VERTICALALIGNMENT__,
536 (String) GraphicController.getController().getProperty(id, __GO_UI_VERTICALALIGNMENT__));
537 SwingViewWidget.update(uiControlObject, __GO_POSITION__,
538 (Double[]) GraphicController.getController().getProperty(id, __GO_POSITION__));
542 public void deleteObject(String id) {
543 final TypedObject requestedObject = allObjects.get(id);
544 if (requestedObject != null) {
545 switch (requestedObject.getType()) {
547 final SwingScilabTab tab = (SwingScilabTab) requestedObject.getValue();
549 SwingUtilities.invokeLater(new Runnable() {
551 DockingManager.close(tab);
552 DockingManager.unregisterDockable((Dockable) tab);
553 ClosingOperationsManager.unregisterClosingOperation(tab);
554 ClosingOperationsManager.removeDependency(tab);
555 ClosingOperationsManager.checkTabForClosing(tab);
562 SwingScilabWaitBar bar = (SwingScilabWaitBar) requestedObject.getValue();
567 // uicontrol case: the object is destroyed when its parent updates its children
570 allObjects.remove(id);
575 public void updateObject(String id, int property) {
576 TypedObject registeredObject = allObjects.get(id);
577 DEBUG("SwingView", "Update" + property);
579 /* On uicontrol style is set after object creation */
580 if (registeredObject == null && property == __GO_STYLE__) {
581 int style = (Integer) GraphicController.getController().getProperty(id, __GO_STYLE__);
582 allObjects.put(id, CreateObjectFromType(style, id));
585 /* Removes the swing object if its parent is not display */
586 if (registeredObject != null && property == __GO_PARENT__) {
587 String parentId = (String) GraphicController.getController().getProperty(id, __GO_PARENT__);
588 TypedObject registeredParent = allObjects.get(parentId);
589 if (registeredParent == null) {
590 allObjects.remove(id);
593 int type = (Integer) GraphicController.getController().getProperty(id, __GO_TYPE__);
594 /* Children list update */
595 if (registeredObject != null && property == __GO_CHILDREN__) {
596 String[] newChildren = (String[]) GraphicController.getController().getProperty(id, __GO_CHILDREN__);
600 * FIGURE CHILDREN UPDATE
603 updateFigureChildren(id, newChildren);
606 * CONSOLE CHILDREN UPDATE
608 case __GO_CONSOLE__ :
609 updateConsoleChildren(id, newChildren);
612 * MENU CHILDREN UPDATE
615 updateMenuChildren(id, newChildren);
618 * CONTEXTMENU CHILDREN UPDATE
620 case __GO_UICONTEXTMENU__ :
621 updateContextMenuChildren(id, newChildren);
624 * UICONTROL "FRAME" CHILDREN UPDATE
626 case __GO_UICONTROL__ :
627 int style = (Integer) GraphicController.getController().getProperty(id, __GO_STYLE__);
628 if (style == __GO_UI_FRAME__) {
629 updateFrameChildren(id, newChildren);
636 * When the CHECKED property is updated for a UIMENU,
637 * the object is converted to a SwingScilabCheckBoxMenuItem is not already of this type
639 if (registeredObject != null && property == __GO_UI_CHECKED__) {
640 if (type == __GO_UIMENU__) {
641 TypedObject updatedObject = allObjects.get(id);
642 switch (updatedObject.getType()) {
644 SwingScilabMenu meAsAMenu = (SwingScilabMenu) updatedObject.getValue();
645 Container parent = meAsAMenu.getParent();
646 parent.remove(meAsAMenu);
647 allObjects.put(id, CreateObjectFromType(__GO_UICHECKEDMENU__, id));
648 SwingScilabCheckBoxMenuItem meAsAMenuItem = (SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue();
649 parent.add(meAsAMenuItem);
650 registeredObject = allObjects.get(id);
653 String parentId = (String) GraphicController.getController().getProperty(id, __GO_PARENT__);
654 int index = ((SwingScilabMenu) allObjects.get(parentId).getValue())
655 .getComponentZOrder((SwingScilabMenuItem) allObjects.get(id).getValue());
656 ((SwingScilabMenu) allObjects.get(parentId).getValue()).remove((SwingScilabMenuItem) allObjects.get(id).getValue());
657 allObjects.put(id, CreateObjectFromType(__GO_UICHECKEDMENU__, id));
658 registeredObject = allObjects.get(id);
659 ((SwingScilabMenu) allObjects.get(parentId).getValue()).add((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue(), index);
668 * When the SEPARATOR property is updated for a UIMENU,
669 * When the property is set to TRUE: A separator is added if not already done
670 * When the property is set to FALSE: The previous separator is removed if it exists
672 if (registeredObject != null && property == __GO_UI_SEPARATOR__) {
673 if (type == __GO_UIMENU__) {
674 String parentId = (String) GraphicController.getController().getProperty(id, __GO_PARENT__);
675 int menuPosition = -1;
676 Component[] allChildren = ((SwingScilabMenu) allObjects.get(parentId).getValue()).getMenuComponents();
677 for (int kChild = 0; kChild < allChildren.length; kChild++) {
678 if (allChildren[kChild] == (Component) allObjects.get(id).getValue()) {
679 menuPosition = kChild;
683 if (menuPosition < 0) {
686 boolean newSeparatorMode = (Boolean) GraphicController.getController().getProperty(id, __GO_UI_SEPARATOR__);
687 if (newSeparatorMode) { // Add a separator above the menu
688 ((SwingScilabMenu) allObjects.get(parentId).getValue()).insertSeparator(menuPosition);
689 } else { // Remove the separator above the menu (if there is one)
690 if (menuPosition == 0) { // There is no object above the menu
693 if (((SwingScilabMenu) allObjects.get(parentId).getValue()).getComponent(menuPosition - 1) instanceof JSeparator) {
694 ((SwingScilabMenu) allObjects.get(parentId).getValue()).remove(menuPosition - 1);
700 if (registeredObject != null) {
701 SwingViewObject swingObject = registeredObject.getValue();
702 if (swingObject != null) {
703 swingObject.update(property, GraphicController.getController().getProperty(id, property));
709 * Update the figure children (called by generic updateObject method)
710 * @param id the id of the figure
711 * @param newChildren the new children IDs list
713 private void updateFigureChildren(String id, String[] newChildren) {
714 TypedObject updatedObject = allObjects.get(id);
715 Container updatedComponent = (SwingScilabTab) updatedObject.getValue();
716 boolean needRevalidate = false;
719 for (String childId : newChildren) {
720 if (!updatedObject.hasChild(childId)) {
723 updatedObject.addChild(childId);
725 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
727 /* Add an uicontrol */
728 if (childType == __GO_UICONTROL__) {
729 ((SwingScilabTab) updatedComponent).addMember(allObjects.get(childId).getValue());
730 needRevalidate = true;
734 if (childType == __GO_UIMENU__) {
735 TypedObject childAsTypedObject = allObjects.get(childId);
736 switch (childAsTypedObject.getType()) {
739 allObjects.remove(childId);
740 allObjects.put(childId, CreateObjectFromType(__GO_UIPARENTMENU__, childId));
741 ((Container) ((SwingScilabTab) updatedComponent).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) allObjects.get(childId).getValue());
743 default: /* UiParentMenu */
744 ((Container) ((SwingScilabTab) updatedComponent).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) allObjects.get(childId).getValue());
747 needRevalidate = true;
752 // Remove children which have been deleted
753 Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
754 // Clone the children set to avoid concurrent accesses
755 String[] oldChildrenSet = updatedObject.getChildren().toArray(new String[updatedObject.getChildren().size()]);
756 for (String childId : oldChildrenSet) {
757 if (!newChildrenSet.contains(childId)) {
760 updatedObject.removeChild(childId);
762 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
764 /* Remove an uicontrol */
765 if (childType == __GO_UICONTROL__) {
766 ((SwingScilabTab) updatedComponent).removeMember(allObjects.get(childId).getValue());
767 needRevalidate = true;
770 /* Remove an uimenu */
771 if (childType == __GO_UIMENU__) {
772 TypedObject childAsTypedObject = allObjects.get(childId);
773 switch (childAsTypedObject.getType()) {
775 ((Container) ((SwingScilabTab) updatedComponent).getMenuBar().getAsSimpleMenuBar()).remove((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
777 default: /* UiParentMenu */
778 ((Container) ((SwingScilabTab) updatedComponent).getMenuBar().getAsSimpleMenuBar()).remove((SwingScilabMenu) allObjects.get(childId).getValue());
781 needRevalidate = true;
785 if (needRevalidate && updatedComponent != null) {
786 ((View) updatedComponent).revalidate();
791 * Update the frame children (called by generic updateObject method)
792 * @param id the id of the figure
793 * @param newChildren the new children IDs list
795 private void updateFrameChildren(String id, String[] newChildren) {
796 TypedObject updatedObject = allObjects.get(id);
797 Container updatedComponent = (SwingScilabFrame) updatedObject.getValue();
798 boolean needRevalidate = false;
801 for (String childId : newChildren) {
802 if (!updatedObject.hasChild(childId)) {
805 updatedObject.addChild(childId);
807 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
809 /* Add an uicontrol */
810 if (childType == __GO_UICONTROL__) {
811 ((SwingScilabFrame) updatedComponent).addMember(allObjects.get(childId).getValue());
812 needRevalidate = true;
817 // Remove children which have been deleted
818 Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
819 // Clone the children set to avoid concurrent accesses
820 String[] oldChildrenSet = updatedObject.getChildren().toArray(new String[updatedObject.getChildren().size()]);
821 for (String childId : oldChildrenSet) {
822 if (!newChildrenSet.contains(childId)) {
825 updatedObject.removeChild(childId);
827 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
829 /* Remove an uicontrol */
830 if (childType == __GO_UICONTROL__) {
831 updatedComponent.remove((Component) allObjects.get(childId).getValue());
832 needRevalidate = true;
836 if (needRevalidate && updatedComponent != null) {
837 ((JPanel) updatedComponent).revalidate();
842 * Update the Console menus (called by generic updateObject method)
843 * @param id the id of the console object
844 * @param newChildren the new children IDs list
846 private void updateConsoleChildren(String id, String[] newChildren) {
847 TypedObject updatedObject = allObjects.get(id);
848 Container updatedComponent = (SwingScilabTab) updatedObject.getValue();
849 boolean needRevalidate = false;
852 for (String childId : newChildren) {
853 if (!updatedObject.hasChild(childId)) {
856 updatedObject.addChild(childId);
858 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
860 if (childType == __GO_UIMENU__) {
861 TypedObject childAsTypedObject = allObjects.get(childId);
862 switch (childAsTypedObject.getType()) {
865 allObjects.put(childId, CreateObjectFromType(__GO_UIPARENTMENU__, childId));
866 ((Container) ((SwingScilabTab) updatedObject.getValue()).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) allObjects.get(childId).getValue());
868 default: /* UiParentMenu */
869 ((Container) ((SwingScilabTab) updatedObject.getValue()).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) allObjects.get(childId).getValue());
872 needRevalidate = true;
877 // Remove children which have been deleted
878 Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
879 Object[] updatedObjectChildren = updatedObject.getChildren().toArray();
880 for (int i = 0 ; i < updatedObjectChildren.length ; ++i) {
881 String childId = (String) updatedObjectChildren[i];
882 if (!newChildrenSet.contains(childId)) {
885 updatedObject.removeChild(childId);
887 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
889 if (childType == __GO_UIMENU__) {
890 ((Container) ((SwingScilabTab) allObjects.get(id).getValue()).getMenuBar().getAsSimpleMenuBar()).remove((SwingScilabMenu) allObjects.get(childId).getValue());
891 needRevalidate = true;
895 if (needRevalidate && updatedComponent != null) {
896 updatedComponent.validate();
902 * Update the children of a menu menus (called by generic updateObject method)
903 * @param id the id of the menu object
904 * @param newChildren the new children IDs list
906 private void updateMenuChildren(String id, String[] newChildren) {
907 TypedObject updatedObject = allObjects.get(id);
908 Container updatedComponent = null;
909 boolean needRevalidate = false;
910 int updatedObjectPosition = 0;
911 TypedObject newParent = null;
914 for (String childId : newChildren) {
915 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
916 if (childType == __GO_UIMENU__) {
917 if (!updatedObject.hasChild(childId)) {
919 updatedObject.addChild(childId);
921 TypedObject childAsTypedObject = allObjects.get(childId);
922 Object addedChild = allObjects.get(childId).getValue();
923 JComponent parent = null;
924 switch (updatedObject.getType()) {
926 updatedComponent = (SwingScilabMenuItem) updatedObject.getValue();
927 parent = (JComponent) updatedComponent.getParent();
928 switch (childAsTypedObject.getType()) {
930 /* Replace the item by a parent menu */
931 updatedObjectPosition = parent.getComponentZOrder((SwingScilabMenuItem) allObjects.get(id).getValue());
932 parent.remove((SwingScilabMenuItem) allObjects.get(id).getValue());
933 newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
934 allObjects.put(id, newParent);
935 newParent.addChild(childId);
936 parent.add((SwingScilabMenu) allObjects.get(id).getValue(), updatedObjectPosition);
937 /* Update the created menu */
938 ((SwingScilabMenu) allObjects.get(id).getValue()).add((SwingScilabMenuItem) addedChild);
941 /* Replace the item by a parent menu */
942 updatedObjectPosition = parent.getComponentZOrder((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
943 parent.remove((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
944 newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
945 allObjects.put(id, newParent);
946 newParent.addChild(childId);
947 parent.add((SwingScilabMenu) allObjects.get(id).getValue(), updatedObjectPosition);
948 /* Update the created menu */
949 ((SwingScilabMenu) allObjects.get(id).getValue()).add((SwingScilabCheckBoxMenuItem) addedChild);
951 default: /* UiParentMenu */
952 ((SwingScilabMenu) allObjects.get(id).getValue()).add((SwingScilabMenu) addedChild);
957 updatedComponent = (SwingScilabCheckBoxMenuItem) updatedObject.getValue();
958 parent = (JComponent) updatedComponent.getParent();
959 switch (childAsTypedObject.getType()) {
961 /* Replace the item by a parent menu */
962 updatedObjectPosition = parent.getComponentZOrder((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
963 parent.remove((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
964 newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
965 allObjects.put(id, newParent);
966 newParent.addChild(childId);
967 parent.add((SwingScilabMenu) allObjects.get(id).getValue(), updatedObjectPosition);
968 /* Update the created menu */
969 ((SwingScilabMenu) allObjects.get(id).getValue()).add((SwingScilabMenuItem) allObjects.get(childId).getValue());
972 /* Replace the item by a parent menu */
973 updatedObjectPosition = parent.getComponentZOrder((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
974 parent.remove((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
975 newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
976 allObjects.put(id, newParent);
977 newParent.addChild(childId);
978 parent.add((SwingScilabMenu) allObjects.get(id).getValue(), updatedObjectPosition);
979 /* Update the created menu */
980 ((SwingScilabMenu) allObjects.get(id).getValue()).add((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
982 default: /* UiParentMenu */
983 System.out.println("childAsTypedObject.getType() = UiParentMenu");
984 ((SwingScilabMenu) allObjects.get(id).getValue()).add((SwingScilabMenu) allObjects.get(childId).getValue());
988 default: /* UiParentMenu */
989 SwingScilabMenu updatedMenu = (SwingScilabMenu) updatedObject.getValue();
990 updatedComponent = updatedMenu;
991 switch (childAsTypedObject.getType()) {
993 updatedMenu.add((SwingScilabMenuItem) allObjects.get(childId).getValue());
996 updatedMenu.add((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
998 default: /* UiParentMenu */
999 /* Java can not add a JMenu in a JMenu */
1000 /* We need to convert the child into a MenuItem */
1001 allObjects.put(childId, CreateObjectFromType(__GO_UIMENU__, childId));
1002 updatedMenu.add((SwingScilabMenuItem) allObjects.get(childId).getValue());
1007 needRevalidate = true;
1011 // Remove children which have been deleted
1012 Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
1013 String[] oldChildren = updatedObject.getChildren().toArray(new String[updatedObject.getChildren().size()]);
1014 for (int childIndex = 0; childIndex < oldChildren.length; childIndex++) {
1015 String childId = oldChildren[childIndex];
1016 if (!newChildrenSet.contains(childId)) {
1018 updatedObject.removeChild(childId);
1019 updatedComponent = (SwingScilabMenu) updatedObject.getValue();
1021 TypedObject childAsTypedObject = allObjects.get(childId);
1022 SwingScilabMenu updatedMenu = (SwingScilabMenu) allObjects.get(id).getValue();
1023 Object removedMenu = allObjects.get(childId).getValue();
1025 switch (childAsTypedObject.getType()) {
1027 updatedMenu.remove((SwingScilabMenuItem) removedMenu);
1030 updatedMenu.remove((SwingScilabCheckBoxMenuItem) removedMenu);
1033 updatedMenu.remove((SwingScilabMenu) removedMenu);
1036 needRevalidate = true;
1039 if (needRevalidate && updatedComponent != null) {
1040 updatedComponent.validate();
1044 * Update the children of a contextmenu (called by generic updateObject method)
1045 * @param id the id of the contextmenu object
1046 * @param newChildren the new children IDs list
1048 private void updateContextMenuChildren(String id, String[] newChildren) {
1049 TypedObject updatedObject = allObjects.get(id);
1050 Container updatedComponent = null;
1051 boolean needRevalidate = false;
1054 for (String childId : newChildren) {
1055 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1056 if (childType == __GO_UIMENU__) {
1057 if (!updatedObject.hasChild(childId)) {
1060 updatedObject.addChild(childId);
1062 TypedObject childAsTypedObject = allObjects.get(childId);
1063 SwingScilabContextMenu updatedMenu = (SwingScilabContextMenu) updatedObject.getValue();
1064 updatedComponent = updatedMenu;
1065 switch (childAsTypedObject.getType()) {
1067 updatedMenu.add((SwingScilabMenuItem) allObjects.get(childId).getValue());
1070 updatedMenu.add((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
1072 default: /* UiParentMenu */
1073 updatedMenu.add((SwingScilabMenu) allObjects.get(childId).getValue());
1076 needRevalidate = true;
1080 // Remove children which have been deleted
1081 Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
1082 for (String childId : updatedObject.getChildren()) {
1083 if (!newChildrenSet.contains(childId)) {
1085 updatedObject.removeChild(childId);
1086 updatedComponent = (SwingScilabMenu) updatedObject.getValue();
1088 TypedObject childAsTypedObject = allObjects.get(childId);
1089 SwingScilabContextMenu updatedMenu = (SwingScilabContextMenu) allObjects.get(id).getValue();
1090 Object removedMenu = allObjects.get(childId).getValue();
1092 switch (childAsTypedObject.getType()) {
1094 updatedMenu.remove((SwingScilabMenuItem) removedMenu);
1097 updatedMenu.remove((SwingScilabCheckBoxMenuItem) removedMenu);
1100 updatedMenu.remove((SwingScilabMenu) removedMenu);
1103 needRevalidate = true;
1106 if (needRevalidate && updatedComponent != null) {
1107 updatedComponent.validate();