]> git.llucax.com Git - z.facultad/75.10/miklolife.git/blob - demo/src/Vistas/ABMGenerico.cs
* Ahi ya va queriendo, ya se afilia el titular
[z.facultad/75.10/miklolife.git] / demo / src / Vistas / ABMGenerico.cs
1
2 using System;
3 using System.Reflection;
4 using System.Collections;
5 using Gtk;
6 using Glade;
7
8 using Controlador;
9
10 public class ABMGenerico
11 {
12         Glade.XML xml;
13         [Widget] Table tabla;
14         Type current;
15         Hashtable widgets;
16
17         public ABMGenerico (Type objectType)
18         {
19                 widgets = new Hashtable ();
20
21                 PropertyInfo[] properties = objectType.GetProperties ();
22                 current = objectType;
23                 
24                 xml = new Glade.XML (null, "abm_generico.glade", "abm_one", null);
25                 xml.Autoconnect (this);
26
27                 uint i=0;
28                 foreach( PropertyInfo mf in properties ) {
29                         Widget l = GetWidget (mf);
30                         if (l != null) {
31                                 tabla.Attach (l, 0, 1, i, i+1);
32                                 Entry e = new Entry ();
33                                 tabla.Attach (e, 1, 2, i, i+1);
34                                 l.Show ();
35                                 e.Show ();
36                                 widgets[mf.Name] = e;
37                         }
38                         i++;
39                 }
40         }
41
42         private Widget GetWidget (PropertyInfo mf)
43         {
44                 Widget w = null;
45                 if (mf.PropertyType.Equals (typeof(string)) 
46                         || mf.PropertyType.Equals (typeof(int))
47                         || mf.PropertyType.Equals (typeof(float))) {
48                         Label l = new Label (GetLabel (mf.Name) + " : ");
49                         l.Justify = Justification.Left;
50                         l.Xpad = 0;
51                         l.Xalign = 0;
52                         w = l;
53                 }
54                 return w;
55         }
56
57         private string GetLabel (string name)
58         {
59                 int i = 0;
60                 string s = (string)name.Clone ();
61                 while (i < s.Length) {
62                         if (Char.IsUpper(s[i]) && (i>0)) {
63                                 s = s.Insert (i, " ");
64                                 i++;
65                         }
66                         i++;
67                 }
68                 return s;
69         }
70
71         public void OnDialogResponse (object o, ResponseArgs args)
72         {
73                 if (args.ResponseId == ResponseType.Cancel)
74                         return; 
75         
76                 PropertyInfo[] properties = current.GetProperties ();
77
78                 System.Object output = Activator.CreateInstance (current);
79                 
80                 foreach (string key in widgets.Keys) {
81                         PropertyInfo p = current.GetProperty (key);
82                         Entry e = (Entry)widgets[key];
83                         Console.WriteLine ("{0} = {1}", key, e.Text);
84                         if (p.PropertyType.Equals (typeof(string)))
85                                 p.SetValue (output, e.Text, null);
86                         if (p.PropertyType.Equals (typeof(int)))
87                                 p.SetValue (output, Int32.Parse (e.Text), null);
88                         if (p.PropertyType.Equals (typeof(float))) {
89                                 p.SetValue (output, (float)Double.Parse (e.Text), null);
90                         }
91                 }
92
93                 Controller c = new Controller ();
94                 
95                 c.GenericSave (output);
96
97                 c.Dispose ();
98         }
99         
100         public void Run ()
101         {
102                 Dialog w = (Dialog)xml.GetWidget ("abm_one");
103                 w.Run ();
104                 w.Destroy ();
105         }
106 }
107