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