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