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