Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/04/09 17:16:39 (15 years ago)
Author:
mkommend
Message:

implemented ContentAttribute and adapted MainFormManager (ticket #771)

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  
    2525
    2626namespace 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;
    3032    }
    3133
    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;
    3565    }
    3666  }
  • trunk/sources/HeuristicLab.MainForm/3.2/HeuristicLab.MainForm-3.2.csproj

    r2458 r2466  
    9292    <Compile Include="Properties\AssemblyInfo.cs" />
    9393    <Compile Include="UserInterfaceItemBase.cs" />
    94     <Compile Include="DefaultViewAttribute.cs" />
     94    <Compile Include="ContentAttribute.cs" />
    9595  </ItemGroup>
    9696  <ItemGroup>
  • trunk/sources/HeuristicLab.MainForm/3.2/Interfaces/IView.cs

    r2458 r2466  
    3333    void OnClosed(object sender, EventArgs e);
    3434  }
    35 
    36   public interface IView<T> : IView {
    37     void View(T item);
    38     T Item { get; }
    39   }
    4035}
  • trunk/sources/HeuristicLab.MainForm/3.2/MainFormManager.cs

    r2458 r2466  
    2929    private static object locker;
    3030    private static IMainForm mainform;
    31     private static Dictionary<Type, List<Type>> views;
     31    private static HashSet<Type> views;
    3232    private static Dictionary<Type, Type> defaultViews;
    3333
    3434    static MainFormManager() {
    3535      locker = new object();
    36       views = new Dictionary<Type, List<Type>>();
     36      views = new HashSet<Type>();
    3737      defaultViews = new Dictionary<Type, Type>();
    3838    }
     
    4646          IEnumerable<Type> types =
    4747            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)
    4949            select t;
    5050
    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;
    6558            }
    6659          }
    6760        } else
    6861          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.IsGenericTypeDefinition
    77        select type;
    78 
    79       foreach (Type interfaceType in interfaceTypes) {
    80         yield return interfaceType.GetGenericArguments()[0];
    8162      }
    8263    }
     
    9071    }
    9172
    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;
    9977    }
    10078
    10179    public static bool ViewCanViewObject(IView view, object o) {
    102       return GetViewTypes(o.GetType()).Contains(view.GetType());
     80      return ContentAttribute.CanViewType(view.GetType(), o.GetType());
    10381    }
    10482
    105     public static Type GetDefaultViewType(Type viewableType) {
     83    public static Type GetDefaultViewType(Type contentType) {
    10684      //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];
    10987
    11088      //check base classes for default view
    111       Type type = viewableType;
     89      Type type = contentType;
    11290      while (type.BaseType != null && !defaultViews.ContainsKey(type)) {
    11391        type = type.BaseType;
     
    11896      //check if exact one implemented interface has a default view
    11997      List<Type> temp = (from t in defaultViews.Keys
    120                          where t.IsAssignableFrom(viewableType) && t.IsInterface
     98                         where t.IsAssignableFrom(contentType) && t.IsInterface
    12199                         select t).ToList();
    122100      if (temp.Count == 1)
     
    124102      //more than one default view for implemented interfaces are found
    125103      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.");
    127105      return null;
    128106    }
    129107
    130     public static IView<T> CreateDefaultView<T>(T objectToView) {
     108    public static IView CreateDefaultView(object objectToView) {
    131109      Type t = GetDefaultViewType(objectToView.GetType());
    132110      if (t == null)
    133111        return null;
    134112      else
    135         return (IView<T>)Activator.CreateInstance(t);
     113        return (IView)Activator.CreateInstance(t, objectToView);
    136114    }
    137115  }
Note: See TracChangeset for help on using the changeset viewer.