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