Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2466 for trunk/sources


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

implemented ContentAttribute and adapted MainFormManager (ticket #771)

Location:
trunk/sources
Files:
6 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.MainForm.Test/3.2/EditorView.cs

    r2464 r2466  
    1111
    1212namespace 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() {
    1617      InitializeComponent();
     18    }
     19
     20    public EditorView(ArrayList list)
     21      : this() {
    1722    }
    1823
     
    2227      IEnumerable<Type> views1 = MainFormManager.GetViewTypes(typeof(IList));
    2328      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();
    3231
    33       MainFormManager.MainForm.HideView(this);
     32      ArrayList list = new ArrayList();
     33      IView defaultView = MainFormManager.CreateDefaultView(list);
     34      MainFormManager.MainForm.ShowView(defaultView);
    3435      this.OnChanged();
    3536    }
     
    4546      }
    4647    }
    47 
    48     #region IView<ArrayList> Members
    49 
    50     public void View(ArrayList item) {
    51       throw new NotImplementedException();
    52     }
    53 
    54     public ArrayList Item {
    55       get { throw new NotImplementedException(); }
    56     }
    57 
    58     #endregion
    5948  }
    6049}
  • trunk/sources/HeuristicLab.MainForm.Test/3.2/FormView.cs

    r2464 r2466  
    1111
    1212namespace 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 {
    1615    public FormView1() {
    1716      InitializeComponent();
    1817    }
    19 
    20     #region IView<ICollection> Members
    21 
    22     public void View(ICollection item) {
    23       throw new NotImplementedException();
    24     }
    25 
    26     public ICollection Item {
    27       get { throw new NotImplementedException(); }
    28     }
    29 
    30     #endregion
    3118  }
    3219}
  • trunk/sources/HeuristicLab.MainForm.Test/3.2/FormView2.cs

    r2464 r2466  
    1010
    1111namespace 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 {
    1414    public FormView2() {
    1515      InitializeComponent();
    1616    }
    17 
    18     #region IView<IEnumerable> Members
    19 
    20     public void View(IEnumerable item) {
    21       throw new NotImplementedException();
    22     }
    23 
    24     public IEnumerable Item {
    25       get { throw new NotImplementedException(); }
    26     }
    27 
    28     #endregion
    2917  }
    3018}
  • 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.