Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2553 was 2468, checked in by mkommend, 15 years ago

renamed method for determining default viewable types in ContentAttribute (ticket #771)

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