Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.MainForm/3.2/MainFormManager.cs @ 2691

Last change on this file since 2691 was 2591, checked in by gkronber, 15 years ago

Copied refactored plugin infrastructure from branch and merged changeset r2586:2589 from branch into the trunk. #799

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
45          IEnumerable<Type> types =
46            from t in ApplicationManager.Manager.GetTypes(typeof(IView))
47            where !t.IsAbstract && !t.IsInterface && !t.IsGenericType && ContentAttribute.HasContentAttribute(t)
48            select t;
49
50          foreach (Type viewType in types) {
51            views.Add(viewType);
52            foreach (Type contentType in ContentAttribute.GetDefaultViewableTypes(viewType)) {
53              if (defaultViews.ContainsKey(contentType))
54                throw new ArgumentException("DefaultView for type " + contentType + " is " + defaultViews[contentType] +
55                  ". Can't register additional DefaultView " + viewType + ".");
56              defaultViews[contentType] = viewType;
57            }
58          }
59        } else
60          throw new ArgumentException("A mainform was already associated with the mainform manager.");
61      }
62    }
63
64    public static IMainForm MainForm {
65      get { return mainform; }
66    }
67
68    public static T GetMainForm<T>() where T : IMainForm {
69      return (T)mainform;
70    }
71
72    public static IEnumerable<Type> GetViewTypes(Type contentType) {
73      return from v in views
74             where ContentAttribute.CanViewType(v, contentType)
75             select v;
76    }
77
78    public static bool ViewCanViewObject(IView view, object o) {
79      return ContentAttribute.CanViewType(view.GetType(), o.GetType());
80    }
81
82    public static Type GetDefaultViewType(Type contentType) {
83      //check if viewableType has a default view
84      if (defaultViews.ContainsKey(contentType))
85        return defaultViews[contentType];
86
87      //check base classes for default view
88      Type type = contentType;
89      while (type.BaseType != null && !defaultViews.ContainsKey(type)) {
90        type = type.BaseType;
91      }
92      if (defaultViews.ContainsKey(type))
93        return defaultViews[type];
94
95      //check if exact one implemented interface has a default view
96      List<Type> temp = (from t in defaultViews.Keys
97                         where t.IsAssignableFrom(contentType) && t.IsInterface
98                         select t).ToList();
99      if (temp.Count == 1)
100        return defaultViews[temp[0]];
101      //more than one default view for implemented interfaces are found
102      if (temp.Count > 1)
103        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.");
104      return null;
105    }
106
107    public static IView CreateDefaultView(object objectToView) {
108      Type t = GetDefaultViewType(objectToView.GetType());
109      if (t == null)
110        return null;
111      else
112        return (IView)Activator.CreateInstance(t, objectToView);
113    }
114
115    public static IView CreateView(Type viewType) {
116      if (!typeof(IView).IsAssignableFrom(viewType))
117        throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is not of type IView.");
118      return (IView)Activator.CreateInstance(viewType);
119    }
120
121    public static IView CreateView(Type viewType, object objectToView) {
122      if (!typeof(IView).IsAssignableFrom(viewType))
123        throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is not of type IView.");
124      return (IView)Activator.CreateInstance(viewType, objectToView);
125    }
126  }
127}
Note: See TracBrowser for help on using the repository browser.