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