#region License Information
/* HeuristicLab
* Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HeuristicLab.PluginInfrastructure;
namespace HeuristicLab.MainForm {
public static class MainFormManager {
private static object locker;
private static IMainForm mainform;
private static Dictionary> views;
private static Dictionary defaultViews;
static MainFormManager() {
locker = new object();
views = new Dictionary>();
defaultViews = new Dictionary();
}
public static void RegisterMainForm(IMainForm mainform) {
lock (locker) {
if (MainFormManager.mainform == null) {
MainFormManager.mainform = mainform;
DiscoveryService ds = new DiscoveryService();
IEnumerable types =
from t in ds.GetTypes(typeof(IView))
where !t.IsAbstract && !t.IsInterface && !t.IsGenericType
select t;
foreach (Type t in types) {
foreach (Type viewableType in GetViewableType(t)) {
if (viewableType != null) {
if (!views.ContainsKey(viewableType))
views[viewableType] = new List();
views[viewableType].Add(t);
object[] attributes = t.GetCustomAttributes(typeof(DefaultView), false);
if (attributes != null && attributes.Length == 1) {
if (defaultViews.ContainsKey(viewableType))
throw new ArgumentException("DefaultView for type " + viewableType + " is " + defaultViews[viewableType] +
". Can't register additional DefaultView " + t + ".");
defaultViews[viewableType] = t;
}
}
}
}
} else
throw new ArgumentException("A mainform was already associated with the mainform manager.");
}
}
private static IEnumerable GetViewableType(Type t) {
IEnumerable interfaceTypes =
from type in t.GetInterfaces()
where type.Namespace == "HeuristicLab.MainForm" && type.Name.StartsWith("IView") &&
type.IsGenericType && !type.IsGenericTypeDefinition
select type;
foreach (Type interfaceType in interfaceTypes) {
yield return interfaceType.GetGenericArguments()[0];
}
}
public static IMainForm MainForm {
get { return mainform; }
}
public static T GetMainForm() where T : IMainForm {
return (T)mainform;
}
public static IEnumerable GetViewTypes(Type viewableType) {
List viewsForType = new List();
foreach (KeyValuePair> v in views) {
if (v.Key.IsAssignableFrom(viewableType))
viewsForType.AddRange(v.Value);
}
return viewsForType.Distinct();
}
public static bool ViewCanViewObject(IView view, object o) {
return GetViewTypes(o.GetType()).Contains(view.GetType());
}
public static Type GetDefaultViewType(Type viewableType) {
if (defaultViews.ContainsKey(viewableType))
return defaultViews[viewableType];
else {
List temp = (from t in defaultViews.Keys
where t.IsAssignableFrom(viewableType)
select t).ToList();
//no assignable type found
if (temp.Count == 0)
return null;
//only one assignable type found => return this one
else if (temp.Count == 1)
return defaultViews[temp[0]];
//more assignable types found => sort the types according to their assignable types
//and return most specific type => except there is a conflict
else {
temp.Sort(delegate(Type t1, Type t2) {
if (t1.IsAssignableFrom(t2))
return 1;
else if (t2.IsAssignableFrom(t1))
return -1;
else
return 0;
}
);
if (temp[1].IsAssignableFrom(temp[0]))
return defaultViews[temp[0]];
else
throw new Exception("Could not determine which is the default view for type " + viewableType.ToString() + ".");
}
}
}
public static IView CreateDefaultView(T objectToView) {
Type t = GetDefaultViewType(objectToView.GetType());
if (t == null)
return null;
else
return (IView)Activator.CreateInstance(t);
}
}
}