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