Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.MainForm/3.2/MainFormManager.cs @ 4593

Last change on this file since 4593 was 2744, checked in by epitzer, 15 years ago

update to new PluginInfrastructure (#802)

File size: 4.9 KB
RevLine 
[2433]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
21using System;
[2426]22using System.Collections.Generic;
23using System.Linq;
24using System.Text;
[2433]25using HeuristicLab.PluginInfrastructure;
[2426]26
27namespace HeuristicLab.MainForm {
[2433]28  public static class MainFormManager {
29    private static object locker;
30    private static IMainForm mainform;
[2466]31    private static HashSet<Type> views;
[2433]32    private static Dictionary<Type, Type> defaultViews;
33
34    static MainFormManager() {
35      locker = new object();
[2466]36      views = new HashSet<Type>();
[2433]37      defaultViews = new Dictionary<Type, Type>();
[2426]38    }
39
40    public static void RegisterMainForm(IMainForm mainform) {
[2433]41      lock (locker) {
42        if (MainFormManager.mainform == null) {
43          MainFormManager.mainform = mainform;
[2444]44          IEnumerable<Type> types =
[2744]45            from t in ApplicationManager.Manager.GetTypes(typeof(IView))
[2466]46            where !t.IsAbstract && !t.IsInterface && !t.IsGenericType && ContentAttribute.HasContentAttribute(t)
[2444]47            select t;
[2433]48
[2466]49          foreach (Type viewType in types) {
50            views.Add(viewType);
[2468]51            foreach (Type contentType in ContentAttribute.GetDefaultViewableTypes(viewType)) {
[2466]52              if (defaultViews.ContainsKey(contentType))
53                throw new ArgumentException("DefaultView for type " + contentType + " is " + defaultViews[contentType] +
54                  ". Can't register additional DefaultView " + viewType + ".");
55              defaultViews[contentType] = viewType;
[2433]56            }
57          }
58        } else
59          throw new ArgumentException("A mainform was already associated with the mainform manager.");
60      }
[2426]61    }
62
[2433]63    public static IMainForm MainForm {
[2426]64      get { return mainform; }
65    }
66
[2433]67    public static T GetMainForm<T>() where T : IMainForm {
[2426]68      return (T)mainform;
69    }
[2433]70
[2466]71    public static IEnumerable<Type> GetViewTypes(Type contentType) {
72      return from v in views
73             where ContentAttribute.CanViewType(v, contentType)
74             select v;
[2433]75    }
76
77    public static bool ViewCanViewObject(IView view, object o) {
[2466]78      return ContentAttribute.CanViewType(view.GetType(), o.GetType());
[2433]79    }
80
[2466]81    public static Type GetDefaultViewType(Type contentType) {
[2456]82      //check if viewableType has a default view
[2466]83      if (defaultViews.ContainsKey(contentType))
84        return defaultViews[contentType];
[2456]85
86      //check base classes for default view
[2466]87      Type type = contentType;
[2456]88      while (type.BaseType != null && !defaultViews.ContainsKey(type)) {
89        type = type.BaseType;
[2443]90      }
[2456]91      if (defaultViews.ContainsKey(type))
92        return defaultViews[type];
93
94      //check if exact one implemented interface has a default view
95      List<Type> temp = (from t in defaultViews.Keys
[2466]96                         where t.IsAssignableFrom(contentType) && t.IsInterface
[2456]97                         select t).ToList();
98      if (temp.Count == 1)
99        return defaultViews[temp[0]];
100      //more than one default view for implemented interfaces are found
101      if (temp.Count > 1)
[2466]102        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.");
[2456]103      return null;
[2433]104    }
105
[2466]106    public static IView CreateDefaultView(object objectToView) {
[2433]107      Type t = GetDefaultViewType(objectToView.GetType());
108      if (t == null)
109        return null;
110      else
[2466]111        return (IView)Activator.CreateInstance(t, objectToView);
[2433]112    }
[2467]113
114    public static IView CreateView(Type viewType) {
115      if (!typeof(IView).IsAssignableFrom(viewType))
116        throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is not of type IView.");
117      return (IView)Activator.CreateInstance(viewType);
118    }
119
120    public static IView CreateView(Type viewType, object objectToView) {
121      if (!typeof(IView).IsAssignableFrom(viewType))
122        throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is not of type IView.");
123      return (IView)Activator.CreateInstance(viewType,objectToView);
124    }
[2426]125  }
126}
Note: See TracBrowser for help on using the repository browser.