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

Last change on this file since 2702 was 2702, checked in by mkommend, 10 years ago

removed test cases in MainFormManager ctor (ticket #857)

File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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  class StringDict<T> : Dictionary<string, T> {
31  }
32  public static class MainFormManager {
33    private static object locker;
34    private static IMainForm mainform;
35    private static HashSet<Type> views;
36    private static Dictionary<Type, Type> defaultViews;
37
38    static MainFormManager() {
39      locker = new object();
40      views = new HashSet<Type>();
41      defaultViews = new Dictionary<Type, Type>();
42    }
43
44    public static void RegisterMainForm(IMainForm mainform) {
45      lock (locker) {
46        if (MainFormManager.mainform != null)
47          throw new ArgumentException("A mainform was already associated with the mainform manager.");
48        if (mainform == null)
49          throw new ArgumentException("Could not associate null as a mainform in the mainform manager.");
50
51        MainFormManager.mainform = mainform;
52        IEnumerable<Type> types =
53          from t in ApplicationManager.Manager.GetTypes(typeof(IView))
54          where !t.IsAbstract && !t.IsInterface && ContentAttribute.HasContentAttribute(t)
55          select t;
56
57        foreach (Type viewType in types) {
58          views.Add(viewType);
59          foreach (Type contentType in ContentAttribute.GetDefaultViewableTypes(viewType)) {
60            if (defaultViews.ContainsKey(contentType))
61              throw new ArgumentException("DefaultView for type " + contentType + " is " + defaultViews[contentType] +
62                ". Can't register additional DefaultView " + viewType + ".");
63            defaultViews[contentType] = viewType;
64          }
65        }
66      }
67    }
68
69    public static IMainForm MainForm {
70      get { return mainform; }
71    }
72
73    public static T GetMainForm<T>() where T : IMainForm {
74      return (T)mainform;
75    }
76
77    public static IEnumerable<Type> GetViewTypes(Type contentType) {
78      return from v in views
79             where ContentAttribute.CanViewType(v, contentType)
80             select v;
81    }
82
83    public static bool ViewCanViewObject(IView view, object o) {
84      return ContentAttribute.CanViewType(view.GetType(), o.GetType());
85    }
86
87    public static Type GetDefaultViewType(Type contentType) {
88      //check if viewableType has a default view
89      if (defaultViews.ContainsKey(contentType))
90        return defaultViews[contentType];
91
92      //check base classes for default view
93      Type type = contentType;
94      while (type != null) {
95        foreach (Type defaultViewType in defaultViews.Keys) {
96          if (type == defaultViewType || type.CheckGenericTypes(defaultViewType))
97            return defaultViews[defaultViewType];
98        }
99        type = type.BaseType;
100      }
101
102      //check if exactly one implemented interface has a default view
103      List<Type> temp = (from t in defaultViews.Keys
104                         where t.IsInterface && contentType.IsAssignableTo(t)
105                         select t).ToList();
106      if (temp.Count == 1)
107        return defaultViews[temp[0]];
108      //more than one default view for implemented interfaces are found
109      if (temp.Count > 1)
110        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.");
111      return null;
112    }
113
114    public static IView CreateDefaultView(object objectToView) {
115      Type t = GetDefaultViewType(objectToView.GetType());
116      if (t == null)
117        return null;
118
119      Type viewType = TransformGenericTypeDefinition(t, objectToView);
120      if (viewType == null)
121        return null;
122
123      return (IView)Activator.CreateInstance(viewType, objectToView);
124    }
125
126    public static IView CreateView(Type viewType) {
127      if (!typeof(IView).IsAssignableFrom(viewType))
128        throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is not of type IView.");
129      if (viewType.IsGenericTypeDefinition)
130        throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is a generic type definition.");
131
132      return (IView)Activator.CreateInstance(viewType);
133    }
134
135    public static IView CreateView(Type viewType, object objectToView) {
136      if (!typeof(IView).IsAssignableFrom(viewType))
137        throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is not of type IView.");
138
139      Type t = TransformGenericTypeDefinition(viewType, objectToView);
140      if (t == null)
141        return null;
142
143      return (IView)Activator.CreateInstance(t, objectToView);
144    }
145
146    private static Type TransformGenericTypeDefinition(Type type, object objectToView) {
147      if (!type.IsGenericTypeDefinition)
148        return type;
149
150      Type[] typeGenericArguments = type.GetGenericArguments();
151      Type[] objectGenericArguments = objectToView.GetType().GetGenericArguments();
152
153      for (int i = 0; i < typeGenericArguments.Length; i++) {
154        foreach (Type typeConstraint in typeGenericArguments[i].GetGenericParameterConstraints()) {
155          if (!typeConstraint.IsAssignableFrom(objectGenericArguments[i]))
156            return null;
157        }
158      }
159
160      Type t = type.MakeGenericType(objectToView.GetType().GetGenericArguments());
161      return t;
162    }
163  }
164}
Note: See TracBrowser for help on using the repository browser.