fix fallthrough in gui
[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             }
247             case __GO_UI_ICON__ : {
248                 File file = new File((String)value);
249                 if (file.exists() == false) {
250                     String filename = FindIconHelper.findImage((String)value);
251                     file = new File(filename);
252                 }
253
254                 try {
255                     BufferedImage icon = ImageIO.read(file);
256                     component.getParentWindow().setIconImage(new ImageIcon(icon).getImage());
257                 } catch (IOException e) {
258                 }
259                 break;
260             }
261         }
262     }
263
264     /**
265      * Update the title of the Tab
266      * @param figureName figure_name property
267      * @param figureId figure_id property
268      */
269     private static void updateTitle(SwingScilabPanel component, String figureName, Integer figureId) {
270         if ((figureName != null) && (figureId != null)) {
271             String figureTitle = figureName.replaceFirst("%d", figureId.toString());
272             component.setName(figureTitle);
273         }
274     }
275
276     /**
277      * Update the menus callbacks when they are linked to the figure ID
278      * @param children the children UID
279      * @param parentFigureId the figure ID
280      */
281     private static void updateChildrenCallbacks(Integer[] children, int parentFigureId) {
282         for (int kChild = 0; kChild < children.length; kChild++) {
283             Integer childType = (Integer) GraphicController.getController().getProperty(children[kChild], __GO_TYPE__);
284             if (childType != null && (
285                         childType == __GO_UIMENU__
286                         || childType == __GO_UIPARENTMENU__
287                         || childType == __GO_UICHILDMENU__
288                         || childType == __GO_UICHECKEDMENU__)) {
289                 String cb = (String) GraphicController.getController().getProperty(children[kChild], __GO_CALLBACK__);
290                 SwingView.getFromId(children[kChild]).update(__GO_CALLBACK__, replaceFigureID(cb, parentFigureId));
291                 Integer[] menuChildren = (Integer[]) GraphicController.getController().getProperty(children[kChild], __GO_CHILDREN__);
292                 updateChildrenCallbacks(menuChildren, parentFigureId);
293             }
294         }
295     }
296
297     /**
298      * Replace pattern [SCILAB_FIGURE_ID] by the figure index
299      * @param initialString string read in XML file
300      * @param parentFigureId the figure ID
301      * @return callback string
302      */
303     private static String replaceFigureID(String initialString, Integer parentFigureId) {
304         return initialString.replaceAll("\\[SCILAB_FIGURE_ID\\]", Integer.toString(parentFigureId));
305     }
306
307     /**
308      * Add a SwingViewObject (from SwingView.java) to container and returns its index
309      * @param member the member to add
310      */
311     protected static void addMember(SwingScilabPanel component, SwingViewObject member) {
312         //member.get
313         Uicontrol uicontrol = (Uicontrol) GraphicModel.getModel().getObjectFromId(member.getId());
314         if (component.getWidgetPane().getLayout() instanceof BorderLayout) {
315             switch (uicontrol.getBorderPositionAsEnum()) {
316                 case BOTTOM:
317                     component.getWidgetPane().add((Component) member, BorderLayout.SOUTH);
318                     break;
319                 case TOP:
320                     component.getWidgetPane().add((Component) member, BorderLayout.NORTH);
321                     break;
322                 case LEFT:
323                     component.getWidgetPane().add((Component) member, BorderLayout.WEST);
324                     break;
325                 case RIGHT:
326                     component.getWidgetPane().add((Component) member, BorderLayout.EAST);
327                     break;
328                 case CENTER:
329                     component.getWidgetPane().add((Component) member, BorderLayout.CENTER);
330                     break;
331                 default:
332                     break;
333             }
334         } else if (component.getWidgetPane().getLayout() instanceof GridBagLayout) {
335             GridBagConstraints constraints = new GridBagConstraints();
336
337             // Grid
338             Integer[] grid = uicontrol.getGridBagGrid();
339             constraints.gridx = grid[0];
340             constraints.gridy = grid[1];
341             constraints.gridwidth = grid[2];
342             constraints.gridheight = grid[3];
343
344             // Weight
345             Double[] weight = uicontrol.getGridBagWeight();
346             constraints.weightx = weight[0];
347             constraints.weighty = weight[1];
348
349             // Anchor
350             switch (uicontrol.getGridBagAnchorAsEnum()) {
351                 case LEFT :
352                     constraints.anchor = GridBagConstraints.EAST;
353                     break;
354                 case UPPER :
355                     constraints.anchor = GridBagConstraints.NORTH;
356                     break;
357                 case LOWER:
358                     constraints.anchor = GridBagConstraints.SOUTH;
359                     break;
360                 case LOWER_LEFT:
361                     constraints.anchor = GridBagConstraints.SOUTHEAST;
362                     break;
363                 case LOWER_RIGHT:
364                     constraints.anchor = GridBagConstraints.SOUTHWEST;
365                     break;
366                 case RIGHT:
367                     constraints.anchor = GridBagConstraints.WEST;
368                     break;
369                 case UPPER_LEFT:
370                     constraints.anchor = GridBagConstraints.NORTHEAST;
371                     break;
372                 case UPPER_RIGHT:
373                     constraints.anchor = GridBagConstraints.NORTHWEST;
374                     break;
375                 case CENTER :
376                 default :
377                     constraints.anchor = GridBagConstraints.CENTER;
378                     break;
379             }
380
381             // Fill
382             switch (uicontrol.getGridBagFillAsEnum()) {
383                 case BOTH :
384                     constraints.fill = GridBagConstraints.BOTH;
385                     break;
386                 case HORIZONTAL:
387                     constraints.fill = GridBagConstraints.HORIZONTAL;
388                     break;
389                 case VERTICAL:
390                     constraints.fill = GridBagConstraints.VERTICAL;
391                     break;
392                 case NONE:
393                 default:
394                     constraints.fill = GridBagConstraints.NONE;
395                     break;
396             }
397
398             // Insets
399             // TODO : add Insets
400
401             // Padding
402             Integer[] padding = uicontrol.getGridBagPadding();
403             constraints.ipadx = padding[0];
404             constraints.ipady = padding[1];
405
406             component.getWidgetPane().add((Component) member, constraints);
407             component.getWidgetPane().revalidate();
408         } else {
409             if (member instanceof SwingScilabScrollableFrame || member instanceof SwingScilabFrame) {
410                 component.getWidgetPane().add((Component) member, JLayeredPane.FRAME_CONTENT_LAYER);
411             } else {
412                 component.getWidgetPane().add((Component) member, JLayeredPane.DEFAULT_LAYER + 1);
413             }
414         }
415     }
416
417     protected static void  removeMember(SwingScilabPanel component, SwingViewObject member) {
418         component.getWidgetPane().remove((Component) member);
419     }
420 }