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