Fix #7982 - roo javascript completion
[roobuilder] / src / JsRender / Node.vala
1
2 // test..
3 // valac gitlive/app.Builder.js/JsRender/Lang.vala gitlive/app.Builder.js/JsRender/Node.vala --pkg gee-1.0 --pkg=json-glib-1.0 -o /tmp/Lang ;/tmp/Lang
4
5
6 /*
7  * 
8  * props:
9  * 
10  * key value view of properties.
11  * 
12  * Old standard..
13  * XXXXX : YYYYY  -- standard - should be rendered as XXXX : "YYYY" usually.
14  * |XXXXX : YYYYY  -- standard - should be rendered as XXXX : YYYY usually.
15  * |init  -- the initialization...
16  * *prop : a property which is actually an object definition... 
17  * *args : contructor args
18  * .ctor : Full contruct line...  
19  * 
20  * Newer code
21  * ".Gee.ArrayList<Xcls_fileitem>:fileitems" ==> # type  name 
22  * ".signal:void:open": "(JsRender.JsRender file)" ==> @ type name
23  *  "|void:clearFiles": "() .... some code...."  | type name
24  *
25  * 
26  * 
27  * 
28  * 
29  * Standardize this crap...
30  * 
31  * standard properties (use to set)
32  *          If they are long values show the dialog..
33  * 
34  * bool is_xxx  :: can show a pulldown.. (true/false)
35  * string html  
36  * $ string html  = string with value interpolated eg. baseURL + ".." 
37  *  Clutter.ActorAlign x_align  (typed)  -- shows pulldowns if type is ENUM? 
38  * $ untypedvalue = javascript untyped value... 
39  * 
40  * object properties (not part of the GOjbect being wrapped?
41  * # Gee.ArrayList<Xcls_fileitem> fileitems
42  * 
43  * signals
44  * @ void open 
45  * 
46  * methods -- always text editor..
47  * | void clearFiles
48  * | someJSmethod
49  * 
50  * specials
51  * * prop -- string
52  * * args  -- string
53  * * ctor -- string
54  * * init -- big string?
55  * 
56  * event handlers (listeners)
57  *   just shown 
58  * 
59  * -----------------
60  * special ID values
61  *  +XXXX -- indicates it's a instance property / not glob...
62  *  *XXXX -- skip writing glob property (used as classes that can be created...)
63  *  _XXXX -- (string) a translatable string.
64  * 
65  * 
66  *  FORMATING?
67 .method {
68          color : green;
69          font-weight: bold;      
70 }
71 .prop {
72         color : #333;
73 }
74 .prop-code {
75     font-style: italic;
76  }
77 .listener {
78     color: #600;
79     font-weight: bold;   
80 }
81 .special { 
82   color : #00c;    font-weight: bold;    
83
84
85 */
86
87
88
89
90
91
92 public class JsRender.Node : GLib.Object {
93         
94
95         public static int uid_count = 0;
96         
97         public int oid { get; private set; }
98         public Node parent;
99         private Gee.ArrayList<Node> items; // child items..
100         public GLib.ListStore  childstore; // must be kept in sync with items
101         public GLib.ListStore?  propstore; // must be kept in sync with items
102         public string  xvala_cls;
103         public string xvala_xcls; // 'Xcls_' + id;
104         public string xvala_id; // item id or ""
105         
106         // line markers..
107         public int line_start;
108         public int line_end;
109         public Gee.ArrayList<int> lines;
110         public Gee.HashMap<int,string> line_map; // store of l:xxx or p:....  // fixme - not needed as we can store line numbers in props now.
111         public Gee.ArrayList<int> node_lines; 
112         public Gee.HashMap<int,Node> node_lines_map; // store of l:xxx or p:....
113         
114         
115         public string node_pad = "";
116         
117         private int _updated_count = 0;
118         public int updated_count { 
119                 get {
120                         return this._updated_count; 
121                 }
122                 set  {
123                         this.nodeTitleProp = ""; // ?? should trigger set?
124                         this.iconFilename = "";
125                         this._updated_count = value;
126                         if (this.parent != null) {
127                                 this.parent.updated_count++;
128                         }
129                 }
130  
131         } // changes to this trigger updates on the tree..
132
133         public Node()
134         {
135                 this.items = new Gee.ArrayList<Node>();
136                 //this._props = new Gee.HashMap<string,NodeProp>();
137                 //this._listeners = new Gee.HashMap<string,NodeProp>(); // Nodeprop can include line numbers..
138                 this.propstore = new GLib.ListStore(typeof(NodeProp)); // Nodeprop can include line numbers..
139                 this.xvala_cls = "";
140                 this.xvala_xcls = "";
141                 this.xvala_id = "";
142                 this.parent = null;
143                 this.line_start = -1;
144                 this.line_end = -1;             
145                 this.lines = new Gee.ArrayList<int>();
146                 this.line_map = new Gee.HashMap<int,string>();
147                 this.node_lines = new Gee.ArrayList<int>();
148                 this.node_lines_map = new Gee.HashMap<int,Node>();
149                 this.childstore = new GLib.ListStore( typeof(Node));
150                 this.oid = uid_count++;
151                 
152         }
153         
154         public  Gee.ArrayList<Node> readItems()
155         {
156                 return this.items; // note should not modify add/remove from this directly..
157                 
158         }
159         public void setNodeLine(int line, Node node) {
160                 //print("Add node @ %d\n", line);
161                 if (this.node_lines_map.has_key(line)) {
162                         return;
163                 }
164                 this.node_lines.add(line);
165                 this.node_lines_map.set(line, node);
166                 
167         }
168         
169         public void setLine(int line, string type, string prop) 
170         {
171                 //GLib.debug("set prop %s (%s) to line %d", prop, type, line);
172                 if (this.line_map.has_key(line)) {
173                         if  (this.line_map.get(line) != "e:"  ) {
174                                 return;
175                         }
176                 } else {
177                         this.lines.add(line);
178                 }
179                 this.line_map.set(line, type + ":" + prop);
180                 if (type == "e" || type == "p" ) {
181                 
182                         if (prop == "" || !this.props.has_key(prop)) {
183                                 ///GLib.debug("cant find prop '%s'", prop);
184                                 return;
185                         }
186                         
187                         var prope = this.props.get(prop);
188                         if (prope != null && type =="p") { 
189                                 prope.start_line = line;
190                         }
191                         if (prope != null && type =="e") { 
192                                 prope.end_line = line;
193                         }       
194                         
195                 }
196                 if (type == "l" || type =="x") {
197                         if (prop == "" || !this.listeners.has_key(prop)) {
198                                 //GLib.debug("cant find listener '%s'", prop);
199                                 return;
200                         }
201                         
202                         var prope = this.listeners.get(prop);
203                         if (prope != null && type =="l") { 
204                                 prope.start_line = line;
205                         }
206                         if (prope != null && type =="x") { 
207                                 prope.end_line = line;
208                         }       
209                         
210                 
211                 }
212                 
213                 
214                 
215                 //GLib.debug("setLine %d, %s", line, type + ":" + prop);
216         }
217         public void sortLines() {
218                 //print("sortLines\n");
219                 this.lines.sort((a,b) => {   
220                         return (int)a-(int)b;
221                 });
222                 this.node_lines.sort((a,b) => {   
223                         return (int)a-(int)b;
224                 });
225         }
226         public Node? lineToNode(int line)
227         {
228                 //print("Searching for line %d\n",line);
229                 var l = -1;
230                 //foreach(int el in this.node_lines) {
231                         //print("all lines %d\n", el);
232                 //}
233                 
234                 
235                 foreach(int el in this.node_lines) {
236                         //print("?match %d\n", el);
237                         if (el < line) {
238                                 
239                                 l = el;
240                                 //print("LESS\n");
241                                 continue;
242                         }
243                         if (el == line) {
244                                 //print("SAME\n");
245                                 l = el;
246                                 break;
247                         }
248                         if (l > -1) {
249                                 var ret = this.node_lines_map.get(l);
250                                 if (line > ret.line_end) {
251                                         return null;
252                                 }
253                                 //print("RETURNING NODE ON LINE %d", l);
254                                 return ret;
255                         }
256                         return null;
257                         
258                 }
259                 if (l > -1) {
260                         var ret = this.node_lines_map.get(l);
261                         if (line > ret.line_end) {
262                                 return null;
263                         }
264                         //print("RETURNING NODE ON LINE %d", l);
265                         return ret;
266
267                 }
268                 return null;
269                 
270         }
271         
272         
273         public NodeProp? lineToProp(int line)
274         {
275                 
276                 for(var i= 0; i < this.propstore.get_n_items();i++) {
277                         var p = (NodeProp) this.propstore.get_item(i);
278                         GLib.debug("prop %s lines %d -> %d", p.name, p.start_line, p.end_line);
279                         if (p.start_line > line) {
280                                 continue;
281                         }
282                         if (line > p.end_line) {
283                                 continue;
284                         }
285                         return p;
286                 }
287                 return null;
288         }
289                 
290                  
291         
292         public bool getPropertyRange(string prop, out int start, out int end)
293         {
294                 end = 0;
295                 start = -1;
296                 foreach(int el in this.lines) {
297                         if (start < 0) {
298                                 if (this.line_map.get(el) == prop) {
299                                         start = el;
300                                         end = el;
301                                 }
302                                 continue;
303                         }
304                         end = el -1;
305                         break;
306                 }
307                 return start > -1;
308         
309         
310         }
311         
312         public void dumpProps(string indent = "")
313         {
314                 print("%s:\n" , this.fqn());
315                 foreach(int el in this.lines) {
316                         print("%d: %s%s\n", el, indent, this.line_map.get(el));
317                 }
318                 foreach(Node n in this.items) {
319                         n.dumpProps(indent + "  ");
320                 }
321         }
322         
323         
324         
325         public string uid()
326         {
327                 if (this.props.get("id") == null) {
328                         return "uid-%d".printf(this.oid);
329                 }
330                 return this.props.get("id").val;
331         }
332         
333         
334         public bool hasChildren()
335         {
336                 return this.items.size > 0;
337         }
338         public bool hasXnsType()
339         {
340                 if (this.props.get("xns") != null && this.props.get("xtype") != null) {
341                         return true;
342                         
343                 }
344                 return false;
345         }
346         
347         public string FQN { // for sorting
348                 owned get { return this.fqn(); }
349                 private set  {}
350         }
351         
352         public string fqn()
353         {
354                 if (!this.hasXnsType ()) {
355                         return "";
356                 }
357                 return this.props.get("xns").val + "." + this.props.get("xtype").val; 
358
359         }
360         public void setFqn(string name)
361         {
362                 var ar = name.split(".");
363                 var l = name.length - (ar[ar.length-1].length +1);
364                 
365
366                 
367                 if (this.props.has_key("xtype")) {
368                         this.props.get("xtype").val = ar[ar.length-1];
369                 } else {
370                         this.add_prop(new NodeProp.prop("xtype", "",  ar[ar.length-1]));                
371                 }       
372                 if (this.props.has_key("xns")) {
373                         this.props.get("xns").val = name.substring(0, l);
374                 } else {
375                         this.add_prop(new NodeProp.raw("xns", "", name.substring(0, l)));               
376                 }       
377                 
378                 
379                 //print("setFQN %s to %s\n", name , this.fqn());
380                                
381
382         }
383         // wrapper around get props that returns empty string if not found.
384         //overrides Glib.object.get (hence new)
385         public new string get(string key)
386         {
387                 
388                 var v = this.props.get(key);
389                 return v == null ? "" : v.val;
390         }       
391                  
392         public  NodeProp? get_prop(string key)
393         {
394                 
395                 return this.props.get(key);
396                 
397         }
398         
399  
400         
401
402
403         public bool has(string key)
404         {
405                 return this.props.has_key(key);
406                  
407          
408         }
409
410         public void  remove()
411         {
412                 if (this.parent == null) {
413                         GLib.debug("remove - parent is null?");
414                         return;
415                 }
416                 var nlist = new Gee.ArrayList<Node>();
417                 for (var i =0;i < this.parent.items.size; i++) {
418                         if (this.parent.items.get(i) == this) {
419                                 continue;
420                         }
421                         nlist.add(this.parent.items.get(i));
422                 }
423                 uint pos;
424                 if ( this.parent.childstore.find(this, out pos)) {
425                         this.parent.childstore.remove(pos);
426                 } 
427                 
428                 this.parent.items = nlist;
429                 this.parent = null;
430
431         }
432          
433         /* creates javascript based on the rules */
434         public Node? findProp(string n) {
435                 for(var i=0;i< this.items.size;i++) {
436                         var p = this.items.get(i).get("* prop");
437                         if (p  == null) {
438                                 continue;
439                         }
440                         if (p == n) {
441                                 return this.items.get(i);
442                         }
443                 }
444                 return null;
445
446         }
447
448         
449         
450          
451         static Json.Generator gen = null;
452         
453         public string quoteString(string str)
454         {
455                 if (Node.gen == null) {
456                         Node.gen = new Json.Generator();
457                 }
458                  var n = new Json.Node(Json.NodeType.VALUE);
459                 n.set_string(str);
460  
461                 Node.gen.set_root (n);
462                 return  Node.gen.to_data (null);   
463         }
464
465         public void loadFromJsonString(string str, int ver)
466         {
467                 var pa = new Json.Parser();
468                 try {
469                         pa.load_from_data(str);
470                 } catch (GLib.Error e) {
471                         GLib.debug("Error loading string?");
472                         return;
473                 }
474                 var new_node = pa.get_root();
475                 var obj = new_node.get_object ();
476                      
477                 this.loadFromJson(obj, ver);
478         }
479         
480  
481
482         public void loadFromJson(Json.Object obj, int version) {
483                  
484                 obj.foreach_member((o , key, value) => {
485                         //print(key+"\n");
486                         if (key == "items") {
487                                 var ar = value.get_array();
488                                 ar.foreach_element( (are, ix, el) => {
489                                         var node = new Node();
490                                         node.parent = this;
491                                         node.loadFromJson(el.get_object(), version);
492                                         this.items.add(node);
493                                         this.childstore.append(node);
494                                 });
495                                 return;
496                         }
497                         if (key == "listeners") {
498                                 var li = value.get_object();
499                                 li.foreach_member((lio , li_key, li_value) => {
500                                         this.add_prop(new NodeProp.listener(li_key, this.jsonNodeAsString(li_value)));
501                                         //this.listeners.set(li_key,  new NodeProp.listener(li_key, this.jsonNodeAsString(li_value)));
502                                 });
503                                 return;
504                         }
505                         
506
507                         var rkey = key;
508                         var sval = this.jsonNodeAsString(value);
509                 
510                         if (version == 1) {
511                                 rkey = this.upgradeKey(key, sval);
512                         }
513                         var n =  new NodeProp.from_json(rkey, sval);
514                                 
515                         this.add_prop(n );
516
517
518                 });
519                 
520                 
521                 
522
523
524
525         }
526         
527         // converts the array into a string with line breaks.
528         public string jsonNodeAsString(Json.Node node)
529         {
530                 
531                 if (node.get_node_type() == Json.NodeType.ARRAY) {
532                         var  buffer = new GLib.StringBuilder();
533                         var ar = node.get_array();
534                         for (var i = 0; i < ar.get_length(); i++) {
535                                 if (i >0 ) {
536                                         buffer.append_c('\n');
537                                 }
538                                 buffer.append(ar.get_string_element(i));
539                         }
540                         return buffer.str;
541                 }
542         // hopeflyu only type value..           
543                 var sv =  Value (typeof (string));                      
544                 var v = node.get_value();
545                 v.transform(ref sv);
546                 return (string)sv;
547
548         }
549         
550         // really old files...
551
552         public string upgradeKey(string key, string val)
553         {
554                 // convert V1 to V2
555                 if (key.length < 1) {
556                         return key;
557                 }
558                 switch(key) {
559                         case "*prop":
560                         case "*args":
561                         case ".ctor":
562                         case "|init":
563                                 return "* " + key.substring(1);
564                                 
565                         case "pack":
566                                 return "* " + key;
567                 }
568                 if (key[0] == '.') { // v2 does not start with '.' ?
569                         var bits = key.substring(1).split(":");
570                         if (bits[0] == "signal") {
571                                 return "@" + string.joinv(" ", bits).substring(bits[0].length);
572                         }
573                         return "# " + string.joinv(" ", bits);                  
574                 }
575                 if (key[0] != '|' || key[1] == ' ') { // might be a v2 file..
576                         return key;
577                 }
578                 var bits = key.substring(1).split(":");
579                 // two types '$' or '|' << for methods..
580                 // javascript 
581                 if  (Regex.match_simple ("^function\\s*(", val.strip())) {
582                         return "| " + key.substring(1);
583                 }
584                 // vala function..
585                 
586                 if  (Regex.match_simple ("^\\(", val.strip())) {
587                 
588                         return "| " + string.joinv(" ", bits);
589                 }
590                 
591                 // guessing it's a property..
592                 return "$ " + string.joinv(" ", bits);
593                 
594                 
595
596         }
597
598
599
600
601
602         
603         public Node  deepClone()
604         {
605                 var n = new Node();
606                 n.loadFromJson(this.toJsonObject(), 2);
607                 return n;
608
609         }
610         public string toJsonString()
611         {
612                 if (Node.gen == null) {
613                         Node.gen = new Json.Generator();
614                         gen.pretty =  true;
615                         gen.indent = 1;
616                 }
617                 var n = new Json.Node(Json.NodeType.OBJECT);
618                 n.set_object(this.toJsonObject () );
619                 Node.gen.set_root (n);
620                 return  Node.gen.to_data (null);   
621         }
622         
623         public void jsonObjectAddStringValue(Json.Object obj, string key, string v)
624         {
625                 if (v.index_of_char('\n',0) < 0) {
626                         obj.set_string_member(key,v);
627                         return;
628                 }
629                 var aro = new Json.Array();
630                 var ar = v.split("\n");
631                 for(var i =0;i < ar.length;i++) {
632                         aro.add_string_element(ar[i]);
633                 }
634                 obj.set_array_member(key,aro);
635         }
636         
637         public Json.Object toJsonObject()
638         {
639                 var ret = new Json.Object();
640
641                 // listeners...
642                 if (this.listeners.size > 0) {
643                         var li = new Json.Object();
644                         ret.set_object_member("listeners", li);
645                         var liter = this.listeners.map_iterator();
646                         while (liter.next()) {
647                                 this.jsonObjectAddStringValue(li, liter.get_value().to_json_key(), liter.get_value().val);
648                         }
649                 }
650                 //props
651                 if (this.props.size > 0 ) {
652                         var iter = this.props.map_iterator();
653                         while (iter.next()) {
654                                 this.jsonObjectsetMember(ret, iter.get_value().to_json_key(), iter.get_value().val);
655                         }
656                 }
657                 if (this.items.size > 0) {
658                         var ar = new Json.Array();
659                         ret.set_array_member("items", ar);
660                 
661                         // children..
662                         for(var i =0;i < this.items.size;i++) {
663                                 ar.add_object_element(this.items.get(i).toJsonObject());
664                         }
665                 }
666                 return ret;
667                 
668  
669         }
670          
671         public void jsonObjectsetMember(Json.Object o, string key, string val) {
672                 if (Lang.isBoolean(val)) {
673                         o.set_boolean_member(key, val.down() == "false" ? false : true);
674                         return;
675                 }
676                 
677                 
678                 if (Lang.isNumber(val)) {
679                         if (val.contains(".")) {
680                                 //print( "ADD " + key + "=" + val + " as a double?\n");
681                                 o.set_double_member(key, double.parse (val));
682                                 return;
683
684                         }
685                         //print( "ADD " + key + "=" + val + " as a int?\n")  ;
686                         o.set_int_member(key,long.parse(val));
687                         return;
688                 }
689                 ///print( "ADD " + key + "=" + val + " as a string?\n");
690                 this.jsonObjectAddStringValue(o,key,val);
691                 //o.set_string_member(key,val);
692                 
693         }
694         
695         
696         public string nodeTipProp { 
697                 set {
698                         // NOOp ??? should 
699                 }
700                 owned get {
701                          return  this.nodeTip();
702                 } 
703         }
704         // fixme this needs to better handle 'user defined types etc..
705         public string nodeTip()
706         {
707                 var ret = this.nodeTitle(true);
708                 var spec = "";
709                 var funcs = "";
710                 var props = "";
711                 var listen = "";
712  
713                 var uprops = "";
714                 // sort?
715                 
716                 var keys = new  Gee.ArrayList<string>();
717                 foreach(var k in this.props.keys) {
718                         keys.add(k);
719                 }
720                 keys.sort((a,b) => {
721                          return Posix.strcmp(a, b);
722                 
723                 });
724                 
725                 
726                 foreach(var pk in keys) {
727                          
728                         var prop = this.props.get(pk);
729                         var i = prop.name.strip();
730                         
731                         var val = prop.val;
732                         val = val == null ? "" : val;
733                         
734                         switch(prop.ptype) {
735                                 case PROP: 
736                                 case RAW: // should they be the same?
737                                 
738                                         props += "\n\t" + GLib.Markup.escape_text(prop.rtype) +
739                                                 " <b>" + GLib.Markup.escape_text(i) +"</b> : " + 
740                                                 GLib.Markup.escape_text(val.split("\n")[0]);
741                                                 
742                                         break;
743                                         
744                         
745                                 
746                                 case METHOD :
747                                         funcs += "\n\t" + GLib.Markup.escape_text(prop.rtype) +
748                                                 " <b>" + GLib.Markup.escape_text(i) +"</b> : "  +
749                                                 GLib.Markup.escape_text(val.split("\n")[0]);
750                                         break;
751                                         
752                                  
753                                 case USER : // user defined.
754                                         uprops += "\n\t<b>" + 
755                                                 GLib.Markup.escape_text(i) +"</b> : " + 
756                                                 GLib.Markup.escape_text(val.split("\n")[0]);
757                                         break;
758                                         
759                                 case SPECIAL : // * prop| args | ctor | init
760                                         spec += "\n\t<b>" + 
761                                                 GLib.Markup.escape_text(i) +"</b> : " + 
762                                                 GLib.Markup.escape_text(val.split("\n")[0]);
763                                         break;
764                                         
765                                 case LISTENER : return  "";  // always raw...
766                                 // not used
767                                 default:
768                                         break;;
769                         
770                         }
771                          
772                         
773                 }
774                 
775                 keys = new  Gee.ArrayList<string>();
776                 foreach(var k in this.listeners.keys) {
777                         keys.add(k);
778                 }
779                 keys.sort((a,b) => {
780                          return Posix.strcmp(a, b);
781                 
782                 });
783                 
784                 foreach(var pk in keys) {
785                          
786                         var prop = this.listeners.get(pk);
787                         var i =  prop.name.strip();
788                         
789                         var val = prop.val.strip();
790                         if (val == null || val.length < 1) {
791                                 continue;
792                         }
793                          listen += "\n\t<b>" + 
794                                         GLib.Markup.escape_text(i) +"</b> : " + 
795                                         GLib.Markup.escape_text(val.split("\n")[0]);
796                         
797                 }
798                 
799                 
800                 if (props.length > 0) {
801                         ret+="\n\nProperties:" + props;
802                 }
803                 if (uprops.length > 0) {
804                         ret+="\n\nUser defined Properties:" + uprops;
805                 } 
806                 
807                 
808                 if (funcs.length > 0) {
809                         ret+="\n\nMethods:" + funcs;
810                 } 
811                 if (listen.length > 0) {
812                         ret+="\n\nListeners:" + listen;
813                 } 
814                 if (spec.length > 0) {
815                         ret+="\n\nSpecial:" + spec;
816                 } 
817                 
818                 return ret;
819
820         }
821         
822         public string nodeTitleProp { 
823                 set {
824                         // NOOp ??? should 
825                 }
826                 owned get {
827                          return  this.nodeTitle();
828                 } 
829         }
830         
831         
832         
833         
834         
835         
836         public string nodeTitle(bool for_tip = false) 
837         {
838                 string[] txt = {};
839
840                 //var sr = (typeof(c['+buildershow']) != 'undefined') &&  !c['+buildershow'] ? true : false;
841                 //if (sr) txt.push('<s>');
842
843                 if (this.has("* prop"))   { txt += (GLib.Markup.escape_text(this.get("* prop")) + ":"); }
844                 
845                 //if (renderfull && c['|xns']) {
846                 var fqn = this.fqn();
847                 var fqn_ar = fqn.split(".");
848                 txt += for_tip || fqn.length < 1 ? fqn : fqn_ar[fqn_ar.length -1];
849                 
850                 if (fqn == "Roo.bootstrap.Element" && this.has("tag")) {
851                    txt = {};
852                    txt += GLib.Markup.escape_text(this.get("tag").up());
853                 }
854                 
855                 //if (c.xtype)    { txt.push(c.xtype); }
856                         
857                 if (this.has("id"))      { txt += ("<b>[id=" + GLib.Markup.escape_text(this.get("id")) + "]</b>"); }
858                 if (this.has("fieldLabel")){ txt += ("[" + GLib.Markup.escape_text(this.get("fieldLabel")) + "]"); }
859                 if (this.has("boxLabel"))  { txt += ("[" + GLib.Markup.escape_text(this.get("boxLabel"))+ "]"); }
860                 
861                 
862                 if (this.has("layout")) { txt += ("<i>" + GLib.Markup.escape_text(this.get("layout")) + "</i>"); }
863                 if (this.has("title"))   { txt += ("<b>" + GLib.Markup.escape_text(this.get("title")) + "</b>"); }
864                 if (this.has("html") && this.get("html").length > 0)     { 
865                         var ht = this.get("html").split("\n");
866                         if (ht.length > 1) {
867                                 txt += ("<b>" + GLib.Markup.escape_text(ht[0]) + "...</b>");
868                         } else { 
869                                 txt += ("<b>" + GLib.Markup.escape_text(this.get("html")) + "</b>");
870                         }
871                 }
872                 if (this.has("label"))   { txt += ("<b>" + GLib.Markup.escape_text(this.get("label"))+ "</b>"); }
873                 if (this.has("header"))   { txt += ("<b>" + GLib.Markup.escape_text(this.get("header")) + "</b>"); }
874                 if (this.has("legend"))  { txt += ("<b>" + GLib.Markup.escape_text(this.get("legend")) + "</b>"); }
875                 if (this.has("text"))     { txt += ("<b>" + GLib.Markup.escape_text(this.get("text")) + "</b>"); }
876                 if (this.has("name"))     { txt += ("<b>" + GLib.Markup.escape_text(this.get("name"))+ "</b>"); }
877                 if (this.has("region")) { txt += ("<i>(" + GLib.Markup.escape_text(this.get("region")) + ")</i>"); }
878                 if (this.has("dataIndex")){ txt += ("[" + GLib.Markup.escape_text(this.get("dataIndex")) + "]"); }
879                 // class is quite important on bootstrap..
880                 if (this.has("cls")){ txt += ("<b>[cls=" + GLib.Markup.escape_text(this.get("cls")) + "]</b>"); }               
881                 
882                 // other 'specials?'
883                 if (fqn == "Roo.bootstrap.Link") {
884                         txt += ("<b>href=" + (this.has("name") ?  GLib.Markup.escape_text(this.get("name")) : "?" ) + "</b>");
885                         if (this.has("fa")){ txt += ("<b>[fa=" + GLib.Markup.escape_text(this.get("fa")) + "]</b>"); }                                  
886                 }
887
888
889
890                 // for flat classes...
891                 //if (typeof(c["*class"]"))!= "undefined")  { txt += ("<b>" +  c["*class"]+  "</b>"); }
892                 //if (typeof(c["*extends"]"))!= "undefined")  { txt += (": <i>" +  c["*extends"]+  "</i>"); }
893                 
894                 
895                 //if (sr) txt.push('</s>');
896                 return (txt.length == 0) ? "Element" : string.joinv(" ", txt);
897         }
898         // used by trees to display icons?
899         // needs more thought?!?
900         public string iconFilename { 
901                 set {
902                         // NOOp ??? should 
903                 }
904                 owned get {
905                         var clsname = this.fqn();
906     
907                         var clsb = clsname.split(".");
908                     var sub = clsb.length > 1 ? clsb[1].down()  : "";
909                         var fn = "/usr/share/glade/pixmaps/hicolor/16x16/actions/widget-gtk-" + sub + ".png";
910                         //if (FileUtils.test (fn, FileTest.IS_REGULAR)) {
911                                 return fn;
912                         //}
913                         //return "/dev/null"; //???
914                 } 
915         }
916         
917          
918         
919         public void insertAfter(Node child, Node after) 
920         {
921                 this.insertChild(this.items.index_of(after) + 1, child);
922         }
923         public void insertBefore(Node child, Node before)       
924         {
925                 this.insertChild(this.items.index_of(before), child);
926         }
927         
928         public void insertChild(int pos, Node child)
929         {
930                 this.items.insert(pos, child);
931                 this.childstore.insert(pos, child);
932                 child.parent = this;
933         }
934         public void appendChild(Node child)
935         {
936                 this.items.add( child);
937                 this.childstore.append(child);
938                 child.parent = this;
939         }
940         
941         
942         /**
943         
944         properties
945                 previous we had listeners / and props
946                 
947                 we really need to store this as flat array - keep it simple!?
948                 
949                 getValue(key)
950                 update(key, value)
951                 
952                 
953         
954         */
955         
956
957         
958         
959         public void loadProps(GLib.ListStore model) 
960         {
961         
962                 // fixme sorting?? - no need to loop twice .. just use sorting.!
963                 var oldstore = this.propstore;
964                 this.propstore = model;
965                 for(var i =  0; i < oldstore.n_items; i++ ) {
966                         var it = (NodeProp) oldstore.get_item(i);
967                     model.append(it);
968                         
969                 }
970                 this.sortProps();
971            
972    }
973    // used to replace propstore, so it does not get wiped by editing a node
974    public void dupeProps()
975    {
976                 GLib.debug("dupeProps START");
977                 var oldstore = this.propstore;
978                 this.propstore = new GLib.ListStore(typeof(NodeProp));;
979                 for(var i =  0; i < oldstore.n_items; i++ ) {
980                         var it = (NodeProp) oldstore.get_item(i);
981                         this.propstore.append(it);
982                 }
983                 GLib.debug("dupeProps END");
984         }
985         
986    
987    public void remove_prop(NodeProp prop)
988         {
989                 uint pos;
990                 if (!this.propstore.find(prop, out pos)) {
991                         return;
992                 }
993                 this.propstore.remove(pos);
994                 this.updated_count++;
995                 
996         }   
997    
998         public bool has_prop_key(NodeProp prop) 
999         {
1000                 for(var i =  0; i < this.propstore.n_items; i++ ) {
1001                         var it = (NodeProp) this.propstore.get_item(i);
1002                         if (it.ptype == prop.ptype && it.to_index_key() == prop.to_index_key()) {
1003                                 return true;
1004                         }
1005                         
1006                 }
1007                 return false;
1008            
1009         }
1010         
1011          
1012         
1013         
1014         public void add_prop(NodeProp prop)
1015         {
1016                 if (this.has_prop_key(prop) && !prop.to_index_key().has_suffix("[]")) {
1017                         GLib.warning("duplicate key' %s'- can not add - call has_prop_key first", prop.to_index_key());
1018                         return;
1019                 }
1020                 prop.parent = this;
1021                 this.propstore.append(prop);
1022                 this.sortProps();
1023                 
1024                 this.updated_count++;
1025                 
1026                 
1027         }
1028         
1029         int props_updated_count = -1;
1030         Gee.HashMap<string,NodeProp> props_cache;
1031         
1032         public Gee.HashMap<string,NodeProp> props {
1033                 owned get {
1034                         if (this.updated_count == this.props_updated_count) {
1035                                 return this.props_cache;
1036                         }
1037                          this.props_cache = new Gee.HashMap<string,NodeProp>(); // the properties..
1038
1039                         for(var i =  0; i < this.propstore.n_items; i++ ) {
1040                                 var it = (NodeProp) this.propstore.get_item(i);
1041                                 if (it.ptype != NodePropType.LISTENER) {
1042                                 //      GLib.debug("props add key %s", it.to_index_key());
1043                                         this.props_cache.set( it.to_index_key() , it);
1044                                 }
1045                         }
1046                         this.props_updated_count = this.updated_count;
1047                         return this.props_cache;
1048                 }
1049                 private set {
1050                         GLib.error("do not set listerners direclty");
1051                 }
1052         }
1053         
1054         int listeners_updated_count = -1;
1055         Gee.HashMap<string,NodeProp> listeners_cache;
1056         
1057         //private Gee.HashMap<string,NodeProp> _listeners; // the listeners..
1058         public Gee.HashMap<string,NodeProp> listeners {
1059                 owned get {
1060                         if (this.updated_count == this.listeners_updated_count) {
1061                                 return this.listeners_cache;
1062                         }
1063                         
1064                         this.listeners_cache = new Gee.HashMap<string,NodeProp>(); // the properties..
1065
1066                         for(var i =  0; i < this.propstore.n_items; i++ ) {
1067                                 var it = (NodeProp) this.propstore.get_item(i);
1068                                 if (it.ptype == NodePropType.LISTENER) {
1069                                         this.listeners_cache.set( it.to_index_key() , it);
1070                                 }
1071                         }
1072                         this.listeners_updated_count = this.updated_count;
1073                         return this.listeners_cache;;
1074                 }
1075                 private set {
1076                         GLib.error("do not set listerners direclty");
1077                 }
1078         }
1079         private void sortProps ()
1080         {
1081         
1082                 this.propstore.sort( (a, b) => {
1083
1084                         return Posix.strcmp( ((NodeProp)a).to_sort_key(),  ((NodeProp)b).to_sort_key());
1085                         
1086                 });
1087          
1088         
1089         }
1090 }