1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 HeuristicLab.Common;
|
---|
26 | using HeuristicLab.PluginInfrastructure;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.MainForm {
|
---|
29 | public static class MainFormManager {
|
---|
30 | private static object locker;
|
---|
31 | private static IMainForm mainform;
|
---|
32 | private static HashSet<Type> views;
|
---|
33 | private static Dictionary<Type, Type> defaultViews;
|
---|
34 |
|
---|
35 | static MainFormManager() {
|
---|
36 | locker = new object();
|
---|
37 | mainform = null;
|
---|
38 | views = new HashSet<Type>();
|
---|
39 | defaultViews = new Dictionary<Type, Type>();
|
---|
40 | }
|
---|
41 |
|
---|
42 | public static void RegisterMainForm(IMainForm mainForm) {
|
---|
43 | lock (locker) {
|
---|
44 | if (MainFormManager.mainform != null)
|
---|
45 | throw new ArgumentException("A mainform was already associated with the mainform manager.");
|
---|
46 | if (mainForm == null)
|
---|
47 | throw new ArgumentException("Could not associate null as a mainform in the mainform manager.");
|
---|
48 |
|
---|
49 | MainFormManager.mainform = mainForm;
|
---|
50 | IEnumerable<Type> types =
|
---|
51 | from t in ApplicationManager.Manager.GetTypes(typeof(IContentView), true, true)
|
---|
52 | where !t.IsAbstract && !t.IsInterface && ContentAttribute.HasContentAttribute(t)
|
---|
53 | select t;
|
---|
54 |
|
---|
55 | foreach (Type viewType in types) {
|
---|
56 | views.Add(viewType);
|
---|
57 | foreach (Type contentType in ContentAttribute.GetDefaultViewableTypes(viewType)) {
|
---|
58 | if (defaultViews.ContainsKey(contentType))
|
---|
59 | throw new ArgumentException("DefaultView for type " + contentType + " is " + defaultViews[contentType] +
|
---|
60 | ". Can't register additional DefaultView " + viewType + ".");
|
---|
61 | defaultViews[contentType] = viewType;
|
---|
62 | }
|
---|
63 | }
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | public static IMainForm MainForm {
|
---|
68 | get { return mainform; }
|
---|
69 | }
|
---|
70 |
|
---|
71 | public static T GetMainForm<T>() where T : IMainForm {
|
---|
72 | return (T)mainform;
|
---|
73 | }
|
---|
74 |
|
---|
75 | public static IEnumerable<Type> GetViewTypes(Type contentType) {
|
---|
76 | CheckForContentType(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 | CheckForContentType(contentType);
|
---|
89 | List<Type> viewTypes = new List<Type>(GetViewTypes(contentType));
|
---|
90 | if (returnOnlyMostSpecificViewTypes) {
|
---|
91 | Type defaultViewType = null;
|
---|
92 | try {
|
---|
93 | defaultViewType = GetDefaultViewType(contentType);
|
---|
94 | }
|
---|
95 | catch (InvalidOperationException) { }
|
---|
96 |
|
---|
97 | foreach (Type viewType in viewTypes.ToList()) {
|
---|
98 | if ((viewType != defaultViewType) && viewTypes.Any(t => t.IsSubclassOf(viewType)))
|
---|
99 | viewTypes.Remove(viewType);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | return viewTypes;
|
---|
103 | }
|
---|
104 |
|
---|
105 | public static bool ViewCanViewContent(IContentView view, IContent content) {
|
---|
106 | return ContentAttribute.CanViewType(view.GetType(), content.GetType());
|
---|
107 | }
|
---|
108 |
|
---|
109 | public static Type GetDefaultViewType(Type contentType) {
|
---|
110 | CheckForContentType(contentType);
|
---|
111 | //check base classes for default view
|
---|
112 | Type type = contentType;
|
---|
113 | while (type != null) {
|
---|
114 | //check classes
|
---|
115 | foreach (Type defaultContentType in defaultViews.Keys) {
|
---|
116 | if (type == defaultContentType || type.CheckGenericTypes(defaultContentType))
|
---|
117 | return TransformGenericTypeDefinition(defaultViews[defaultContentType], contentType);
|
---|
118 | }
|
---|
119 |
|
---|
120 | //check interfaces hierarchy of implemented and not inherited interfaces
|
---|
121 | var nonInheritedInterfaces = type.GetInterfaces().Where(i => !i.IsAssignableFrom(type.BaseType));
|
---|
122 | var interfaces = new HashSet<Type>(nonInheritedInterfaces);
|
---|
123 |
|
---|
124 | while (interfaces.Any()) {
|
---|
125 | interfaces.RemoveWhere(i => interfaces.Any(x => x.GetInterfaces().Contains(i)));
|
---|
126 |
|
---|
127 | List<Type> defaultViewList = (from defaultContentType in defaultViews.Keys
|
---|
128 | where interfaces.Contains(defaultContentType) ||
|
---|
129 | interfaces.Any(i => i.CheckGenericTypes(defaultContentType))
|
---|
130 | select defaultViews[defaultContentType]).ToList();
|
---|
131 |
|
---|
132 | //return only most spefic view as default view
|
---|
133 | foreach (Type viewType in defaultViewList.ToList()) {
|
---|
134 | if (defaultViewList.Any(t => t.IsSubclassOf(viewType)))
|
---|
135 | defaultViewList.Remove(viewType);
|
---|
136 | }
|
---|
137 |
|
---|
138 | if (defaultViewList.Count == 1)
|
---|
139 | return TransformGenericTypeDefinition(defaultViewList[0], contentType);
|
---|
140 | else if (defaultViewList.Count > 1)
|
---|
141 | 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.");
|
---|
142 |
|
---|
143 | interfaces = new HashSet<Type>(interfaces.SelectMany(i => i.GetInterfaces()));
|
---|
144 | }
|
---|
145 |
|
---|
146 | type = type.BaseType;
|
---|
147 | }
|
---|
148 |
|
---|
149 | return null;
|
---|
150 | }
|
---|
151 |
|
---|
152 | public static IContentView CreateDefaultView(Type contentType) {
|
---|
153 | CheckForContentType(contentType);
|
---|
154 | Type t = GetDefaultViewType(contentType);
|
---|
155 | if (t == null)
|
---|
156 | return null;
|
---|
157 |
|
---|
158 | return (IContentView)Activator.CreateInstance(t);
|
---|
159 | }
|
---|
160 | public static IContentView CreateView(Type viewType) {
|
---|
161 | CheckForContentViewType(viewType);
|
---|
162 | if (viewType.IsGenericTypeDefinition)
|
---|
163 | throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is a generic type definition.");
|
---|
164 |
|
---|
165 | return (IContentView)Activator.CreateInstance(viewType);
|
---|
166 | }
|
---|
167 |
|
---|
168 | private static void CheckForContentType(Type contentType) {
|
---|
169 | if (!typeof(IContent).IsAssignableFrom(contentType))
|
---|
170 | throw new ArgumentException("DefaultViews are only specified for types of IContent and not for " + contentType + ".");
|
---|
171 | }
|
---|
172 | private static void CheckForContentViewType(Type viewType) {
|
---|
173 | if (!typeof(IContentView).IsAssignableFrom(viewType))
|
---|
174 | throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is not of type IContentView.");
|
---|
175 | }
|
---|
176 |
|
---|
177 | private static Type TransformGenericTypeDefinition(Type viewType, Type contentType) {
|
---|
178 | if (contentType.IsGenericTypeDefinition)
|
---|
179 | throw new ArgumentException("The content type " + contentType.ToString() + " must not be a generic type definition.");
|
---|
180 |
|
---|
181 | if (!viewType.IsGenericTypeDefinition)
|
---|
182 | return viewType;
|
---|
183 |
|
---|
184 | Type contentTypeBaseType = null;
|
---|
185 | foreach (Type type in ContentAttribute.GetViewableTypes(viewType)) {
|
---|
186 | contentTypeBaseType = contentType;
|
---|
187 | while (contentTypeBaseType != null && (!contentTypeBaseType.IsGenericType ||
|
---|
188 | type.GetGenericTypeDefinition() != contentTypeBaseType.GetGenericTypeDefinition()))
|
---|
189 | contentTypeBaseType = contentTypeBaseType.BaseType;
|
---|
190 |
|
---|
191 | //check interfaces for generic type arguments
|
---|
192 | if (contentTypeBaseType == null) {
|
---|
193 | IEnumerable<Type> implementedInterfaces = contentType.GetInterfaces().Where(t => t.IsGenericType);
|
---|
194 | foreach (Type implementedInterface in implementedInterfaces) {
|
---|
195 | if (implementedInterface.CheckGenericTypes(type))
|
---|
196 | contentTypeBaseType = implementedInterface;
|
---|
197 | }
|
---|
198 | }
|
---|
199 | if (contentTypeBaseType != null) break;
|
---|
200 | }
|
---|
201 |
|
---|
202 | if (!contentTypeBaseType.IsGenericType)
|
---|
203 | 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.");
|
---|
204 |
|
---|
205 | Type[] viewTypeGenericArguments = viewType.GetGenericArguments();
|
---|
206 | Type[] contentTypeGenericArguments = contentTypeBaseType.GetGenericArguments();
|
---|
207 |
|
---|
208 | if (contentTypeGenericArguments.Length != viewTypeGenericArguments.Length)
|
---|
209 | throw new ArgumentException("Neiter the type (" + contentType.ToString() + ") nor any of its base types specifies " +
|
---|
210 | viewTypeGenericArguments.Length + " generic type arguments.");
|
---|
211 |
|
---|
212 | for (int i = 0; i < viewTypeGenericArguments.Length; i++) {
|
---|
213 | foreach (Type typeConstraint in viewTypeGenericArguments[i].GetGenericParameterConstraints()) {
|
---|
214 | if (!typeConstraint.IsAssignableFrom(contentTypeGenericArguments[i]))
|
---|
215 | return null;
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | Type returnType = viewType.MakeGenericType(contentTypeGenericArguments);
|
---|
220 | return returnType;
|
---|
221 | }
|
---|
222 | }
|
---|
223 | }
|
---|