saveGui : exclude background from figure properties
[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 import java.util.Arrays;
16
17 import javax.xml.parsers.DocumentBuilder;
18 import javax.xml.parsers.DocumentBuilderFactory;
19 import javax.xml.parsers.ParserConfigurationException;
20 import javax.xml.transform.OutputKeys;
21 import javax.xml.transform.Transformer;
22 import javax.xml.transform.TransformerException;
23 import javax.xml.transform.TransformerFactory;
24 import javax.xml.transform.dom.DOMSource;
25 import javax.xml.transform.stream.StreamResult;
26
27 import org.scilab.modules.commons.CommonFileUtils;
28 import org.scilab.modules.graphic_objects.axes.Axes;
29 import org.scilab.modules.graphic_objects.console.Console;
30 import org.scilab.modules.graphic_objects.figure.Figure;
31 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
32 import org.scilab.modules.graphic_objects.graphicModel.GraphicModel;
33 import org.scilab.modules.graphic_objects.graphicObject.GraphicObject;
34 import org.scilab.modules.graphic_objects.uicontrol.Uicontrol;
35 import org.scilab.modules.graphic_objects.uicontrol.Uicontrol.UicontrolStyle;
36 import org.scilab.modules.graphic_objects.uicontrol.frame.border.FrameBorder;
37 import org.scilab.modules.graphic_objects.uicontrol.frame.border.FrameBorderType;
38 import org.scilab.modules.graphic_objects.uimenu.Uimenu;
39 import org.scilab.modules.graphic_objects.utils.LayoutType;
40 import org.w3c.dom.Document;
41 import org.w3c.dom.Element;
42 import org.w3c.dom.Node;
43
44
45 public class XmlSaver {
46     static private Figure defaultFig = null;
47     static private FrameBorder defaultBorder = null;
48
49     public static String save(int figure, String filename, boolean reverseChildren) {
50         try {
51             //init default values
52             defaultFig = GraphicModel.getFigureModel();
53             defaultBorder = new FrameBorder();
54
55             DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
56             DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
57
58             //root elements
59             Document doc = docBuilder.newDocument();
60             //standalone="yes"
61             doc.setXmlStandalone(true);
62
63             //scilab header
64             Element header = doc.createElement("scilabgui");
65             setAttribute(header, "version", "1.0", "");
66             setAttribute(header, "usedeprecatedskin", createAttribute(Console.getConsole().getUseDeprecatedLF()), "");
67
68             //figure
69             Element figureElement = createFigure(doc, figure, reverseChildren);
70             appendChild(header, figureElement);
71             appendChild(doc, header);
72
73             //write the content into xml file
74             TransformerFactory transformerFactory = TransformerFactory.newInstance();
75             Transformer transformer = transformerFactory.newTransformer();
76             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
77             transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
78             doc.getDocumentElement().normalize();
79             DOMSource source = new DOMSource(doc);
80
81
82             //process filename
83             File f = new File(filename);
84             if (f.isAbsolute() == false) {
85                 String initialDirectoryPath = CommonFileUtils.getCWD();
86                 f = new File(initialDirectoryPath + File.separator + filename);
87             }
88
89             StreamResult result =  new StreamResult(f);
90             transformer.transform(source, result);
91         } catch (ParserConfigurationException pce) {
92             return pce.getMessage();
93         } catch (TransformerException tfe) {
94             return tfe.getMessage();
95         }
96
97         defaultFig = 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(Figure.RotationType.enumToString(fig.getRotationAsEnum())), createAttribute(Figure.RotationType.enumToString(defaultFig.getRotationAsEnum())));
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(Figure.BarType.enumToString(fig.getToolbarAsEnum())), createAttribute(Figure.BarType.enumToString(defaultFig.getToolbarAsEnum())));
147         //toolbar_visible
148         setAttribute(elemFig, "toolbar_visible", createAttribute(fig.getToolbarVisible()), createAttribute(defaultFig.getToolbarVisible()));
149         //menubar
150         setAttribute(elemFig, "menubar", createAttribute(Figure.BarType.enumToString(fig.getMenubarAsEnum())), createAttribute(Figure.BarType.enumToString(defaultFig.getMenubarAsEnum())));
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(LayoutType.enumToString(fig.getLayoutAsEnum())), createAttribute(LayoutType.enumToString(defaultFig.getLayoutAsEnum())));
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 createAxes(doc, id, reverseChildren);
208             }
209         }
210
211         return doc.createElement("fake");
212     }
213
214     private static Element createAxes(Document doc, Integer id, boolean reverseChildren) {
215         GraphicController controller = GraphicController.getController();
216         Axes axes = (Axes)controller.getObjectFromId(id);
217
218         Element elemAxes = doc.createElement("axes");
219         setAttribute(elemAxes, "tag", createAttribute(axes.getTag()), createAttribute(""));
220
221         return elemAxes;
222     }
223
224     private static Element createBorder(Document doc, Integer id) {
225         return createBorder(doc, id, "border");
226     }
227
228     private static Element createBorder(Document doc, Integer id, String borderName) {
229         GraphicController controller = GraphicController.getController();
230         FrameBorder border = (FrameBorder)controller.getObjectFromId(id);
231
232         if (border == null || border.getStyleAsEnum() == FrameBorderType.NONE) {
233             return null;
234         }
235
236         Element elemBorder = doc.createElement(borderName);
237
238         setAttribute(elemBorder, "style", createAttribute(border.getStyleAsEnum().toString().toLowerCase()), "");
239
240         switch (border.getStyleAsEnum()) {
241             case BEVEL: {
242                 setAttribute(elemBorder, "type", createAttribute(border.getBorderType()), createAttribute(defaultBorder.getBorderType()));
243                 setAttribute(elemBorder, "hlout", createAttribute(border.getHlOut()), createAttribute(defaultBorder.getHlOut()));
244                 setAttribute(elemBorder, "hlin", createAttribute(border.getHlIn()), createAttribute(defaultBorder.getHlIn()));
245                 setAttribute(elemBorder, "shadowout", createAttribute(border.getShadowOut()), createAttribute(defaultBorder.getShadowOut()));
246                 setAttribute(elemBorder, "shadowin", createAttribute(border.getShadowIn()), createAttribute(defaultBorder.getShadowIn()));
247                 break;
248             }
249             case COMPOUND: {
250                 Element out = createBorder(doc, border.getOutBorder(), "out");
251                 if (out != null) {
252                     appendChild(elemBorder, out);
253                 }
254
255                 Element in = createBorder(doc, border.getInBorder(), "in");
256                 if (in != null) {
257                     appendChild(elemBorder, in);
258                 }
259                 break;
260             }
261             case EMPTY: {
262                 setAttribute(elemBorder, "position", createAttribute(border.getPosition()), createAttribute(defaultBorder.getPosition()));
263                 break;
264             }
265             case ETCHED: {
266                 setAttribute(elemBorder, "type", createAttribute(border.getBorderType()), createAttribute(defaultBorder.getBorderType()));
267                 setAttribute(elemBorder, "hl", createAttribute(border.getHlOut()), createAttribute(defaultBorder.getHlOut()));
268                 setAttribute(elemBorder, "shadow", createAttribute(border.getShadowOut()), createAttribute(defaultBorder.getShadowOut()));
269                 break;
270             }
271             case LINE: {
272                 setAttribute(elemBorder, "color", createAttribute(border.getColor()), createAttribute(defaultBorder.getColor()));
273                 setAttribute(elemBorder, "thickness", createAttribute(border.getThickness()), createAttribute(defaultBorder.getThickness()));
274                 setAttribute(elemBorder, "rounded", createAttribute(border.getRounded()), createAttribute(defaultBorder.getRounded()));
275                 break;
276             }
277             case MATTE: {
278                 setAttribute(elemBorder, "position", createAttribute(border.getPosition()), createAttribute(defaultBorder.getPosition()));
279                 setAttribute(elemBorder, "color", createAttribute(border.getColor()), createAttribute(defaultBorder.getColor()));
280                 break;
281             }
282             case SOFTBEVEL: {
283                 setAttribute(elemBorder, "type", createAttribute(border.getBorderType()), createAttribute(defaultBorder.getBorderType()));
284                 setAttribute(elemBorder, "hlout", createAttribute(border.getHlOut()), createAttribute(defaultBorder.getHlOut()));
285                 setAttribute(elemBorder, "hlin", createAttribute(border.getHlIn()), createAttribute(defaultBorder.getHlIn()));
286                 setAttribute(elemBorder, "shadowout", createAttribute(border.getShadowOut()), createAttribute(defaultBorder.getShadowOut()));
287                 setAttribute(elemBorder, "shadowin", createAttribute(border.getShadowIn()), createAttribute(defaultBorder.getShadowIn()));
288                 break;
289             }
290             case TITLED: {
291                 setAttribute(elemBorder, "title", createAttribute(border.getTitle()), createAttribute(defaultBorder.getTitle()));
292                 setAttribute(elemBorder, "justification", createAttribute(border.getJustification()), createAttribute(defaultBorder.getJustification()));
293                 setAttribute(elemBorder, "titleposition", createAttribute(border.getTitlePosition()), createAttribute(defaultBorder.getTitlePosition()));
294                 setAttribute(elemBorder, "fontname", createAttribute(border.getFontName()), createAttribute(defaultBorder.getFontName()));
295                 setAttribute(elemBorder, "fontangle", createAttribute(border.getFontAngle()), createAttribute(defaultBorder.getFontAngle()));
296                 setAttribute(elemBorder, "fontsize", createAttribute(border.getFontSize()), createAttribute(defaultBorder.getFontSize()));
297                 setAttribute(elemBorder, "fontweight", createAttribute(border.getFontWeight()), createAttribute(defaultBorder.getFontWeight()));
298                 setAttribute(elemBorder, "color", createAttribute(border.getColor()), createAttribute(defaultBorder.getColor()));
299                 Element title = createBorder(doc, border.getTitleBorder(), "title");
300                 if (title != null) {
301                     appendChild(elemBorder, title);
302                 }
303                 break;
304             }
305             case NONE:
306             default: {
307                 break;
308             }
309         }
310         return elemBorder;
311     }
312
313     private static Element createUicontrol(Document doc, Integer id, boolean reverseChildren) {
314         GraphicController controller = GraphicController.getController();
315         Uicontrol uic = (Uicontrol)controller.getObjectFromId(id);
316         Uicontrol defaultUi = initDefaultui(uic.getStyle());
317         Element elemUi = doc.createElement(uic.getStyleAsEnum().toString().toLowerCase());
318
319         //BackgroundColor
320         setAttribute(elemUi, "backgroundcolor", createAttribute(uic.getBackgroundColor()), createAttribute(defaultUi.getBackgroundColor()));
321         //Enable
322         setAttribute(elemUi, "enable", createAttribute(uic.getEnable()), createAttribute(defaultUi.getEnable()));
323         //FontAngle
324         setAttribute(elemUi, "fontangle", createAttribute(uic.getFontAngle()), createAttribute(defaultUi.getFontAngle()));
325         //FontName
326         setAttribute(elemUi, "fontname", createAttribute(uic.getFontName()), createAttribute(defaultUi.getFontName()));
327         //FontSize
328         setAttribute(elemUi, "fontsize", createAttribute(uic.getFontSize()), createAttribute(defaultUi.getFontSize()));
329         //FontUnits
330         setAttribute(elemUi, "fontunits", createAttribute(uic.getFontUnits()), createAttribute(defaultUi.getFontUnits()));
331         //FontWeight
332         setAttribute(elemUi, "fontweight", createAttribute(uic.getFontWeight()), createAttribute(defaultUi.getFontWeight()));
333         //ForegroundColor
334         setAttribute(elemUi, "foregroundcolor", createAttribute(uic.getForegroundColor()), createAttribute(defaultUi.getForegroundColor()));
335         //HorizontalAlignment
336         setAttribute(elemUi, "horizontalalignment", createAttribute(uic.getHorizontalAlignment()), createAttribute(defaultUi.getHorizontalAlignment()));
337         //ListboxTop
338         setAttribute(elemUi, "listboxtop", createAttribute(uic.getListboxTop()), createAttribute(defaultUi.getListboxTop()));
339         //Max
340         setAttribute(elemUi, "max", createAttribute(uic.getMax()), createAttribute(defaultUi.getMax()));
341         //Min
342         setAttribute(elemUi, "min", createAttribute(uic.getMin()), createAttribute(defaultUi.getMin()));
343         //Relief
344         setAttribute(elemUi, "relief", createAttribute(uic.getRelief()), createAttribute(defaultUi.getRelief()));
345         //SliderStep
346         setAttribute(elemUi, "sliderstep", createAttribute(uic.getSliderStep()), createAttribute(defaultUi.getSliderStep()));
347         //String
348         //do not save string and value of tab/layer to avoid setting bad index during loading
349         if (uic.getStyle() != __GO_UI_TAB__ && uic.getStyle() != __GO_UI_LAYER__) {
350             String[] uicSstr = uic.getString();
351             String[] modelStr = defaultUi.getString();
352             if (Arrays.deepEquals(uicSstr, modelStr) == false) {
353                 createStringArray(doc, elemUi, "string", uicSstr, uic.getStringColNb());
354             }
355         }
356         //TooltipString
357         String[] uicToolTip = uic.getTooltipString();
358         String[] modelToolTip = defaultUi.getTooltipString();
359         if (Arrays.deepEquals(uicToolTip, modelToolTip) == false) {
360             createStringArray(doc, elemUi, "tooltipstring", uicToolTip, 1);
361         }
362         //Units
363         setAttribute(elemUi, "units", createAttribute(uic.getUnits()), createAttribute(defaultUi.getUnits()));
364         //Value
365         //do not save string and value of tab/layer to avoid setting bad index during loading
366         if (uic.getStyle() != __GO_UI_TAB__ && uic.getStyle() != __GO_UI_LAYER__) {
367             setAttribute(elemUi, "value", createAttribute(uic.getUiValue()), createAttribute(defaultUi.getUiValue()));
368         }
369         //VerticalAlignment
370         setAttribute(elemUi, "verticalalignment", createAttribute(uic.getVerticalAlignment()), createAttribute(defaultUi.getVerticalAlignment()));
371         //Visible
372         setAttribute(elemUi, "visible", createAttribute(uic.getVisible()), createAttribute(!defaultUi.getVisible()));
373         //Callback
374         setAttribute(elemUi, "callback", createAttribute(uic.getCallbackString()), createAttribute(defaultUi.getCallbackString()));
375         //Callback_Type
376         setAttribute(elemUi, "callback_type", createAttribute(uic.getCallbackType()), createAttribute(defaultUi.getCallbackType()));
377
378         //Constraints
379         Integer parent = (Integer)controller.getProperty(uic.getIdentifier(), __GO_PARENT__);
380         Integer iLayout = (Integer)controller.getProperty(parent, __GO_LAYOUT__);
381         LayoutType layout = LayoutType.intToEnum(iLayout);
382
383         if (layout == LayoutType.GRIDBAG) {
384             setAttribute(elemUi, "gridbaggrid", createAttribute(uic.getGridBagGrid()), createAttribute(defaultUi.getGridBagGrid()));
385             setAttribute(elemUi, "gridbagweight", createAttribute(uic.getGridBagWeight()), createAttribute(defaultUi.getGridBagWeight()));
386             setAttribute(elemUi, "gridbagfill", createAttribute(Uicontrol.FillType.enumToString(uic.getGridBagFillAsEnum())), createAttribute(Uicontrol.FillType.enumToString(defaultUi.getGridBagFillAsEnum())));
387             setAttribute(elemUi, "gridbaganchor", createAttribute(Uicontrol.AnchorType.enumToString(uic.getGridBagAnchorAsEnum())), createAttribute(Uicontrol.AnchorType.enumToString(defaultUi.getGridBagAnchorAsEnum())));
388             setAttribute(elemUi, "gridbagpadding", createAttribute(uic.getGridBagPadding()), createAttribute(defaultUi.getGridBagPadding()));
389             setAttribute(elemUi, "gridbagpreferredsize", createAttribute(uic.getGridBagPreferredSize()), createAttribute(defaultUi.getGridBagPreferredSize()));
390         } else if (layout == LayoutType.BORDER) {
391             setAttribute(elemUi, "borderposition", createAttribute(Uicontrol.BorderLayoutType.enumToString(uic.getBorderPositionAsEnum())), createAttribute(Uicontrol.BorderLayoutType.enumToString(defaultUi.getBorderPositionAsEnum())));
392             setAttribute(elemUi, "borderpreferredsize", createAttribute(uic.getBorderPreferredSize()), createAttribute(defaultUi.getBorderPreferredSize()));
393         } else if (layout == LayoutType.NONE) {
394             //Position
395             setAttribute(elemUi, "position", createAttribute(uic.getUiPosition()), createAttribute(defaultUi.getUiPosition()));
396         }
397
398         //Margins
399         setAttribute(elemUi, "margins", createAttribute(uic.getMargins()), createAttribute(defaultUi.getMargins()));
400         //Groupname
401         setAttribute(elemUi, "groupname", createAttribute(uic.getGroupName()), createAttribute(defaultUi.getGroupName()));
402
403         //icon
404         setAttribute(elemUi, "icon", createAttribute(uic.getIcon()), createAttribute(defaultUi.getIcon()));
405         //Tag
406         setAttribute(elemUi, "tag", createAttribute(uic.getTag()), createAttribute(defaultUi.getTag()));
407
408         //only for frame type
409         if (uic.getStyleAsEnum() == UicontrolStyle.FRAME) {
410             //Border
411             Integer iBorder = uic.getFrameBorder();
412             Element border = createBorder(doc, iBorder);
413             if (border != null) {
414                 appendChild(elemUi, createBorder(doc, iBorder));
415             }
416
417             //Layout
418             setAttribute(elemUi, "layout", createAttribute(LayoutType.enumToString(uic.getLayoutAsEnum())), createAttribute(LayoutType.enumToString(defaultUi.getLayoutAsEnum())));
419             //Layout_options
420             if (uic.getLayoutAsEnum() == LayoutType.GRID) {
421                 setAttribute(elemUi, "gridoptgrid", createAttribute(uic.getGridOptGrid()), createAttribute(defaultUi.getGridOptGrid()));
422                 setAttribute(elemUi, "gridoptpadding", createAttribute(uic.getGridOptPadding()), createAttribute(defaultUi.getGridOptPadding()));
423             } else if (uic.getLayoutAsEnum() == LayoutType.BORDER) {
424                 setAttribute(elemUi, "borderoptpadding", createAttribute(uic.getBorderOptPadding()), createAttribute(defaultUi.getBorderOptPadding()));
425             }
426         }
427
428         if (uic.getStyleAsEnum() == UicontrolStyle.FRAME || uic.getStyleAsEnum() == UicontrolStyle.EDIT) {
429             //Scrollable
430             setAttribute(elemUi, "scrollable", createAttribute(uic.getScrollable()), createAttribute(defaultUi.getScrollable()));
431         }
432
433         controller.deleteObject(defaultUi.getIdentifier());
434         //children
435         Integer[] children = uic.getChildren();
436         if (reverseChildren) {
437             for (int i = children.length - 1 ; i >= 0 ; i--) {
438                 if ((Integer)controller.getProperty(children[i], __GO_TYPE__) != __GO_UI_FRAME_BORDER__) {
439                     appendChild(elemUi, createElement(doc, children[i], reverseChildren));
440                 }
441             }
442         } else {
443             for (int i = 0 ; i < children.length ; i++) {
444                 if ((Integer)controller.getProperty(children[i], __GO_TYPE__) != __GO_UI_FRAME_BORDER__) {
445                     appendChild(elemUi, createElement(doc, children[i], reverseChildren));
446                 }
447             }
448         }
449         return elemUi;
450     }
451
452     private static Element createUimenu(Document doc, Integer id, boolean reverseChildren) {
453         GraphicController controller = GraphicController.getController();
454         Uimenu uim = (Uimenu)controller.getObjectFromId(id);
455         //bypass hidden menu
456         if (uim.isHidden()) {
457             return null;
458         }
459
460         Uimenu defaultMenu = initDefaultMenu();
461         Element elemMenu = doc.createElement("menu");
462
463         //enable
464         setAttribute(elemMenu, "enable", createAttribute(uim.getEnable()), createAttribute(defaultMenu.getEnable()));
465         //foregroundcolor
466         setAttribute(elemMenu, "foregroundcolor", createAttribute(uim.getForegroundColor()), createAttribute(defaultMenu.getForegroundColor()));
467         //label
468         setAttribute(elemMenu, "label", createAttribute(uim.getLabel()), createAttribute(defaultMenu.getLabel()));
469         //handle_visible
470         setAttribute(elemMenu, "hidden", createAttribute(uim.isHidden()), createAttribute(defaultMenu.isHidden()));
471         //visible
472         setAttribute(elemMenu, "visible", createAttribute(uim.getVisible()), createAttribute(defaultMenu.getVisible()));
473         //callback
474         setAttribute(elemMenu, "callback", createAttribute(uim.getCallbackString()), createAttribute(defaultMenu.getCallbackString()));
475         //callback_type
476         setAttribute(elemMenu, "callback_type", createAttribute(uim.getCallbackType()), createAttribute(defaultMenu.getCallbackType()));
477         //checked
478         setAttribute(elemMenu, "checked", createAttribute(uim.getChecked()), createAttribute(defaultMenu.getChecked()));
479         //icon
480         setAttribute(elemMenu, "icon", createAttribute(uim.getIcon()), createAttribute(defaultMenu.getIcon()));
481         //tag
482         setAttribute(elemMenu, "tag", createAttribute(uim.getTag()), createAttribute(defaultMenu.getTag()));
483
484         //children
485         Integer[] children = uim.getChildren();
486         for (int i = 0; i < children.length; i++) {
487             appendChild(elemMenu, createElement(doc, children[i], reverseChildren));
488         }
489
490         return elemMenu;
491     }
492
493     private static String createAttribute(Boolean val) {
494         if (val == null) {
495             return "off";
496         }
497
498         if (val) {
499             return "on";
500         } else {
501             return "off";
502         }
503     }
504
505     private static String createAttribute(Integer val) {
506         if (val == null) {
507             return null;
508         }
509
510         StringBuilder builder = new StringBuilder();
511         builder.append(val.intValue());
512         return builder.toString();
513     }
514
515     private static String createAttribute(String val) {
516         if (val == null) {
517             return null;
518         }
519         return val;
520     }
521
522     private static String createAttribute(Double val) {
523         if (val == null) {
524             return null;
525         }
526
527         StringBuilder builder = new StringBuilder();
528         builder.append(val.doubleValue());
529         return builder.toString();
530     }
531
532     //    private static String createAttribute(Boolean[] val) {
533     //        if(val == null || val.length == 0) {
534     //            return null;
535     //        }
536     //
537     //        StringBuilder str = new StringBuilder();
538     //
539     //        str.append(createAttribute(val[0]));
540     //
541     //        for(int i = 1 ; i < val.length; i++)
542     //        {
543     //            str.append(",");
544     //            str.append(createAttribute(val[i]));
545     //        }
546     //
547     //        return str.toString();
548     //    }
549
550
551     private static String createAttribute(String[] val) {
552         if (val == null || val.length == 0) {
553             return null;
554         }
555
556         StringBuilder str = new StringBuilder();
557
558         str.append(createAttribute(val[0]));
559
560         for (int i = 1 ; i < val.length; i++) {
561             str.append(",");
562             str.append(createAttribute(val[i]));
563         }
564
565         return str.toString();
566     }
567
568     private static String createAttribute(Double[] val) {
569         if (val == null || val.length == 0) {
570             return null;
571         }
572
573         StringBuilder str = new StringBuilder();
574
575         str.append(createAttribute(val[0]));
576
577         for (int i = 1 ; i < val.length; i++) {
578             str.append(",");
579             str.append(createAttribute(val[i]));
580         }
581
582         return str.toString();
583     }
584
585     private static String createAttribute(Integer[] val) {
586         if (val == null || val.length == 0) {
587             return null;
588         }
589
590         StringBuilder str = new StringBuilder();
591
592         str.append(createAttribute(val[0]));
593
594         for (int i = 1 ; i < val.length; i++) {
595             str.append(",");
596             str.append(createAttribute(val[i]));
597         }
598
599         return str.toString();
600     }
601
602     private static void setAttribute(Element elem, String property, String value, String ref) {
603         if (value == null || property == null || elem == null || (ref != null && value.equals(ref))) {
604             return;
605         }
606
607         elem.setAttribute(property, value);
608     }
609
610     private static Uicontrol initDefaultui(Integer uicontrolStyle) {
611         Integer uic = GraphicController.getController().askObject(GraphicObject.getTypeFromName(uicontrolStyle));
612         return (Uicontrol) GraphicController.getController().getObjectFromId(uic);
613     }
614
615     private static Uimenu initDefaultMenu() {
616         Integer uic = GraphicController.getController().askObject(GraphicObject.getTypeFromName(__GO_UIMENU__));
617         return (Uimenu) GraphicController.getController().getObjectFromId(uic);
618     }
619
620     private static void createStringArray(Document doc, Element parent, String property, String[] value, Integer cols) {
621         //create a new Node to store string
622         Element elemString = doc.createElement("string");
623         Integer rows = value.length / cols;
624
625         setAttribute(elemString, "property", property, "");
626         setAttribute(elemString, "rows", rows.toString(), "");
627         setAttribute(elemString, "cols", cols.toString(), "");
628
629         for (int i = 0 ; i < rows * cols ; i++) {
630             Element elemSub = doc.createElement("stringitem");
631             setAttribute(elemSub, "value", value[i], null);
632             elemString.appendChild(elemSub);
633         }
634
635         parent.appendChild(elemString);
636     }
637
638 }