b559c71e04a0ff71b4bc1bf4fb669f42dedb834d
[app.Builder.js] / Builder / LeftProjectTree.js
1 //<Script type="text/javascript">
2 Gio = imports.gi.Gio;
3 Gtk = imports.gi.Gtk;
4 Gdk = imports.gi.Gdk;
5 GObject = imports.gi.GObject;
6 Pango = imports.gi.Pango ;
7
8
9 XObject = imports.XObject.XObject;
10 console = imports.console;
11
12 ProjectManager      = imports.Builder.Provider.ProjectManager.ProjectManager;
13 EditProject         = imports.Builder.EditProject.EditProject;
14 DialogNewComponent  = imports.Builder.DialogNewComponent.DialogNewComponent;
15 LeftTree            = imports.Builder.LeftTree.LeftTree;
16
17 // http://www.google.com/codesearch/p?hl=en#EKZaOgYQHwo/unstable/sources/sylpheed-2.2.9.tar.bz2%7C1erxr_ilM1o/sylpheed-2.2.9/src/folderview.c&q=gtk_tree_view_get_drag_dest_row
18
19
20 Gtk.rc_parse_string(
21             "style \"gtkcombobox-style\" {\n" + 
22             "    GtkComboBox::appears-as-list = 1\n" +
23             "}\n"+
24             "class \"GtkComboBox\" style \"gtkcombobox-style\"\n");
25
26
27  
28 LeftProjectTree = new XObject({
29         
30         xtype : Gtk.VBox,
31         
32         showNoProjectSelected : function()
33         {
34            StandardErrorDialog.show("Select a Project first.");
35         },
36         
37         
38         
39         items : [
40             {
41                 
42                 xtype: Gtk.Toolbar,
43                 pack : ['pack_start', false , true ], // expand // fill.
44                 listeners : {
45                     'size-allocate': function(w,a) {
46                     
47                         
48                         //LeftProjectTree.get('combo').el.set_size_request( 
49                         //        Gtk.allocation_get_width(a)-50,-1);
50                         
51                         
52                     }
53                 },
54                 items : [
55                     {
56                         
57                         xtype: Gtk.ToolItem,
58                         pack : [ 'insert', 0],
59                         expand: true,
60                         
61                         items : [
62                         
63                             {
64                                 id : 'combo',
65                                 
66                                 xtype : Gtk.ComboBox,
67                                 //pack : [ 'insert', 1],
68                                 expand: true,
69                                 
70                                 init : function () 
71                                 {
72                                     XObject.prototype.init.call(this); 
73                                     this.el.add_attribute(this.items[0].el , 'markup', 1 );  
74                                 },
75                             
76                                 setValue : function(fn)
77                                 {
78                                     var el = this.el;
79                                     el.set_active(-1);
80                                     var data = ProjectManager.projects;
81                                     data.forEach(function(n, ix) {
82                                         if (fn == n.fn) {
83                                             el.set_active(ix);
84                                             return false;
85                                         }
86                                     });
87                                 },
88                                 getValue : function() 
89                                 {
90                                     var ix = this.el.get_active();
91                                     if (ix < 0 ) {
92                                         return false;
93                                     }
94                                     var data =  ProjectManager.projects;
95                                     return data[ix].fn;
96                                     
97                                 },
98                                 
99                                 
100                                 listeners : {
101                                       
102                                     changed : function() {
103                                         var fn = this.getValue();
104                                         var pm  = ProjectManager;
105                                         LeftProjectTree.get('model').loadProject(pm.getByFn(fn))
106                                     }
107                                 },
108                                 items : [
109                                    {
110                                           
111                                         xtype : Gtk.CellRendererText,
112                                         pack : ['pack_start']
113                                         
114                                     },
115                                     {
116                                         id : 'combomodel',
117                                         pack : [ 'set_model' ],
118                                         xtype : Gtk.ListStore,
119                                         
120                                         init :  function ()
121                                         {
122                                             XObject.prototype.init.call(this); 
123                           
124                                             this.el.set_column_types ( 2, [
125                                                 GObject.TYPE_STRING,  // real key
126                                                 GObject.TYPE_STRING // real type
127                                                 
128                                                 
129                                             ] );
130                                                 
131                                              
132                                         },
133                                        
134                                        
135                                         
136                                         loadData : function (data) {
137                                             
138                                             var ov = LeftProjectTree.get('combo').getValue();
139                                             this.el.clear();
140                                             var iter = new Gtk.TreeIter();
141                                             var el = this.el;
142                                             data.forEach(function(p) {
143                                                 
144                                                 el.append(iter);
145                                                 
146                                                  
147                                                 el.set_value(iter, 0, p.fn);
148                                                 el.set_value(iter, 1, p.name);
149                                                 
150                                             });
151                                             
152                                             LeftProjectTree.get('combo').setValue(ov);
153                                             
154                                         }
155                                          
156                                     }
157                                   
158                                          
159                                 ]
160                         
161                             }
162                         ]
163                          
164                         
165                         
166                     },
167                     {
168                         
169                         
170                         xtype: Gtk.ToolButton,
171                         pack : [ 'insert', 1],
172                         label : "Manage",
173                         'stock-id' :  Gtk.STOCK_EDIT,
174                         listeners : {
175                             clicked: function() {
176                                 this.get('menu').el.show_all();
177                                 this.get('menu').el.popup(null, null, null, 
178                                     null, 1, Gtk.get_current_event_time());
179                                 
180                             }
181                         },
182                         
183                         items : [
184                             {
185                                 id : 'menu',
186                                 xtype : Gtk.Menu,
187                                 pack : [ false ],
188                                 
189                                 
190                                 items :  [
191                                     {
192                                         xtype : Gtk.MenuItem,
193                                         pack : [ 'append' ],
194                                         label : "New Project",
195                                         listeners : {
196                                             activate : function () {
197                                                 
198                                                 
199                                                 EditProject.show({
200                                                     success : function(pr) {
201                                                         LeftProjectTree.get('combo').setValue(pr.fn);
202                                                     }
203                                                 });
204                                             }
205                                         }
206                                     },
207                                     {
208                                         
209                                         
210                                         xtype : Gtk.MenuItem,
211                                         pack : [ 'append' ],
212                                         label : "Add Directory To Current Project",
213                                         listeners : {
214                                             activate : function () {
215                                                 
216                                                 var fn = LeftProjectTree.get('combo').getValue();
217                                                 if (!fn) {
218                                                     LeftProjectTree.showNoProjectSelected();
219                                                     return true;
220                                                 }
221                                                 
222                                                 
223                                                 var dc = new Gtk.FileChooserDialog({
224                                                     action : Gtk.FileChooserAction.SELECT_FOLDER,
225                                                     modal: true,
226                                                     'select-multiple' : false,
227                                                     "show-hidden" : true,
228                                                 });
229                                                 dc.add_button("Add To Project", Gtk.ResponseType.ACCEPT );
230                                                 dc.add_button("Cancel",Gtk.ResponseType.CANCEL);
231                                                 
232                                                 if (dc.run() != Gtk.ResponseType.ACCEPT) {
233                                                     
234                                                     dc.destroy();
235                                                     return;
236                                                 }
237                                                     
238                                                 //Seed.print(dc.get_filename());
239                                                 var pm  = ProjectManager;
240                                                 pm.getByFn(fn).add(dc.get_filename(), 'dir');
241                                                 dc.destroy();
242                                                 
243                                                  
244                                             }
245                                         }
246                                     },
247                                     {
248                                         
249                                         
250                                         xtype : Gtk.MenuItem,
251                                         pack : [ 'append' ],
252                                         label : "Add File To Current Project",
253                                         listeners : {
254                                             activate : function () {
255                                                 var fn = LeftProjectTree.get('combo').getValue();
256                                                 if (!fn) {
257                                                     LeftProjectTree.showNoProjectSelected();
258                                                     return true;
259                                                 }
260                                                 
261                                                 
262                                                 var dc = new Gtk.FileChooserDialog({
263                                                     action : Gtk.FileChooserAction.OPEN,
264                                                     modal: true,
265                                                     'select-multiple' : false, // later..
266                                                     "show-hidden" : true,
267                                                 });
268                                                 
269                                                 dc.add_button("Add To Project", Gtk.ResponseType.ACCEPT );
270                                                 dc.add_button("Cancel",Gtk.ResponseType.CANCEL);
271                                                 
272                                                 if (dc.run() != Gtk.ResponseType.ACCEPT) {
273                                                     
274                                                     dc.destroy();
275                                                     return;
276                                                 }
277                                                     
278                                                 //Seed.print(dc.get_filename());
279                                                 
280                                                 ProjectManager.getByFn(fn).add(dc.get_filename(), 'file');
281                                                 dc.destroy();
282                                                 
283                                                  
284                                             }
285                                         }
286                                     },
287                                     
288                                     {
289                                          
290                                         
291                                         xtype : Gtk.MenuItem,
292                                         pack : [ 'append' ],
293                                         label : 'Add Component',
294                                         listeners : {
295                                             activate : function () {
296                                                 var fn = LeftProjectTree.get('combo').getValue();
297                                                 if (!fn) {
298                                                     LeftProjectTree.showNoProjectSelected();
299                                                     return true;
300                                                 }
301                                                 
302                                                 DialogNewComponent.show({
303                                                     project : ProjectManager.getByFn(fn)
304                                                 });
305                                                 
306                                                  
307                                             }
308                                         }
309                                     }
310                                     
311                                  
312                                 ]
313                             }
314                         ]
315                         
316                         
317                     }
318                 
319                 ]
320                 
321                 
322         
323             },
324
325             {
326                 
327                 
328                 xtype: Gtk.ScrolledWindow,
329                 smooth_scroll : true,
330                 shadow_type : Gtk.ShadowType.IN,
331                  init :  function ()
332                 {
333                     XObject.prototype.init.call(this); 
334               
335                     this.el.set_policy  (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC );
336                     //set_size_request : [-1,400]
337                 },
338                 items : [        
339                     {
340                         
341                         id : 'view',  
342                         
343                         xtype : Gtk.TreeView,
344                         headers_visible :  false,
345                         enable_tree_lines :  true ,
346                         tooltip_column : 1,
347                           //  set_reorderable: [1]
348                         
349                         init :  function ()
350                         {
351                             XObject.prototype.init.call(this); 
352                             var description = new Pango.FontDescription.c_new();
353                             description.set_size(8000);
354                             this.el.modify_font(description);
355                             
356                             this.selection = this.el.get_selection();
357                             this.selection.set_mode( Gtk.SelectionMode.SINGLE);
358                         },
359                         listeners : {
360                             
361                             'cursor-changed'  : function(tv, a) { 
362                                 //select -- should save existing...
363                                 var iter = new Gtk.TreeIter();
364                                 
365                                 if (this.selection.count_selected_rows() < 1) {
366                                     //XN.get('Builder.LeftTree.model').
367                                     LeftTree.get('model').load( false);
368                                     
369                                     return;
370                                 }
371                                 var model = LeftProjectTree.get('model');
372                                 //console.log('changed');
373                                 var s = this.selection;
374                                 s.get_selected(model, iter);
375                                 value = new GObject.Value('');
376                                 model.el.get_value(iter, 2, value);
377                                 
378                                 console.log(value.value);// id..
379                                 
380                                 var file = LeftProjectTree.project.getById(value.value);
381                                 
382                                 
383                                 console.log(file);
384                                 
385                                 var LeftTopPanel        = imports.Builder.LeftTopPanel.LeftTopPanel;
386
387                                 var nb = LeftTopPanel.get('expander');
388                                 nb.el.expanded = false;
389                                 nb.listeners.activate.call(nb);
390                                 //_expander.el.set_expanded(false);
391
392                                 var ltm = LeftTree.get('model');
393                                 ltm.loadFile(file);
394                                 
395                                 return true;
396                                 
397                                 
398                             
399                             }
400                         },
401                         
402                         items  : [
403                             {
404                                 pack : ['set_model'],
405                                 
406                                 xtype : Gtk.TreeStore,
407                                 id : 'model',
408                                 init :  function ()
409                                 {
410                                     XObject.prototype.init.call(this);    
411                                     this.el.set_column_types ( 3, [
412                                             GObject.TYPE_STRING, // title 
413                                             GObject.TYPE_STRING, // tip
414                                             GObject.TYPE_STRING // id..
415                                             ] );
416                            
417                                     
418                                 },
419                                 activeIter : false, // fixme - should not use iters..
420                                 
421                                 
422                                  
423                                 
424                                 loadProject : function (pr)
425                                 {
426                                     
427                                     
428                                     this.el.clear();
429                                     if (!pr) {
430                                         return;
431                                     }
432                                     LeftProjectTree.project = pr;
433                                     this.load(pr.toTree());
434                                     LeftProjectTree.get('view').el.expand_all();
435                                     // needs more thought!!??
436                                   
437                                     
438                                     
439                                 },
440                                 
441                                 load : function(tr,iter)
442                                 {
443                                     console.dump(tr);
444                                     console.log('Project tree load: ' + tr.length);
445                                     var citer = new Gtk.TreeIter();
446                                     //this.insert(citer,iter,0);
447                                     
448                                     var _this = this;
449                                     tr.forEach(function (r) {
450                                         if (!iter) {
451                                             _this.el.append(citer);   
452                                         } else {
453                                             _this.el.insert(citer,iter,-1);
454                                         }
455                                         _this.el.set_value(citer, 0,  '' + r.getTitle() ); // title 
456                                         _this.el.set_value(citer, 1, '' + r.getTitleTip()); // tip
457                                         _this.el.set_value(citer, 2, '' + r.id ); //id
458                                         if (r.cn && r.cn.length) {
459                                             _this.load(r.cn, citer);
460                                         }
461                                         
462                                     });
463                                     
464                                 },
465                                 
466                                 
467                                 
468                                 
469                                 getValue: function (iter, col) {
470                                     var gval = new GObject.Value('');
471                                     this.el.get_value(iter, col ,gval);
472                                     return  gval.value;
473                                     
474                                     
475                                 }
476                                 
477                                 
478                                 
479                               //  this.expand_all();
480                             },
481                             
482                               
483                             {
484                                 pack : ['append_column'],
485                                 
486                                 xtype : Gtk.TreeViewColumn,
487                                 items : [
488                                     {
489                                         
490                                         xtype : Gtk.CellRendererText,
491                                         pack: [ 'pack_start']
492                                           
493                                     } 
494                                 ],
495                                      init :  function ()
496                                 {
497                                     XObject.prototype.init.call(this);    
498                             
499                                     this.el.add_attribute(this.items[0].el , 'markup', 0 );
500                                     
501                                 }
502                               
503                             }
504                             
505                        ]
506                     }
507                 ]
508                         
509             }
510         ]
511                      
512     }
513 );