- Timestamp:
- 11/04/09 17:16:39 (15 years ago)
- Location:
- trunk/sources
- Files:
-
- 6 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.MainForm.Test/3.2/EditorView.cs
r2464 r2466 11 11 12 12 namespace HeuristicLab.MainForm.Test { 13 [DefaultViewAttribute] 14 public partial class EditorView : ViewBase, IView<ArrayList> { 15 public EditorView() { 13 [Content(typeof(ArrayList),IsDefaultView=true)] 14 public partial class EditorView : ViewBase { 15 public EditorView() 16 : base() { 16 17 InitializeComponent(); 18 } 19 20 public EditorView(ArrayList list) 21 : this() { 17 22 } 18 23 … … 22 27 IEnumerable<Type> views1 = MainFormManager.GetViewTypes(typeof(IList)); 23 28 views1.ToString(); 24 //IEnumerable<Type> views2 = MainFormManager.GetViewTypes(typeof(object)); 25 //views2.ToString(); 26 Type def2 = MainFormManager.GetDefaultViewType(typeof(IList)); 27 def2.ToString(); 28 Type def1 = MainFormManager.GetDefaultViewType(typeof(ArrayList)); 29 def1.ToString(); 30 //Type def3 = MainFormManager.GetDefaultViewType(typeof(object)); 31 //def3.ToString(); 29 IEnumerable<Type> views2 = MainFormManager.GetViewTypes(typeof(IEnumerable)); 30 views2.ToString(); 32 31 33 MainFormManager.MainForm.HideView(this); 32 ArrayList list = new ArrayList(); 33 IView defaultView = MainFormManager.CreateDefaultView(list); 34 MainFormManager.MainForm.ShowView(defaultView); 34 35 this.OnChanged(); 35 36 } … … 45 46 } 46 47 } 47 48 #region IView<ArrayList> Members49 50 public void View(ArrayList item) {51 throw new NotImplementedException();52 }53 54 public ArrayList Item {55 get { throw new NotImplementedException(); }56 }57 58 #endregion59 48 } 60 49 } -
trunk/sources/HeuristicLab.MainForm.Test/3.2/FormView.cs
r2464 r2466 11 11 12 12 namespace HeuristicLab.MainForm.Test { 13 [DefaultViewAttribute] 14 public partial class FormView1 : ViewBase,IView<ICollection> { 15 private int[] array; 13 [Content(typeof(IList),true)] 14 public partial class FormView1 : FormView2 { 16 15 public FormView1() { 17 16 InitializeComponent(); 18 17 } 19 20 #region IView<ICollection> Members21 22 public void View(ICollection item) {23 throw new NotImplementedException();24 }25 26 public ICollection Item {27 get { throw new NotImplementedException(); }28 }29 30 #endregion31 18 } 32 19 } -
trunk/sources/HeuristicLab.MainForm.Test/3.2/FormView2.cs
r2464 r2466 10 10 11 11 namespace HeuristicLab.MainForm.Test { 12 public partial class FormView2 : ViewBase, IView<IEnumerable> {13 private int[] array;12 [Content(typeof(IEnumerable))] 13 public partial class FormView2 : ViewBase { 14 14 public FormView2() { 15 15 InitializeComponent(); 16 16 } 17 18 #region IView<IEnumerable> Members19 20 public void View(IEnumerable item) {21 throw new NotImplementedException();22 }23 24 public IEnumerable Item {25 get { throw new NotImplementedException(); }26 }27 28 #endregion29 17 } 30 18 } -
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.