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