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