481d09b260a179f2001df0bc3a4d3ce29269aa14
[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                 if (component.getParentWindow().getNbDockedObjects() == 1) {
156                     component.getParentWindow().setVisible((Boolean) value);
157                 }
158                 break;
159             case __GO_INFOBAR_VISIBLE__ :
160                 component.getInfoBar().setVisible((Boolean) value);
161                 break;
162             case __GO_TOOLBAR_VISIBLE__ :
163                 component.getToolBar().setVisible((Boolean) value);
164                 break;
165             case __GO_MENUBAR_VISIBLE__ :
166                 component.getMenuBar().setVisible((Boolean) value);
167                 break;
168             case __GO_RESIZE__ :
169                 component.getParentWindow().setResizable((Boolean) value);
170                 break;
171             case __GO_LAYOUT__ :
172                 LayoutType newLayout = LayoutType.intToEnum((Integer) value);
173                 switch (newLayout) {
174                     case BORDER :
175                         Integer[] padding = (Integer[]) GraphicController.getController().getProperty(component.getId(), __GO_BORDER_OPT_PADDING__);
176                         component.getWidgetPane().setLayout(new BorderLayout(padding[0], padding[1]));
177                         component.getWidgetPane().setLayout(new BorderLayout());
178                         break;
179                     case GRIDBAG :
180                         component.getWidgetPane().setLayout(new GridBagLayout());
181                         break;
182                     case GRID :
183                         component.getWidgetPane().setLayout(new GridLayout());
184                         break;
185                     case NONE :
186                     default:
187                         component.getWidgetPane().setLayout(null);
188                         break;
189                 }
190                 break;
191         }
192     }
193
194     /**
195      * Update the title of the Tab
196      * @param figureName figure_name property
197      * @param figureId figure_id property
198      */
199     private static void updateTitle(SwingScilabPanel component, String figureName, Integer figureId) {
200         if ((figureName != null) && (figureId != null)) {
201             String figureTitle = figureName.replaceFirst("%d", figureId.toString());
202             component.setName(figureTitle);
203         }
204     }
205
206     /**
207      * Update the menus callbacks when they are linked to the figure ID
208      * @param children the children UID
209      * @param parentFigureId the figure ID
210      */
211     private static void updateChildrenCallbacks(Integer[] children, int parentFigureId) {
212         for (int kChild = 0; kChild < children.length; kChild++) {
213             Integer childType = (Integer) GraphicController.getController().getProperty(children[kChild], __GO_TYPE__);
214             if (childType != null && (
215                         childType == __GO_UIMENU__
216                         || childType == __GO_UIPARENTMENU__
217                         || childType == __GO_UICHILDMENU__
218                         || childType == __GO_UICHECKEDMENU__)) {
219                 String cb = (String) GraphicController.getController().getProperty(children[kChild], __GO_CALLBACK__);
220                 SwingView.getFromId(children[kChild]).update(__GO_CALLBACK__, replaceFigureID(cb, parentFigureId));
221                 Integer[] menuChildren = (Integer[]) GraphicController.getController().getProperty(children[kChild], __GO_CHILDREN__);
222                 updateChildrenCallbacks(menuChildren, parentFigureId);
223             }
224         }
225     }
226
227     /**
228      * Replace pattern [SCILAB_FIGURE_ID] by the figure index
229      * @param initialString string read in XML file
230      * @param parentFigureId the figure ID
231      * @return callback string
232      */
233     private static String replaceFigureID(String initialString, Integer parentFigureId) {
234         return initialString.replaceAll("\\[SCILAB_FIGURE_ID\\]", Integer.toString(parentFigureId));
235     }
236
237     /**
238      * Add a SwingViewObject (from SwingView.java) to container and returns its index
239      * @param member the member to add
240      */
241     protected static void addMember(SwingScilabPanel component, SwingViewObject member) {
242         //member.get
243         Uicontrol uicontrol = (Uicontrol) GraphicModel.getModel().getObjectFromId(member.getId());
244         if (component.getWidgetPane().getLayout() instanceof BorderLayout) {
245             switch (uicontrol.getBorderPositionAsEnum()) {
246                 case SOUTH:
247                     component.getWidgetPane().add((Component) member, BorderLayout.SOUTH);
248                     break;
249                 case NORTH:
250                     component.getWidgetPane().add((Component) member, BorderLayout.NORTH);
251                     break;
252                 case WEST:
253                     component.getWidgetPane().add((Component) member, BorderLayout.WEST);
254                     break;
255                 case EAST:
256                     component.getWidgetPane().add((Component) member, BorderLayout.EAST);
257                     break;
258                 case CENTER:
259                     component.getWidgetPane().add((Component) member, BorderLayout.CENTER);
260                     break;
261                 default:
262                     break;
263             }
264         } else if (component.getWidgetPane().getLayout() instanceof GridBagLayout) {
265             GridBagConstraints constraints = new GridBagConstraints();
266
267             // Grid
268             Integer[] grid = uicontrol.getGridBagGrid();
269             constraints.gridx = grid[0];
270             constraints.gridy = grid[1];
271             constraints.gridwidth = grid[2];
272             constraints.gridheight = grid[3];
273
274             // Weight
275             Double[] weight = uicontrol.getGridBagWeight();
276             constraints.weightx = weight[0];
277             constraints.weighty = weight[1];
278
279             // Anchor
280             switch (uicontrol.getGridBagAnchorAsEnum()) {
281                 case LEFT :
282                     constraints.anchor = GridBagConstraints.EAST;
283                     break;
284                 case UPPER :
285                     constraints.anchor = GridBagConstraints.NORTH;
286                     break;
287                 case LOWER:
288                     constraints.anchor = GridBagConstraints.SOUTH;
289                     break;
290                 case LOWER_LEFT:
291                     constraints.anchor = GridBagConstraints.SOUTHEAST;
292                     break;
293                 case LOWER_RIGHT:
294                     constraints.anchor = GridBagConstraints.SOUTHWEST;
295                     break;
296                 case RIGHT:
297                     constraints.anchor = GridBagConstraints.WEST;
298                     break;
299                 case UPPER_LEFT:
300                     constraints.anchor = GridBagConstraints.NORTHEAST;
301                     break;
302                 case UPPER_RIGHT:
303                     constraints.anchor = GridBagConstraints.NORTHWEST;
304                     break;
305                 case CENTER :
306                 default :
307                     constraints.anchor = GridBagConstraints.CENTER;
308                     break;
309             }
310
311             // Fill
312             switch (uicontrol.getGridBagFillAsEnum()) {
313                 case BOTH :
314                     constraints.fill = GridBagConstraints.BOTH;
315                     break;
316                 case HORIZONTAL:
317                     constraints.fill = GridBagConstraints.HORIZONTAL;
318                     break;
319                 case VERTICAL:
320                     constraints.fill = GridBagConstraints.VERTICAL;
321                     break;
322                 case NONE:
323                 default:
324                     constraints.fill = GridBagConstraints.NONE;
325                     break;
326             }
327
328             // Insets
329             // TODO : add Insets
330
331             // Padding
332             Integer[] padding = uicontrol.getGridBagPadding();
333             constraints.ipadx = padding[0];
334             constraints.ipady = padding[1];
335
336             component.getWidgetPane().add((Component) member, constraints);
337             component.getWidgetPane().revalidate();
338         } else {
339             if (member instanceof SwingScilabFrame) {
340                 component.getWidgetPane().add((Component) member, JLayeredPane.FRAME_CONTENT_LAYER);
341             } else {
342                 component.getWidgetPane().add((Component) member, JLayeredPane.DEFAULT_LAYER + 1);
343             }
344         }
345     }
346     
347     protected static void  removeMember(SwingScilabPanel component,SwingViewObject member) {
348         component.getWidgetPane().remove((Component) member);
349     }
350 }