00001 namespace Grendel.Base { 00002 using System; 00003 using System.Reflection; 00004 00005 public class Inspector : BaseCanvasObject{ 00006 ICanvasObject target; 00007 Type targetType; 00008 00009 private static Type[] supportedTypes = new Type[]{ 00010 typeof(bool), 00011 typeof(string), 00012 typeof(int), 00013 typeof(double), 00014 typeof(Color) 00015 }; 00016 00017 public Inspector(Canvas canvas, ICanvasObject target) 00018 : base(canvas){ 00019 this.target = target; 00020 targetType = this.target.GetType(); 00021 } 00022 00023 public void MethodLauncher(){ 00024 IDialogBuilder db = Canvas.DialogBuilder; 00025 foreach(MethodInfo mi in targetType.GetMethods(BindingFlags.Instance | BindingFlags.Public)) 00026 if(mi.GetParameters().Length == 0 && !mi.IsSpecialName 00027 && mi.ReturnType == typeof(void)) { 00028 db.AddButton(mi.Name); 00029 } 00030 string methodName = db.ExecuteDialog(200, StandardButtons.Cancel); 00031 if(methodName == "Cancel" || methodName == "") 00032 return; 00033 targetType.GetMethod(methodName).Invoke(target, null); 00034 Canvas.Invalidate(); 00035 } 00036 00037 public void PropertyDialog() { 00038 IDialogBuilder db = Canvas.DialogBuilder; 00039 foreach(PropertyInfo p in targetType.GetProperties()) { 00040 if(Array.IndexOf(supportedTypes, p.PropertyType) >= 0) 00041 db.AddInput(p.Name, p.GetValue(target,null)); 00042 } 00043 string button = db.ExecuteDialog(300, StandardButtons.OK | StandardButtons.Cancel); 00044 if(button == "Cancel" || button == "") 00045 return; 00046 foreach(string key in db.Keys) { 00047 targetType.GetProperty(key).SetValue(target, db.GetValue(key), null); 00048 } 00049 Canvas.Invalidate(); 00050 } 00051 } 00052 }