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