/// <summary> /// Utility function - this should be moved to a factory /// </summary> public static bool Save(ViewActionMap data, string filename, bool overwrite) { var exists = File.Exists(filename); if (exists) { if (overwrite) { File.Delete(filename); } else { return(false); } } XmlSerializer xmlSerializer = new XmlSerializer(typeof(ViewActionMap)); using (StreamWriter writer = System.IO.File.CreateText(filename)) { xmlSerializer.Serialize(writer, data); } return(true); }
/// <summary> /// Exprimental /// </summary> private static void CreateNewActionConfig(string name) { var path = System.IO.Path.Combine(System.Environment.CurrentDirectory, name); var config = new ViewActionMap(true); //save the config ViewActionMap.Save(config, path, true); }
/// <summary> /// Loads a generic file for all classes in project /// </summary> private static ViewActionMap GetDefaultViewActionMap() { var path = Path.Combine(System.Environment.CurrentDirectory, "default.mvcmap"); if (File.Exists(path)) { ViewActionMap result = ViewActionMap.Load(path); return(result); } else { return(new ViewActionMap(true)); //fallback } }
/// <summary> /// Loads a specific file /// </summary> private static ViewActionMap GetNamedViewActionMap(string className, bool useDefaltIfNotFound) { var path = Path.Combine(System.Environment.CurrentDirectory, String.Format("{0}.mvcmap", className)); if (File.Exists(path)) { ViewActionMap result = ViewActionMap.Load(path); return(result); } else if (useDefaltIfNotFound) { return(GetDefaultViewActionMap()); } else { throw new Exception("The mvcmap file specified was not found"); } }
/// <summary> /// Utility function - this should be moved to a factory /// </summary> public static ViewActionMap Load(string filename) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(ViewActionMap)); ViewActionMap data = null; try { using (var reader = new StreamReader(new FileStream(filename, FileMode.Open, FileAccess.Read))) { data = (ViewActionMap)xmlSerializer.Deserialize(reader); } } catch (Exception ex) //should specialise this. { System.Diagnostics.Debug.WriteLine(ex.Message); data = null; //just to be sure } return(data); }
/// <summary> /// Utility function - this should be moved to a factory /// </summary> public static bool Save(ViewActionMap data, string filename, bool overwrite) { var path = Path.GetFullPath(filename); //this *should* convert to a long path, except the file acces methods mess this up still... if (File.Exists(path)) { //we bail if we have the "don't overwrite" bit set if (!overwrite) { return(false); } else { using (var file = System.IO.File.OpenWrite(path)) { file.SetLength(0); //wipe out the file contents var xmlSerializer = new XmlSerializer(typeof(ViewActionMap)); using (StreamWriter writer = new StreamWriter(file)) { xmlSerializer.Serialize(writer, data); } file.Close(); //avoid locking etc } } } else { var xmlSerializer = new XmlSerializer(typeof(ViewActionMap)); using (StreamWriter writer = System.IO.File.CreateText(filename)) { xmlSerializer.Serialize(writer, data); } } return(true); }
/// <summary> /// This is going to initially be fairly slow /// </summary> private static void AddMappedActions(string viewName, KeyValuePair <string, Type> control, StringBuilder hook, StringBuilder action, CompilerFlags flags, ViewActionMap map) { var added = new List <String>(); //tally, to avoid double adds //we first add the generic stuff foreach (var ev in map.GlobalMap) { if (!added.Contains(ev.EventName)) { //does the event exist? var evi = control.Value.GetEvent(ev.EventName); if (evi == null) { continue; } else { AddAction(viewName, control.Key, ev.EventName, ev.EventArgsName, hook, action, flags); added.Add(ev.EventName); } } } //IEnumerable<ViewControlAction> controlMapItems = map.ControlActionMap.F(ev => ev.ControlType == control.Value.Name); var controlMapItems = from item in map.ControlActionMap where (item.ControlType == control.Value.Name || item.ControlType == control.Value.FullName) //this seems like a good idea, probably a bug since we extended the controlmaps to use the full name for non System.Windows.Forms controls....? select item; if (controlMapItems == null) { return; } else { foreach (var controlMap in controlMapItems) { if (controlMap != null) { foreach (var ev in controlMap.ControlActions) { //this has to be here if (!added.Contains(ev.EventName)) { AddAction(viewName, control.Key, ev.EventName, ev.EventArgsName, hook, action, flags); added.Add(ev.EventName); } } } } } }
/// <summary> /// /// </summary> /// <param name="tree"></param> /// <param name="isAbstract"></param> /// <returns></returns> public static string ClassGenerator(ControlTree tree, CompilerFlags flags) { string prefix = (flags.IsAbstract ? ABSTRACT_PREFIX : String.Empty); StringBuilder code = new StringBuilder(); code.AppendLine("/*Auto generated - this code was generated by the MvcFramework compiler, created by RatCow Soft - \r\n See http://code.google.com/p/ratcowsoftopensource/ */ \r\n\r\nusing System; \r\nusing System.Windows.Forms;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing System.Reflection;\r\n\r\n//3rd Party\r\nusing RatCow.MvcFramework;\r\n"); code.AppendFormat("namespace {0}\r\n", tree.NamespaceName); code.AppendLine("{"); StringBuilder code_s1 = new StringBuilder(); if (flags.CreateEmptyNonDesignedFile) { //if we are creating a stub, we should put the inheritence in the stub, to allow it to be altered (e.g. modal) code_s1.AppendFormat("\tinternal partial class {1}{0}Controller\r\n", tree.ClassName, prefix); } else { code_s1.AppendFormat("\tinternal partial class {1}{0}Controller: BaseController<{0}>\r\n", tree.ClassName, prefix); } code_s1.AppendLine("\t{"); //constructor code_s1.AppendFormat("\t\tpublic {1}{0}Controller() : base()\r\n", tree.ClassName, prefix); code_s1.AppendLine("\t\t{\r\n\t\t}\r\n"); StringBuilder code_s2 = new StringBuilder(); code_s2.AppendLine("\r\n#region GUI glue code\r\n"); code_s2.AppendFormat("\tpartial class {1}{0}Controller\r\n", tree.ClassName, prefix); code_s2.AppendLine("\t{"); if (flags.RestrictActions) { //load the actions file ViewActionMap map = (flags.UseDefaultActionsFile ? GetDefaultViewActionMap() : GetNamedViewActionMap(tree.ClassName, true)); //we now have access to the controls foreach (var control in tree.Controls) { //System.Console.WriteLine(" var {0} : {1} ", control.Name, control.GetType().Name); //add the declaration to code_s2 code_s2.AppendFormat("\t\t[Outlet(\"{1}\")]\r\n\t\tpublic {0} {1} ", control.Value.FullName, control.Key); //add var code_s2.AppendLine("{ get; set; }"); //this should find all known event types AddMappedActions(tree.ClassName, control, code_s2, code_s1, flags, map); //specific listView hooks if (control.Value == typeof(System.Windows.Forms.ListView)) { AddListViewHandler(control.Key, code_s2, code_s1, flags); } } } else { //we now have access to the controls foreach (var control in tree.Controls) { //System.Console.WriteLine(" var {0} : {1} ", control.Name, control.GetType().Name); //add the declaration to code_s2 code_s2.AppendFormat("\t\t[Outlet(\"{1}\")]\r\n\t\tpublic {0} {1} ", control.Value.FullName, control.Key); //add var code_s2.AppendLine("{ get; set; }"); //this should find all known event types AddKnownActions(tree.ClassName, control, code_s2, code_s1, flags); //specific listView hooks if (control.Value == typeof(System.Windows.Forms.ListView)) { AddListViewHandler(control.Key, code_s2, code_s1, flags); } } } //some boiler plate code which helps set the data for a LisViewHelper code_s2.AppendLine("\t\tprotected void SetData<T>(ListViewHelper<T> helper, List<T> data) where T : class"); code_s2.AppendLine("\t\t{\r\n\t\t\t//Auto generated call"); code_s2.AppendLine("\t\t\tType t = helper.GetType();"); code_s2.AppendLine("\t\t\tt.InvokeMember(\"SetData\", BindingFlags.Default | BindingFlags.InvokeMethod, null, helper, new object[] { data });"); code_s2.AppendLine("\t\t}\r\n"); code_s1.AppendLine("\t}"); //end of class declaration code.AppendLine(code_s1.ToString()); code_s2.AppendLine("\t}"); //end of class declaration code_s2.AppendLine("#endregion /*GUI glue code*/"); code.AppendLine(code_s2.ToString()); code.AppendLine("}"); return(code.ToString()); }