src/Builder4/WindowState.vala
[app.Builder.js] / src / Builder4 / WindowState.vala
1 /**
2  * as state management is a bit too complicated inside the builder
3  * it's better to seperate this into this class
4  * 
5  * This class has references to all the Class instances that make up the window..
6  * 
7  */
8 public class WindowState : Object 
9 {
10     public MainWindow win;
11
12     public enum State {
13         PREVIEW,
14         OBJECT,
15         PROP,
16         LISTENER,
17         CODE
18     };
19
20     public State state;
21
22     
23     public Xcls_WindowLeftTree  left_tree;
24     public Xcls_WindowAddProp   add_props;
25     public Xcls_LeftProps       left_props;
26     public Xcls_ProjectSettings projectsettings;
27     public ValaProjectSettings  vala_projectsettings;
28     public Xcls_RightPalete     rightpalete;
29     public Editor               code_editor;    
30     public Xcls_WindowRooView   window_rooview;
31     public Xcls_GtkView         window_gladeview;
32     public Xcls_DialogNewComponent new_file_dialog;     
33
34     // ctor 
35     public WindowState(MainWindow win)
36     {
37         this.win = win;
38         // initialize
39
40         // left elements..
41         this.leftTreeInit();
42         this.propsListInit();
43
44         // on clutter space...
45         this.projectEditInit();
46         this.codeEditInit();
47         this.fileViewInit();
48         
49         // adding stuff
50         this.objectAddInit();
51         this.propsAddInit();
52      
53         
54         // previews...
55         this.gtkViewInit();
56         this.webkitViewInit();
57
58         // dialogs
59
60         this.fileNewInit();
61     }
62
63
64     // left tree
65
66     public void leftTreeInit()
67     {
68      
69         this.left_tree = new Xcls_WindowLeftTree();
70         this.left_tree.ref();
71         this.left_tree.main_window = _this.win;
72     
73         this.win.tree.el.pack_start(this.left_tree.el,true, true,0);
74         this.left_tree.el.show_all();
75            
76         this.left_tree.before_node_change.connect(() => {
77             return this.leftTreeBeforeChange();
78
79         });
80
81         this.left_tree.node_selected.connect((sel) => {
82             this.leftTreeNodeSelected(sel);
83         });
84      
85         this.left_tree.changed.connect(() => {
86             this.window_rooview.requestRedraw();
87             this.left_tree.model.file.save();
88         });
89          
90     }
91
92     public bool leftTreeBeforeChange(JsRender.Node? sel)
93     {
94         if (this.state != "codeedit") {
95             this.left_props.finish_editing();
96             return true;
97         }
98         if (!this.code_editor.saveContents()) {
99             return false;
100         }
101         return false;
102     }
103     
104     public void leftTreeNodeSelected(JsRender.Node? sel)
105     {
106
107         print("node_selected called %s\n", (sel == null) ? "NULL" : "a value");
108
109         if (sel == null) {
110             this.left_props.el.hide();
111         } 
112         this.left_props.el.show();
113         this.left_props.load(this.left_tree.getActiveFile(), sel);
114         switch (this.state) {
115             
116             case State.OBJECT: 
117                   
118                  if (sel == null) {
119                     this.rightpalete.clear();
120                     break;
121                 }
122                 this.rightpalete.load(this.left_tree.getActiveFile().palete(), sel.fqn());
123                 break;
124                  
125         
126            case State.PROP:
127                 if (sel == null) {
128                     this.add_props.clear();
129                     break;
130                 }
131                 this.add_props.show(this.left_tree.getActiveFile().palete(), "props", sel.fqn());
132                 break;
133
134             case State.LISTENER:
135                
136                 if (sel == null) {
137                     this.add_props.clear();
138                     break;
139                 }
140                 this.add_props.show(_this.left_tree.getActiveFile().palete(), "signals", sel.fqn());
141                 break;
142                 
143             case State.CODEEDIT:
144             // SAVE FIRST???
145         
146             this.codeEditHide();
147             break;
148                
149                             
150         }
151          
152
153     }
154
155
156
157
158     public void propsListInit()
159     {
160     
161         this.left_props =new Xcls_LeftProps();
162         this.left_props.ref();
163         this.left_props.main_window = _this;
164         this.win.props.el.pack_start(this.left_props.el,true, true,0);
165         this.left_props.el.show_all();
166     
167         this.left_props.show_editor.connect( (file, node, type,  key) => {
168             this.codeEditShow(file, node, type,  key);
169         });
170
171     
172         this.left_props.stop_editor.connect( () => {
173             if (this.state != "codeedit") {
174                 return true;
175             }
176     
177             var ret =  this.code_editor.saveContents();
178             if (!ret) {
179                 return false;
180             }
181             this.codeEditHide();
182             return ret;
183         });
184     
185         this.left_props.changed.connect(() => {
186               if (this.left_tree.getActiveFile().xtype == "Roo" ) {
187                    this.window_rooview.requestRedraw();
188                    
189                } else {
190                   this.window_gladeview.loadFile(this.left_tree.getActiveFile());
191               }
192               this.left_tree.model.updateSelected();
193               this.left_tree.model.file.save();
194         });
195     
196
197
198     }
199
200     //-------------  projects edit
201
202     public void projectEditInit()
203     {
204         this.projectsettings  =new Xcls_ProjectSettings();
205         this.projectsettings.ref();  /// really?
206     
207         this.vala_projectsettings  =new ValaProjectSettings();
208         this.vala_projectsettings.ref();
209         this.vala_projectsettings.window = this;
210     
211         ((Gtk.Container)(this.win.projecteditview.el.get_widget())).add(this.projectsettings.el);
212         //this.projectsettings.el.show_all();
213
214         var stage = this.win.projecteditview.el.get_stage();
215         stage.set_background_color(  Clutter.Color.from_string("#000"));
216     
217         this.projectsettings.buttonPressed.connect((btn) => {
218              if (this.left_tree.getActiveFile().xtype == "Roo" ) {
219                 if (btn == "save") {
220                     this.window_rooview.view.renderJS(true);
221                 }
222                 if (btn == "apply") {
223                     this.window_rooview.view.renderJS(true);
224                     return;
225                 }
226             } else {
227                 // do nothing for gtk..
228             }
229             if (btn == "save" || btn == "apply") {
230                 this.win.project.save();
231          
232             }
233             
234             this.projectEditHide();
235              
236          });
237
238     }
239     // ----------- object adding
240     public void objectAddInit()
241     {
242
243         this.rightpalete  = new Xcls_RightPalete();
244         this.rightpalete.ref();  /// really?
245         ((Gtk.Container)(this.win.objectview.el.get_widget())).add(this.rightpalete.el);
246         //this.projectsettings.el.show_all();
247
248         stage = _this.win.objectview.el.get_stage();
249         stage.set_background_color(  Clutter.Color.from_string("#000"));
250            
251     }
252     
253     // -----------  properties adding list...
254     // listener uses the properties 
255     public void propsAddInit()
256     {
257     // Add properties
258         this.add_props  = new Xcls_WindowAddProp();
259         this.add_props.ref();  /// really?
260         ((Gtk.Container)(this.win.addpropsview.el.get_widget())).add(this.add_props.el);
261         //this.projectsettings.el.show_all();
262
263         var  stage = _this.win.addpropsview.el.get_stage();
264         stage.set_background_color(  Clutter.Color.from_string("#000"));
265
266
267         this.add_props.select.connect( (key,type,skel, etype) => {
268             this.left_props.addProp(etype, key, skel, type);
269         });
270
271     }
272     public void propsAddShow()
273     {
274
275     }
276     public void propsAddHide()
277     {
278     
279     }
280
281
282
283     
284     // ----------- Add / Edit listener
285     // listener uses the properties 
286     //public void listenerInit()     { }
287     public void listenerShow()
288     {
289
290     }
291     public void listenerHide()
292     {
293     
294     }
295
296     // -------------- codeEditor
297
298     public void codeEditInit()
299     {
300         this.code_editor  = new  Editor();
301         this.code_editor.ref();  /// really?
302         ((Gtk.Container)(this.win.codeeditview.el.get_widget())).add(this.code_editor.el);
303         //this.projectsettings.el.show_all();
304
305         stage = _this.win.codeeditview.el.get_stage();
306         stage.set_background_color(  Clutter.Color.from_string("#000"));
307         // editor.save...
308
309         this.code_editor.save.connect( () => {
310              this.left_tree.model.file.save();
311              this.left_tree.model.updateSelected();
312         });
313         
314     }
315
316     // ----------- file view
317
318     public void fileViewInit()
319     {
320         stage = _this.rooview.el.get_stage(); \\ seems odd... 
321         this.clutterfiles = new Xcls_ClutterFiles();
322         this.clutterfiles.ref();
323         stage.add_child(this.clutterfiles.el);
324         this.clutterfiles.el.show_all();
325
326
327         this.clutterfiles.open.connect((file) => { 
328             this.fileViewOpen(file);
329         });
330
331     }
332     public void fileNewInit()
333     {
334         this.new_file_dialog = new Xcls_DialogNewComponent();
335         // force it modal to the main window..
336         this.new_file_dialog.el.set_transient_for(this.el);
337         this.new_file_dialog.el.set_modal(true);
338     
339         this.new_file_dialog.success.connect((project,file) =>
340         {
341             this.fileViewOpen(file);
342         });
343
344     }
345
346     
347     public void fileViewOpen(JsRender.JsRender file)
348     {
349         this.win.project = file.project;
350         this.previewShow();
351             this.left_tree.model.loadFile(file);
352     
353         var ctr= ((Gtk.Container)(this.win.rooview.el.get_widget()));
354         var ctr_p= ((Gtk.Container)(this.win.projecteditview.el.get_widget()));
355     
356         if (file.xtype == "Roo" ) { 
357             ctr.foreach( (w) => { ctr.remove(w); });
358             ctr_p.foreach( (w) => { ctr_p.remove(w); });
359             ctr.add(this.window_rooview.el);
360             ctr_p.add(this.projectsettings.el);            
361             this.window_rooview.loadFile(file);
362             this.window_rooview.el.show_all();
363             this.projectsettings.el.show_all();            
364
365         } else {
366             ctr.foreach( (w) => { ctr.remove(w); });
367             ctr_p.foreach( (w) => { ctr_p.remove(w); });            
368             ctr.add(this.window_gladeview.el);
369             ctr_p.add(this.vala_projectsettings.el);
370             this.window_gladeview.loadFile(file);
371             this.window_gladeview.el.show_all();
372             this.vala_projectsettings.el.show_all();
373         }
374         print("OPEN : " + file.name);
375         this.editpane.el.set_position(_this.editpane.el.max_position);
376         this.win.setTitle(file.project.name + " : " +file.name);
377              
378
379         }
380
381     
382     // ---------  webkit view
383     public void webkitViewInit()
384     {
385         this.window_rooview  =new Xcls_WindowRooView();
386         this.window_rooview.ref();
387         ((Gtk.Container)(this.win.rooview.el.get_widget())).add(this.window_rooview.el);
388         this.window_rooview.el.show_all();
389
390         stage = this.win.rooview.el.get_stage();
391         stage.set_background_color(  Clutter.Color.from_string("#000"));
392     }
393
394     // ------ Gtk  - view
395
396     public void gtkViewInit()
397     {
398         this.window_gladeview  =new Xcls_GtkView();
399         this.window_gladeview.ref();
400     }
401
402     // -- buttons show hide.....
403
404     public void buttonsShowHide()
405     {
406         // basically hide everything, then show the relivant..
407
408          this.win.backbutton.el.hide();
409     
410         this.win.projectbutton.el.hide(); // show file nav...
411         this.win.editfilebutton.el.hide();
412         this.win.projecteditbutton.el.hide();
413          
414         
415         this.win.objectshowbutton.el.hide(); // add objects
416         this.win.addpropbutton.el.hide();  
417         this.win.addlistenerbutton.el.hide(); 
418
419     
420     
421         this.win.addprojectbutton.el.hide();
422         this.win.addfilebutton.el.hide();
423         this.win.delprojectbutton.el.hide();
424         this.win.new_window.el.hide();
425
426         
427         switch (this.state) {
428             
429             case State.PREVIEW:  // this is the default state when working...
430                
431                 this.win.projectbutton.el.show(); // show file nav...
432                 this.win.editfilebutton.el.show();
433                 this.win.projecteditbutton.el.show();
434                  
435                 
436                 this.win.objectshowbutton.el.show(); // add objects
437                 this.win.addpropbutton.el.show();  
438                 this.win.addlistenerbutton.el.show(); 
439                 break;
440             
441            
442             case State.CODE: 
443             case State.PROP:
444             case State.LISTENER:
445             case State.OBJECT
446                 
447                 this.win.backbutton.el.show();
448                 this.win.objectshowbutton.el.show(); // add objects
449                 this.win.addpropbutton.el.show();  
450                 this.win.addlistenerbutton.el.show(); 
451                 
452                 
453         
454
455     }
456
457
458     
459
460     
461 }
462
463