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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.PluginInfrastructure;
|
---|
27 | using System.Diagnostics;
|
---|
28 |
|
---|
29 | namespace 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 | List<Type> viewTypes = (from v in views
|
---|
79 | where ContentAttribute.CanViewType(v, contentType)
|
---|
80 | select v).ToList();
|
---|
81 | //transform generic type definitions to generic types
|
---|
82 | for (int i = 0; i < viewTypes.Count; i++) {
|
---|
83 | viewTypes[i] = TransformGenericTypeDefinition(viewTypes[i], contentType);
|
---|
84 | }
|
---|
85 | return viewTypes.Where(t => t != null);
|
---|
86 | }
|
---|
87 |
|
---|
88 | public static bool ViewCanViewObject(IView view, object o) {
|
---|
89 | return ContentAttribute.CanViewType(view.GetType(), o.GetType());
|
---|
90 | }
|
---|
91 |
|
---|
92 | public static Type GetDefaultViewType(Type contentType) {
|
---|
93 | //check if viewableType has a default view
|
---|
94 | if (defaultViews.ContainsKey(contentType))
|
---|
95 | return defaultViews[contentType];
|
---|
96 |
|
---|
97 | //check base classes for default view
|
---|
98 | Type type = contentType;
|
---|
99 | while (type != null) {
|
---|
100 | foreach (Type defaultContentType in defaultViews.Keys) {
|
---|
101 | if (type == defaultContentType || type.CheckGenericTypes(defaultContentType))
|
---|
102 | return TransformGenericTypeDefinition(defaultViews[defaultContentType], contentType);
|
---|
103 | }
|
---|
104 | type = type.BaseType;
|
---|
105 | }
|
---|
106 |
|
---|
107 | //check if exactly one implemented interface has a default view
|
---|
108 | List<Type> temp = (from t in defaultViews.Keys
|
---|
109 | where t.IsInterface && contentType.IsAssignableTo(t)
|
---|
110 | select t).ToList();
|
---|
111 | if (temp.Count == 1)
|
---|
112 | return TransformGenericTypeDefinition(defaultViews[temp[0]], contentType);
|
---|
113 | //more than one default view for implemented interfaces are found
|
---|
114 | if (temp.Count > 1)
|
---|
115 | 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.");
|
---|
116 | return null;
|
---|
117 | }
|
---|
118 |
|
---|
119 | public static IView CreateDefaultView(object objectToView) {
|
---|
120 | Type t = GetDefaultViewType(objectToView.GetType());
|
---|
121 | if (t == null)
|
---|
122 | return null;
|
---|
123 |
|
---|
124 | return (IView)Activator.CreateInstance(t, objectToView);
|
---|
125 | }
|
---|
126 |
|
---|
127 | public static IView CreateView(Type viewType) {
|
---|
128 | if (!typeof(IView).IsAssignableFrom(viewType))
|
---|
129 | throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is not of type IView.");
|
---|
130 | if (viewType.IsGenericTypeDefinition)
|
---|
131 | throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is a generic type definition.");
|
---|
132 |
|
---|
133 | return (IView)Activator.CreateInstance(viewType);
|
---|
134 | }
|
---|
135 |
|
---|
136 | public static IView CreateView(Type viewType, object objectToView) {
|
---|
137 | if (!typeof(IView).IsAssignableFrom(viewType))
|
---|
138 | throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is not of type IView.");
|
---|
139 | if (viewType.IsGenericTypeDefinition)
|
---|
140 | throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is a generic type definition.");
|
---|
141 |
|
---|
142 | return (IView)Activator.CreateInstance(viewType, objectToView);
|
---|
143 | }
|
---|
144 |
|
---|
145 | private static Type TransformGenericTypeDefinition(Type viewType, Type contentType) {
|
---|
146 | if (contentType.IsGenericTypeDefinition)
|
---|
147 | throw new ArgumentException("The content type " + contentType.ToString() + " must not be a generic type definition.");
|
---|
148 |
|
---|
149 | if (!viewType.IsGenericTypeDefinition)
|
---|
150 | return viewType;
|
---|
151 |
|
---|
152 | Type contentTypeBaseType = null;
|
---|
153 | foreach (Type type in ContentAttribute.GetViewableTypes(viewType)) {
|
---|
154 | contentTypeBaseType = contentType;
|
---|
155 | while (contentTypeBaseType != null && (!contentTypeBaseType.IsGenericType ||
|
---|
156 | type.GetGenericTypeDefinition() != contentTypeBaseType.GetGenericTypeDefinition()))
|
---|
157 | contentTypeBaseType = contentTypeBaseType.BaseType;
|
---|
158 |
|
---|
159 | //check interfaces for generic type arguments
|
---|
160 | if (contentTypeBaseType == null) {
|
---|
161 | IEnumerable<Type> implementedInterfaces = contentType.GetInterfaces().Where(t => t.IsGenericType);
|
---|
162 | foreach (Type implementedInterface in implementedInterfaces) {
|
---|
163 | if (implementedInterface.CheckGenericTypes(type))
|
---|
164 | contentTypeBaseType = implementedInterface;
|
---|
165 | }
|
---|
166 | }
|
---|
167 | if (contentTypeBaseType != null) break;
|
---|
168 | }
|
---|
169 |
|
---|
170 | if (!contentTypeBaseType.IsGenericType)
|
---|
171 | 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.");
|
---|
172 |
|
---|
173 | Type[] viewTypeGenericArguments = viewType.GetGenericArguments();
|
---|
174 | Type[] contentTypeGenericArguments = contentTypeBaseType.GetGenericArguments();
|
---|
175 |
|
---|
176 | if (contentTypeGenericArguments.Length != viewTypeGenericArguments.Length)
|
---|
177 | throw new ArgumentException("Neiter the type (" + contentType.ToString() + ") nor any of its base types specifies " +
|
---|
178 | viewTypeGenericArguments.Length + " generic type arguments.");
|
---|
179 |
|
---|
180 | for (int i = 0; i < viewTypeGenericArguments.Length; i++) {
|
---|
181 | foreach (Type typeConstraint in viewTypeGenericArguments[i].GetGenericParameterConstraints()) {
|
---|
182 | if (!typeConstraint.IsAssignableFrom(contentTypeGenericArguments[i]))
|
---|
183 | return null;
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | Type returnType = viewType.MakeGenericType(contentTypeGenericArguments);
|
---|
188 | return returnType;
|
---|
189 | }
|
---|
190 | }
|
---|
191 | }
|
---|