Palete/Palete.vala
[app.Builder.js] / Palete / Palete.vala
1
2 namespace Palete 
3 {
4     public errordomain Error {
5         INVALID_TYPE,
6         NEED_IMPLEMENTING,
7                 MISSING_FILE,
8                 INVALID_VALUE
9     }
10
11         public class Usage : Object 
12         {
13                 public GLib.List<string> left;
14                 public GLib.List<string> right;
15                 public Usage(GLib.List<string> ileft, GLib.List<string> iright)
16                 {
17                         this.left = ileft.copy();
18                         this.right=  iright.copy();
19                 }
20         }
21
22         
23
24     static Gee.HashMap<string,Palete>? cache = null;
25     
26         public static Palete factory(string xtype)
27     {
28         if (cache == null) {
29             cache = new Gee.HashMap<string,Palete>();
30         }
31         if (cache.get(xtype) != null) {
32             return cache.get(xtype);
33         }
34         switch(xtype) {
35             case "Gtk":
36                 cache.set(xtype, new Gtk());
37                 break;
38             //case "Roo":
39             //    cache.set(xtype, new Roo());
40                 break;
41             default:
42                 throw new Error.INVALID_TYPE("invalid argument to Palete factory");
43         }
44         return cache.get(xtype);
45     }
46        
47
48     public abstract class Palete : Object 
49     {
50         
51        
52         public string name;
53
54                 public GLib.List<Usage> map;
55                 
56         public Palete()
57         {
58             // nothing?
59         }
60         
61         
62         //map : false, // array of mappings   left: [] , right : []
63         
64         //items : false, // the tree of nodes.
65         
66         
67
68
69         string  guessName(JsRender.Node ar) throws Error // turns the object into full name.
70         {
71             throw new Error.NEED_IMPLEMENTING("xxx. guessName needs implimenting");
72         }
73             
74
75             
76         /**
77          * gather a  list of potentional objects that can be added..
78          * 
79          */
80         /*
81         gatherList: function (existing) {
82             existing = existing || [];
83            // existing.push('*top'); // always have top
84             var ret  = []; 
85             console.log("GATHER LIST? " + this.map.length);
86             
87             
88             function addRight(right) {
89                 right.forEach(function(r) {
90                     if (ret.indexOf(r) > -1) {
91                         return;
92                     }
93                     ret.push(r);
94                 });
95             }
96             
97             this.map.forEach(function(m) {
98                 var done = false
99                 m.left.forEach( function(left) {
100                     if (done) return; 
101                     
102                     var l = left.replace(/:.*$/, '');
103                    // print("chk:" + l + " in " + existing.join(',')); 
104                     if (existing.indexOf(l) > -1) {
105                         addRight(m.right);
106                         done =true;
107                         //return true; // no more needed..
108                     }
109                 });
110                 
111             });
112             ret.sort();
113             
114            // console.dump(ret);
115             return ret;
116             
117             
118             
119         },
120         */
121         public string[] getDropList(string rval)
122         {
123             // should be a bit more than this..
124                         // -> it should look for all elements that inherit 
125             string[] ret = {};
126                         var rt = new GLib.List<string>();
127                         for (var i = 0; i < this.map.length(); i++) {
128                                 var m = this.map.nth_data(i);
129                                 
130                 if (m.right.index(rval) > -1) {
131                                         print("found RIGHT, adding left\n");
132                                         
133                                         for(var ii =0; ii < m.left.length(); ii++) {
134                                 var l = m.left.nth_data(ii);
135                                                 
136                         if (rt.index(l) > -1) {
137                             continue;
138                         }
139                                                 print("ADD " + string.joinv(", ", ret) + "\n");
140                         ret += l;
141                                                 rt.append(l);
142                     }
143                 }
144                 
145             }
146             //console.log("DROP LIST:");
147             //console.dump(ret);
148             return ret;
149             
150         }
151         /**
152          * basic guess type.. 
153          * 
154          * /
155         findType : function (data, prop, value)
156         {
157             if (prop[0] == '|') {
158                 return 'function';
159             }
160             return typeof(value);
161         },
162         
163         
164         findOptions : function(ename)
165         {
166             switch(ename.toLowerCase()) {
167                 case 'boolean': 
168                     return [ 'true', 'false' ];
169                 // everything else does not have options.
170                 case 'string': 
171                 case 'utf8': 
172                 case 'int': 
173                 case 'uint': 
174                 case 'function': 
175                     return false;
176                 default: 
177                     console.log("OOPS: = unknown type: " + ename);
178                     return false;
179             }
180         },
181         confirmCanAdd: function(parent, child) {
182             // confirms that one obj can be added to another.
183             // returns true, for items, or list of properties that can hold it..
184             return true;
185             
186         },
187         getDefaultPack: function(pname, cname) {
188             return 'add';
189         },
190         */
191         public void saveTemplate (string name, JsRender.Node data)
192         {
193
194                         var gn = data.fqn();
195             // store it in user's directory..
196             var appdir =  GLib.Environment.get_home_dir() + "/.Builder"; 
197
198                         
199             if (!GLib.FileUtils.test(appdir+ "/" + gn, GLib.FileTest.IS_DIR)) {
200                                 GLib.File.new_for_path (appdir+ "/" + gn).make_directory ();
201                                 
202             }
203             GLib.FileUtils.set_contents(appdir+ "/" + gn + "/" +  name + ".json", data.toJsonString());
204             
205         }
206         
207         /**
208          * list templates - in home directory (and app dir in future...)
209          * @param {String} name  - eg. Gtk.Window..
210          * @return {Array} list of templates available..
211          */
212           
213         public  GLib.List<string> listTemplates (JsRender.Node node)
214         {
215             
216             var gn = node.fqn();
217             
218             var ret = new GLib.List<string>();
219             var dir= GLib.Environment.get_home_dir() + "/.Builder/" + gn;
220                         if (!GLib.FileUtils.test(dir, GLib.FileTest.IS_DIR)) {
221                         return ret;
222                         }
223                         
224
225
226                                     
227                         var f = File.new_for_path(dir);
228         
229             var file_enum = f.enumerate_children(GLib.FileAttribute.STANDARD_DISPLAY_NAME, GLib.FileQueryInfoFlags.NONE, null);
230              
231             FileInfo next_file; 
232             while ((next_file = file_enum.next_file(null)) != null) {
233                 var n = next_file.get_display_name();
234                         if (!Regex.match_simple ("\\.json$", n)) {
235                                         continue;
236                                 }
237                                 ret.append( dir + "/" + n);
238             }
239             return ret;
240             
241         }
242  
243         public JsRender.Node? loadTemplate(string path)
244         {
245
246                     var pa = new Json.Parser();
247             pa.load_from_file(path);
248             var node = pa.get_root();
249             
250             if (node.get_node_type () != Json.NodeType.OBJECT) {
251                         return null;
252                 }
253             var obj = node.get_object ();
254              
255             var ret = new JsRender.Node();
256             ret.loadFromJson(obj);
257             return ret;
258         }
259             
260         public abstract void fillPack(JsRender.Node node,JsRender.Node parent); 
261     }
262
263
264 }
265
266
267