Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Mainform refactoring/HeuristicLab.MainForm/3.2/MainFormManager.cs @ 2456

Last change on this file since 2456 was 2456, checked in by mkommend, 13 years ago

implemented last changes in MainForm as discussed with SWA (ticket #771)

File size: 5.1 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 Dictionary<Type, List<Type>> views;
32    private static Dictionary<Type, Type> defaultViews;
33
34    static MainFormManager() {
35      locker = new object();
36      views = new Dictionary<Type, List<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
49            select t;
50
51          foreach (Type t in types) {
52            foreach (Type viewableType in GetViewableType(t)) {
53              if (viewableType != null) {
54                if (!views.ContainsKey(viewableType))
55                  views[viewableType] = new List<Type>();
56                views[viewableType].Add(t);
57
58                if (DefaultViewAttribute.IsDefaultView(t)) {
59                  if (defaultViews.ContainsKey(viewableType))
60                    throw new ArgumentException("DefaultView for type " + viewableType + " is " + defaultViews[viewableType] +
61                      ". Can't register additional DefaultView " + t + ".");
62                  defaultViews[viewableType] = t;
63                }
64              }
65            }
66          }
67        } else
68          throw new ArgumentException("A mainform was already associated with the mainform manager.");
69      }
70    }
71
72    private static IEnumerable<Type> GetViewableType(Type t) {
73      IEnumerable<Type> interfaceTypes =
74       from type in t.GetInterfaces()
75       where type.Namespace == "HeuristicLab.MainForm" && type.Name.StartsWith("IView") &&
76             type.IsGenericType && !type.IsGenericTypeDefinition
77       select type;
78
79      foreach (Type interfaceType in interfaceTypes) {
80        yield return interfaceType.GetGenericArguments()[0];
81      }
82    }
83
84    public static IMainForm MainForm {
85      get { return mainform; }
86    }
87
88    public static T GetMainForm<T>() where T : IMainForm {
89      return (T)mainform;
90    }
91
92    public static IEnumerable<Type> GetViewTypes(Type viewableType) {
93      List<Type> viewsForType = new List<Type>();
94      foreach (KeyValuePair<Type, List<Type>> v in views) {
95        if (v.Key.IsAssignableFrom(viewableType))
96          viewsForType.AddRange(v.Value);
97      }
98      return viewsForType.Distinct();
99    }
100
101    public static bool ViewCanViewObject(IView view, object o) {
102      return GetViewTypes(o.GetType()).Contains(view.GetType());
103    }
104
105    public static Type GetDefaultViewType(Type viewableType) {
106      //check if viewableType has a default view
107      if (defaultViews.ContainsKey(viewableType))
108        return defaultViews[viewableType];
109
110      //check base classes for default view
111      Type type = viewableType;
112      while (type.BaseType != null && !defaultViews.ContainsKey(type)) {
113        type = type.BaseType;
114      }
115      if (defaultViews.ContainsKey(type))
116        return defaultViews[type];
117
118      //check if exact one implemented interface has a default view
119      List<Type> temp = (from t in defaultViews.Keys
120                         where t.IsAssignableFrom(viewableType) && t.IsInterface
121                         select t).ToList();
122      if (temp.Count == 1)
123        return defaultViews[temp[0]];
124      //more than one default view for implemented interfaces are found
125      if (temp.Count > 1)
126        throw new Exception("Could not determine which is the default view for type " + viewableType.ToString() + ". Because more than one implemented interfaces have a default view.");
127      return null;
128    }
129
130    public static IView<T> CreateDefaultView<T>(T objectToView) {
131      Type t = GetDefaultViewType(objectToView.GetType());
132      if (t == null)
133        return null;
134      else
135        return (IView<T>)Activator.CreateInstance(t);
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.