improve management of axes_size and figure_size
[scilab.git] / scilab / modules / graphic_objects / src / java / org / scilab / modules / graphic_objects / xmlloader / GOBuilder.java
1 package org.scilab.modules.graphic_objects.xmlloader;
2
3 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_BORDER_OPT_PADDING__;
4 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_CALLBACKTYPE__;
5 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_CALLBACK__;
6 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_CLOSEREQUESTFCN__;
7 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_FIGURE__;
8 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_GRID_OPT_GRID__;
9 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_GRID_OPT_PADDING__;
10 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_HIDDEN__;
11 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_LAYOUT__;
12 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_LINE_THICKNESS__;
13 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_MARGINS__;
14 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_MENUBAR_VISIBLE__;
15 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_NAME__;
16 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_POSITION__;
17 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_RESIZE__;
18 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_SIZE__;
19 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_STYLE__;
20 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_TAG__;
21 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_TITLE__;
22 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_TYPE__;
23 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UIMENU__;
24 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_BACKGROUNDCOLOR__;
25 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_BORDER_POSITION__;
26 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_BORDER_PREFERREDSIZE__;
27 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_CHECKBOX__;
28 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_CHECKED__;
29 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_EDIT__;
30 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_ENABLE__;
31 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTANGLE__;
32 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTNAME__;
33 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTSIZE__;
34 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FONTWEIGHT__;
35 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FOREGROUNDCOLOR__;
36 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME_BORDER_COLOR__;
37 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME_BORDER_HIGHLIGHT_IN__;
38 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME_BORDER_HIGHLIGHT_OUT__;
39 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME_BORDER_IN_BORDER__;
40 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME_BORDER_JUSTIFICATION__;
41 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME_BORDER_OUT_BORDER__;
42 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME_BORDER_POSITION__;
43 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME_BORDER_ROUNDED__;
44 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME_BORDER_SHADOW_IN__;
45 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME_BORDER_SHADOW_OUT__;
46 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME_BORDER_STYLE__;
47 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME_BORDER_TITLE__;
48 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME_BORDER__;
49 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME_SCROLLABLE__;
50 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_FRAME__;
51 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_GRIDBAG_ANCHOR__;
52 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_GRIDBAG_FILL__;
53 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_GRIDBAG_GRID__;
54 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_GRIDBAG_PADDING__;
55 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_GRIDBAG_PREFERREDSIZE__;
56 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_GRIDBAG_WEIGHT__;
57 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_GROUP_NAME__;
58 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_HORIZONTALALIGNMENT__;
59 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_ICON__;
60 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_IMAGE__;
61 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_LABEL__;
62 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_LISTBOX__;
63 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_MAX__;
64 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_MIN__;
65 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_POPUPMENU__;
66 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_PUSHBUTTON__;
67 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_RADIOBUTTON__;
68 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_SCROLLABLE__;
69 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_SPINNER__;
70 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_STRING__;
71 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_TAB__;
72 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_TEXT__;
73 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_VISIBLE__;
74
75 import java.awt.Color;
76 import java.lang.reflect.Field;
77 import java.util.HashMap;
78 import java.util.Map;
79
80 import org.scilab.modules.graphic_objects.builder.Builder;
81 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
82 import org.scilab.modules.graphic_objects.graphicObject.GraphicObject;
83 import org.scilab.modules.graphic_objects.graphicObject.GraphicObject.Type;
84 import org.scilab.modules.graphic_objects.uicontrol.Uicontrol;
85 import org.scilab.modules.graphic_objects.uicontrol.frame.border.FrameBorder;
86 import org.scilab.modules.graphic_objects.uicontrol.frame.border.FrameBorder.BorderType;
87 import org.scilab.modules.graphic_objects.uicontrol.frame.border.FrameBorderType;
88 import org.scilab.modules.graphic_objects.utils.LayoutType;
89 import org.xml.sax.Attributes;
90
91 public class GOBuilder {
92     public static Integer figureBuilder(GraphicController controller, Attributes attributes) {
93         Integer fig = Builder.createFigure(false, 0, 0, false, false, null, null, null, false, false, false);
94         //Integer fig = Builder.createNewFigureWithAxes();
95         String item = null;
96
97         // id
98         XmlTools.setPropAsString(fig, __GO_TAG__, attributes.getValue("id"));
99
100         // position
101         Integer[] position = (Integer[]) controller.getProperty(fig, __GO_POSITION__);
102
103         // posX
104         item = attributes.getValue("posX");
105         if (item != null) {
106             position[0] = Integer.parseInt(item);
107         }
108
109         // posY
110         item = attributes.getValue("posY");
111         if (item != null) {
112             position[1] = Integer.parseInt(item);
113         }
114
115         // set position
116         controller.setProperty(fig, __GO_POSITION__, position);
117
118         Integer[] size = (Integer[]) controller.getProperty(fig, __GO_SIZE__);
119         // width
120         item = attributes.getValue("width");
121         if (item != null) {
122             size[0] = Integer.parseInt(item);
123         }
124
125         // height
126         item = attributes.getValue("height");
127         if (item != null) {
128             size[1] = Integer.parseInt(item);
129         }
130
131         // set size
132         controller.setProperty(fig, __GO_SIZE__, size);
133
134         // title
135         XmlTools.setPropAsString(fig, __GO_NAME__, attributes.getValue("title"));
136
137         // resizable
138         XmlTools.setPropAsBoolean(fig, __GO_RESIZE__, attributes.getValue("resizable"));
139
140         // layout and layout_options
141         item = attributes.getValue("layout");
142         if (item == null) {
143             item = "name:border"; //default layout is border
144         }
145
146         setLayoutProperty(controller, fig, item);
147
148
149         //icon
150         item = attributes.getValue("icon");
151         if (item != null) {
152             controller.setProperty(fig, __GO_UI_ICON__, item);
153         }
154
155         item = attributes.getValue("onclose");
156         if (item != null) {
157             controller.setProperty(fig, __GO_CLOSEREQUESTFCN__, item);
158         }
159
160         // visible
161         XmlTools.setPropAsBoolean(fig, __GO_VISIBLE__, attributes.getValue("visible"));
162
163
164         return fig;
165     }
166
167     public static Integer axesBuilder(GraphicController controller, Attributes attributes) {
168         Integer axes = GraphicController.getController().askObject(Type.AXES);
169         String id = attributes.getValue("id");
170         if (id != null) {
171             controller.setProperty(axes, __GO_TAG__, id);
172         }
173         return axes;
174     }
175
176     public static Integer uicontrolBuilder(GraphicController controller, int type, Attributes attributes, int parent) {
177         if (type == __GO_UI_FRAME__) {
178             String item = attributes.getValue("scrollable");
179             if (item != null && item.equals("true")) {
180                 type = __GO_UI_FRAME_SCROLLABLE__;
181             }
182         }
183
184         Integer uic = 0;
185         if (type == -1) { //label latex
186             uic = controller.askObject(GraphicObject.getTypeFromName(__GO_UI_TEXT__));
187             controller.setProperty(uic, __GO_UI_STRING__, new String[] {"$$"});
188         } else if (type == -2) {
189             uic = controller.askObject(GraphicObject.getTypeFromName(__GO_UI_EDIT__));
190             controller.setProperty(uic, __GO_UI_MIN__, 0.0);
191             controller.setProperty(uic, __GO_UI_MAX__, 2.0);
192         } else {
193             uic = controller.askObject(GraphicObject.getTypeFromName(type));
194         }
195
196         return uicontrolUpdater(controller, uic, attributes, parent);
197     }
198
199     public static Integer uicontrolUpdater(GraphicController controller, int uic, Attributes attributes, int parent) {
200         return uicontrolUpdater(controller, uic, attributes, parent, null);
201     }
202
203     public static Integer uicontrolUpdater(GraphicController controller, int uic, Attributes attributes, int parent, Map<String, String> fromModel) {
204         String item = null;
205
206         try {
207             Map<String, String> xmlAttributes = new HashMap<String, String>();
208             Map<String, String> map = null;
209             // Common attributes
210
211             if (fromModel != null) {
212                 for (Map.Entry<String, String> prop : fromModel.entrySet()) {
213                     xmlAttributes.put(prop.getKey(), prop.getValue());
214                 }
215             }
216
217             for (int i = 0; i < attributes.getLength(); i++) {
218                 String name = attributes.getLocalName(i);
219                 String value = attributes.getValue(i);
220                 xmlAttributes.put(name, value);
221             }
222
223             // id
224             XmlTools.setPropAsString(uic, __GO_TAG__, xmlAttributes.get("id"));
225
226             // layout and layout_options
227             item = xmlAttributes.get("layout");
228
229             if (item == null) {
230                 item = "name:none";
231             }
232             setLayoutProperty(controller, uic, item);
233
234             //visible
235             item = xmlAttributes.get("visible");
236             if (item == null || item.equals("true") || item.equals("on")) {
237                 controller.setProperty(uic, __GO_VISIBLE__, true);
238             } else {
239                 controller.setProperty(uic, __GO_VISIBLE__, false);
240             }
241
242             //enable
243             boolean enable = true;
244             item = attributes.getValue("enable");
245             if (item != null && (item.equals("false") || item.equals("off"))) {
246                 enable = false;
247             }
248
249             //enabled
250             item = attributes.getValue("enabled");
251             if (item != null && (item.equals("false") || item.equals("off"))) {
252                 enable = false;
253             }
254
255             controller.setProperty(uic, __GO_UI_ENABLE__, enable);
256
257             //backgroundcolor
258             item = xmlAttributes.get("background");
259             if (item != null) {
260                 controller.setProperty(uic, __GO_UI_BACKGROUNDCOLOR__, getColor(item));
261             }
262
263             // constraints
264
265             // get parent layout
266             LayoutType layout = LayoutType.NONE;
267             if (parent != 0) {
268                 layout = LayoutType.intToEnum((Integer) controller.getProperty(parent, __GO_LAYOUT__));
269             }
270
271
272             //font properties
273
274             //fontname
275             item = xmlAttributes.get("font-name");
276             if (item != null) {
277                 controller.setProperty(uic, __GO_UI_FONTNAME__, item);
278             }
279
280             item = xmlAttributes.get("fontname");
281             if (item != null) {
282                 controller.setProperty(uic, __GO_UI_FONTNAME__, item);
283             }
284
285             //fontsize
286             item = xmlAttributes.get("font-size");
287             if (item != null) {
288                 controller.setProperty(uic, __GO_UI_FONTSIZE__, Double.parseDouble(item));
289             }
290
291             item = xmlAttributes.get("fontsize");
292             if (item != null) {
293                 controller.setProperty(uic, __GO_UI_FONTSIZE__, Double.parseDouble(item));
294             }
295
296             //units and position
297             String[] units = null;
298             item = xmlAttributes.get("units");
299             if (item != null) {
300                 units = item.split("[,;]");
301             }
302
303             //position, take care of units values
304             item = xmlAttributes.get("position");
305             if (item != null) {
306                 Double[] pos = new Double[4];
307                 Double[] parentSize = new Double[] {934.0, 511.0};
308                 String[] strPos = item.split("[,;]");
309                 for (int i = 0 ; i < strPos.length ; i++) {
310                     pos[i] = Double.parseDouble(strPos[i]);
311                     if (units != null && units[i].equals("n")) {
312                         //convert %age to pixel
313                         pos[i] = parentSize[i % 2] * pos[i];
314                     }
315                 }
316
317                 controller.setProperty(uic,  __GO_POSITION__, pos);
318             }
319
320             item = xmlAttributes.get("opaque");
321             if (item != null && (item.equals("false") || item.equals("off"))) {
322                 controller.setProperty(uic, __GO_UI_BACKGROUNDCOLOR__, new Double[] { -2.0, -2.0, -2.0});
323             }
324
325             if (layout != LayoutType.NONE) {
326                 item = xmlAttributes.get("constraint");
327                 if (item != null) {
328                     map = CSSParser.parseLine(item);
329                 }
330
331                 switch (layout) {
332                     case BORDER: {
333                         item = XmlTools.getFromMap(map, "position", "center");
334                         controller.setProperty(uic, __GO_UI_BORDER_POSITION__, Uicontrol.BorderLayoutType.stringToEnum(item).ordinal());
335                         Integer[] preferredsize = new Integer[] { -1, -1};
336                         item = xmlAttributes.get("preferred-size");
337                         if (item != null) {
338                             String[] pref = item.split("[,;]");
339                             for (int i = 0; i < pref.length && i < 4; i++) {
340                                 preferredsize[i] = Integer.parseInt(pref[i]);
341                             }
342                         }
343
344                         controller.setProperty(uic, __GO_UI_BORDER_PREFERREDSIZE__, preferredsize);
345                         break;
346                     }
347                     case GRIDBAG: {
348                         Integer[] grid = new Integer[] { 0, 0, 0, 0 };
349                         grid[0] = Integer.parseInt(XmlTools.getFromMap(map, "gridx", "0"));
350                         grid[1] = Integer.parseInt(XmlTools.getFromMap(map, "gridy", "0"));
351                         grid[2] = Integer.parseInt(XmlTools.getFromMap(map, "gridwidth", "1"));
352                         grid[3] = Integer.parseInt(XmlTools.getFromMap(map, "gridheight", "1"));
353
354                         Double[] weight = new Double[] { 0.0, 0.0 };
355                         weight[0] = Double.parseDouble(XmlTools.getFromMap(map, "weightx", "1.0"));
356                         weight[1] = Double.parseDouble(XmlTools.getFromMap(map, "weighty", "1.0"));
357
358                         Integer fill = Uicontrol.FillType.stringToEnum(XmlTools.getFromMap(map, "fill", "none")).ordinal();
359                         Integer anchor = Uicontrol.AnchorType.stringToEnum(XmlTools.getFromMap(map, "anchor", "center")).ordinal();
360
361                         Integer[] padding = new Integer[] { 0, 0 };
362                         padding[0] = Integer.parseInt(XmlTools.getFromMap(map, "ipadx", "0"));
363                         padding[1] = Integer.parseInt(XmlTools.getFromMap(map, "ipady", "0"));
364
365                         Integer[] preferredsize = new Integer[] { -1, -1};
366                         item = xmlAttributes.get("preferred-size");
367                         if (item != null) {
368                             String[] pref = item.split("[,;]");
369                             for (int i = 0; i < pref.length && i < 4; i++) {
370                                 preferredsize[i] = Integer.parseInt(pref[i]);
371                             }
372                         }
373                         controller.setProperty(uic, __GO_UI_GRIDBAG_GRID__, grid);
374                         controller.setProperty(uic, __GO_UI_GRIDBAG_WEIGHT__, weight);
375                         controller.setProperty(uic, __GO_UI_GRIDBAG_FILL__, fill);
376                         controller.setProperty(uic, __GO_UI_GRIDBAG_ANCHOR__, anchor);
377                         controller.setProperty(uic, __GO_UI_GRIDBAG_PADDING__, padding);
378                         controller.setProperty(uic, __GO_UI_GRIDBAG_PREFERREDSIZE__, preferredsize);
379                         break;
380                     }
381                     default:
382                         break;
383                 }
384
385                 // insets -> marging
386                 String insets = XmlTools.getFromMap(map, "insets", "0,0,0,0");
387                 Double[] margins = new Double[] { 0.0, 0.0, 0.0, 0.0 };
388                 String[] inset = insets.split("[,;]");
389                 for (int i = 0; i < inset.length && i < 4; i++) {
390                     margins[i] = Double.parseDouble(inset[i]);
391                 }
392
393                 controller.setProperty(uic, __GO_MARGINS__, margins);
394             }
395
396             // specific attributes
397             Integer type = (Integer) controller.getProperty(uic, __GO_STYLE__);
398             switch (type) {
399                 case __GO_UI_FRAME__: {
400                     // border
401                     item = xmlAttributes.get("border");
402                     if (item != null) {
403                         map = CSSParser.parseLine(item);
404                     }
405
406                     FrameBorderType borderType = FrameBorderType.stringToEnum(XmlTools.getFromMap(map, "name", "none"));
407                     Integer border = createBorder(controller, uic, borderType, map);
408                     controller.setProperty(uic, __GO_UI_FRAME_BORDER__, border);
409
410                     String[] text = new String[1];
411                     text[0] = xmlAttributes.get("tab-title");
412                     if (text[0] != null) {
413                         controller.setProperty(uic, __GO_UI_STRING__, text);
414                     }
415
416                     item = xmlAttributes.get("scrollable");
417                     if (item == null || item.equals("false")) {
418                         controller.setProperty(uic, __GO_UI_SCROLLABLE__, false);
419                     } else {
420                         controller.setProperty(uic, __GO_UI_SCROLLABLE__, true);
421                     }
422
423                     break;
424                 }
425
426                 case __GO_UI_TAB__: {
427                     item = xmlAttributes.get("ui-style");
428                     if (item != null) {
429                         map = CSSParser.parseLine(item);
430                         item = XmlTools.getFromMap(map, "bold", "false");
431                         if (item.equals("true")) {
432                             controller.setProperty(uic, __GO_UI_FONTWEIGHT__, "bold");
433                         }
434
435                         item = XmlTools.getFromMap(map, "italic", "false");
436                         if (item.equals("true")) {
437                             controller.setProperty(uic, __GO_UI_FONTANGLE__, "italic");
438                         }
439                     }
440                     break;
441                 }
442                 case __GO_UI_IMAGE__: {
443                     String[] text = new String[1];
444                     text[0] = xmlAttributes.get("image");
445                     if (text[0] != null) {
446                         controller.setProperty(uic, __GO_UI_STRING__, text);
447                     }
448                     break;
449                 }
450                 case __GO_UI_CHECKBOX__: {
451                     String[] text = new String[1];
452                     text[0] = xmlAttributes.get("text");
453                     if (text[0] != null) {
454                         controller.setProperty(uic, __GO_UI_STRING__, text);
455                     }
456
457                     //groupname
458                     item = xmlAttributes.get("button-group");
459                     if (item != null && item.equals("") == false) {
460                         controller.setProperty(uic, __GO_UI_GROUP_NAME__, item);
461                     }
462
463                     //callback
464                     item = xmlAttributes.get("onclick");
465                     if (item != null && item.equals("") == false) {
466                         controller.setProperty(uic, __GO_CALLBACK__, item);
467                         controller.setProperty(uic, __GO_CALLBACKTYPE__, 0);
468                     }
469
470                     break;
471                 }
472                 case __GO_UI_RADIOBUTTON__: {
473                     String[] text = new String[1];
474                     text[0] = xmlAttributes.get("text");
475                     if (text[0] != null) {
476                         controller.setProperty(uic, __GO_UI_STRING__, text);
477                     }
478
479                     //groupname
480                     item = xmlAttributes.get("button-group");
481                     if (item != null && item.equals("") == false) {
482                         controller.setProperty(uic, __GO_UI_GROUP_NAME__, item);
483                     }
484
485                     //callback
486                     item = xmlAttributes.get("onclick");
487                     if (item != null && item.equals("") == false) {
488                         controller.setProperty(uic, __GO_CALLBACK__, item);
489                         controller.setProperty(uic, __GO_CALLBACKTYPE__, 0);
490                     }
491
492                     break;
493                 }
494                 case __GO_UI_PUSHBUTTON__: {
495
496                     //string
497                     String[] text = new String[1];
498                     text[0] = xmlAttributes.get("text");
499                     if (text[0] != null) {
500                         controller.setProperty(uic, __GO_UI_STRING__, text);
501                     }
502
503
504                     //icon
505                     item = xmlAttributes.get("icon");
506                     if (item != null) {
507                         controller.setProperty(uic, __GO_UI_ICON__, item);
508                     }
509
510                     //callback
511                     item = xmlAttributes.get("onclick");
512                     if (item != null && item.equals("") == false) {
513                         controller.setProperty(uic, __GO_CALLBACK__, item);
514                         controller.setProperty(uic, __GO_CALLBACKTYPE__, 0);
515                     }
516
517                     break;
518                 }
519                 case __GO_UI_TEXT__: { //label
520                     controller.setProperty(uic, __GO_UI_HORIZONTALALIGNMENT__, "left");
521                     item = xmlAttributes.get("alignment");
522                     if (item != null) {
523                         if (item.equals("left")) {
524                             controller.setProperty(uic, __GO_UI_HORIZONTALALIGNMENT__, "left");
525                         } else if (item.equals("center")) {
526                             controller.setProperty(uic, __GO_UI_HORIZONTALALIGNMENT__, "center");
527                         } else if (item.equals("right")) {
528                             controller.setProperty(uic, __GO_UI_HORIZONTALALIGNMENT__, "right");
529                         }
530                     }
531
532                     String[] text = new String[1];
533                     text[0] = xmlAttributes.get("text");
534                     if (text[0] != null) {
535                         String[] old = (String[])controller.getProperty(uic, __GO_UI_STRING__);
536                         if (old != null && old[0] != null && old[0].equals("$$")) {
537                             text[0] = "$" + text[0] + "$";
538                         }
539                         controller.setProperty(uic, __GO_UI_STRING__, text);
540                     }
541
542                     //icon
543                     item = xmlAttributes.get("icon");
544                     if (item != null) {
545                         controller.setProperty(uic, __GO_UI_ICON__, item);
546                     }
547
548                     //color
549                     item = xmlAttributes.get("color");
550                     if (item != null) {
551                         Color color = Color.decode(item);
552                         Double[] val = new Double[] {(double) color.getRed() / 255, (double) color.getGreen() / 255, (double) color.getBlue() / 255};
553                         controller.setProperty(uic, __GO_UI_FOREGROUNDCOLOR__, val);
554                     }
555
556                     item = xmlAttributes.get("ui-style");
557                     if (item != null) {
558                         map = CSSParser.parseLine(item);
559                         item = XmlTools.getFromMap(map, "bold", "false");
560                         if (item.equals("true")) {
561                             controller.setProperty(uic, __GO_UI_FONTWEIGHT__, "bold");
562                         }
563
564                         item = XmlTools.getFromMap(map, "italic", "false");
565                         if (item.equals("true")) {
566                             controller.setProperty(uic, __GO_UI_FONTANGLE__, "italic");
567                         }
568                     }
569
570                     break;
571                 }
572                 case __GO_UI_EDIT__ : {
573                     controller.setProperty(uic, __GO_UI_HORIZONTALALIGNMENT__, "left");
574                     item = xmlAttributes.get("alignment");
575                     if (item != null) {
576                         if (item.equals("left")) {
577                             controller.setProperty(uic, __GO_UI_HORIZONTALALIGNMENT__, "left");
578                         } else if (item.equals("center")) {
579                             controller.setProperty(uic, __GO_UI_HORIZONTALALIGNMENT__, "center");
580                         } else if (item.equals("right")) {
581                             controller.setProperty(uic, __GO_UI_HORIZONTALALIGNMENT__, "right");
582                         }
583                     }
584
585                     item = xmlAttributes.get("scrollable");
586                     if (item != null && (item.equals("true") || item.equals("on"))) {
587                         controller.setProperty(uic, __GO_UI_SCROLLABLE__, true);
588                     }
589
590                     //callback
591                     item = xmlAttributes.get("onenter");
592                     if (item != null && item.equals("") == false) {
593                         controller.setProperty(uic, __GO_CALLBACK__, item);
594                         controller.setProperty(uic, __GO_CALLBACKTYPE__, 0);
595                     }
596
597                     item = xmlAttributes.get("onfocusloss");
598                     if (item != null && item.equals("") == false) {
599                         controller.setProperty(uic, __GO_CALLBACK__, item);
600                         controller.setProperty(uic, __GO_CALLBACKTYPE__, 0);
601                     }
602
603                     break;
604                 }
605                 case __GO_UI_POPUPMENU__ : {
606                     item = xmlAttributes.get("items");
607                     if (item != null && item.equals("") == false) {
608                         controller.setProperty(uic, __GO_UI_STRING__, new String[] {item});
609                     }
610
611                     item = xmlAttributes.get("prototype-display-value");
612                     if (item != null && item.equals("") == false) {
613                         controller.setProperty(uic, __GO_UI_MAX__, new Double(item.length()));
614                     }
615
616                     //callback
617                     item = xmlAttributes.get("onchange");
618                     if (item != null && item.equals("") == false) {
619                         controller.setProperty(uic, __GO_CALLBACK__, item);
620                         controller.setProperty(uic, __GO_CALLBACKTYPE__, 0);
621                     }
622
623                     break;
624                 }
625                 case __GO_UI_LISTBOX__ : {
626                     item = xmlAttributes.get("items");
627                     if (item != null && item.equals("") == false) {
628                         controller.setProperty(uic, __GO_UI_STRING__, new String[] {item});
629                     }
630
631                     item = xmlAttributes.get("multiple");
632                     if (item != null && item.equals("true")) {
633                         controller.setProperty(uic, __GO_UI_MIN__, 0.0);
634                         controller.setProperty(uic, __GO_UI_MAX__, 2.0);
635                     }
636
637                     //callback
638                     item = xmlAttributes.get("onclick");
639                     if (item != null && item.equals("") == false) {
640                         controller.setProperty(uic, __GO_CALLBACK__, item);
641                         controller.setProperty(uic, __GO_CALLBACKTYPE__, 0);
642                     }
643
644                     break;
645                 }
646                 case __GO_UI_SPINNER__: {
647
648                 }
649             }
650
651         } catch (CSSParserException e) {
652             e.printStackTrace();
653         }
654
655         return uic;
656     }
657
658     private static void setLayoutProperty(GraphicController controller, Integer uid, String item) {
659         try {
660             Map<String, String> map = null;
661             map = CSSParser.parseLine(item);
662             item = map.get("name");
663             LayoutType layout = LayoutType.stringToEnum(item);
664
665             // layout_options
666             switch (layout) {
667                 case BORDER: {
668                     Integer[] pad = new Integer[] { 0, 0 };
669                     pad[0] = Integer.parseInt(XmlTools.getFromMap(map, "hgap", "0"));
670                     pad[1] = Integer.parseInt(XmlTools.getFromMap(map, "vgap", "0"));
671
672                     controller.setProperty(uid, __GO_BORDER_OPT_PADDING__, pad);
673                     break;
674                 }
675                 case GRID: {
676                     Integer[] grid = new Integer[] { 0, 0 };
677                     grid[0] = Integer.parseInt(XmlTools.getFromMap(map, "rows", "0"));
678                     grid[1] = Integer.parseInt(XmlTools.getFromMap(map, "cols", "0"));
679
680                     Integer[] pad = new Integer[] { 0, 0 };
681                     pad[0] = Integer.parseInt(XmlTools.getFromMap(map, "hgap", "0"));
682                     pad[1] = Integer.parseInt(XmlTools.getFromMap(map, "vgap", "0"));
683
684                     controller.setProperty(uid, __GO_GRID_OPT_GRID__, grid);
685                     controller.setProperty(uid, __GO_GRID_OPT_PADDING__, pad);
686                     break;
687                 }
688                 default:
689                 case GRIDBAG:
690                 case NONE:
691                     // nothing to do
692                     break;
693             }
694             controller.setProperty(uid, __GO_LAYOUT__, layout.ordinal());
695         } catch (CSSParserException e) {
696             e.printStackTrace();
697         }
698
699     }
700
701     public static Integer uimenuBuilder(GraphicController controller, Attributes attributes, int parent) {
702         Integer parentType = (Integer)controller.getProperty(parent, __GO_TYPE__);
703         if (parentType == __GO_FIGURE__) {
704             controller.setProperty(parent, __GO_MENUBAR_VISIBLE__, true);
705         }
706
707         Integer uim = controller.askObject(GraphicObject.getTypeFromName(__GO_UIMENU__));
708
709         String item = null;
710
711         item = attributes.getValue("id");
712         if (item != null) {
713             controller.setProperty(uim, __GO_TAG__, item);
714         }
715
716         item = attributes.getValue("text");
717         if (item != null) {
718             controller.setProperty(uim, __GO_UI_LABEL__, item);
719         }
720
721         item = attributes.getValue("onclick");
722         if (item != null) {
723             controller.setProperty(uim, __GO_CALLBACK__, item);
724             controller.setProperty(uim, __GO_CALLBACKTYPE__, 0);
725         }
726
727         item = attributes.getValue("checked");
728         if (item != null && (item.equals("true") || item.equals("on"))) {
729             controller.setProperty(uim, __GO_UI_CHECKED__, true);
730         }
731
732         item = attributes.getValue("icon");
733         if (item != null) {
734             controller.setProperty(uim, __GO_UI_ICON__, item);
735         }
736
737         return uim;
738     }
739
740     private static Integer createBorder(GraphicController controller, Integer uic, FrameBorderType borderType, Map<String, String> map) {
741         Integer border = controller.askObject(GraphicObject.getTypeFromName(__GO_UI_FRAME_BORDER__));
742         controller.setProperty(border, __GO_UI_FRAME_BORDER_STYLE__, borderType.ordinal());
743         controller.setProperty(border, __GO_HIDDEN__, true);
744
745         switch (borderType) {
746             case SOFTBEVEL:
747             case BEVEL: {
748                 BorderType etching = BorderType.stringToEnum(XmlTools.getFromMap(map, "type", "none"));
749                 String color = XmlTools.getFromMap(map, "color", "black");
750
751                 controller.setProperty(border, __GO_TYPE__, etching.ordinal());
752                 controller.setProperty(border, __GO_UI_FRAME_BORDER_HIGHLIGHT_OUT__, color);
753                 controller.setProperty(border, __GO_UI_FRAME_BORDER_HIGHLIGHT_IN__, color);
754                 controller.setProperty(border, __GO_UI_FRAME_BORDER_SHADOW_OUT__, color);
755                 controller.setProperty(border, __GO_UI_FRAME_BORDER_SHADOW_IN__, color);
756                 break;
757             }
758             case COMPOUND: {
759                 FrameBorderType borderOut = FrameBorderType.stringToEnum(XmlTools.getFromMap(map, "border-out", "none"));
760                 FrameBorderType borderIn = FrameBorderType.stringToEnum(XmlTools.getFromMap(map, "border-in", "none"));
761
762                 Integer out = createBorder(controller, border, borderOut, map);
763                 Integer in = createBorder(controller, border, borderIn, map);
764
765                 controller.setProperty(border, __GO_UI_FRAME_BORDER_OUT_BORDER__, out);
766
767                 controller.setProperty(border, __GO_UI_FRAME_BORDER_IN_BORDER__, in);
768
769                 break;
770             }
771             case EMPTY: {
772                 Double[] position = new Double[4];
773                 position[0] = Double.parseDouble(XmlTools.getFromMap(map, "top", "0"));
774                 position[1] = Double.parseDouble(XmlTools.getFromMap(map, "left", "0"));
775                 position[2] = Double.parseDouble(XmlTools.getFromMap(map, "right", "0"));
776                 position[3] = Double.parseDouble(XmlTools.getFromMap(map, "bottom", "0"));
777
778                 controller.setProperty(border, __GO_POSITION__, position);
779                 break;
780             }
781             case ETCHED: {
782                 BorderType etching = BorderType.stringToEnum(XmlTools.getFromMap(map, "type", "none"));
783                 String color = XmlTools.getFromMap(map, "color", "black");
784
785                 controller.setProperty(border, __GO_TYPE__, etching.ordinal());
786                 controller.setProperty(border, __GO_UI_FRAME_BORDER_HIGHLIGHT_OUT__, color);
787                 controller.setProperty(border, __GO_UI_FRAME_BORDER_HIGHLIGHT_IN__, color);
788                 break;
789             }
790             case LINE: {
791                 String color = XmlTools.getFromMap(map, "color", "black");
792                 Integer thickness = Integer.parseInt(XmlTools.getFromMap(map, "thickness", "1"));
793                 Boolean rounded = Boolean.parseBoolean(XmlTools.getFromMap(map, "rounded", "false"));
794
795                 controller.setProperty(border, __GO_UI_FRAME_BORDER_COLOR__, color);
796                 controller.setProperty(border, __GO_LINE_THICKNESS__, thickness);
797                 controller.setProperty(border, __GO_UI_FRAME_BORDER_ROUNDED__, rounded);
798                 break;
799             }
800             case MATTE: {
801                 Double[] position = new Double[4];
802                 position[0] = Double.parseDouble(XmlTools.getFromMap(map, "top", "0"));
803                 position[1] = Double.parseDouble(XmlTools.getFromMap(map, "left", "0"));
804                 position[2] = Double.parseDouble(XmlTools.getFromMap(map, "right", "0"));
805                 position[3] = Double.parseDouble(XmlTools.getFromMap(map, "bottom", "0"));
806                 String color = XmlTools.getFromMap(map, "color", "black");
807
808                 controller.setProperty(border, __GO_POSITION__, position);
809                 controller.setProperty(border, __GO_UI_FRAME_BORDER_COLOR__, color);
810                 break;
811             }
812             case TITLED: {
813
814                 String title = XmlTools.getFromMap(map, "title", "");
815                 FrameBorder.JustificationType justify = FrameBorder.JustificationType.stringToEnum(XmlTools.getFromMap(map, "justify", "leading"));
816                 String fontName = XmlTools.getFromMap(map, "font-name", "");
817                 String fontAngle = XmlTools.getFromMap(map, "italic", "false") == "false" ? "normal" : "italic";
818                 Integer fontSize = Integer.parseInt(XmlTools.getFromMap(map, "font-size", "0"));
819                 String fontWeight = XmlTools.getFromMap(map, "bold", "false") == "false" ? "normal" : "bold";
820
821                 FrameBorder.TitlePositionType position = FrameBorder.TitlePositionType.stringToEnum(XmlTools.getFromMap(map, "position", "leading"));
822
823                 String color = XmlTools.getFromMap(map, "color", "black");
824
825                 FrameBorderType borderOut = FrameBorderType.stringToEnum(XmlTools.getFromMap(map, "border", ""));
826                 if (borderOut != null) {
827                     Integer out = createBorder(controller, border, borderOut, map);
828                     controller.setProperty(border, __GO_UI_FRAME_BORDER_TITLE__, out);
829                 }
830
831                 controller.setProperty(border, __GO_TITLE__, title);
832                 controller.setProperty(border, __GO_UI_FRAME_BORDER_JUSTIFICATION__, justify.ordinal());
833                 controller.setProperty(border, __GO_UI_FONTNAME__, fontName);
834                 controller.setProperty(border, __GO_UI_FONTANGLE__, fontAngle);
835                 controller.setProperty(border, __GO_UI_FONTSIZE__, fontSize);
836                 controller.setProperty(border, __GO_UI_FONTWEIGHT__, fontWeight);
837                 controller.setProperty(border, __GO_UI_FRAME_BORDER_POSITION__, position.ordinal());
838                 controller.setProperty(border, __GO_UI_FRAME_BORDER_COLOR__, color);
839
840                 break;
841             }
842             case NONE:
843             default:
844                 break;
845         }
846
847         controller.setGraphicObjectRelationship(uic, border);
848         return border;
849     }
850
851     private static Double[] getColor(String str) {
852         try {
853             Field field = Class.forName("java.awt.Color").getField(str);
854             Color color = (Color) field.get(null);
855             return new Double[] {(double) color.getRed() / 255, (double) color.getGreen() / 255, (double) color.getBlue() / 255};
856         } catch (Exception e) {
857             return new Double[] {(double) Color.black.getRed() / 255, (double) Color.black.getGreen() / 255, (double) Color.black.getBlue() / 255};
858         }
859     }
860 }