Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2696 was 2696, checked in by mkommend, 15 years ago

first version of redesigned MainForm (ticket #857)

File size: 8.0 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      Type listString = typeof(List<string>);
44      Type list = typeof(List<>);
45      Type ilist = typeof(IList<>);
46      Type stringStringDict = typeof(Dictionary<string, string>);
47      Type dict = typeof(Dictionary<,>);
48      Type stringDict = typeof(StringDict<>);
49      Type stringIntDict = typeof(StringDict<int>);
50      Type stringStringDict2 = typeof(StringDict<string>);
51
52      bool result;
53      result = listString.IsAssignableTo(list);
54      Debug.Assert(result);
55
56      Debug.Assert(stringStringDict2.IsAssignableTo(stringDict));
57      Debug.Assert(stringStringDict2.IsAssignableTo(stringStringDict));
58      Debug.Assert(stringStringDict2.IsAssignableTo(dict));
59      Debug.Assert(!stringStringDict2.IsAssignableTo(stringIntDict));
60      Debug.Assert(!stringIntDict.IsAssignableTo(stringStringDict));
61
62      result = list.IsAssignableTo(ilist);
63      Debug.Assert(result);
64      result = listString.IsAssignableTo(ilist);
65      Debug.Assert(result);
66      result = list.IsAssignableTo(listString);
67      Debug.Assert(!result);
68      result = ilist.IsAssignableTo(listString);
69      Debug.Assert(!result);
70      result = ilist.IsAssignableTo(list);
71      Debug.Assert(!result);
72
73      Type stackedListList = list.MakeGenericType(typeof(List<>));
74      Type stackedListListint = typeof(List<List<int>>);
75      Type istackedListListint = typeof(IList<List<int>>);
76      Type stackedListIListint = typeof(List<IList<int>>);
77      Debug.Assert(stackedListListint.IsAssignableTo(list));
78      Debug.Assert(stackedListListint.IsAssignableTo(ilist));
79      Debug.Assert(!stackedListListint.IsAssignableTo(listString));
80      Debug.Assert(stackedListListint.IsAssignableTo(istackedListListint));
81      Debug.Assert(!stackedListListint.IsAssignableTo(stackedListIListint));
82    }
83
84    public static void RegisterMainForm(IMainForm mainform) {
85      lock (locker) {
86        if (MainFormManager.mainform != null)
87          throw new ArgumentException("A mainform was already associated with the mainform manager.");
88        if (mainform == null)
89          throw new ArgumentException("Could not associate null as a mainform in the mainform manager.");
90
91        MainFormManager.mainform = mainform;
92        IEnumerable<Type> types =
93          from t in ApplicationManager.Manager.GetTypes(typeof(IView))
94          where !t.IsAbstract && !t.IsInterface && ContentAttribute.HasContentAttribute(t)
95          select t;
96
97        foreach (Type viewType in types) {
98          views.Add(viewType);
99          foreach (Type contentType in ContentAttribute.GetDefaultViewableTypes(viewType)) {
100            if (defaultViews.ContainsKey(contentType))
101              throw new ArgumentException("DefaultView for type " + contentType + " is " + defaultViews[contentType] +
102                ". Can't register additional DefaultView " + viewType + ".");
103            defaultViews[contentType] = viewType;
104          }
105        }
106      }
107    }
108
109    public static IMainForm MainForm {
110      get { return mainform; }
111    }
112
113    public static T GetMainForm<T>() where T : IMainForm {
114      return (T)mainform;
115    }
116
117    public static IEnumerable<Type> GetViewTypes(Type contentType) {
118      return from v in views
119             where ContentAttribute.CanViewType(v, contentType)
120             select v;
121    }
122
123    public static bool ViewCanViewObject(IView view, object o) {
124      return ContentAttribute.CanViewType(view.GetType(), o.GetType());
125    }
126
127    public static Type GetDefaultViewType(Type contentType) {
128      //check if viewableType has a default view
129      if (defaultViews.ContainsKey(contentType))
130        return defaultViews[contentType];
131
132      //check base classes for default view
133      Type type = contentType;
134      while (type != null) {
135        foreach (Type defaultViewType in defaultViews.Keys) {
136          if (type == defaultViewType || type.CheckGenericTypes(defaultViewType))
137            return defaultViews[defaultViewType];
138        }
139        type = type.BaseType;
140      }
141
142      //check if exactly one implemented interface has a default view
143      List<Type> temp = (from t in defaultViews.Keys
144                         where t.IsInterface && contentType.IsAssignableTo(t)
145                         select t).ToList();
146      if (temp.Count == 1)
147        return defaultViews[temp[0]];
148      //more than one default view for implemented interfaces are found
149      if (temp.Count > 1)
150        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.");
151      return null;
152    }
153
154    public static IView CreateDefaultView(object objectToView) {
155      Type t = GetDefaultViewType(objectToView.GetType());
156      if (t == null)
157        return null;
158
159      Type viewType = TransformGenericTypeDefinition(t, objectToView);
160      if (viewType == null)
161        return null;
162
163      return (IView)Activator.CreateInstance(viewType, objectToView);
164    }
165
166    public static IView CreateView(Type viewType) {
167      if (!typeof(IView).IsAssignableFrom(viewType))
168        throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is not of type IView.");
169      if (viewType.IsGenericTypeDefinition)
170        throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is a generic type definition.");
171
172      return (IView)Activator.CreateInstance(viewType);
173    }
174
175    public static IView CreateView(Type viewType, object objectToView) {
176      if (!typeof(IView).IsAssignableFrom(viewType))
177        throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is not of type IView.");
178
179      Type t = TransformGenericTypeDefinition(viewType, objectToView);
180      if (t == null)
181        return null;
182
183      return (IView)Activator.CreateInstance(t, objectToView);
184    }
185
186    private static Type TransformGenericTypeDefinition(Type type, object objectToView) {
187      if (!type.IsGenericTypeDefinition)
188        return type;
189
190      Type[] typeGenericArguments = type.GetGenericArguments();
191      Type[] objectGenericArguments = objectToView.GetType().GetGenericArguments();
192
193      for (int i = 0; i < typeGenericArguments.Length; i++) {
194        foreach (Type typeConstraint in typeGenericArguments[i].GetGenericParameterConstraints()) {
195          if (!typeConstraint.IsAssignableFrom(objectGenericArguments[i]))
196            return null;
197        }
198      }
199
200      Type t = type.MakeGenericType(objectToView.GetType().GetGenericArguments());
201      return t;
202    }
203  }
204}
Note: See TracBrowser for help on using the repository browser.