/
model.cs
639 lines (465 loc) · 20.4 KB
/
model.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
//using BrightIdeasSoftware;
namespace myWay
{
public partial class model : Form
{
// delegados para las llamadas desde los user control..
public delegate void functioncall(string oldName, object oa);
private event functioncall formFunctionPointer;
public delegate void functioncallField(string oldNameTable, string oldNameField, object oa);
private event functioncallField formFunctionPointerField;
public project actualProject;
protected string tableBeingEdited = "";
public model()
{
InitializeComponent();
}
private void model_Load(object sender, EventArgs e)
{
cargarTablas();
// System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
//this.BackgroundImage =
// (Bitmap)resources.GetObject("$this.BackgroundImage");
//pcb1.Image = (Bitmap)resources.GetObject("$this.database_48.png");
//fillGridTables();
}
private void cargarTablas()
{
if (actualProject != null)
{
lbTables.Items.Clear();
foreach (table item in actualProject.tables)
{
if (!item.deleted)
{
// lbTables.Items.Add(item);
ComponentFactory.Krypton.Toolkit.KryptonListItem kl = new ComponentFactory.Krypton.Toolkit.KryptonListItem();
// kl.LongText = item.Name;
kl.ShortText = item.Name;
lbTables.Items.Add(kl);
}
}
}
lbFields.Items.Clear();
}
private void cargarCampos()
{
if (lbTables.SelectedItem != null)
{
lbFields.Items.Clear();
foreach (table item in actualProject.tables)
{
if (item.Name.Equals(lbTables.SelectedItem.ToString()))
{
foreach (field campito in item.fields)
{
if (!campito.deleted)
{
//lbFields.Items.Add(item);
ComponentFactory.Krypton.Toolkit.KryptonListItem kl = new ComponentFactory.Krypton.Toolkit.KryptonListItem();
kl.LongText = campito.type.ToString();
kl.ShortText = campito.Name;
// kl.ShortText=item.TargetName;
lbFields.Items.Add(kl);
}
}
}
}
}
lbFields.SelectedIndex = 0;
}
private void cargarValidationRules()
{
lbValidationRules.Items.Clear();
foreach (table item in actualProject.tables)
{
if (!item.deleted && item.Name.Equals(lbTables.SelectedItem.ToString()))
{
foreach (field fi in item.fields)
{
if (fi.Name.Equals(lbFields.SelectedItem.ToString()))
{
foreach (validationRule vali in fi.validationRules)
{
ComponentFactory.Krypton.Toolkit.KryptonListItem kl = new ComponentFactory.Krypton.Toolkit.KryptonListItem();
kl.ShortText = vali.name;
//kl.LongText = campito.type.ToString();
// kl.ShortText=item.TargetName;
lbValidationRules.Items.Add(kl);
}
}
}
}
}
}
//private void cargarArbol()
//{
// if (actualProject != null)
// {
// // lets clean
// trActualProject.Nodes.Clear();
// foreach (table item in actualProject.tables)
// {
// tableNode parent = new tableNode(item);
// parent.ImageIndex = 4;
// parent.SelectedImageIndex = 3;
// parent.Tag = "table";
// foreach (field fi in item.fields)
// {
// fieldNode child = new fieldNode(fi );
// if (fi.isKey)
// {
// child.ImageIndex = 5;
// child.SelectedImageIndex = 5;
// }
// else
// {
// child.ImageIndex = 4;
// child.SelectedImageIndex = 4;
// }
// child.Tag = "field";
// parent.Nodes.Add(child);
// }
// trActualProject.Nodes.Add(parent);
// }
// }
//}
private void kryptonListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
selectedTables();
}
private void lbFields_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
// assign propertyGrid to this field..
foreach (table item in actualProject.tables)
{
if (!item.deleted && item.Name.Equals(lbTables.SelectedItem.ToString()))
{
foreach (field fi in item.fields)
{
if (fi.Name.Equals(lbFields.SelectedItem.ToString()))
{
propertyGrid1.SelectedObject = fi;
// now fill the validation rules
cargarValidationRules();
myWay.userControls.userControlEditField uc = new myWay.userControls.userControlEditField();
uc.txtField.Text = lbFields.SelectedItem.ToString();
uc.oldNameTable = lbTables.SelectedItem.ToString();
uc.oldNameField = lbFields.SelectedItem.ToString();
formFunctionPointerField += new functioncallField(alterField);
uc.userFunctionPointerField = formFunctionPointerField;
kp1.Controls.Clear();
kp1.Controls.Add(uc);
uc.txtField.Focus();
}
}
}
}
//// obsolete
//if (lbFields.SelectedItem is field)
//{
// field fi= new field();
// fi = (field)lbFields.SelectedItem;
// // we fill the validation rule listbox...
// lbValidationRules.Items.Clear();
// foreach (validationRule item in fi.validationRules)
// {
// lbValidationRules.Items.Add(item);
// }
//}
}
catch (Exception)
{
}
}
private void butNewTable_Click(object sender, EventArgs e)
{
// create a user control to edit the table
myWay.userControls.userControlEditTable uc = new myWay.userControls.userControlEditTable();
uc.txtTable.Text = "";
uc.butModify.Text = "Insert";
uc.oldName = "";
formFunctionPointer += new functioncall(alterTable);
uc.userFunctionPointer = formFunctionPointer;
kp1.Controls.Clear();
kp1.Controls.Add(uc);
//kp1.Controls[0].Focus();
uc.txtTable.Focus();
}
private void butDeleteTable_Click(object sender, EventArgs e)
{
foreach (table item in actualProject.tables)
{
if (item.Name.Equals(lbTables.SelectedItem.ToString()))
{
item.deleted=true;
actualProject.saveProject(Path.Combine(util.projects_dir, actualProject.name) + ".xml");
// extra clean...
kp1.Controls.Clear();
}
}
cargarTablas();
}
private void changeType(object sender, EventArgs e)
{
cargarTablas();
}
private void butNewField_Click(object sender, EventArgs e)
{
if (!lbTables.SelectedItem.ToString().Equals(""))
{
// create a user control to edit the table
myWay.userControls.userControlEditField uc = new myWay.userControls.userControlEditField();
uc.txtField.Text = "";
uc.butModifyField.Text = "Insert";
uc.oldNameTable = lbTables.SelectedItem.ToString();
uc.oldNameField = "";
formFunctionPointerField += new functioncallField(alterField);
uc.userFunctionPointerField = formFunctionPointerField;
kp1.Controls.Clear();
kp1.Controls.Add(uc);
//kp1.Controls[0].Focus();
uc.txtField.Focus();
}
//if (lbTables.SelectedItem != null)
//{
// foreach (table item in actualProject.tables)
// {
// if (item.Name.Equals(lbTables.SelectedItem.ToString()))
// {
// // lets create the field
// field campito = new field();
// campito.Name = txtField.Text;
// campito.type = field.fieldType._integer;
// campito.createdNew = true;
// item.fields.Add(campito);
// cargarCampos();
// }
// }
//}
}
private void klRelations_SelectedIndexChanged(object sender, EventArgs e)
{
// assign propertyGrid to this relation..
foreach (table item in actualProject.tables)
{
if (!item.deleted && item.Name.Equals(lbTables.SelectedItem.ToString()))
{
foreach (relation rel in item.relations)
{
if (rel.name.Equals(klRelations.SelectedItem.ToString()))
{
propertyGrid1.SelectedObject = rel;
}
}
}
}
}
private void kryptonButton2_Click(object sender, EventArgs e)
{
if (!txtValidationRule.Text.Equals(""))
{
// assign propertyGrid to this field..
foreach (table item in actualProject.tables)
{
if (!item.deleted && item.Name.Equals(lbTables.SelectedItem.ToString()))
{
foreach (field fi in item.fields)
{
if (fi.Name.Equals(lbFields.SelectedItem.ToString()))
{
bool exists = false;
// check if validation rule exists ...
foreach (validationRule vali in fi.validationRules)
{
if (vali.name == txtValidationRule.Text)
exists = true;
}
if (!exists)
{
validationRule vr = new validationRule();
vr.name = txtValidationRule.Text;
fi.validationRules.Add(vr);
}
// propertyGrid1.SelectedObject = vr;
}
}
}
}
cargarValidationRules();
}
}
private void lbValidationRules_SelectedIndexChanged(object sender, EventArgs e)
{
// assign propertyGrid to this field..
foreach (table item in actualProject.tables)
{
if (!item.deleted && item.Name.Equals(lbTables.SelectedItem.ToString()))
{
foreach (field fi in item.fields)
{
if (fi.Name.Equals(lbFields.SelectedItem.ToString()))
{
foreach (validationRule vali in fi.validationRules)
{
if (vali.name == lbValidationRules.SelectedItem.ToString())
propertyGrid1.SelectedObject = vali;
}
}
}
}
}
}
// funciones llamadas por los user control
private void alterTable(string oldName, object tablita)
{
table tab = new table();
tab = (table)tablita;
// if oldName its empty its a new table...
if (oldName.Equals(""))
{
actualProject.tables.Add(tab);
}
else
{
foreach (table item in actualProject.tables)
{
if (item.Name.Equals(oldName))
{
item.Name = tab.Name;
item.nameChanged = true;
}
}
}
actualProject.saveProject(Path.Combine(util.projects_dir, actualProject.name) + ".xml");
cargarTablas();
kp1.Controls.Clear();
}
// funciones llamadas por los user control
private void alterField(string oldNameTable, string oldNameField, object campito)
{
field cp = new field();
cp = (field)campito;
// if oldName its empty its a new field..
if (oldNameField.Equals(""))
{
foreach (table item in actualProject.tables)
{
if (item.Name.Equals(oldNameTable))
{
item.fields.Add(campito);
}
}
}
else
{
foreach (table item in actualProject.tables)
{
if (item.Name.Equals(oldNameTable))
{
foreach (field itemf in item.fields)
{
if (itemf.Name.Equals(oldNameTable))
{
itemf.Name = cp.Name;
itemf.nameChanged = true;
}
}
}
}
}
kp1.Controls.Clear();
actualProject.saveProject(Path.Combine(util.projects_dir, actualProject.name) + ".xml");
cargarCampos();
}
private void butDeleteField_Click(object sender, EventArgs e)
{
foreach (table item in actualProject.tables)
{
if (item.Name.Equals(lbTables.SelectedItem.ToString()))
{
foreach (field itemf in item.fields)
{
if (itemf.Name.Equals(lbFields.SelectedItem.ToString()))
{
itemf.deleted = true;
}
}
}
}
// extra clean...
kp1.Controls.Clear();
actualProject.saveProject(Path.Combine(util.projects_dir, actualProject.name) + ".xml");
cargarCampos();
}
private void lbTables_Click(object sender, EventArgs e)
{
selectedTables();
} // lbTables_Click
private void selectedTables()
{
try
{
//txtTable.Text = lbTables.SelectedItem.ToString();
lbFields.Items.Clear();
foreach (table item in actualProject.tables)
{
if (!item.deleted && item.Name.Equals(lbTables.SelectedItem.ToString()))
{
propertyGrid1.SelectedObject = item;
cargarCampos();
//lbFields.Items.Clear();
//foreach (field itemx in item.fields)
//{
// //lbFields.Items.Add(item);
// ComponentFactory.Krypton.Toolkit.KryptonListItem kl = new ComponentFactory.Krypton.Toolkit.KryptonListItem();
// kl.ShortText = itemx.Name;
// kl.LongText = itemx.type.ToString();
// // kl.ShortText=item.TargetName;
// lbFields.Items.Add(kl);
//}
klRelations.Items.Clear();
foreach (relation rel in item.relations)
{
ComponentFactory.Krypton.Toolkit.KryptonListItem kl = new ComponentFactory.Krypton.Toolkit.KryptonListItem();
kl.ShortText = rel.name;
//kl.LongText = rel.name.ToString();
// kl.ShortText=item.TargetName;
klRelations.Items.Add(kl);
}
}
}
// create a user control to edit the table
myWay.userControls.userControlEditTable uc = new myWay.userControls.userControlEditTable();
uc.txtTable.Text = lbTables.SelectedItem.ToString();
uc.oldName = lbTables.SelectedItem.ToString();
formFunctionPointer += new functioncall(alterTable);
uc.userFunctionPointer = formFunctionPointer;
kp1.Controls.Clear();
kp1.Controls.Add(uc);
uc.txtTable.Focus();
}
catch (Exception)
{
}
}
private void propertyGrid1_Click(object sender, EventArgs e)
{
}
private void lbTables_MouseDown(object sender, MouseEventArgs e)
{
selectedTables();
}
}
}