Changeset 2466 for trunk/sources/HeuristicLab.MainForm
- Timestamp:
- 11/04/09 17:16:39 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.MainForm/3.2
- Files:
-
- 3 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.MainForm/3.2/ContentAttribute.cs
r2459 r2466 25 25 26 26 namespace HeuristicLab.MainForm { 27 [AttributeUsage(AttributeTargets.Class)] 28 public class DefaultViewAttribute : Attribute { 29 public DefaultViewAttribute() { 27 [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] 28 public class ContentAttribute : Attribute { 29 private Type type; 30 public ContentAttribute(Type type) { 31 this.type = type; 30 32 } 31 33 32 public static bool IsDefaultView(Type t) { 33 object[] attributes = t.GetCustomAttributes(typeof(DefaultViewAttribute), false); 34 return attributes != null && attributes.Length > 0; 34 public ContentAttribute(Type type, bool isDefaultView) 35 : this(type) { 36 this.isDefaultView = isDefaultView; 37 } 38 39 private bool isDefaultView; 40 public bool IsDefaultView { 41 get { return this.isDefaultView; } 42 set { this.isDefaultView = value; } 43 } 44 45 public static bool HasContentAttribute(Type viewType) { 46 ContentAttribute[] attributes = (ContentAttribute[])viewType.GetCustomAttributes(typeof(ContentAttribute), true); 47 return attributes.Length != 0; 48 } 49 50 public static bool CanViewType(Type viewType, Type content) { 51 ContentAttribute[] attributes = (ContentAttribute[])viewType.GetCustomAttributes(typeof(ContentAttribute), false); 52 return attributes.Any(a => a.type.IsAssignableFrom(content)); 53 } 54 55 public static bool IsDefaultViewForType(Type viewType, Type content) { 56 ContentAttribute[] attributes = (ContentAttribute[])viewType.GetCustomAttributes(typeof(ContentAttribute), false); 57 return attributes.Any(a => a.isDefaultView && a.type == content); 58 } 59 60 public static IEnumerable<Type> GetTypesWhereViewTypeIsDefaultView(Type viewType) { 61 ContentAttribute[] attributes = (ContentAttribute[])viewType.GetCustomAttributes(typeof(ContentAttribute), true); 62 return from a in attributes 63 where a.isDefaultView 64 select a.type; 35 65 } 36 66 } -
trunk/sources/HeuristicLab.MainForm/3.2/HeuristicLab.MainForm-3.2.csproj
r2458 r2466 92 92 <Compile Include="Properties\AssemblyInfo.cs" /> 93 93 <Compile Include="UserInterfaceItemBase.cs" /> 94 <Compile Include=" DefaultViewAttribute.cs" />94 <Compile Include="ContentAttribute.cs" /> 95 95 </ItemGroup> 96 96 <ItemGroup> -
trunk/sources/HeuristicLab.MainForm/3.2/Interfaces/IView.cs
r2458 r2466 33 33 void OnClosed(object sender, EventArgs e); 34 34 } 35 36 public interface IView<T> : IView {37 void View(T item);38 T Item { get; }39 }40 35 } -
trunk/sources/HeuristicLab.MainForm/3.2/MainFormManager.cs
r2458 r2466 29 29 private static object locker; 30 30 private static IMainForm mainform; 31 private static Dictionary<Type, List<Type>> views;31 private static HashSet<Type> views; 32 32 private static Dictionary<Type, Type> defaultViews; 33 33 34 34 static MainFormManager() { 35 35 locker = new object(); 36 views = new Dictionary<Type, List<Type>>();36 views = new HashSet<Type>(); 37 37 defaultViews = new Dictionary<Type, Type>(); 38 38 } … … 46 46 IEnumerable<Type> types = 47 47 from t in ds.GetTypes(typeof(IView)) 48 where !t.IsAbstract && !t.IsInterface && !t.IsGenericType 48 where !t.IsAbstract && !t.IsInterface && !t.IsGenericType && ContentAttribute.HasContentAttribute(t) 49 49 select t; 50 50 51 foreach (Type t in types) { 52 foreach (Type viewableType in GetViewableType(t)) { 53 if (viewableType != null) { 54 if (!views.ContainsKey(viewableType)) 55 views[viewableType] = new List<Type>(); 56 views[viewableType].Add(t); 57 58 if (DefaultViewAttribute.IsDefaultView(t)) { 59 if (defaultViews.ContainsKey(viewableType)) 60 throw new ArgumentException("DefaultView for type " + viewableType + " is " + defaultViews[viewableType] + 61 ". Can't register additional DefaultView " + t + "."); 62 defaultViews[viewableType] = t; 63 } 64 } 51 foreach (Type viewType in types) { 52 views.Add(viewType); 53 foreach (Type contentType in ContentAttribute.GetTypesWhereViewTypeIsDefaultView(viewType)) { 54 if (defaultViews.ContainsKey(contentType)) 55 throw new ArgumentException("DefaultView for type " + contentType + " is " + defaultViews[contentType] + 56 ". Can't register additional DefaultView " + viewType + "."); 57 defaultViews[contentType] = viewType; 65 58 } 66 59 } 67 60 } else 68 61 throw new ArgumentException("A mainform was already associated with the mainform manager."); 69 }70 }71 72 private static IEnumerable<Type> GetViewableType(Type t) {73 IEnumerable<Type> interfaceTypes =74 from type in t.GetInterfaces()75 where type.Namespace == "HeuristicLab.MainForm" && type.Name.StartsWith("IView") &&76 type.IsGenericType && !type.IsGenericTypeDefinition77 select type;78 79 foreach (Type interfaceType in interfaceTypes) {80 yield return interfaceType.GetGenericArguments()[0];81 62 } 82 63 } … … 90 71 } 91 72 92 public static IEnumerable<Type> GetViewTypes(Type viewableType) { 93 List<Type> viewsForType = new List<Type>(); 94 foreach (KeyValuePair<Type, List<Type>> v in views) { 95 if (v.Key.IsAssignableFrom(viewableType)) 96 viewsForType.AddRange(v.Value); 97 } 98 return viewsForType.Distinct(); 73 public static IEnumerable<Type> GetViewTypes(Type contentType) { 74 return from v in views 75 where ContentAttribute.CanViewType(v, contentType) 76 select v; 99 77 } 100 78 101 79 public static bool ViewCanViewObject(IView view, object o) { 102 return GetViewTypes(o.GetType()).Contains(view.GetType());80 return ContentAttribute.CanViewType(view.GetType(), o.GetType()); 103 81 } 104 82 105 public static Type GetDefaultViewType(Type viewableType) {83 public static Type GetDefaultViewType(Type contentType) { 106 84 //check if viewableType has a default view 107 if (defaultViews.ContainsKey( viewableType))108 return defaultViews[ viewableType];85 if (defaultViews.ContainsKey(contentType)) 86 return defaultViews[contentType]; 109 87 110 88 //check base classes for default view 111 Type type = viewableType;89 Type type = contentType; 112 90 while (type.BaseType != null && !defaultViews.ContainsKey(type)) { 113 91 type = type.BaseType; … … 118 96 //check if exact one implemented interface has a default view 119 97 List<Type> temp = (from t in defaultViews.Keys 120 where t.IsAssignableFrom( viewableType) && t.IsInterface98 where t.IsAssignableFrom(contentType) && t.IsInterface 121 99 select t).ToList(); 122 100 if (temp.Count == 1) … … 124 102 //more than one default view for implemented interfaces are found 125 103 if (temp.Count > 1) 126 throw new Exception("Could not determine which is the default view for type " + viewableType.ToString() + ". Because more than one implemented interfaces have a default view.");104 throw new Exception("Could not determine which is the default view for type " + contentType.ToString() + ". Because more than one implemented interfaces have a default view."); 127 105 return null; 128 106 } 129 107 130 public static IView <T> CreateDefaultView<T>(TobjectToView) {108 public static IView CreateDefaultView(object objectToView) { 131 109 Type t = GetDefaultViewType(objectToView.GetType()); 132 110 if (t == null) 133 111 return null; 134 112 else 135 return (IView <T>)Activator.CreateInstance(t);113 return (IView)Activator.CreateInstance(t, objectToView); 136 114 } 137 115 }
Note: See TracChangeset
for help on using the changeset viewer.