Fix #7959 - new project flow - use void func to callback after each step, tidy up...
[roobuilder] / src / Builder4 / EditProject.bjs
1 {
2  "build_module" : "builder",
3  "gen_extended" : false,
4  "items" : [
5   {
6    "# WindowState? windowstate" : "null",
7    "$ Project.Callback doneObj" : "null",
8    "$ deletable" : true,
9    "$ xns" : "Gtk",
10    "@ void canceled" : "()",
11    "Project.Project result" : "null",
12    "bool modal" : true,
13    "default_width" : 600,
14    "items" : [
15     {
16      "$ xns" : "Gtk",
17      "* prop" : "titlebar",
18      "* title" : "New Project",
19      "bool show_title_buttons" : false,
20      "items" : [
21       {
22        "$ Gtk.Align halign" : "Gtk.Align.END",
23        "$ xns" : "Gtk",
24        "* pack" : "pack_start",
25        "bool hexpand" : false,
26        "label" : "Cancel",
27        "listeners" : {
28         "clicked" : [
29          "( ) => {",
30          "   ",
31          "    _this.el.hide();",
32          "\t_this.canceled();",
33          "    ",
34          "",
35          "}",
36          ""
37         ]
38        },
39        "xtype" : "Button"
40       },
41       {
42        "$ Gtk.Align halign" : "Gtk.Align.START",
43        "$ css_classes" : "{ \"suggested-action\" }",
44        "$ xns" : "Gtk",
45        "* pack" : "pack_end",
46        "bool hexpand" : false,
47        "id" : "ok_btn",
48        "label" : "OK",
49        "listeners" : {
50         "clicked" : [
51          "( ) => {",
52          "   var err_dialog = Xcls_StandardErrorDialog.singleton();",
53          "   ",
54          "   ",
55          "   \tif (_this.ptype_dd.getValue().length < 1) {",
56          "        err_dialog.show(_this.el,\"You have to set Project type\");             ",
57          "        return;",
58          "    }",
59          "   var fn = _this.parent_dd.getValue();",
60          "   var is_existing = false;",
61          "   var is_new_folder = false;",
62          "   switch (_this.type_dd.getValue()) {",
63          "\t   \tcase \"Existing Folder\":",
64          "\t\t   \tif (_this.folder_dd.getValue().length < 1) {",
65          "\t\t\t\terr_dialog.show(_this.el,\"You have to set Folder\");             ",
66          "\t\t\t\treturn;",
67          "\t\t\t}",
68          "\t\t\tfn += \"/\" + _this.folder_dd.getValue();",
69          "\t\t\tis_existing = true;",
70          "\t\t\tbreak;",
71          "\t   \t",
72          "\t   \tcase \"New Folder\":",
73          "\t\t   \tif (_this.name_entry.getValue().length < 1) {",
74          "\t\t\t\terr_dialog.show(_this.el,\"You have enter a Project Name\");             ",
75          "\t\t\t\treturn;",
76          "\t\t\t}",
77          "\t\t\tfn += \"/\" + _this.name_entry.getValue();\t   ",
78          "\t\t\t",
79          "\t\t\tif (FileUtils.test( fn ,FileTest.EXISTS)) {",
80          "\t\t\t\terr_dialog.show(_this.el,\"That folder already exists\");             ",
81          "\t\t\t\treturn;\t\t\t",
82          "\t\t\t}",
83          "\t\t\tvar dir = File.new_for_path(fn);",
84          "\t\t\ttry {",
85          "\t\t\t\tdir.make_directory();\t",
86          "\t\t\t} catch (Error e) {",
87          "\t\t\t\tGLib.error(\"Failed to make directory %s\", fn);",
88          "\t\t\t} ",
89          "\t\t\tis_new_folder = true;",
90          "\t\t\tbreak;",
91          "\t\t\t",
92          "\t   \tdefault:",
93          "\t   \t\treturn;",
94          "   \t\t",
95          "    }",
96          "   ",
97          "   ",
98          "  ",
99          "    ",
100          "    _this.el.hide();",
101          "    ",
102          "    ",
103          "",
104          "  ",
105          "    GLib.debug(\"add %s\\n\" , fn);",
106          "    try {",
107          "\t\tvar project = Project.Project.factory(_this.ptype_dd.getValue(), fn);",
108          "\t\tif (is_new_folder) {\t",
109          "\t\t\tproject.initialize();",
110          "\t\t\t",
111          "\t\t} else {",
112          "\t\t\tproject.load();",
113          "\t\t}",
114          "\t\t",
115          "\t\tproject.save();",
116          "\t\t Project.Project.saveProjectList();",
117          " \t\t_this.result = project;",
118          "\t\tif (is_new_folder || is_existing) {",
119          "\t\t\t   ",
120          "\t\t",
121          "\t    \t _this.windowstate.projectPopoverShow(",
122          "\t    \t \t_this.el.transient_for, project,  _this.doneObj ",
123          "    \t \t);",
124          "    \t } else {",
125          "    \t \tif (_this.doneObj != null) {",
126          "\t\t    \t _this.doneObj.call(project);",
127          "\t    \t }",
128          "    \t }",
129          "\t\t",
130          "\t\treturn;",
131          "\t} catch (Error e) {",
132          "\t\tGLib.debug(\"got error? %s\" , e.message);",
133          "\t}",
134          "\t ",
135          "",
136          "}",
137          ""
138         ]
139        },
140        "xtype" : "Button"
141       }
142      ],
143      "xtype" : "HeaderBar"
144     },
145     {
146      "$ homogeneous" : false,
147      "$ xns" : "Gtk",
148      "* prop" : "child",
149      "Gtk.Orientation orientation" : "Gtk.Orientation.VERTICAL",
150      "bool expand" : true,
151      "int margin_bottom" : 10,
152      "int margin_end" : 10,
153      "int margin_start" : 10,
154      "int margin_top" : 10,
155      "items" : [
156       {
157        "$ xns" : "Gtk",
158        "* columns" : 2,
159        "int column_spacing" : 4,
160        "int margin_bottom" : 20,
161        "int row_spacing" : 4,
162        "items" : [
163         {
164          "$ Gtk.Align halign" : "Gtk.Align.START",
165          "$ xns" : "Gtk",
166          "id" : "type_lbl",
167          "string label" : "Create a Project from:",
168          "xtype" : "Label"
169         },
170         {
171          "$ uint selected" : "Gtk.INVALID_LIST_POSITION",
172          "$ xns" : "Gtk",
173          "* init" : [
174           "{",
175           "",
176           "}",
177           ""
178          ],
179          "bool hexpand" : true,
180          "id" : "type_dd",
181          "items" : [
182           {
183            "$ xns" : "Gtk",
184            "* prop" : "model",
185            "strings" : "{  \"New Folder\", \"Existing Folder\" /*,  \"Checkout from git\"  */ }",
186            "xtype" : "StringList"
187           }
188          ],
189          "listeners" : {
190           "notify[\"selected\"]" : [
191            "( ) => {",
192            "\t ",
193            "\tif (this.el.selected == Gtk.INVALID_LIST_POSITION) {",
194            "\t\treturn;",
195            "\t}",
196            "\tvar m = (Gtk.StringList) this.el.model;",
197            "\tGLib.debug(\"selected item: %s\", m.get_string(this.el.selected));",
198            "\t",
199            "\t_this.hideAll();",
200            "\t_this.parent_lbl.el.show();",
201            "\t_this.parent_dd.el.show();",
202            "\t_this.parent_dd.load();",
203            "\t/*",
204            "\t\t\t break;",
205            " \t",
206            " \tswitch (m.get_string(this.el.selected)) {",
207            "\t\tcase \"New Folder\":",
208            "\t\t    _this.name_lbl.el.show();          ",
209            "     \t\t_this.name_entry.el.show();     ",
210            "\t\t\t break;",
211            "\t\tcase \"Existing Folder\":",
212            "\t\t\t_this.folder_lbl.el.show();",
213            "\t\t\t _this.folder_dd.el.show();     \t",
214            "\t\t\t break;",
215            "\t\t\t ",
216            "\t\tcase \"Checkout from git\":",
217            "\t\t    _this.name_lbl.el.show();          ",
218            "\t\t\t_this.name_entry.el.show();  ",
219            "\t\t\tbreak;   ",
220            "\t\tdefault:",
221            "\t\t\t_this.hideAll();",
222            "\t\t\tbreak;",
223            "",
224            "\t}\t",
225            "\t",
226            "\t*/",
227            "\t ",
228            " ",
229            "              ",
230            "          ",
231            "    ",
232            "\t\t",
233            "} ",
234            ""
235           ]
236          },
237          "xtype" : "DropDown",
238          "| string getValue" : [
239           "() {",
240           "\tvar m = (Gtk.StringList) this.el.model;",
241           "\treturn  m.get_string(this.el.selected);",
242           "\t",
243           "}"
244          ]
245         },
246         {
247          "$ Gtk.Align halign" : "Gtk.Align.START",
248          "$ xns" : "Gtk",
249          "id" : "parent_lbl",
250          "string label" : "In Folder:",
251          "xtype" : "Label"
252         },
253         {
254          "$ xns" : "Gtk",
255          "bool hexpand" : true,
256          "id" : "parent_dd",
257          "items" : [
258           {
259            "$ xns" : "Gtk",
260            "* prop" : "model",
261            "strings" : "{ \"gitlive\", \"Projects\", \"Select\" }",
262            "xtype" : "StringList"
263           }
264          ],
265          "listeners" : {
266           "notify[\"selected\"]" : [
267            "( ) => {",
268            "\tif (this.el.selected == Gtk.INVALID_LIST_POSITION) {",
269            "\t\t_this.hideAll();",
270            "\t\t_this.parent_lbl.el.show();",
271            "     \t_this.parent_dd.el.show();   ",
272            " \t",
273            "\t\treturn;",
274            "\t}",
275            "\t",
276            "\t",
277            "\t",
278            "\tif (this.getValue() == \"Select Folder\") {",
279            "\t\tvar fd = new Gtk.FileDialog();",
280            "\t\tfd.title = \"Select Folder\";",
281            "\t\tfd.modal = true;",
282            "\t\t",
283            "\t\tfd.select_folder.begin(_this.el, null, (obj, res) => {",
284            "\t\t\ttry {",
285            "\t\t \tvar f = fd.select_folder.end(res);",
286            "\t\t\t\tthis.extra_value = f.get_path();",
287            "\t\t\t\tvar sl = (Gtk.StringList) this.el.model;\t",
288            "\t\t\t\t",
289            "\t\t\t\tsl.remove(sl.get_n_items()-1);",
290            "\t\t\t\t",
291            "\t\t\t\tsl.append(this.extra_value);",
292            "\t\t\t\tsl.append(\"Select Folder\");",
293            "\t\t\t\tthis.el.selected = sl.get_n_items()-2;",
294            "\t\t\t} catch (GLib.Error e) {",
295            "\t\t\t\t// do nothing?",
296            "\t\t\t}",
297            "\t\t\t",
298            "\t\t});",
299            "\t\treturn;",
300            "",
301            "\t}",
302            "\t_this.hideAll();",
303            "\t_this.parent_lbl.el.show();",
304            " \t_this.parent_dd.el.show();   ",
305            " \t",
306            "\t",
307            "   _this.ptype_lbl.el.show();          ",
308            "   _this.ptype_dd.el.show();  ",
309            "\t// folder selected...",
310            "\tswitch(_this.type_dd.getValue()) {",
311            "\t\tcase \"New Folder\":",
312            "\t\t   _this.name_lbl.el.show();          ",
313            "    \t   _this.name_entry.el.show(); ",
314            "    \t   _this.name_entry.el.text = \"\";",
315            "\t\t   _this.ptype_lbl.el.show();          ",
316            "\t\t   _this.ptype_dd.el.show();  ",
317            "    \t   break;",
318            "    \t   ",
319            "\t\tcase \"Existing Folder\":",
320            "\t\t\t_this.folder_lbl.el.show();",
321            "\t\t \t_this.folder_dd.el.show();",
322            "\t\t \t_this.folder_dd.load();",
323            "\t\t   ",
324            "\t\t \tbreak;",
325            "\t\t \t",
326            "\t\tcase \"Checkout from git\":",
327            "\t\t   _this.name_lbl.el.show();          ",
328            "    \t   _this.name_entry.el.show(); ",
329            "    \t   _this.name_lbl.el.label= \"not yet\";",
330            "    \t   _this.name_entry.el.text = \"this is not supported yet\";",
331            "\t\t\tbreak;",
332            "\t\tdefault:",
333            "\t\t\tbreak;",
334            "\t}",
335            "    ",
336            "\t",
337            "",
338            "}",
339            ""
340           ]
341          },
342          "string extra_value" : "",
343          "xtype" : "DropDown",
344          "| string getValue" : [
345           "() {",
346           "\tvar m = (Gtk.StringList) this.el.model;",
347           "\treturn  m.get_string(this.el.selected);",
348           "\t",
349           "}"
350          ],
351          "| void load" : [
352           "() {",
353           "",
354           "\tvar sl = (Gtk.StringList) this.el.model;\t",
355           "\tvar hd = GLib.Environment.get_home_dir();",
356           "\twhile(sl.get_n_items() > 0)  {",
357           "\t\tsl.remove(0);",
358           "\t}",
359           "\tif (FileUtils.test(hd + \"/gitlive\" ,FileTest.IS_DIR)) {",
360           "\t\tsl.append(hd + \"/gitlive\");",
361           "\t}",
362           "\tif (FileUtils.test(hd + \"/Projects\" ,FileTest.IS_DIR)) {",
363           "\t\tsl.append(hd + \"/Projects\");",
364           "\t}",
365           "\tif (this.extra_value != \"\" && FileUtils.test(this.extra_value ,FileTest.IS_DIR)) {",
366           "\t\tsl.append(this.extra_value);",
367           "\t}",
368           "\t",
369           "\tsl.append(\"Select Folder\");",
370           "\tthis.el.selected = Gtk.INVALID_LIST_POSITION;",
371           "\t",
372           "}"
373          ]
374         },
375         {
376          "$ Gtk.Align halign" : "Gtk.Align.START",
377          "$ xns" : "Gtk",
378          "id" : "folder_lbl",
379          "string label" : "Add Folder",
380          "xtype" : "Label"
381         },
382         {
383          "$ xns" : "Gtk",
384          "bool hexpand" : true,
385          "id" : "folder_dd",
386          "items" : [
387           {
388            "$ xns" : "Gtk",
389            "* prop" : "model",
390            "strings" : "{ \"gitlive\", \"Projects\", \"Select\" }",
391            "xtype" : "StringList"
392           }
393          ],
394          "listeners" : {
395           "notify[\"selected\"]" : [
396            "() => {",
397            "\tvar fn = this.getValue();",
398            "\tif (fn == \"\") {",
399            "\t\treturn;",
400            "\t}",
401            "\tvar p  = _this.parent_dd.getValue();",
402            "\tif (!FileUtils.test(p  + \"/\" + fn + \"/.roobuilder.jcfg\"  , GLib.FileTest.EXISTS)) {",
403            "\t\treturn;",
404            "\t}",
405            "\tvar ty = Project.Project.peekProjectType(p  + \"/\" + fn + \"/.roobuilder.jcfg\" );",
406            "\tif (ty == \"\") {",
407            "\t\treturn;",
408            "\t}",
409            "\t_this.ptype_dd.setValue(ty);",
410            "\t ",
411            " }",
412            " ",
413            " "
414           ]
415          },
416          "xtype" : "DropDown",
417          "| string getValue" : [
418           "() {",
419           "\tvar m = (Gtk.StringList) this.el.model;",
420           "\treturn this.el.selected  == Gtk.INVALID_LIST_POSITION ?",
421           "\t\t\t \"\" : m.get_string(this.el.selected);",
422           "\t",
423           "}"
424          ],
425          "| void load" : [
426           "() {",
427           "\tvar p  = _this.parent_dd.getValue();",
428           "\tvar f = File.new_for_path(p);",
429           "\tvar sl = (Gtk.StringList) this.el.model;\t",
430           "\twhile(sl.get_n_items() > 0)  {",
431           "\t\tsl.remove(0);",
432           "\t}",
433           "\tvar gstr = new Gee.ArrayList<string>();",
434           "\t",
435           "\t",
436           "\ttry {",
437           "\t\tvar file_enum = f.enumerate_children(",
438           "\t\t\tGLib.FileAttribute.STANDARD_DISPLAY_NAME, ",
439           "\t\t\tGLib.FileQueryInfoFlags.NONE, null);",
440           "\t  ",
441           " \t",
442           "\t\t",
443           "\t\tFileInfo next_file; ",
444           "\t\t ",
445           "\t\twhile ((next_file = file_enum.next_file(null)) != null) {",
446           "\t\t\tvar fn = next_file.get_display_name();",
447           "\t\t ",
448           "\t\t\tif (fn[0] == '.') { // skip hidden",
449           "\t\t\t\tcontinue;",
450           "\t\t\t}",
451           "\t\t\t ",
452           "\t\t\tif (!FileUtils.test(p  + \"/\" + fn, GLib.FileTest.IS_DIR)) {",
453           "\t\t\t\tcontinue;",
454           "\t\t\t}",
455           "\t\t\tif (null != Project.Project.getProjectByPath(p  + \"/\" + fn)) {",
456           "\t\t\t\tcontinue;",
457           "\t\t\t}",
458           "\t\t",
459           "\t\t\tgstr.add(fn);",
460           "\t\t}",
461           "\t} catch (GLib.Error e) {",
462           "\t\t// do nothing.. ",
463           "\t}",
464           "\tgstr.sort((a,b) => {",
465           "\t\treturn Posix.strcmp(a.down(), b.down());",
466           "\t});",
467           "",
468           "\tforeach(var str in gstr) {",
469           "\t\tsl.append(str);",
470           "\t}",
471           "\t ",
472           "}"
473          ]
474         },
475         {
476          "$ Gtk.Align halign" : "Gtk.Align.START",
477          "$ xns" : "Gtk",
478          "id" : "name_lbl",
479          "string label" : "Named (New Folder Name)",
480          "xtype" : "Label"
481         },
482         {
483          "$ xns" : "Gtk",
484          "bool hexpand" : true,
485          "id" : "name_entry",
486          "xtype" : "Entry",
487          "| string getValue" : [
488           "() {",
489           "\treturn this.el.text;",
490           "}"
491          ]
492         },
493         {
494          "$ Gtk.Align halign" : "Gtk.Align.START",
495          "$ xns" : "Gtk",
496          "id" : "ptype_lbl",
497          "label" : "Project type :",
498          "xtype" : "Label"
499         },
500         {
501          "$ xns" : "Gtk",
502          "bool hexpand" : true,
503          "id" : "ptype_dd",
504          "items" : [
505           {
506            "$ xns" : "Gtk",
507            "* prop" : "model",
508            "strings" : [
509             "{  ",
510             "\t\"Gtk - A Gtk / Vala Desktop project\", ",
511             "\t\"Roo - A Web  Javascript Project using the Roo Library\"",
512             "\t/*,  \"Flutter\" */ ",
513             "}"
514            ],
515            "xtype" : "StringList"
516           }
517          ],
518          "listeners" : {
519           "notify[\"selected\"]" : [
520            "( ) => {",
521            "",
522            "\t_this.ok_btn.el.hide();\t",
523            "\tif (this.getValue() != \"\") {",
524            "\t   _this.ok_btn.el.show();",
525            "\t}",
526            "",
527            "}",
528            ""
529           ]
530          },
531          "xtype" : "DropDown",
532          "| string getValue" : [
533           "() {",
534           "\tvar m = (Gtk.StringList) this.el.model;",
535           "\tvar str = this.el.selected == Gtk.INVALID_LIST_POSITION ?",
536           "\t\t\t \"\" : m.get_string(this.el.selected);",
537           "\t\t\t ",
538           " \tvar ar = str.split(\" \");",
539           " \treturn ar[0];",
540           "\t",
541           "}"
542          ],
543          "| void setValue" : [
544           "(string val) {",
545           "\tvar m = (Gtk.StringList) this.el.model;",
546           "\tfor(var i = 0; i < m.get_n_items();i++) {",
547           "\t\tif (m.get_string(i) == val) {",
548           "\t\t\tthis.el.selected = i;",
549           "\t\t\tbreak;",
550           "\t\t}",
551           "\t}",
552           "\tthis.el.selected = Gtk.INVALID_LIST_POSITION;",
553           "",
554           "}"
555          ]
556         }
557        ],
558        "xtype" : "Grid"
559       }
560      ],
561      "xtype" : "Box"
562     }
563    ],
564    "name" : "EditProject",
565    "string title" : "New Project",
566    "xtype" : "Window",
567    "| void hideAll" : [
568     "() {",
569     " \t_this.parent_lbl.el.hide();",
570     "     _this.parent_dd.el.hide();   ",
571     " \t",
572     " \t_this.folder_lbl.el.hide();",
573     "     _this.folder_dd.el.hide();     ",
574     "     _this.name_lbl.el.hide();          ",
575     "     _this.name_entry.el.hide();     ",
576     "     _this.ptype_lbl.el.hide();          ",
577     "     _this.ptype_dd.el.hide();          ",
578     "      _this.ok_btn.el.hide();   ",
579     "    ",
580     "}"
581    ],
582    "| void show" : [
583     "(Project.Callback doneObj) {",
584     "     this.doneObj= doneObj;",
585     "    _this.hideAll(); ",
586     "     // hide stuff..",
587     "     _this.type_dd.el.selected = Gtk.INVALID_LIST_POSITION;",
588     "     _this.folder_dd.el.selected = Gtk.INVALID_LIST_POSITION;",
589     "     _this.ptype_dd.el.selected = Gtk.INVALID_LIST_POSITION;",
590     "     _this.parent_dd.extra_value = \"\";",
591     "    //[ 'xtype'  ].forEach(function(k) {",
592     "    //    _this.get(k).setValue(typeof(c[k]) == 'undefined' ? '' : c[k]);",
593     "    //});",
594     "\t// shouild set path..",
595     "    ",
596     "    this.el.show();",
597     "    ",
598     "}"
599    ]
600   }
601  ],
602  "name" : "EditProject"
603 }