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