source: trunk/sources/HeuristicLab.MainForm/3.2/MainFormManager.cs @ 3255

Last change on this file since 3255 was 3255, checked in by swagner, 11 years ago

Fixed MainFormManager.GetViewTypes that it includes default views in each case (#958).

File size: 8.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.PluginInfrastructure;
27using System.Diagnostics;
28
29namespace HeuristicLab.MainForm {
30  public static class MainFormManager {
31    private static object locker;
32    private static IMainForm mainform;
33    private static HashSet<Type> views;
34    private static Dictionary<Type, Type> defaultViews;
35
36    static MainFormManager() {
37      locker = new object();
38      mainform = null;
39      views = new HashSet<Type>();
40      defaultViews = new Dictionary<Type, Type>();
41    }
42
43    public static void RegisterMainForm(IMainForm mainForm) {
44      lock (locker) {
45        if (MainFormManager.mainform != null)
46          throw new ArgumentException("A mainform was already associated with the mainform manager.");
47        if (mainForm == null)
48          throw new ArgumentException("Could not associate null as a mainform in the mainform manager.");
49
50        MainFormManager.mainform = mainForm;
51        IEnumerable<Type> types =
52          from t in ApplicationManager.Manager.GetTypes(typeof(IView))
53          where !t.IsAbstract && !t.IsInterface && ContentAttribute.HasContentAttribute(t)
54          select t;
55
56        foreach (Type viewType in types) {
57          views.Add(viewType);
58          foreach (Type contentType in ContentAttribute.GetDefaultViewableTypes(viewType)) {
59            if (defaultViews.ContainsKey(contentType))
60              throw new ArgumentException("DefaultView for type " + contentType + " is " + defaultViews[contentType] +
61                ". Can't register additional DefaultView " + viewType + ".");
62            defaultViews[contentType] = viewType;
63          }
64        }
65      }
66    }
67
68    public static IMainForm MainForm {
69      get { return mainform; }
70    }
71
72    public static T GetMainForm<T>() where T : IMainForm {
73      return (T)mainform;
74    }
75
76    public static IEnumerable<Type> GetViewTypes(Type contentType) {
77      List<Type> viewTypes = (from v in views
78                              where ContentAttribute.CanViewType(v, contentType)
79                              select v).ToList();
80      //transform generic type definitions to generic types
81      for (int i = 0; i < viewTypes.Count; i++) {
82        viewTypes[i] = TransformGenericTypeDefinition(viewTypes[i], contentType);
83      }
84      return viewTypes.Where(t => t != null);
85    }
86
87    public static IEnumerable<Type> GetViewTypes(Type contentType, bool returnOnlyMostSpecificViewTypes) {
88      List<Type> viewTypes = new List<Type>(GetViewTypes(contentType));
89      if (returnOnlyMostSpecificViewTypes) {
90        Type defaultViewType = null;
91        try {
92          defaultViewType = GetDefaultViewType(contentType);
93        }
94        catch (InvalidOperationException) { }
95
96        foreach (Type viewType in viewTypes.ToList()) {
97          if((viewType != defaultViewType) && viewTypes.Any(t => t.IsSubclassOf(viewType)))
98            viewTypes.Remove(viewType);
99        }
100      }
101      return viewTypes;
102    }
103
104    public static bool ViewCanViewObject(IView view, object content) {
105      return ContentAttribute.CanViewType(view.GetType(), content.GetType());
106    }
107
108    public static Type GetDefaultViewType(Type contentType) {
109      //check if viewableType has a default view
110      if (defaultViews.ContainsKey(contentType))
111        return defaultViews[contentType];
112
113      //check base classes for default view
114      Type type = contentType;
115      while (type != null) {
116        foreach (Type defaultContentType in defaultViews.Keys) {
117          if (type == defaultContentType || type.CheckGenericTypes(defaultContentType))
118            return TransformGenericTypeDefinition(defaultViews[defaultContentType], contentType);
119        }
120        type = type.BaseType;
121      }
122
123      //check if exactly one implemented interface has a default view
124      List<Type> temp = (from t in defaultViews.Keys
125                         where t.IsInterface && contentType.IsAssignableTo(t)
126                         select t).ToList();
127      if (temp.Count == 1)
128        return TransformGenericTypeDefinition(defaultViews[temp[0]], contentType);
129      //more than one default view for implemented interfaces are found
130      if (temp.Count > 1)
131        throw new InvalidOperationException("Could not determine which is the default view for type " + contentType.ToString() + ". Because more than one implemented interfaces have a default view.");
132      return null;
133    }
134
135    public static IView CreateDefaultView(object content) {
136      Type t = GetDefaultViewType(content.GetType());
137      if (t == null)
138        return null;
139
140      return (IView)Activator.CreateInstance(t, content);
141    }
142
143    public static IView CreateView(Type viewType) {
144      if (!typeof(IView).IsAssignableFrom(viewType))
145        throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is not of type IView.");
146      if (viewType.IsGenericTypeDefinition)
147        throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is a generic type definition.");
148
149      return (IView)Activator.CreateInstance(viewType);
150    }
151
152    public static IView CreateView(Type viewType, object content) {
153      if (!typeof(IView).IsAssignableFrom(viewType))
154        throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is not of type IView.");
155      Type view = viewType;
156      if (view.IsGenericTypeDefinition)
157        view = TransformGenericTypeDefinition(view, content.GetType());
158
159      return (IView)Activator.CreateInstance(view, content);
160    }
161
162    private static Type TransformGenericTypeDefinition(Type viewType, Type contentType) {
163      if (contentType.IsGenericTypeDefinition)
164        throw new ArgumentException("The content type " + contentType.ToString() + " must not be a generic type definition.");
165
166      if (!viewType.IsGenericTypeDefinition)
167        return viewType;
168
169      Type contentTypeBaseType = null;
170      foreach (Type type in ContentAttribute.GetViewableTypes(viewType)) {
171        contentTypeBaseType = contentType;
172        while (contentTypeBaseType != null && (!contentTypeBaseType.IsGenericType ||
173              type.GetGenericTypeDefinition() != contentTypeBaseType.GetGenericTypeDefinition()))
174          contentTypeBaseType = contentTypeBaseType.BaseType;
175
176        //check interfaces for generic type arguments
177        if (contentTypeBaseType == null) {
178          IEnumerable<Type> implementedInterfaces = contentType.GetInterfaces().Where(t => t.IsGenericType);
179          foreach (Type implementedInterface in implementedInterfaces) {
180            if (implementedInterface.CheckGenericTypes(type))
181              contentTypeBaseType = implementedInterface;
182          }
183        }
184        if (contentTypeBaseType != null) break;
185      }
186
187      if (!contentTypeBaseType.IsGenericType)
188        throw new ArgumentException("Neither content type itself nor any of its base classes is a generic type. Could not determine generic type argument for the view.");
189
190      Type[] viewTypeGenericArguments = viewType.GetGenericArguments();
191      Type[] contentTypeGenericArguments = contentTypeBaseType.GetGenericArguments();
192
193      if (contentTypeGenericArguments.Length != viewTypeGenericArguments.Length)
194        throw new ArgumentException("Neiter the type (" + contentType.ToString() + ") nor any of its base types specifies " +
195          viewTypeGenericArguments.Length + " generic type arguments.");
196
197      for (int i = 0; i < viewTypeGenericArguments.Length; i++) {
198        foreach (Type typeConstraint in viewTypeGenericArguments[i].GetGenericParameterConstraints()) {
199          if (!typeConstraint.IsAssignableFrom(contentTypeGenericArguments[i]))
200            return null;
201        }
202      }
203
204      Type returnType = viewType.MakeGenericType(contentTypeGenericArguments);
205      return returnType;
206    }
207  }
208}
Note: See TracBrowser for help on using the repository browser.