[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;
|
---|
[2426] | 28 |
|
---|
| 29 | namespace HeuristicLab.MainForm {
|
---|
[2433] | 30 | public static class MainFormManager {
|
---|
| 31 | private static object locker;
|
---|
| 32 | private static IMainForm mainform;
|
---|
[2466] | 33 | private static HashSet<Type> views;
|
---|
[2433] | 34 | private static Dictionary<Type, Type> defaultViews;
|
---|
| 35 |
|
---|
| 36 | static MainFormManager() {
|
---|
| 37 | locker = new object();
|
---|
[2761] | 38 | mainform = null;
|
---|
[2466] | 39 | views = new HashSet<Type>();
|
---|
[2433] | 40 | defaultViews = new Dictionary<Type, Type>();
|
---|
[2426] | 41 | }
|
---|
| 42 |
|
---|
[2761] | 43 | public static void RegisterMainForm(IMainForm mainForm) {
|
---|
[2433] | 44 | lock (locker) {
|
---|
[2696] | 45 | if (MainFormManager.mainform != null)
|
---|
| 46 | throw new ArgumentException("A mainform was already associated with the mainform manager.");
|
---|
[2761] | 47 | if (mainForm == null)
|
---|
[2696] | 48 | throw new ArgumentException("Could not associate null as a mainform in the mainform manager.");
|
---|
[2433] | 49 |
|
---|
[2761] | 50 | MainFormManager.mainform = mainForm;
|
---|
[2696] | 51 | IEnumerable<Type> types =
|
---|
| 52 | from t in ApplicationManager.Manager.GetTypes(typeof(IView))
|
---|
| 53 | where !t.IsAbstract && !t.IsInterface && ContentAttribute.HasContentAttribute(t)
|
---|
| 54 | select t;
|
---|
[2433] | 55 |
|
---|
[2696] | 56 | foreach (Type viewType in types) {
|
---|
| 57 | views.Add(viewType);
|
---|
| 58 | foreach (Type contentType in ContentAttribute.GetDefaultViewableTypes(viewType)) {
|
---|
| 59 | if (defaultViews.ContainsKey(contentType))
|
---|
| 60 | throw new ArgumentException("DefaultView for type " + contentType + " is " + defaultViews[contentType] +
|
---|
| 61 | ". Can't register additional DefaultView " + viewType + ".");
|
---|
| 62 | defaultViews[contentType] = viewType;
|
---|
[2433] | 63 | }
|
---|
[2696] | 64 | }
|
---|
[2433] | 65 | }
|
---|
[2426] | 66 | }
|
---|
| 67 |
|
---|
[2433] | 68 | public static IMainForm MainForm {
|
---|
[2426] | 69 | get { return mainform; }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[2433] | 72 | public static T GetMainForm<T>() where T : IMainForm {
|
---|
[2426] | 73 | return (T)mainform;
|
---|
| 74 | }
|
---|
[2433] | 75 |
|
---|
[2466] | 76 | public static IEnumerable<Type> GetViewTypes(Type contentType) {
|
---|
[2724] | 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 | }
|
---|
[2726] | 84 | return viewTypes.Where(t => t != null);
|
---|
[2433] | 85 | }
|
---|
| 86 |
|
---|
[2992] | 87 | public static IEnumerable<Type> GetViewTypes(Type contentType, bool returnOnlyMostSpecificViewTypes) {
|
---|
| 88 | List<Type> viewTypes = new List<Type>(GetViewTypes(contentType));
|
---|
| 89 | if (returnOnlyMostSpecificViewTypes) {
|
---|
| 90 | foreach (Type viewType in viewTypes.ToList()) {
|
---|
| 91 | if(viewTypes.Any(t => t.IsSubclassOf(viewType)))
|
---|
| 92 | viewTypes.Remove(viewType);
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | return viewTypes;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[2761] | 98 | public static bool ViewCanViewObject(IView view, object content) {
|
---|
| 99 | return ContentAttribute.CanViewType(view.GetType(), content.GetType());
|
---|
[2433] | 100 | }
|
---|
| 101 |
|
---|
[2466] | 102 | public static Type GetDefaultViewType(Type contentType) {
|
---|
[2456] | 103 | //check if viewableType has a default view
|
---|
[2466] | 104 | if (defaultViews.ContainsKey(contentType))
|
---|
| 105 | return defaultViews[contentType];
|
---|
[2456] | 106 |
|
---|
| 107 | //check base classes for default view
|
---|
[2466] | 108 | Type type = contentType;
|
---|
[2696] | 109 | while (type != null) {
|
---|
[2724] | 110 | foreach (Type defaultContentType in defaultViews.Keys) {
|
---|
| 111 | if (type == defaultContentType || type.CheckGenericTypes(defaultContentType))
|
---|
| 112 | return TransformGenericTypeDefinition(defaultViews[defaultContentType], contentType);
|
---|
[2696] | 113 | }
|
---|
[2456] | 114 | type = type.BaseType;
|
---|
[2443] | 115 | }
|
---|
[2456] | 116 |
|
---|
[2696] | 117 | //check if exactly one implemented interface has a default view
|
---|
[2456] | 118 | List<Type> temp = (from t in defaultViews.Keys
|
---|
[2696] | 119 | where t.IsInterface && contentType.IsAssignableTo(t)
|
---|
[2456] | 120 | select t).ToList();
|
---|
| 121 | if (temp.Count == 1)
|
---|
[2724] | 122 | return TransformGenericTypeDefinition(defaultViews[temp[0]], contentType);
|
---|
[2456] | 123 | //more than one default view for implemented interfaces are found
|
---|
| 124 | if (temp.Count > 1)
|
---|
[2761] | 125 | 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.");
|
---|
[2456] | 126 | return null;
|
---|
[2433] | 127 | }
|
---|
| 128 |
|
---|
[2761] | 129 | public static IView CreateDefaultView(object content) {
|
---|
| 130 | Type t = GetDefaultViewType(content.GetType());
|
---|
[2433] | 131 | if (t == null)
|
---|
| 132 | return null;
|
---|
[2696] | 133 |
|
---|
[2761] | 134 | return (IView)Activator.CreateInstance(t, content);
|
---|
[2433] | 135 | }
|
---|
[2467] | 136 |
|
---|
| 137 | public static IView CreateView(Type viewType) {
|
---|
| 138 | if (!typeof(IView).IsAssignableFrom(viewType))
|
---|
| 139 | throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is not of type IView.");
|
---|
[2696] | 140 | if (viewType.IsGenericTypeDefinition)
|
---|
| 141 | throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is a generic type definition.");
|
---|
| 142 |
|
---|
[2467] | 143 | return (IView)Activator.CreateInstance(viewType);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[2761] | 146 | public static IView CreateView(Type viewType, object content) {
|
---|
[2467] | 147 | if (!typeof(IView).IsAssignableFrom(viewType))
|
---|
| 148 | throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is not of type IView.");
|
---|
[2810] | 149 | Type view = viewType;
|
---|
| 150 | if (view.IsGenericTypeDefinition)
|
---|
| 151 | view = TransformGenericTypeDefinition(view, content.GetType());
|
---|
[2696] | 152 |
|
---|
[2810] | 153 | return (IView)Activator.CreateInstance(view, content);
|
---|
[2467] | 154 | }
|
---|
[2696] | 155 |
|
---|
[2724] | 156 | private static Type TransformGenericTypeDefinition(Type viewType, Type contentType) {
|
---|
| 157 | if (contentType.IsGenericTypeDefinition)
|
---|
| 158 | throw new ArgumentException("The content type " + contentType.ToString() + " must not be a generic type definition.");
|
---|
[2696] | 159 |
|
---|
[2724] | 160 | if (!viewType.IsGenericTypeDefinition)
|
---|
| 161 | return viewType;
|
---|
[2696] | 162 |
|
---|
[2726] | 163 | Type contentTypeBaseType = null;
|
---|
[2724] | 164 | foreach (Type type in ContentAttribute.GetViewableTypes(viewType)) {
|
---|
[2726] | 165 | contentTypeBaseType = contentType;
|
---|
[2724] | 166 | while (contentTypeBaseType != null && (!contentTypeBaseType.IsGenericType ||
|
---|
| 167 | type.GetGenericTypeDefinition() != contentTypeBaseType.GetGenericTypeDefinition()))
|
---|
| 168 | contentTypeBaseType = contentTypeBaseType.BaseType;
|
---|
| 169 |
|
---|
| 170 | //check interfaces for generic type arguments
|
---|
| 171 | if (contentTypeBaseType == null) {
|
---|
| 172 | IEnumerable<Type> implementedInterfaces = contentType.GetInterfaces().Where(t => t.IsGenericType);
|
---|
| 173 | foreach (Type implementedInterface in implementedInterfaces) {
|
---|
[2726] | 174 | if (implementedInterface.CheckGenericTypes(type))
|
---|
[2724] | 175 | contentTypeBaseType = implementedInterface;
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
[2726] | 178 | if (contentTypeBaseType != null) break;
|
---|
[2724] | 179 | }
|
---|
| 180 |
|
---|
| 181 | if (!contentTypeBaseType.IsGenericType)
|
---|
| 182 | 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.");
|
---|
| 183 |
|
---|
| 184 | Type[] viewTypeGenericArguments = viewType.GetGenericArguments();
|
---|
| 185 | Type[] contentTypeGenericArguments = contentTypeBaseType.GetGenericArguments();
|
---|
| 186 |
|
---|
| 187 | if (contentTypeGenericArguments.Length != viewTypeGenericArguments.Length)
|
---|
| 188 | throw new ArgumentException("Neiter the type (" + contentType.ToString() + ") nor any of its base types specifies " +
|
---|
| 189 | viewTypeGenericArguments.Length + " generic type arguments.");
|
---|
| 190 |
|
---|
| 191 | for (int i = 0; i < viewTypeGenericArguments.Length; i++) {
|
---|
| 192 | foreach (Type typeConstraint in viewTypeGenericArguments[i].GetGenericParameterConstraints()) {
|
---|
| 193 | if (!typeConstraint.IsAssignableFrom(contentTypeGenericArguments[i]))
|
---|
[2696] | 194 | return null;
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[2724] | 198 | Type returnType = viewType.MakeGenericType(contentTypeGenericArguments);
|
---|
| 199 | return returnType;
|
---|
[2696] | 200 | }
|
---|
[2426] | 201 | }
|
---|
| 202 | }
|
---|