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