9e9318fd9ad468bbbd61c27804df25b9c540974f
[scilab.git] / scilab / modules / graphic_objects / src / java / org / scilab / modules / graphic_objects / xmlloader / XmlSaver.java
1 package org.scilab.modules.graphic_objects.xmlloader;
2
3 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_AXES__;
4 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_LAYOUT__;
5 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_PARENT__;
6 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_TYPE__;
7 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UICONTEXTMENU__;
8 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UICONTROL__;
9 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UIMENU__;
10 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_LAYER__;
11 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_TAB__;
12
13 import java.io.File;
14
15 import javax.xml.parsers.DocumentBuilder;
16 import javax.xml.parsers.DocumentBuilderFactory;
17 import javax.xml.parsers.ParserConfigurationException;
18 import javax.xml.transform.OutputKeys;
19 import javax.xml.transform.Transformer;
20 import javax.xml.transform.TransformerException;
21 import javax.xml.transform.TransformerFactory;
22 import javax.xml.transform.dom.DOMSource;
23 import javax.xml.transform.stream.StreamResult;
24
25 import org.scilab.modules.commons.CommonFileUtils;
26 import org.scilab.modules.graphic_objects.console.Console;
27 import org.scilab.modules.graphic_objects.figure.Figure;
28 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
29 import org.scilab.modules.graphic_objects.graphicModel.GraphicModel;
30 import org.scilab.modules.graphic_objects.graphicObject.GraphicObject;
31 import org.scilab.modules.graphic_objects.uicontrol.Uicontrol;
32 import org.scilab.modules.graphic_objects.uicontrol.Uicontrol.UicontrolStyle;
33 import org.scilab.modules.graphic_objects.uicontrol.frame.border.FrameBorder;
34 import org.scilab.modules.graphic_objects.uicontrol.frame.border.FrameBorderType;
35 import org.scilab.modules.graphic_objects.uimenu.Uimenu;
36 import org.scilab.modules.graphic_objects.utils.LayoutType;
37 import org.w3c.dom.Document;
38 import org.w3c.dom.Element;
39 import org.w3c.dom.Node;
40
41
42 public class XmlSaver {
43     static private Uicontrol defaultUi = null;
44     static private Uimenu defaultMenu = null;
45     static private Figure defaultFig = null;
46     static private FrameBorder defaultBorder = null;
47
48     public static String save(int figure, String filename, boolean reverseChildren) {
49         try {
50             //init default values
51             defaultFig = GraphicModel.getFigureModel();
52             defaultBorder = new FrameBorder();
53
54             DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
55             DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
56
57             //root elements
58             Document doc = docBuilder.newDocument();
59             //standalone="yes"
60             doc.setXmlStandalone(true);
61
62             //scilab header
63             Element header = doc.createElement("scilabgui");
64             setAttribute(header, "version", "1.0", "");
65             setAttribute(header, "usedeprecatedskin", createAttribute(Console.getConsole().getUseDeprecatedLF()), "");
66
67             //figure
68             Element figureElement = createFigure(doc, figure, reverseChildren);
69             appendChild(header, figureElement);
70             appendChild(doc, header);
71
72             //write the content into xml file
73             TransformerFactory transformerFactory = TransformerFactory.newInstance();
74             Transformer transformer = transformerFactory.newTransformer();
75             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
76             transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
77             doc.getDocumentElement().normalize();
78             DOMSource source = new DOMSource(doc);
79
80
81             //process filename
82             File f = new File(filename);
83             if (f.isAbsolute() == false) {
84                 String initialDirectoryPath = CommonFileUtils.getCWD();
85                 f = new File(initialDirectoryPath + File.separator + filename);
86             }
87
88             StreamResult result =  new StreamResult(f);
89             transformer.transform(source, result);
90         } catch (ParserConfigurationException pce) {
91             return pce.getMessage();
92         } catch (TransformerException tfe) {
93             return tfe.getMessage();
94         }
95
96         defaultFig = null;
97         defaultUi = null;
98         defaultBorder = null;
99         return ""; //all good
100     }
101
102     private static Element createFigure(Document doc, int figure, boolean reverseChildren) {
103         GraphicController controller = GraphicController.getController();
104         Figure fig = (Figure)controller.getObjectFromId(figure);
105         Element elemFig = doc.createElement("figure");
106
107         //figure_position
108         setAttribute(elemFig, "figure_position", createAttribute(fig.getPosition()), createAttribute(defaultFig.getPosition()));
109         //figure_size
110         setAttribute(elemFig, "figure_size", createAttribute(fig.getSize()), createAttribute(defaultFig.getSize()));
111         //axes_size
112         setAttribute(elemFig, "axes_size", createAttribute(fig.getAxesSize()), createAttribute(defaultFig.getAxesSize()));
113         //auto_resize
114         setAttribute(elemFig, "auto_resize", createAttribute(fig.getAutoResize()), createAttribute(defaultFig.getAutoResize()));
115         //view_port
116         setAttribute(elemFig, "view_port", createAttribute(fig.getViewport()), createAttribute(defaultFig.getViewport()));
117         //figure_name
118         setAttribute(elemFig, "figure_name", createAttribute(fig.getName()), createAttribute(defaultFig.getName()));
119         //figure_id
120         setAttribute(elemFig, "figure_id", createAttribute(fig.getId()), createAttribute(defaultFig.getId()));
121         //info_message
122         setAttribute(elemFig, "info_message", createAttribute(fig.getInfoMessage()), createAttribute(defaultFig.getInfoMessage()));
123         //pixel_drawing_mode
124         setAttribute(elemFig, "pixel_drawing_mode", createAttribute(fig.getPixelDrawingMode()), createAttribute(defaultFig.getPixelDrawingMode()));
125         //anti_aliasing
126         setAttribute(elemFig, "anti_aliasing", createAttribute(fig.getAntialiasing()), createAttribute(defaultFig.getAntialiasing()));
127         //immediate_drawing
128         setAttribute(elemFig, "immediate_drawing", createAttribute(fig.getImmediateDrawing()), createAttribute(defaultFig.getImmediateDrawing()));
129         //background
130         setAttribute(elemFig, "background", createAttribute(fig.getBackground()), createAttribute(defaultFig.getBackground()));
131         //visible
132         setAttribute(elemFig, "visible", createAttribute(fig.getVisible()), createAttribute(defaultFig.getVisible()));
133         //rotation_style
134         setAttribute(elemFig, "rotation_style", createAttribute(fig.getRotation()), createAttribute(defaultFig.getRotation()));
135         //event_handler
136         setAttribute(elemFig, "event_handler", createAttribute(fig.getEventHandlerString()), createAttribute(defaultFig.getEventHandlerString()));
137         //event_handler_enable
138         setAttribute(elemFig, "event_handler_enable", createAttribute(fig.getEventHandlerEnable()), createAttribute(defaultFig.getEventHandlerEnable()));
139         //resizefcn
140         setAttribute(elemFig, "resizefcn", createAttribute(fig.getResizeFcn()), createAttribute(defaultFig.getResizeFcn()));
141         //closerequestfcn
142         setAttribute(elemFig, "closerequestfcn", createAttribute(fig.getCloseRequestFcn()), createAttribute(defaultFig.getCloseRequestFcn()));
143         //resize
144         setAttribute(elemFig, "resize", createAttribute(fig.getResize()), createAttribute(defaultFig.getResize()));
145         //toolbar
146         setAttribute(elemFig, "toolbar", createAttribute(fig.getToolbar()), createAttribute(defaultFig.getToolbar()));
147         //toolbar_visible
148         setAttribute(elemFig, "toolbar_visible", createAttribute(fig.getToolbarVisible()), createAttribute(defaultFig.getToolbarVisible()));
149         //menubar
150         setAttribute(elemFig, "menubar", createAttribute(fig.getMenubar()), createAttribute(defaultFig.getMenubar()));
151         //menubar_visible
152         setAttribute(elemFig, "menubar_visible", createAttribute(fig.getMenubarVisible()), createAttribute(defaultFig.getMenubarVisible()));
153         //infobar_visible
154         setAttribute(elemFig, "infobar_visible", createAttribute(fig.getInfobarVisible()), createAttribute(defaultFig.getInfobarVisible()));
155         //dockable
156         setAttribute(elemFig, "dockable", createAttribute(fig.getDockable()), createAttribute(defaultFig.getDockable()));
157         //layout
158         setAttribute(elemFig, "layout", createAttribute(fig.getLayout()), createAttribute(defaultFig.getLayout()));
159
160         //default_axes
161         setAttribute(elemFig, "default_axes", createAttribute(fig.hasDefaultAxes()), createAttribute(defaultFig.hasDefaultAxes()));
162         //icon
163         setAttribute(elemFig, "icon", createAttribute(fig.getIcon()), createAttribute(defaultFig.getIcon()));
164         //tag
165         setAttribute(elemFig, "tag", createAttribute(fig.getTag()), createAttribute(defaultFig.getTag()));
166
167         //layout_options
168         if (fig.getLayoutAsEnum() == LayoutType.GRID) {
169             setAttribute(elemFig, "grid_opt_grid", createAttribute(fig.getGridOptGrid()), createAttribute(defaultFig.getGridOptGrid()));
170             setAttribute(elemFig, "grid_opt_padding", createAttribute(fig.getGridOptPadding()), createAttribute(defaultFig.getGridOptPadding()));
171         } else if (fig.getLayoutAsEnum() == LayoutType.BORDER) {
172             setAttribute(elemFig, "border_opt_padding", createAttribute(fig.getBorderOptPadding()), createAttribute(defaultFig.getBorderOptPadding()));
173         }
174
175         //children
176         Integer[] children = fig.getChildren();
177         for (int i = 0; i < children.length; i++) {
178             appendChild(elemFig, createElement(doc, children[i], reverseChildren));
179         }
180         return elemFig;
181     }
182
183     public static void appendChild(Node parent, Node child) {
184         if (child != null) {
185             parent.appendChild(child);
186         }
187     }
188
189     private static Element createElement(Document doc, int id, boolean reverseChildren) {
190         GraphicController controller = GraphicController.getController();
191         Integer type = (Integer)controller.getProperty(id, __GO_TYPE__);
192
193         switch (type) {
194             case __GO_UICONTROL__ : {
195                 return createUicontrol(doc, id, reverseChildren);
196             }
197             case __GO_UIMENU__ : {
198                 //Uimenu uim = (Uimenu)controller.getObjectFromId(id);
199                 return createUimenu(doc, id, reverseChildren);
200             }
201             case __GO_UICONTEXTMENU__ : {
202                 //Uicontextmenu uicm = (Uicontextmenu)controller.getObjectFromId(id);
203                 return doc.createElement("contextmenu");
204             }
205             case __GO_AXES__ : {
206                 //Axes axes = (Axes)controller.getObjectFromId(id);
207                 return doc.createElement("axes");
208             }
209         }
210
211         return doc.createElement("fake");
212     }
213
214     private static Element createBorders(Document doc, Integer id) {
215         return createBorders(doc, id, "borders");
216     }
217
218     private static Element createBorders(Document doc, Integer id, String borderName) {
219         GraphicController controller = GraphicController.getController();
220         FrameBorder border = (FrameBorder)controller.getObjectFromId(id);
221
222         if (border == null || border.getStyleAsEnum() == FrameBorderType.NONE) {
223             return null;
224         }
225
226         Element elemBorders = doc.createElement(borderName);
227
228         setAttribute(elemBorders, "style", createAttribute(border.getStyleAsEnum().toString().toLowerCase()), "");
229
230         switch (border.getStyleAsEnum()) {
231             case BEVEL: {
232                 setAttribute(elemBorders, "type", createAttribute(border.getBorderType()), createAttribute(defaultBorder.getBorderType()));
233                 setAttribute(elemBorders, "hlout", createAttribute(border.getHlOut()), createAttribute(defaultBorder.getHlOut()));
234                 setAttribute(elemBorders, "hlin", createAttribute(border.getHlIn()), createAttribute(defaultBorder.getHlIn()));
235                 setAttribute(elemBorders, "shadowout", createAttribute(border.getShadowOut()), createAttribute(defaultBorder.getShadowOut()));
236                 setAttribute(elemBorders, "shadowin", createAttribute(border.getShadowIn()), createAttribute(defaultBorder.getShadowIn()));
237                 break;
238             }
239             case COMPOUND: {
240                 Element out = createBorders(doc, border.getOutBorder(), "out");
241                 if (out != null) {
242                     appendChild(elemBorders, out);
243                 }
244
245                 Element in = createBorders(doc, border.getInBorder(), "in");
246                 if (in != null) {
247                     appendChild(elemBorders, in);
248                 }
249                 break;
250             }
251             case EMPTY: {
252                 setAttribute(elemBorders, "position", createAttribute(border.getPosition()), createAttribute(defaultBorder.getPosition()));
253                 break;
254             }
255             case ETCHED: {
256                 setAttribute(elemBorders, "type", createAttribute(border.getBorderType()), createAttribute(defaultBorder.getBorderType()));
257                 setAttribute(elemBorders, "hl", createAttribute(border.getHlOut()), createAttribute(defaultBorder.getHlOut()));
258                 setAttribute(elemBorders, "shadow", createAttribute(border.getShadowOut()), createAttribute(defaultBorder.getShadowOut()));
259                 break;
260             }
261             case LINE: {
262                 setAttribute(elemBorders, "color", createAttribute(border.getColor()), createAttribute(defaultBorder.getColor()));
263                 setAttribute(elemBorders, "thickness", createAttribute(border.getThickness()), createAttribute(defaultBorder.getThickness()));
264                 setAttribute(elemBorders, "rounded", createAttribute(border.getRounded()), createAttribute(defaultBorder.getRounded()));
265                 break;
266             }
267             case MATTE: {
268                 setAttribute(elemBorders, "position", createAttribute(border.getPosition()), createAttribute(defaultBorder.getPosition()));
269                 setAttribute(elemBorders, "color", createAttribute(border.getColor()), createAttribute(defaultBorder.getColor()));
270                 break;
271             }
272             case SOFTBEVEL: {
273                 setAttribute(elemBorders, "type", createAttribute(border.getBorderType()), createAttribute(defaultBorder.getBorderType()));
274                 setAttribute(elemBorders, "hlout", createAttribute(border.getHlOut()), createAttribute(defaultBorder.getHlOut()));
275                 setAttribute(elemBorders, "hlin", createAttribute(border.getHlIn()), createAttribute(defaultBorder.getHlIn()));
276                 setAttribute(elemBorders, "shadowout", createAttribute(border.getShadowOut()), createAttribute(defaultBorder.getShadowOut()));
277                 setAttribute(elemBorders, "shadowin", createAttribute(border.getShadowIn()), createAttribute(defaultBorder.getShadowIn()));
278                 break;
279             }
280             case TITLED: {
281                 setAttribute(elemBorders, "title", createAttribute(border.getTitle()), createAttribute(defaultBorder.getTitle()));
282                 setAttribute(elemBorders, "justification", createAttribute(border.getJustification()), createAttribute(defaultBorder.getJustification()));
283                 setAttribute(elemBorders, "titleposition", createAttribute(border.getTitlePosition()), createAttribute(defaultBorder.getTitlePosition()));
284                 setAttribute(elemBorders, "fontname", createAttribute(border.getFontName()), createAttribute(defaultBorder.getFontName()));
285                 setAttribute(elemBorders, "fontangle", createAttribute(border.getFontAngle()), createAttribute(defaultBorder.getFontAngle()));
286                 setAttribute(elemBorders, "fontsize", createAttribute(border.getFontSize()), createAttribute(defaultBorder.getFontSize()));
287                 setAttribute(elemBorders, "fontweight", createAttribute(border.getFontWeight()), createAttribute(defaultBorder.getFontWeight()));
288                 setAttribute(elemBorders, "color", createAttribute(border.getColor()), createAttribute(defaultBorder.getColor()));
289                 Element title = createBorders(doc, border.getTitleBorder(), "title");
290                 if (title != null) {
291                     appendChild(elemBorders, title);
292                 }
293                 break;
294             }
295             case NONE:
296             default: {
297                 break;
298             }
299         }
300         return elemBorders;
301     }
302
303     private static Element createUicontrol(Document doc, Integer id, boolean reverseChildren) {
304         GraphicController controller = GraphicController.getController();
305         Uicontrol uic = (Uicontrol)controller.getObjectFromId(id);
306         initDefaultui(uic.getStyle());
307         Element elemUi = doc.createElement(uic.getStyleAsEnum().toString().toLowerCase());
308
309         //BackgroundColor
310         setAttribute(elemUi, "backgroundcolor", createAttribute(uic.getBackgroundColor()), createAttribute(defaultUi.getBackgroundColor()));
311         //Enable
312         setAttribute(elemUi, "enable", createAttribute(uic.getEnable()), createAttribute(defaultUi.getEnable()));
313         //FontAngle
314         setAttribute(elemUi, "fontangle", createAttribute(uic.getFontAngle()), createAttribute(defaultUi.getFontAngle()));
315         //FontName
316         setAttribute(elemUi, "fontname", createAttribute(uic.getFontName()), createAttribute(defaultUi.getFontName()));
317         //FontSize
318         setAttribute(elemUi, "fontsize", createAttribute(uic.getFontSize()), createAttribute(defaultUi.getFontSize()));
319         //FontUnits
320         setAttribute(elemUi, "fontunits", createAttribute(uic.getFontUnits()), createAttribute(defaultUi.getFontUnits()));
321         //FontWeight
322         setAttribute(elemUi, "fontweight", createAttribute(uic.getFontWeight()), createAttribute(defaultUi.getFontWeight()));
323         //ForegroundColor
324         setAttribute(elemUi, "foregroundcolor", createAttribute(uic.getForegroundColor()), createAttribute(defaultUi.getForegroundColor()));
325         //HorizontalAlignment
326         setAttribute(elemUi, "horizontalalignment", createAttribute(uic.getHorizontalAlignment()), createAttribute(defaultUi.getHorizontalAlignment()));
327         //ListboxTop
328         setAttribute(elemUi, "listboxtop", createAttribute(uic.getListboxTop()), createAttribute(defaultUi.getListboxTop()));
329         //Max
330         setAttribute(elemUi, "max", createAttribute(uic.getMax()), createAttribute(defaultUi.getMax()));
331         //Min
332         setAttribute(elemUi, "min", createAttribute(uic.getMin()), createAttribute(defaultUi.getMin()));
333         //Relief
334         setAttribute(elemUi, "relief", createAttribute(uic.getRelief()), createAttribute(defaultUi.getRelief()));
335         //SliderStep
336         setAttribute(elemUi, "sliderstep", createAttribute(uic.getSliderStep()), createAttribute(defaultUi.getSliderStep()));
337         //String
338         //do not save string and value of tab/layer to avoid setting bad index during loading
339         if (uic.getStyle() != __GO_UI_TAB__ && uic.getStyle() != __GO_UI_LAYER__) {
340             setAttribute(elemUi, "string", createAttribute(uic.getString()), createAttribute(defaultUi.getString()));
341         }
342         //TooltipString
343         setAttribute(elemUi, "tooltipstring", createAttribute(uic.getTooltipString()), createAttribute(defaultUi.getTooltipString()));
344         //Units
345         setAttribute(elemUi, "units", createAttribute(uic.getUnits()), createAttribute(defaultUi.getUnits()));
346         //Value
347         //do not save string and value of tab/layer to avoid setting bad index during loading
348         if (uic.getStyle() != __GO_UI_TAB__ && uic.getStyle() != __GO_UI_LAYER__) {
349             setAttribute(elemUi, "value", createAttribute(uic.getUiValue()), createAttribute(defaultUi.getUiValue()));
350         }
351         //VerticalAlignment
352         setAttribute(elemUi, "verticalalignment", createAttribute(uic.getVerticalAlignment()), createAttribute(defaultUi.getVerticalAlignment()));
353         //Visible
354         setAttribute(elemUi, "visible", createAttribute(uic.getVisible()), createAttribute(!defaultUi.getVisible()));
355         //Callback
356         setAttribute(elemUi, "callback", createAttribute(uic.getCallbackString()), createAttribute(defaultUi.getCallbackString()));
357         //Callback_Type
358         setAttribute(elemUi, "callback_type", createAttribute(uic.getCallbackType()), createAttribute(defaultUi.getCallbackType()));
359
360         //Constraints
361         Integer parent = (Integer)controller.getProperty(uic.getIdentifier(), __GO_PARENT__);
362         Integer iLayout = (Integer)controller.getProperty(parent, __GO_LAYOUT__);
363         LayoutType layout = LayoutType.intToEnum(iLayout);
364
365         if (layout == LayoutType.GRIDBAG) {
366             setAttribute(elemUi, "gridbaggrid", createAttribute(uic.getGridBagGrid()), createAttribute(defaultUi.getGridBagGrid()));
367             setAttribute(elemUi, "gridbagweight", createAttribute(uic.getGridBagWeight()), createAttribute(defaultUi.getGridBagWeight()));
368             setAttribute(elemUi, "gridbagfill", createAttribute(uic.getGridBagFill()), createAttribute(defaultUi.getGridBagFill()));
369             setAttribute(elemUi, "gridbaganchor", createAttribute(uic.getGridBagAnchor()), createAttribute(defaultUi.getGridBagAnchor()));
370             setAttribute(elemUi, "gridbagpadding", createAttribute(uic.getGridBagPadding()), createAttribute(defaultUi.getGridBagPadding()));
371             setAttribute(elemUi, "gridbagpreferredsize", createAttribute(uic.getGridBagPreferredSize()), createAttribute(defaultUi.getGridBagPreferredSize()));
372         } else if (layout == LayoutType.BORDER) {
373             setAttribute(elemUi, "borderposition", createAttribute(uic.getBorderPosition()), createAttribute(defaultUi.getBorderPosition()));
374             setAttribute(elemUi, "borderpreferredsize", createAttribute(uic.getBorderPreferredSize()), createAttribute(defaultUi.getBorderPreferredSize()));
375         } else if (layout == LayoutType.NONE) {
376             //Position
377             setAttribute(elemUi, "position", createAttribute(uic.getUiPosition()), createAttribute(defaultUi.getUiPosition()));
378         }
379
380         //Margins
381         setAttribute(elemUi, "margins", createAttribute(uic.getMargins()), createAttribute(defaultUi.getMargins()));
382         //Groupname
383         setAttribute(elemUi, "groupname", createAttribute(uic.getGroupName()), createAttribute(defaultUi.getGroupName()));
384
385         //icon
386         setAttribute(elemUi, "icon", createAttribute(uic.getIcon()), createAttribute(defaultUi.getIcon()));
387         //Tag
388         setAttribute(elemUi, "tag", createAttribute(uic.getTag()), createAttribute(defaultUi.getTag()));
389
390         //only for frame type
391         if (uic.getStyleAsEnum() == UicontrolStyle.FRAME) {
392             //Borders
393             Integer iBorders = uic.getFrameBorder();
394             Element borders = createBorders(doc, iBorders);
395             if (borders != null) {
396                 appendChild(elemUi, createBorders(doc, iBorders));
397             }
398
399             //Scrollable
400             setAttribute(elemUi, "scrollable", createAttribute(uic.getScrollable()), createAttribute(defaultUi.getScrollable()));
401
402             //Layout
403             setAttribute(elemUi, "layout", createAttribute(uic.getLayout()), createAttribute(defaultUi.getLayout()));
404             //Layout_options
405             if (uic.getLayoutAsEnum() == LayoutType.GRID) {
406                 setAttribute(elemUi, "gridoptgrid", createAttribute(uic.getGridOptGrid()), createAttribute(defaultUi.getGridOptGrid()));
407                 setAttribute(elemUi, "gridoptpadding", createAttribute(uic.getGridOptPadding()), createAttribute(defaultUi.getGridOptPadding()));
408             } else if (uic.getLayoutAsEnum() == LayoutType.BORDER) {
409                 setAttribute(elemUi, "borderoptpadding", createAttribute(uic.getBorderOptPadding()), createAttribute(defaultUi.getBorderOptPadding()));
410             }
411         }
412
413         //children
414         Integer[] children = uic.getChildren();
415         if (reverseChildren) {
416             for (int i = children.length - 1 ; i >= 0 ; i--) {
417                 appendChild(elemUi, createElement(doc, children[i], reverseChildren));
418             }
419         } else {
420             for (int i = 0 ; i < children.length ; i++) {
421                 appendChild(elemUi, createElement(doc, children[i], reverseChildren));
422             }
423         }
424         return elemUi;
425     }
426
427     private static Element createUimenu(Document doc, Integer id, boolean reverseChildren) {
428         GraphicController controller = GraphicController.getController();
429         Uimenu uim = (Uimenu)controller.getObjectFromId(id);
430         //bypass hidden menu
431         if (uim.isHidden()) {
432             return null;
433         }
434
435         initDefaultMenu();
436         Element elemMenu = doc.createElement("menu");
437
438         //enable
439         setAttribute(elemMenu, "enable", createAttribute(uim.getEnable()), createAttribute(defaultMenu.getEnable()));
440         //foregroundcolor
441         setAttribute(elemMenu, "foregroundcolor", createAttribute(uim.getForegroundColor()), createAttribute(defaultMenu.getForegroundColor()));
442         //label
443         setAttribute(elemMenu, "label", createAttribute(uim.getLabel()), createAttribute(defaultMenu.getLabel()));
444         //handle_visible
445         setAttribute(elemMenu, "hidden", createAttribute(uim.isHidden()), createAttribute(defaultMenu.isHidden()));
446         //visible
447         setAttribute(elemMenu, "visible", createAttribute(uim.getVisible()), createAttribute(defaultMenu.getVisible()));
448         //callback
449         setAttribute(elemMenu, "callback", createAttribute(uim.getCallbackString()), createAttribute(defaultMenu.getCallbackString()));
450         //callback_type
451         setAttribute(elemMenu, "callback_type", createAttribute(uim.getCallbackType()), createAttribute(defaultMenu.getCallbackType()));
452         //checked
453         setAttribute(elemMenu, "checked", createAttribute(uim.getChecked()), createAttribute(defaultMenu.getChecked()));
454         //icon
455         setAttribute(elemMenu, "icon", createAttribute(uim.getIcon()), createAttribute(defaultMenu.getIcon()));
456         //tag
457         setAttribute(elemMenu, "tag", createAttribute(uim.getTag()), createAttribute(defaultMenu.getTag()));
458
459         //children
460         Integer[] children = uim.getChildren();
461         for (int i = 0; i < children.length; i++) {
462             appendChild(elemMenu, createElement(doc, children[i], reverseChildren));
463         }
464
465         return elemMenu;
466     }
467
468     private static String createAttribute(Boolean val) {
469         if (val == null) {
470             return "off";
471         }
472
473         if (val) {
474             return "on";
475         } else {
476             return "off";
477         }
478     }
479
480     private static String createAttribute(Integer val) {
481         if (val == null) {
482             return null;
483         }
484
485         StringBuilder builder = new StringBuilder();
486         builder.append(val.intValue());
487         return builder.toString();
488     }
489
490     private static String createAttribute(String val) {
491         if (val == null) {
492             return null;
493         }
494         return val;
495     }
496
497     private static String createAttribute(Double val) {
498         if (val == null) {
499             return null;
500         }
501
502         StringBuilder builder = new StringBuilder();
503         builder.append(val.doubleValue());
504         return builder.toString();
505     }
506
507     //    private static String createAttribute(Boolean[] val) {
508     //        if(val == null || val.length == 0) {
509     //            return null;
510     //        }
511     //
512     //        StringBuilder onoff = new StringBuilder();
513     //
514     //        onoff.append(createAttribute(val[0]));
515     //
516     //        for(int i = 1 ; i < val.length; i++)
517     //        {
518     //            onoff.append(",");
519     //            onoff.append(createAttribute(val[i]));
520     //        }
521     //
522     //        return onoff.toString();
523     //    }
524
525
526     private static String createAttribute(String[] val) {
527         if (val == null || val.length == 0) {
528             return null;
529         }
530
531         StringBuilder onoff = new StringBuilder();
532
533         onoff.append(createAttribute(val[0]));
534
535         for (int i = 1 ; i < val.length; i++) {
536             onoff.append(",");
537             onoff.append(createAttribute(val[i]));
538         }
539
540         return onoff.toString();
541     }
542
543     private static String createAttribute(Double[] val) {
544         if (val == null || val.length == 0) {
545             return null;
546         }
547
548         StringBuilder onoff = new StringBuilder();
549
550         onoff.append(createAttribute(val[0]));
551
552         for (int i = 1 ; i < val.length; i++) {
553             onoff.append(",");
554             onoff.append(createAttribute(val[i]));
555         }
556
557         return onoff.toString();
558     }
559
560     private static String createAttribute(Integer[] val) {
561         if (val == null || val.length == 0) {
562             return null;
563         }
564
565         StringBuilder onoff = new StringBuilder();
566
567         onoff.append(createAttribute(val[0]));
568
569         for (int i = 1 ; i < val.length; i++) {
570             onoff.append(",");
571             onoff.append(createAttribute(val[i]));
572         }
573
574         return onoff.toString();
575     }
576
577     private static void setAttribute(Element elem, String property, String value, String ref) {
578         if (value == null || property == null || elem == null || (ref != null && value.equals(ref))) {
579             return;
580         }
581
582         elem.setAttribute(property, value);
583     }
584
585     private static void initDefaultui(Integer uicontrolStyle) {
586         Integer uic = GraphicController.getController().askObject(GraphicObject.getTypeFromName(uicontrolStyle));
587         defaultUi = (Uicontrol) GraphicController.getController().getObjectFromId(uic);
588     }
589
590     private static void initDefaultMenu() {
591         Integer uic = GraphicController.getController().askObject(GraphicObject.getTypeFromName(__GO_UIMENU__));
592         defaultMenu = (Uimenu) GraphicController.getController().getObjectFromId(uic);
593     }
594
595 }