ToolBar/MenuBar creation update
[scilab.git] / scilab / modules / gui / src / java / org / scilab / modules / gui / bridge / tab / SwingScilabCommonPanel.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2014 - Scilab Enterprises - Bruno JOFRET
4  *
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at
9  * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
10  *
11  */
12 package org.scilab.modules.gui.bridge.tab;
13
14 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_AUTORESIZE__;
15 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_AXES_SIZE__;
16 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_BORDER_OPT_PADDING__;
17 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_CALLBACK__;
18 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_CHILDREN__;
19 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_EVENTHANDLER_ENABLE__;
20 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_EVENTHANDLER_NAME__;
21 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_GRID_OPT_GRID__;
22 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_GRID_OPT_PADDING__;
23 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_ID__;
24 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_INFOBAR_VISIBLE__;
25 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_INFO_MESSAGE__;
26 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_LAYOUT__;
27 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_MENUBAR_VISIBLE__;
28 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_NAME__;
29 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_POSITION__;
30 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_RESIZE__;
31 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_SIZE__;
32 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_TOOLBAR_VISIBLE__;
33 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_TYPE__;
34 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UICHECKEDMENU__;
35 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UICHILDMENU__;
36 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UIMENU__;
37 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UIPARENTMENU__;
38 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_VISIBLE__;
39
40 import java.awt.BorderLayout;
41 import java.awt.Component;
42 import java.awt.Dimension;
43 import java.awt.GridBagConstraints;
44 import java.awt.GridBagLayout;
45 import java.awt.GridLayout;
46
47 import javax.swing.JLayeredPane;
48
49 import org.scilab.modules.graphic_objects.figure.Figure;
50 import org.scilab.modules.graphic_objects.figure.Figure.BarType;
51 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
52 import org.scilab.modules.graphic_objects.graphicModel.GraphicModel;
53 import org.scilab.modules.graphic_objects.uicontrol.Uicontrol;
54 import org.scilab.modules.graphic_objects.utils.LayoutType;
55 import org.scilab.modules.gui.SwingView;
56 import org.scilab.modules.gui.SwingViewObject;
57 import org.scilab.modules.gui.bridge.frame.SwingScilabFrame;
58 import org.scilab.modules.gui.bridge.window.SwingScilabWindow;
59 import org.scilab.modules.gui.events.callback.ScilabCloseCallBack;
60 import org.scilab.modules.gui.toolbar.ToolBar;
61 import org.scilab.modules.gui.utils.Position;
62 import org.scilab.modules.gui.utils.Size;
63 import org.scilab.modules.gui.utils.ToolBarBuilder;
64
65 public class SwingScilabCommonPanel {
66
67     public static final String GRAPHICS_TOOLBAR_DESCRIPTOR = System.getenv("SCI") + "/modules/gui/etc/graphics_toolbar.xml";
68
69     /**
70      * Update the tab after a modification of its properties
71      * @param property the property name
72      * @param value the property value
73      * @see org.scilab.modules.gui.SwingViewObject#update(java.lang.String, java.lang.Object)
74      */
75     protected static void update(SwingScilabPanel component, int property, Object value) {
76         String name;
77         Integer figureId;
78         switch (property) {
79             case  __GO_NAME__ :
80                 name = ((String) value);
81                 figureId = (Integer) GraphicController.getController().getProperty(component.getId(), __GO_ID__);
82                 updateTitle(component, name, figureId);
83                 break;
84             case __GO_ID__ :
85                 /* Update title */
86                 figureId = ((Integer) value);
87                 Figure localFigure = (Figure) GraphicController.getController().getObjectFromId(component.getId());
88                 name = localFigure.getName();
89                 updateTitle(component, name, figureId);
90
91                 /** Update tool bar */
92                 if (localFigure.getToolbarAsEnum() == BarType.FIGURE) {
93                     SwingScilabWindow parentWindow = SwingScilabWindow.allScilabWindows.get(component.getParentWindowId());
94                     ToolBar toolbar = ToolBarBuilder.buildToolBar(GRAPHICS_TOOLBAR_DESCRIPTOR, figureId);
95                     toolbar.setVisible(localFigure.getToolbarVisible());
96                     component.setToolBar(toolbar);
97                     parentWindow.addToolBar(toolbar);
98                 }
99
100                 /* Update callback */
101                 String closingCommand =
102                     "if (get_figure_handle(" + figureId + ") <> []) then"
103                     +      "  if (get(get_figure_handle(" + figureId + "), 'event_handler_enable') == 'on') then"
104                     +      "    execstr(get(get_figure_handle(" + figureId + "), 'event_handler')+'(" + figureId + ", -1, -1, -1000)', 'errcatch', 'm');"
105                     +      "  end;"
106                     +      "  delete(get_figure_handle(" + figureId + "));"
107                     +      "end;";
108                 component.setCallback(null);
109                 component.setCallback(ScilabCloseCallBack.create(component.getId(), closingCommand));
110                 /* Update menus callback */
111                 Integer[] children = (Integer[]) GraphicController.getController().getProperty(component.getId(), __GO_CHILDREN__);
112                 updateChildrenCallbacks(children, figureId);
113                 break;
114             case __GO_SIZE__ :
115                 Integer[] size = (Integer[]) value;
116                 SwingScilabWindow figure = SwingScilabWindow.allScilabWindows.get(component.getParentWindowId());
117                 Size oldFigureSize = figure.getDims();
118                 figure.setDims(new Size(size[0], size[1]));
119                 int deltaFigureX = size[0] - oldFigureSize.getWidth();
120                 int deltaFigureY = size[1] - oldFigureSize.getHeight();
121                 if ( oldFigureSize.getWidth() != 0 && oldFigureSize.getHeight() != 0
122                         && ((oldFigureSize.getWidth() != size[0]) || (oldFigureSize.getHeight() != size[1]))
123                         && ((Boolean) GraphicController.getController().getProperty(component.getId(), __GO_AUTORESIZE__))
124                    ) {
125                     Integer[] axesSize = (Integer[]) GraphicController.getController().getProperty(component.getId(), __GO_AXES_SIZE__);
126                     Integer[] newAxesSize = {axesSize[0] + deltaFigureX, axesSize[1] + deltaFigureY};
127                     GraphicController.getController().setProperty(component.getId(), __GO_AXES_SIZE__, newAxesSize);
128                 }
129                 break;
130             case __GO_POSITION__ :
131                 Integer[] position = (Integer[]) value;
132                 SwingScilabWindow.allScilabWindows.get(component.getParentWindowId()).setPosition(new Position(position[0], position[1]));
133                 break;
134             case __GO_AXES_SIZE__ :
135                 Integer[] axesSize = (Integer[]) value;
136                 Dimension oldAxesSize = component.getContentPane().getSize();
137                 if ( oldAxesSize.getWidth() != 0 && oldAxesSize.getHeight() != 0
138                         && ((oldAxesSize.getWidth() != axesSize[0]) || (oldAxesSize.getHeight() != axesSize[1]))
139                         && ((Boolean) GraphicController.getController().getProperty(component.getId(), __GO_AUTORESIZE__))
140                    ) {
141                     // TODO manage tabs when there are docked (do not change the window size if more than one tab docked)
142                     int deltaX = axesSize[0] - (int) oldAxesSize.getWidth();
143                     int deltaY = axesSize[1] - (int) oldAxesSize.getHeight();
144                     Size parentWindowSize = SwingScilabWindow.allScilabWindows.get(component.getParentWindowId()).getDims();
145                     SwingScilabWindow.allScilabWindows.get(component.getParentWindowId()).setDims(
146                         new Size(parentWindowSize.getWidth() + deltaX, parentWindowSize.getHeight() + deltaY));
147                     Integer figureSize[] = {parentWindowSize.getWidth() + deltaX, parentWindowSize.getHeight() + deltaY};
148                     GraphicController.getController().setProperty(component.getId(), __GO_SIZE__, figureSize);
149                 }
150                 break;
151             case __GO_INFO_MESSAGE__ :
152                 if (component.getInfoBar() != null) {
153                     component.getInfoBar().setText((String) value);
154                 }
155                 break;
156             case __GO_EVENTHANDLER_ENABLE__ :
157                 Boolean enabled = (Boolean) GraphicController.getController().getProperty(component.getId(), __GO_EVENTHANDLER_ENABLE__);
158                 component. setEventHandlerEnabled(enabled);
159                 break;
160             case __GO_EVENTHANDLER_NAME__ :
161                 String eventHandlerName = (String) GraphicController.getController().getProperty(component.getId(), __GO_EVENTHANDLER_NAME__);
162                 component.setEventHandler(eventHandlerName);
163                 break;
164             case __GO_VISIBLE__ :
165                 component.getContentPane().setVisible((Boolean) value);
166                 if (component.getParentWindow().getNbDockedObjects() == 1) {
167                     component.getParentWindow().setVisible((Boolean) value);
168                 }
169                 break;
170             case __GO_INFOBAR_VISIBLE__ :
171                 component.getInfoBar().setVisible((Boolean) value);
172                 break;
173             case __GO_TOOLBAR_VISIBLE__ :
174                 component.getToolBar().setVisible((Boolean) value);
175                 break;
176             case __GO_MENUBAR_VISIBLE__ :
177                 component.getMenuBar().setVisible((Boolean) value);
178                 break;
179             case __GO_RESIZE__ :
180                 component.getParentWindow().setResizable((Boolean) value);
181                 break;
182             case __GO_LAYOUT__ :
183                 LayoutType newLayout = LayoutType.intToEnum((Integer) value);
184                 switch (newLayout) {
185                     case BORDER : {
186                         Integer[] padding = (Integer[]) GraphicController.getController().getProperty(component.getId(), __GO_BORDER_OPT_PADDING__);
187                         component.getWidgetPane().setLayout(new BorderLayout(padding[0], padding[1]));
188                         component.getWidgetPane().setLayout(new BorderLayout());
189                         break;
190                     }
191                     case GRIDBAG : {
192                         component.getWidgetPane().setLayout(new GridBagLayout());
193                         break;
194                     }
195                     case GRID : {
196                         Integer[] padding = (Integer[]) GraphicController.getController().getProperty(component.getId(), __GO_GRID_OPT_PADDING__);
197                         Integer[] grid = (Integer[]) GraphicController.getController().getProperty(component.getId(), __GO_GRID_OPT_GRID__);
198                         if (grid[0] == 0 && grid[1] == 0) {
199                             grid[0] = 1;
200                         }
201                         component.getWidgetPane().setLayout(new GridLayout(grid[0], grid[1], padding[0], padding[1]));
202                         break;
203                     }
204                     case NONE :
205                     default:
206                         component.getWidgetPane().setLayout(null);
207                         break;
208                 }
209                 break;
210             case __GO_GRID_OPT_PADDING__ :
211             case __GO_GRID_OPT_GRID__ : {
212                 Integer layout = (Integer) GraphicController.getController().getProperty(component.getId(), __GO_LAYOUT__);
213                 LayoutType layoutType = LayoutType.intToEnum(layout);
214
215                 if (layoutType != LayoutType.GRID) {
216                     break;
217                 }
218
219                 Integer[] padding = (Integer[]) GraphicController.getController().getProperty(component.getId(), __GO_GRID_OPT_PADDING__);
220                 Integer[] grid = (Integer[]) GraphicController.getController().getProperty(component.getId(), __GO_GRID_OPT_GRID__);
221                 if (grid[0] == 0 && grid[1] == 0) {
222                     grid[0] = 1;
223                 }
224                 component.getWidgetPane().setLayout(new GridLayout(grid[0], grid[1], padding[0], padding[1]));
225                 break;
226             }
227             case __GO_BORDER_OPT_PADDING__ : {
228                 Integer layout = (Integer) GraphicController.getController().getProperty(component.getId(), __GO_LAYOUT__);
229                 LayoutType layoutType = LayoutType.intToEnum(layout);
230
231                 if (layoutType != LayoutType.BORDER) {
232                     break;
233                 }
234
235                 Integer[] padding = (Integer[]) GraphicController.getController().getProperty(component.getId(), __GO_BORDER_OPT_PADDING__);
236                 component.getWidgetPane().setLayout(new BorderLayout(padding[0], padding[1]));
237                 component.getWidgetPane().setLayout(new BorderLayout());
238             }
239         }
240     }
241
242     /**
243      * Update the title of the Tab
244      * @param figureName figure_name property
245      * @param figureId figure_id property
246      */
247     private static void updateTitle(SwingScilabPanel component, String figureName, Integer figureId) {
248         if ((figureName != null) && (figureId != null)) {
249             String figureTitle = figureName.replaceFirst("%d", figureId.toString());
250             component.setName(figureTitle);
251         }
252     }
253
254     /**
255      * Update the menus callbacks when they are linked to the figure ID
256      * @param children the children UID
257      * @param parentFigureId the figure ID
258      */
259     private static void updateChildrenCallbacks(Integer[] children, int parentFigureId) {
260         for (int kChild = 0; kChild < children.length; kChild++) {
261             Integer childType = (Integer) GraphicController.getController().getProperty(children[kChild], __GO_TYPE__);
262             if (childType != null && (
263                         childType == __GO_UIMENU__
264                         || childType == __GO_UIPARENTMENU__
265                         || childType == __GO_UICHILDMENU__
266                         || childType == __GO_UICHECKEDMENU__)) {
267                 String cb = (String) GraphicController.getController().getProperty(children[kChild], __GO_CALLBACK__);
268                 SwingView.getFromId(children[kChild]).update(__GO_CALLBACK__, replaceFigureID(cb, parentFigureId));
269                 Integer[] menuChildren = (Integer[]) GraphicController.getController().getProperty(children[kChild], __GO_CHILDREN__);
270                 updateChildrenCallbacks(menuChildren, parentFigureId);
271             }
272         }
273     }
274
275     /**
276      * Replace pattern [SCILAB_FIGURE_ID] by the figure index
277      * @param initialString string read in XML file
278      * @param parentFigureId the figure ID
279      * @return callback string
280      */
281     private static String replaceFigureID(String initialString, Integer parentFigureId) {
282         return initialString.replaceAll("\\[SCILAB_FIGURE_ID\\]", Integer.toString(parentFigureId));
283     }
284
285     /**
286      * Add a SwingViewObject (from SwingView.java) to container and returns its index
287      * @param member the member to add
288      */
289     protected static void addMember(SwingScilabPanel component, SwingViewObject member) {
290         //member.get
291         Uicontrol uicontrol = (Uicontrol) GraphicModel.getModel().getObjectFromId(member.getId());
292         if (component.getWidgetPane().getLayout() instanceof BorderLayout) {
293             switch (uicontrol.getBorderPositionAsEnum()) {
294                 case BOTTOM:
295                     component.getWidgetPane().add((Component) member, BorderLayout.SOUTH);
296                     break;
297                 case TOP:
298                     component.getWidgetPane().add((Component) member, BorderLayout.NORTH);
299                     break;
300                 case LEFT:
301                     component.getWidgetPane().add((Component) member, BorderLayout.WEST);
302                     break;
303                 case RIGHT:
304                     component.getWidgetPane().add((Component) member, BorderLayout.EAST);
305                     break;
306                 case CENTER:
307                     component.getWidgetPane().add((Component) member, BorderLayout.CENTER);
308                     break;
309                 default:
310                     break;
311             }
312         } else if (component.getWidgetPane().getLayout() instanceof GridBagLayout) {
313             GridBagConstraints constraints = new GridBagConstraints();
314
315             // Grid
316             Integer[] grid = uicontrol.getGridBagGrid();
317             constraints.gridx = grid[0];
318             constraints.gridy = grid[1];
319             constraints.gridwidth = grid[2];
320             constraints.gridheight = grid[3];
321
322             // Weight
323             Double[] weight = uicontrol.getGridBagWeight();
324             constraints.weightx = weight[0];
325             constraints.weighty = weight[1];
326
327             // Anchor
328             switch (uicontrol.getGridBagAnchorAsEnum()) {
329                 case LEFT :
330                     constraints.anchor = GridBagConstraints.EAST;
331                     break;
332                 case UPPER :
333                     constraints.anchor = GridBagConstraints.NORTH;
334                     break;
335                 case LOWER:
336                     constraints.anchor = GridBagConstraints.SOUTH;
337                     break;
338                 case LOWER_LEFT:
339                     constraints.anchor = GridBagConstraints.SOUTHEAST;
340                     break;
341                 case LOWER_RIGHT:
342                     constraints.anchor = GridBagConstraints.SOUTHWEST;
343                     break;
344                 case RIGHT:
345                     constraints.anchor = GridBagConstraints.WEST;
346                     break;
347                 case UPPER_LEFT:
348                     constraints.anchor = GridBagConstraints.NORTHEAST;
349                     break;
350                 case UPPER_RIGHT:
351                     constraints.anchor = GridBagConstraints.NORTHWEST;
352                     break;
353                 case CENTER :
354                 default :
355                     constraints.anchor = GridBagConstraints.CENTER;
356                     break;
357             }
358
359             // Fill
360             switch (uicontrol.getGridBagFillAsEnum()) {
361                 case BOTH :
362                     constraints.fill = GridBagConstraints.BOTH;
363                     break;
364                 case HORIZONTAL:
365                     constraints.fill = GridBagConstraints.HORIZONTAL;
366                     break;
367                 case VERTICAL:
368                     constraints.fill = GridBagConstraints.VERTICAL;
369                     break;
370                 case NONE:
371                 default:
372                     constraints.fill = GridBagConstraints.NONE;
373                     break;
374             }
375
376             // Insets
377             // TODO : add Insets
378
379             // Padding
380             Integer[] padding = uicontrol.getGridBagPadding();
381             constraints.ipadx = padding[0];
382             constraints.ipady = padding[1];
383
384             component.getWidgetPane().add((Component) member, constraints);
385             component.getWidgetPane().revalidate();
386         } else {
387             if (member instanceof SwingScilabFrame) {
388                 component.getWidgetPane().add((Component) member, JLayeredPane.FRAME_CONTENT_LAYER);
389             } else {
390                 component.getWidgetPane().add((Component) member, JLayeredPane.DEFAULT_LAYER + 1);
391             }
392         }
393     }
394     
395     protected static void  removeMember(SwingScilabPanel component,SwingViewObject member) {
396         component.getWidgetPane().remove((Component) member);
397     }
398 }