Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3362 was 2744, checked in by epitzer, 14 years ago

update to new PluginInfrastructure (#802)

File size: 4.9 KB
Line 
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;
22using System.Collections.Generic;
23using System.Linq;
24using System.Text;
25using HeuristicLab.PluginInfrastructure;
26
27namespace HeuristicLab.MainForm {
28  public static class MainFormManager {
29    private static object locker;
30    private static IMainForm mainform;
31    private static HashSet<Type> views;
32    private static Dictionary<Type, Type> defaultViews;
33
34    static MainFormManager() {
35      locker = new object();
36      views = new HashSet<Type>();
37      defaultViews = new Dictionary<Type, Type>();
38    }
39
40    public static void RegisterMainForm(IMainForm mainform) {
41      lock (locker) {
42        if (MainFormManager.mainform == null) {
43          MainFormManager.mainform = mainform;
44          IEnumerable<Type> types =
45            from t in ApplicationManager.Manager.GetTypes(typeof(IView))
46            where !t.IsAbstract && !t.IsInterface && !t.IsGenericType && ContentAttribute.HasContentAttribute(t)
47            select t;
48
49          foreach (Type viewType in types) {
50            views.Add(viewType);
51            foreach (Type contentType in ContentAttribute.GetDefaultViewableTypes(viewType)) {
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;
56            }
57          }
58        } else
59          throw new ArgumentException("A mainform was already associated with the mainform manager.");
60      }
61    }
62
63    public static IMainForm MainForm {
64      get { return mainform; }
65    }
66
67    public static T GetMainForm<T>() where T : IMainForm {
68      return (T)mainform;
69    }
70
71    public static IEnumerable<Type> GetViewTypes(Type contentType) {
72      return from v in views
73             where ContentAttribute.CanViewType(v, contentType)
74             select v;
75    }
76
77    public static bool ViewCanViewObject(IView view, object o) {
78      return ContentAttribute.CanViewType(view.GetType(), o.GetType());
79    }
80
81    public static Type GetDefaultViewType(Type contentType) {
82      //check if viewableType has a default view
83      if (defaultViews.ContainsKey(contentType))
84        return defaultViews[contentType];
85
86      //check base classes for default view
87      Type type = contentType;
88      while (type.BaseType != null && !defaultViews.ContainsKey(type)) {
89        type = type.BaseType;
90      }
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
96                         where t.IsAssignableFrom(contentType) && t.IsInterface
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)
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.");
103      return null;
104    }
105
106    public static IView CreateDefaultView(object objectToView) {
107      Type t = GetDefaultViewType(objectToView.GetType());
108      if (t == null)
109        return null;
110      else
111        return (IView)Activator.CreateInstance(t, objectToView);
112    }
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    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.