Refactor SwingScilabWindow and SwingScilabPanel
[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                     System.err.println("New Axes Size = " + newAxesSize);
117                     GraphicController.getController().setProperty(component.getId(), __GO_AXES_SIZE__, newAxesSize);
118                 }
119                 break;
120             case __GO_POSITION__ :
121                 Integer[] position = (Integer[]) value;
122                 SwingScilabWindow.allScilabWindows.get(component.getParentWindowId()).setPosition(new Position(position[0], position[1]));
123                 break;
124             case __GO_AXES_SIZE__ :
125                 Integer[] axesSize = (Integer[]) value;
126                 Dimension oldAxesSize = component.getContentPane().getSize();
127                 if ( oldAxesSize.getWidth() != 0 && oldAxesSize.getHeight() != 0
128                         && ((oldAxesSize.getWidth() != axesSize[0]) || (oldAxesSize.getHeight() != axesSize[1]))
129                         && ((Boolean) GraphicController.getController().getProperty(component.getId(), __GO_AUTORESIZE__))
130                    ) {
131                     // TODO manage tabs when there are docked (do not change the window size if more than one tab docked)
132                     int deltaX = axesSize[0] - (int) oldAxesSize.getWidth();
133                     int deltaY = axesSize[1] - (int) oldAxesSize.getHeight();
134                     Size parentWindowSize = SwingScilabWindow.allScilabWindows.get(component.getParentWindowId()).getDims();
135                     SwingScilabWindow.allScilabWindows.get(component.getParentWindowId()).setDims(
136                         new Size(parentWindowSize.getWidth() + deltaX, parentWindowSize.getHeight() + deltaY));
137                     Integer figureSize[] = {parentWindowSize.getWidth() + deltaX, parentWindowSize.getHeight() + deltaY};
138                     GraphicController.getController().setProperty(component.getId(), __GO_SIZE__, figureSize);
139                 }
140                 break;
141             case __GO_INFO_MESSAGE__ :
142                 if (component.getInfoBar() != null) {
143                     component.getInfoBar().setText((String) value);
144                 }
145                 break;
146             case __GO_EVENTHANDLER_ENABLE__ :
147                 Boolean enabled = (Boolean) GraphicController.getController().getProperty(component.getId(), __GO_EVENTHANDLER_ENABLE__);
148                 component. setEventHandlerEnabled(enabled);
149                 break;
150             case __GO_EVENTHANDLER_NAME__ :
151                 String eventHandlerName = (String) GraphicController.getController().getProperty(component.getId(), __GO_EVENTHANDLER_NAME__);
152                 component.setEventHandler(eventHandlerName);
153                 break;
154             case __GO_VISIBLE__ :
155                 component.getContentPane().setVisible((Boolean) value);
156                 break;
157             case __GO_INFOBAR_VISIBLE__ :
158                 component.getInfoBar().setVisible((Boolean) value);
159                 break;
160             case __GO_TOOLBAR_VISIBLE__ :
161                 component.getToolBar().setVisible((Boolean) value);
162                 break;
163             case __GO_MENUBAR_VISIBLE__ :
164                 component.getMenuBar().setVisible((Boolean) value);
165                 break;
166             case __GO_RESIZE__ :
167                 component.getParentWindow().setResizable((Boolean) value);
168                 break;
169             case __GO_LAYOUT__ :
170                 LayoutType newLayout = LayoutType.intToEnum((Integer) value);
171                 switch (newLayout) {
172                     case BORDER :
173                         Integer[] padding = (Integer[]) GraphicController.getController().getProperty(component.getId(), __GO_BORDER_OPT_PADDING__);
174                         component.getWidgetPane().setLayout(new BorderLayout(padding[0], padding[1]));
175                         component.getWidgetPane().setLayout(new BorderLayout());
176                         break;
177                     case GRIDBAG :
178                         component.getWidgetPane().setLayout(new GridBagLayout());
179                         break;
180                     case GRID :
181                         component.getWidgetPane().setLayout(new GridLayout());
182                         break;
183                     case NONE :
184                     default:
185                         component.getWidgetPane().setLayout(null);
186                         break;
187                 }
188                 break;
189         }
190     }
191
192     /**
193      * Update the title of the Tab
194      * @param figureName figure_name property
195      * @param figureId figure_id property
196      */
197     private static void updateTitle(SwingScilabPanel component, String figureName, Integer figureId) {
198         if ((figureName != null) && (figureId != null)) {
199             String figureTitle = figureName.replaceFirst("%d", figureId.toString());
200             component.setName(figureTitle);
201         }
202     }
203
204     /**
205      * Update the menus callbacks when they are linked to the figure ID
206      * @param children the children UID
207      * @param parentFigureId the figure ID
208      */
209     private static void updateChildrenCallbacks(Integer[] children, int parentFigureId) {
210         for (int kChild = 0; kChild < children.length; kChild++) {
211             Integer childType = (Integer) GraphicController.getController().getProperty(children[kChild], __GO_TYPE__);
212             if (childType != null && (
213                         childType == __GO_UIMENU__
214                         || childType == __GO_UIPARENTMENU__
215                         || childType == __GO_UICHILDMENU__
216                         || childType == __GO_UICHECKEDMENU__)) {
217                 String cb = (String) GraphicController.getController().getProperty(children[kChild], __GO_CALLBACK__);
218                 SwingView.getFromId(children[kChild]).update(__GO_CALLBACK__, replaceFigureID(cb, parentFigureId));
219                 Integer[] menuChildren = (Integer[]) GraphicController.getController().getProperty(children[kChild], __GO_CHILDREN__);
220                 updateChildrenCallbacks(menuChildren, parentFigureId);
221             }
222         }
223     }
224
225     /**
226      * Replace pattern [SCILAB_FIGURE_ID] by the figure index
227      * @param initialString string read in XML file
228      * @param parentFigureId the figure ID
229      * @return callback string
230      */
231     private static String replaceFigureID(String initialString, Integer parentFigureId) {
232         return initialString.replaceAll("\\[SCILAB_FIGURE_ID\\]", Integer.toString(parentFigureId));
233     }
234
235     /**
236      * Add a SwingViewObject (from SwingView.java) to container and returns its index
237      * @param member the member to add
238      */
239     protected static void addMember(SwingScilabPanel component, SwingViewObject member) {
240         //member.get
241         Uicontrol uicontrol = (Uicontrol) GraphicModel.getModel().getObjectFromId(member.getId());
242         if (component.getWidgetPane().getLayout() instanceof BorderLayout) {
243             switch (uicontrol.getBorderPositionAsEnum()) {
244                 case SOUTH:
245                     component.getWidgetPane().add((Component) member, BorderLayout.SOUTH);
246                     break;
247                 case NORTH:
248                     component.getWidgetPane().add((Component) member, BorderLayout.NORTH);
249                     break;
250                 case WEST:
251                     component.getWidgetPane().add((Component) member, BorderLayout.WEST);
252                     break;
253                 case EAST:
254                     component.getWidgetPane().add((Component) member, BorderLayout.EAST);
255                     break;
256                 case CENTER:
257                     component.getWidgetPane().add((Component) member, BorderLayout.CENTER);
258                     break;
259                 default:
260                     break;
261             }
262         } else if (component.getWidgetPane().getLayout() instanceof GridBagLayout) {
263             GridBagConstraints constraints = new GridBagConstraints();
264
265             // Grid
266             Integer[] grid = uicontrol.getGridBagGrid();
267             constraints.gridx = grid[0];
268             constraints.gridy = grid[1];
269             constraints.gridwidth = grid[2];
270             constraints.gridheight = grid[3];
271
272             // Weight
273             Double[] weight = uicontrol.getGridBagWeight();
274             constraints.weightx = weight[0];
275             constraints.weighty = weight[1];
276
277             // Anchor
278             switch (uicontrol.getGridBagAnchorAsEnum()) {
279                 case LEFT :
280                     constraints.anchor = GridBagConstraints.EAST;
281                     break;
282                 case UPPER :
283                     constraints.anchor = GridBagConstraints.NORTH;
284                     break;
285                 case LOWER:
286                     constraints.anchor = GridBagConstraints.SOUTH;
287                     break;
288                 case LOWER_LEFT:
289                     constraints.anchor = GridBagConstraints.SOUTHEAST;
290                     break;
291                 case LOWER_RIGHT:
292                     constraints.anchor = GridBagConstraints.SOUTHWEST;
293                     break;
294                 case RIGHT:
295                     constraints.anchor = GridBagConstraints.WEST;
296                     break;
297                 case UPPER_LEFT:
298                     constraints.anchor = GridBagConstraints.NORTHEAST;
299                     break;
300                 case UPPER_RIGHT:
301                     constraints.anchor = GridBagConstraints.NORTHWEST;
302                     break;
303                 case CENTER :
304                 default :
305                     constraints.anchor = GridBagConstraints.CENTER;
306                     break;
307             }
308
309             // Fill
310             switch (uicontrol.getGridBagFillAsEnum()) {
311                 case BOTH :
312                     constraints.fill = GridBagConstraints.BOTH;
313                     break;
314                 case HORIZONTAL:
315                     constraints.fill = GridBagConstraints.HORIZONTAL;
316                     break;
317                 case VERTICAL:
318                     constraints.fill = GridBagConstraints.VERTICAL;
319                     break;
320                 case NONE:
321                 default:
322                     constraints.fill = GridBagConstraints.NONE;
323                     break;
324             }
325
326             // Insets
327             // TODO : add Insets
328
329             // Padding
330             Integer[] padding = uicontrol.getGridBagPadding();
331             constraints.ipadx = padding[0];
332             constraints.ipady = padding[1];
333
334             component.getWidgetPane().add((Component) member, constraints);
335             component.getWidgetPane().revalidate();
336         } else {
337             if (member instanceof SwingScilabFrame) {
338                 component.getWidgetPane().add((Component) member, JLayeredPane.FRAME_CONTENT_LAYER);
339             } else {
340                 component.getWidgetPane().add((Component) member, JLayeredPane.DEFAULT_LAYER + 1);
341             }
342         }
343     }
344 }