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_FONTUNITS__;
40 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTSIZE__;
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();
407 SwingScilabListBox listBox = new SwingScilabListBox();
411 SwingScilabPopupMenu popupMenu = new SwingScilabPopupMenu();
413 setDefaultProperties(popupMenu, id);
416 SwingScilabWaitBar progressbar = new SwingScilabWaitBar();
417 progressbar.setIndeterminateMode(true);
418 progressbar.setId(id);
421 SwingScilabPushButton pushButton = new SwingScilabPushButton();
422 pushButton.setId(id);
423 setDefaultProperties(pushButton, id);
426 SwingScilabRadioButton radioButton = new SwingScilabRadioButton();
427 radioButton.setId(id);
428 setDefaultProperties(radioButton, id);
431 SwingScilabSlider slider = new SwingScilabSlider();
433 setDefaultProperties(slider, id);
436 SwingScilabUiTable table = new SwingScilabUiTable();
440 SwingScilabLabel text = new SwingScilabLabel();
442 setDefaultProperties(text, id);
445 throw new UnsupportedOperationException();
446 case UiParentMenu: /* SwingView internal type */
447 SwingScilabMenu parentMenu = new SwingScilabMenu();
448 parentMenu.setId(id);
449 setMenuDefaultProperties(parentMenu, id);
451 case UiChildMenu: /* SwingView internal type */
452 SwingScilabMenuItem childMenu = new SwingScilabMenuItem();
454 setMenuDefaultProperties(childMenu, id);
456 case UiCheckedMenu: /* SwingView internal type */
457 SwingScilabCheckBoxMenuItem checkedMenu = new SwingScilabCheckBoxMenuItem(false);
458 checkedMenu.setId(id);
459 setMenuDefaultProperties(checkedMenu, id);
462 SwingScilabContextMenu contextMenu = new SwingScilabContextMenu();
463 contextMenu.setId(id);
466 SwingScilabWaitBar waitbar = new SwingScilabWaitBar();
467 waitbar.setIndeterminateMode(false);
476 * Initialize all poperties according to model
477 * @param uiMenuObject the uimenu
478 * @param id the uimenu id
480 private void setMenuDefaultProperties(Widget uiMenuObject, String id) {
481 SwingViewMenu.update(uiMenuObject, __GO_CHILDREN__,
482 (String[]) GraphicController.getController().getProperty(id, __GO_CHILDREN__));
483 SwingViewMenu.update(uiMenuObject, __GO_CALLBACK__,
484 (String) GraphicController.getController().getProperty(id, __GO_CALLBACK__));
485 SwingViewMenu.update(uiMenuObject, __GO_CALLBACKTYPE__,
486 (Integer) GraphicController.getController().getProperty(id, __GO_CALLBACKTYPE__));
487 SwingViewMenu.update(uiMenuObject, __GO_UI_CHECKED__,
488 (Boolean) GraphicController.getController().getProperty(id, __GO_UI_CHECKED__));
489 SwingViewMenu.update(uiMenuObject, __GO_UI_ENABLE__,
490 (Boolean) GraphicController.getController().getProperty(id, __GO_UI_ENABLE__));
491 SwingViewMenu.update(uiMenuObject, __GO_UI_FOREGROUNDCOLOR__,
492 (Double[]) GraphicController.getController().getProperty(id, __GO_UI_FOREGROUNDCOLOR__));
493 SwingViewMenu.update(uiMenuObject, __GO_UI_LABEL__,
494 (String) GraphicController.getController().getProperty(id, __GO_UI_LABEL__));
495 SwingViewMenu.update(uiMenuObject, __GO_UI_ICON__,
496 (String) GraphicController.getController().getProperty(id, __GO_UI_ICON__));
500 * Initialize all poperties according to model
501 * @param uiControlObject the uicontrol
502 * @param id the uicontrol id
504 private void setDefaultProperties(Widget uiControlObject, String id) {
505 /* Visible property is set first to avoid to see the object rendered before all its properties to be set (See bug #10346) */
506 SwingViewWidget.update(uiControlObject, __GO_VISIBLE__,
507 (Boolean) GraphicController.getController().getProperty(id, __GO_VISIBLE__));
508 SwingViewWidget.update(uiControlObject, __GO_UI_BACKGROUNDCOLOR__,
509 (Double[]) GraphicController.getController().getProperty(id, __GO_UI_BACKGROUNDCOLOR__));
510 SwingViewWidget.update(uiControlObject, __GO_UI_ENABLE__,
511 (Boolean) GraphicController.getController().getProperty(id, __GO_UI_ENABLE__));
512 SwingViewWidget.update(uiControlObject, __GO_UI_ENABLE__,
513 (Boolean) GraphicController.getController().getProperty(id, __GO_UI_ENABLE__));
514 SwingViewWidget.update(uiControlObject, __GO_UI_FONTANGLE__,
515 (String) GraphicController.getController().getProperty(id, __GO_UI_FONTANGLE__));
516 SwingViewWidget.update(uiControlObject, __GO_UI_FONTNAME__,
517 (String) GraphicController.getController().getProperty(id, __GO_UI_FONTNAME__));
518 SwingViewWidget.update(uiControlObject, __GO_UI_FONTUNITS__,
519 (String) GraphicController.getController().getProperty(id, __GO_UI_FONTUNITS__));
520 SwingViewWidget.update(uiControlObject, __GO_UI_FONTSIZE__,
521 (Double) GraphicController.getController().getProperty(id, __GO_UI_FONTSIZE__));
522 SwingViewWidget.update(uiControlObject, __GO_UI_FONTWEIGHT__,
523 (String) GraphicController.getController().getProperty(id, __GO_UI_FONTWEIGHT__));
524 SwingViewWidget.update(uiControlObject, __GO_UI_FOREGROUNDCOLOR__,
525 (Double[]) GraphicController.getController().getProperty(id, __GO_UI_FOREGROUNDCOLOR__));
526 SwingViewWidget.update(uiControlObject, __GO_UI_HORIZONTALALIGNMENT__,
527 (String) GraphicController.getController().getProperty(id, __GO_UI_HORIZONTALALIGNMENT__));
528 SwingViewWidget.update(uiControlObject, __GO_UI_RELIEF__,
529 (String) GraphicController.getController().getProperty(id, __GO_UI_RELIEF__));
530 SwingViewWidget.update(uiControlObject, __GO_UI_STRING__,
531 (String[]) GraphicController.getController().getProperty(id, __GO_UI_STRING__));
532 SwingViewWidget.update(uiControlObject, __GO_UI_VERTICALALIGNMENT__,
533 (String) GraphicController.getController().getProperty(id, __GO_UI_VERTICALALIGNMENT__));
534 SwingViewWidget.update(uiControlObject, __GO_POSITION__,
535 (Double[]) GraphicController.getController().getProperty(id, __GO_POSITION__));
539 public void deleteObject(String id) {
540 final TypedObject requestedObject = allObjects.get(id);
541 if (requestedObject != null) {
542 switch (requestedObject.getType()) {
544 final SwingScilabTab tab = (SwingScilabTab) requestedObject.getValue();
546 SwingUtilities.invokeLater(new Runnable() {
548 DockingManager.close(tab);
549 DockingManager.unregisterDockable((Dockable) tab);
550 ClosingOperationsManager.unregisterClosingOperation(tab);
551 ClosingOperationsManager.removeDependency(tab);
552 ClosingOperationsManager.checkTabForClosing(tab);
559 SwingScilabWaitBar bar = (SwingScilabWaitBar) requestedObject.getValue();
564 // uicontrol case: the object is destroyed when its parent updates its children
567 allObjects.remove(id);
572 public void updateObject(String id, int property) {
573 TypedObject registeredObject = allObjects.get(id);
574 DEBUG("SwingView", "Update" + property);
576 /* On uicontrol style is set after object creation */
577 if (registeredObject == null && property == __GO_STYLE__) {
578 int style = (Integer) GraphicController.getController().getProperty(id, __GO_STYLE__);
579 allObjects.put(id, CreateObjectFromType(style, id));
582 /* Removes the swing object if its parent is not display */
583 if (registeredObject != null && property == __GO_PARENT__) {
584 String parentId = (String) GraphicController.getController().getProperty(id, __GO_PARENT__);
585 TypedObject registeredParent = allObjects.get(parentId);
586 if (registeredParent == null) {
587 allObjects.remove(id);
590 int type = (Integer) GraphicController.getController().getProperty(id, __GO_TYPE__);
591 /* Children list update */
592 if (registeredObject != null && property == __GO_CHILDREN__) {
593 String[] newChildren = (String[]) GraphicController.getController().getProperty(id, __GO_CHILDREN__);
598 * FIGURE CHILDREN UPDATE
601 updateFigureChildren(id, newChildren);
604 * CONSOLE CHILDREN UPDATE
606 case __GO_CONSOLE__ :
607 updateConsoleChildren(id, newChildren);
610 * MENU CHILDREN UPDATE
613 updateMenuChildren(id, newChildren);
616 * CONTEXTMENU CHILDREN UPDATE
618 case __GO_UICONTEXTMENU__ :
619 updateContextMenuChildren(id, newChildren);
622 * UICONTROL "FRAME" CHILDREN UPDATE
624 case __GO_UICONTROL__ :
625 int style = (Integer) GraphicController.getController().getProperty(id, __GO_STYLE__);
626 if (style == __GO_UI_FRAME__) {
627 updateFrameChildren(id, newChildren);
634 * When the CHECKED property is updated for a UIMENU,
635 * the object is converted to a SwingScilabCheckBoxMenuItem is not already of this type
637 if (registeredObject != null && property == __GO_UI_CHECKED__) {
638 if (type == __GO_UIMENU__) {
639 TypedObject updatedObject = allObjects.get(id);
640 switch (updatedObject.getType()) {
642 allObjects.put(id, CreateObjectFromType(__GO_UICHECKEDMENU__, id));
643 registeredObject = allObjects.get(id);
646 String parentId = (String) GraphicController.getController().getProperty(id, __GO_PARENT__);
647 int index = ((SwingScilabMenu) allObjects.get(parentId).getValue())
648 .getComponentZOrder((SwingScilabMenuItem) allObjects.get(id).getValue());
649 ((SwingScilabMenu) allObjects.get(parentId).getValue()).remove((SwingScilabMenuItem) allObjects.get(id).getValue());
650 allObjects.put(id, CreateObjectFromType(__GO_UICHECKEDMENU__, id));
651 registeredObject = allObjects.get(id);
652 ((SwingScilabMenu) allObjects.get(parentId).getValue()).add((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue(), index);
661 * When the SEPARATOR property is updated for a UIMENU,
662 * When the property is set to TRUE: A separator is added if not already done
663 * When the property is set to FALSE: The previous separator is removed if it exists
665 if (registeredObject != null && property == __GO_UI_SEPARATOR__) {
666 if (type == __GO_UIMENU__) {
667 String parentId = (String) GraphicController.getController().getProperty(id, __GO_PARENT__);
668 int menuPosition = -1;
669 Component[] allChildren = ((SwingScilabMenu) allObjects.get(parentId).getValue()).getMenuComponents();
670 for (int kChild = 0; kChild < allChildren.length; kChild++) {
671 if (allChildren[kChild] == (Component) allObjects.get(id).getValue()) {
672 menuPosition = kChild;
676 if (menuPosition < 0) {
679 boolean newSeparatorMode = (Boolean) GraphicController.getController().getProperty(id, __GO_UI_SEPARATOR__);
680 if (newSeparatorMode) { // Add a separator above the menu
681 ((SwingScilabMenu) allObjects.get(parentId).getValue()).insertSeparator(menuPosition);
682 } else { // Remove the separator above the menu (if there is one)
683 if (menuPosition == 0) { // There is no object above the menu
686 if (((SwingScilabMenu) allObjects.get(parentId).getValue()).getComponent(menuPosition - 1) instanceof JSeparator) {
687 ((SwingScilabMenu) allObjects.get(parentId).getValue()).remove(menuPosition - 1);
693 if (registeredObject != null) {
694 SwingViewObject swingObject = registeredObject.getValue();
695 if (swingObject != null) {
696 swingObject.update(property, GraphicController.getController().getProperty(id, property));
702 * Update the figure children (called by generic updateObject method)
703 * @param id the id of the figure
704 * @param newChildren the new children IDs list
706 private void updateFigureChildren(String id, String[] newChildren) {
707 TypedObject updatedObject = allObjects.get(id);
708 Container updatedComponent = (SwingScilabTab) updatedObject.getValue();
709 boolean needRevalidate = false;
712 for (String childId : newChildren) {
713 if (!updatedObject.hasChild(childId)) {
716 updatedObject.addChild(childId);
718 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
720 /* Add an uicontrol */
721 if (childType == __GO_UICONTROL__) {
722 ((SwingScilabTab) updatedComponent).addMember(allObjects.get(childId).getValue());
723 needRevalidate = true;
727 if (childType == __GO_UIMENU__) {
728 TypedObject childAsTypedObject = allObjects.get(childId);
729 switch (childAsTypedObject.getType()) {
732 allObjects.put(childId, CreateObjectFromType(__GO_UIPARENTMENU__, childId));
733 ((Container) ((SwingScilabTab) updatedComponent).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) allObjects.get(childId).getValue());
735 default: /* UiParentMenu */
736 ((Container) ((SwingScilabTab) updatedComponent).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) allObjects.get(childId).getValue());
739 needRevalidate = true;
744 // Remove children which have been deleted
745 Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
746 // Clone the children set to avoid concurrent accesses
747 String[] oldChildrenSet = updatedObject.getChildren().toArray(new String[updatedObject.getChildren().size()]);
748 for (String childId : oldChildrenSet) {
749 if (!newChildrenSet.contains(childId)) {
752 updatedObject.removeChild(childId);
754 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
756 /* Remove an uicontrol */
757 if (childType ==__GO_UICONTROL__) {
758 ((SwingScilabTab) updatedComponent).removeMember(allObjects.get(childId).getValue());
759 needRevalidate = true;
762 /* Remove an uimenu */
763 if (childType == __GO_UIMENU__) {
764 TypedObject childAsTypedObject = allObjects.get(childId);
765 switch (childAsTypedObject.getType()) {
767 ((Container) ((SwingScilabTab) updatedComponent).getMenuBar().getAsSimpleMenuBar()).remove((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
769 default: /* UiParentMenu */
770 ((Container) ((SwingScilabTab) updatedComponent).getMenuBar().getAsSimpleMenuBar()).remove((SwingScilabMenu) allObjects.get(childId).getValue());
773 needRevalidate = true;
777 if (needRevalidate && updatedComponent != null) {
778 ((View) updatedComponent).revalidate();
783 * Update the frame children (called by generic updateObject method)
784 * @param id the id of the figure
785 * @param newChildren the new children IDs list
787 private void updateFrameChildren(String id, String[] newChildren) {
788 TypedObject updatedObject = allObjects.get(id);
789 Container updatedComponent = (SwingScilabFrame) updatedObject.getValue();
790 boolean needRevalidate = false;
793 for (String childId : newChildren) {
794 if (!updatedObject.hasChild(childId)) {
797 updatedObject.addChild(childId);
799 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
801 /* Add an uicontrol */
802 if (childType == __GO_UICONTROL__) {
803 ((SwingScilabFrame) updatedComponent).addMember(allObjects.get(childId).getValue());
804 needRevalidate = true;
809 // Remove children which have been deleted
810 Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
811 // Clone the children set to avoid concurrent accesses
812 String[] oldChildrenSet = updatedObject.getChildren().toArray(new String[updatedObject.getChildren().size()]);
813 for (String childId : oldChildrenSet) {
814 if (!newChildrenSet.contains(childId)) {
817 updatedObject.removeChild(childId);
819 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
821 /* Remove an uicontrol */
822 if (childType == __GO_UICONTROL__) {
823 updatedComponent.remove((Component) allObjects.get(childId).getValue());
824 needRevalidate = true;
828 if (needRevalidate && updatedComponent != null) {
829 ((JPanel) updatedComponent).revalidate();
834 * Update the Console menus (called by generic updateObject method)
835 * @param id the id of the console object
836 * @param newChildren the new children IDs list
838 private void updateConsoleChildren(String id, String[] newChildren) {
839 TypedObject updatedObject = allObjects.get(id);
840 Container updatedComponent = (SwingScilabTab) updatedObject.getValue();
841 boolean needRevalidate = false;
844 for (String childId : newChildren) {
845 if (!updatedObject.hasChild(childId)) {
848 updatedObject.addChild(childId);
850 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
852 if (childType == __GO_UIMENU__) {
853 TypedObject childAsTypedObject = allObjects.get(childId);
854 switch (childAsTypedObject.getType()) {
857 allObjects.put(childId, CreateObjectFromType(__GO_UIPARENTMENU__, childId));
858 ((Container) ((SwingScilabTab) updatedObject.getValue()).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) allObjects.get(childId).getValue());
860 default: /* UiParentMenu */
861 ((Container) ((SwingScilabTab) updatedObject.getValue()).getMenuBar().getAsSimpleMenuBar()).add((SwingScilabMenu) allObjects.get(childId).getValue());
864 needRevalidate = true;
869 // Remove children which have been deleted
870 Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
871 Object[] updatedObjectChildren = updatedObject.getChildren().toArray();
872 for (int i = 0 ; i < updatedObjectChildren.length ; ++i) {
873 String childId = (String) updatedObjectChildren[i];
874 if (!newChildrenSet.contains(childId)) {
877 updatedObject.removeChild(childId);
879 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
881 if (childType == __GO_UIMENU__) {
882 ((Container) ((SwingScilabTab) allObjects.get(id).getValue()).getMenuBar().getAsSimpleMenuBar()).remove((SwingScilabMenu) allObjects.get(childId).getValue());
883 needRevalidate = true;
887 if (needRevalidate && updatedComponent != null) {
888 updatedComponent.validate();
894 * Update the children of a menu menus (called by generic updateObject method)
895 * @param id the id of the menu object
896 * @param newChildren the new children IDs list
898 private void updateMenuChildren(String id, String[] newChildren) {
899 TypedObject updatedObject = allObjects.get(id);
900 Container updatedComponent = null;
901 boolean needRevalidate = false;
902 int updatedObjectPosition = 0;
903 TypedObject newParent = null;
906 for (String childId : newChildren) {
907 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
908 if (childType == __GO_UIMENU__) {
909 if (!updatedObject.hasChild(childId)) {
911 updatedObject.addChild(childId);
913 TypedObject childAsTypedObject = allObjects.get(childId);
914 Object addedChild = allObjects.get(childId).getValue();
915 JComponent parent = null;
916 switch (updatedObject.getType()) {
918 updatedComponent = (SwingScilabMenuItem) updatedObject.getValue();
919 parent = (JComponent) updatedComponent.getParent();
920 switch (childAsTypedObject.getType()) {
922 /* Replace the item by a parent menu */
923 updatedObjectPosition = parent.getComponentZOrder((SwingScilabMenuItem) allObjects.get(id).getValue());
924 parent.remove((SwingScilabMenuItem) allObjects.get(id).getValue());
925 newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
926 allObjects.put(id, newParent);
927 newParent.addChild(childId);
928 parent.add((SwingScilabMenu) allObjects.get(id).getValue(), updatedObjectPosition);
929 /* Update the created menu */
930 ((SwingScilabMenu) allObjects.get(id).getValue()).add((SwingScilabMenuItem) addedChild);
933 /* Replace the item by a parent menu */
934 updatedObjectPosition = parent.getComponentZOrder((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
935 parent.remove((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
936 newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
937 allObjects.put(id, newParent);
938 newParent.addChild(childId);
939 parent.add((SwingScilabMenu) allObjects.get(id).getValue(), updatedObjectPosition);
940 /* Update the created menu */
941 ((SwingScilabMenu) allObjects.get(id).getValue()).add((SwingScilabCheckBoxMenuItem) addedChild);
943 default: /* UiParentMenu */
944 ((SwingScilabMenu) allObjects.get(id).getValue()).add((SwingScilabMenu) addedChild);
949 updatedComponent = (SwingScilabCheckBoxMenuItem) updatedObject.getValue();
950 parent = (JComponent) updatedComponent.getParent();
951 switch (childAsTypedObject.getType()) {
953 /* Replace the item by a parent menu */
954 updatedObjectPosition = parent.getComponentZOrder((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
955 parent.remove((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
956 newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
957 allObjects.put(id, newParent);
958 newParent.addChild(childId);
959 parent.add((SwingScilabMenu) allObjects.get(id).getValue(), updatedObjectPosition);
960 /* Update the created menu */
961 ((SwingScilabMenu) allObjects.get(id).getValue()).add((SwingScilabMenuItem) allObjects.get(childId).getValue());
964 /* Replace the item by a parent menu */
965 updatedObjectPosition = parent.getComponentZOrder((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
966 parent.remove((SwingScilabCheckBoxMenuItem) allObjects.get(id).getValue());
967 newParent = CreateObjectFromType(__GO_UIPARENTMENU__, id);
968 allObjects.put(id, newParent);
969 newParent.addChild(childId);
970 parent.add((SwingScilabMenu) allObjects.get(id).getValue(), updatedObjectPosition);
971 /* Update the created menu */
972 ((SwingScilabMenu) allObjects.get(id).getValue()).add((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
974 default: /* UiParentMenu */
975 ((SwingScilabMenu) allObjects.get(id).getValue()).add((SwingScilabMenu) allObjects.get(childId).getValue());
979 default: /* UiParentMenu */
980 SwingScilabMenu updatedMenu = (SwingScilabMenu) updatedObject.getValue();
981 updatedComponent = updatedMenu;
982 switch (childAsTypedObject.getType()) {
984 updatedMenu.add((SwingScilabMenuItem) allObjects.get(childId).getValue());
987 updatedMenu.add((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
989 default: /* UiParentMenu */
990 updatedMenu.add((SwingScilabMenu) allObjects.get(childId).getValue());
995 needRevalidate = true;
999 // Remove children which have been deleted
1000 Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
1001 String[] oldChildren = updatedObject.getChildren().toArray(new String[updatedObject.getChildren().size()]);
1002 for (int childIndex = 0; childIndex < oldChildren.length; childIndex++) {
1003 String childId = oldChildren[childIndex];
1004 if (!newChildrenSet.contains(childId)) {
1006 updatedObject.removeChild(childId);
1007 updatedComponent = (SwingScilabMenu) updatedObject.getValue();
1009 TypedObject childAsTypedObject = allObjects.get(childId);
1010 SwingScilabMenu updatedMenu = (SwingScilabMenu) allObjects.get(id).getValue();
1011 Object removedMenu = allObjects.get(childId).getValue();
1013 switch (childAsTypedObject.getType()) {
1015 updatedMenu.remove((SwingScilabMenuItem) removedMenu);
1018 updatedMenu.remove((SwingScilabCheckBoxMenuItem) removedMenu);
1021 updatedMenu.remove((SwingScilabMenu) removedMenu);
1024 needRevalidate = true;
1027 if (needRevalidate && updatedComponent != null) {
1028 updatedComponent.validate();
1032 * Update the children of a contextmenu (called by generic updateObject method)
1033 * @param id the id of the contextmenu object
1034 * @param newChildren the new children IDs list
1036 private void updateContextMenuChildren(String id, String[] newChildren) {
1037 TypedObject updatedObject = allObjects.get(id);
1038 Container updatedComponent = null;
1039 boolean needRevalidate = false;
1042 for (String childId : newChildren) {
1043 int childType = (Integer) GraphicController.getController().getProperty(childId, __GO_TYPE__);
1044 if (childType == __GO_UIMENU__) {
1045 if (!updatedObject.hasChild(childId)) {
1048 updatedObject.addChild(childId);
1050 TypedObject childAsTypedObject = allObjects.get(childId);
1051 SwingScilabContextMenu updatedMenu = (SwingScilabContextMenu) updatedObject.getValue();
1052 updatedComponent = updatedMenu;
1053 switch (childAsTypedObject.getType()) {
1055 updatedMenu.add((SwingScilabMenuItem) allObjects.get(childId).getValue());
1058 updatedMenu.add((SwingScilabCheckBoxMenuItem) allObjects.get(childId).getValue());
1060 default: /* UiParentMenu */
1061 updatedMenu.add((SwingScilabMenu) allObjects.get(childId).getValue());
1064 needRevalidate = true;
1068 // Remove children which have been deleted
1069 Set<String> newChildrenSet = new HashSet<String>(Arrays.asList(newChildren));
1070 for (String childId : updatedObject.getChildren()) {
1071 if (!newChildrenSet.contains(childId)) {
1073 updatedObject.removeChild(childId);
1074 updatedComponent = (SwingScilabMenu) updatedObject.getValue();
1076 TypedObject childAsTypedObject = allObjects.get(childId);
1077 SwingScilabContextMenu updatedMenu = (SwingScilabContextMenu) allObjects.get(id).getValue();
1078 Object removedMenu = allObjects.get(childId).getValue();
1080 switch (childAsTypedObject.getType()) {
1082 updatedMenu.remove((SwingScilabMenuItem) removedMenu);
1085 updatedMenu.remove((SwingScilabCheckBoxMenuItem) removedMenu);
1088 updatedMenu.remove((SwingScilabMenu) removedMenu);
1091 needRevalidate = true;
1094 if (needRevalidate && updatedComponent != null) {
1095 updatedComponent.validate();