Changeset 2433


Ignore:
Timestamp:
10/15/09 14:01:15 (13 years ago)
Author:
mkommend
Message:

first version of generic views (ticket #771)

Location:
branches/Mainform refactoring
Files:
1 added
17 edited

Legend:

Unmodified
Added
Removed
  • branches/Mainform refactoring/HeuristicLab.MainForm.Test/3.2/ButtonItems/NewEditorToolStripButtonItem.cs

    r2426 r2433  
    2424
    2525    public override void Execute() {
    26       new NewEditorAction().Execute(MainFormManager.Instance);
     26      new NewEditorAction().Execute(MainFormManager.MainForm);
    2727    }
    2828  }
  • branches/Mainform refactoring/HeuristicLab.MainForm.Test/3.2/ButtonItems/NewFormToolStripButtonItem.cs

    r2426 r2433  
    2424
    2525    public override void Execute() {
    26       new NewFormAction().Execute(MainFormManager.Instance);
     26      new NewFormAction().Execute(MainFormManager.MainForm);
    2727    }
    2828  }
  • branches/Mainform refactoring/HeuristicLab.MainForm.Test/3.2/ButtonItems/OpenToolStripButtonItem.cs

    r2426 r2433  
    2020
    2121    public override void Execute() {
    22       new OpenAction().Execute(MainFormManager.Instance);
     22      new OpenAction().Execute(MainFormManager.MainForm);
    2323    }
    2424  }
  • branches/Mainform refactoring/HeuristicLab.MainForm.Test/3.2/ButtonItems/SaveToolStripButtonItem.cs

    r2426 r2433  
    2020
    2121    public override void Execute() {
    22       new SaveAction().Execute(MainFormManager.Instance);
     22      new SaveAction().Execute(MainFormManager.MainForm);
    2323    }
    2424
    2525    public override void ActiveViewChanged(object sender, EventArgs e) {
    26       IMainForm mainform = MainFormManager.Instance;
     26      IMainForm mainform = MainFormManager.MainForm;
    2727      if (mainform.ActiveView == null)
    2828        this.ToolStripItem.Enabled = false;
  • branches/Mainform refactoring/HeuristicLab.MainForm.Test/3.2/EditorView.cs

    r2426 r2433  
    1010
    1111namespace HeuristicLab.MainForm.Test {
    12   public partial class EditorView : ViewBase {
     12  [DefaultView]
     13  public partial class EditorView : ViewBase<object>,IView<string> {
    1314    public EditorView() {
    1415      InitializeComponent();
     
    1617
    1718    private void ChangeStateButton_Click(object sender, EventArgs e) {
     19      IEnumerable<Type> views = MainFormManager.GetViewTypes(typeof(string));
     20      views.ToString();
     21      IEnumerable<Type> views1 = MainFormManager.GetViewTypes(typeof(object));
     22      views1.ToString();
     23      Type def1 = MainFormManager.GetDefaultViewType(typeof(string));
     24      def1.ToString();
     25      Type def2 = MainFormManager.GetDefaultViewType(typeof(object));
     26      def2.ToString();
     27
     28      IView<string> v = MainFormManager.CreateDefaultView<string>("x");
     29      v.Caption = "lala";
     30
    1831      this.OnChanged();
    1932    }
     
    2942      }
    3043    }
     44
     45    #region IView<string> Members
     46
     47    public void View(string item) {
     48      throw new NotImplementedException();
     49    }
     50
     51    #endregion
    3152  }
    3253}
  • branches/Mainform refactoring/HeuristicLab.MainForm.Test/3.2/FormView.cs

    r2426 r2433  
    1010
    1111namespace HeuristicLab.MainForm.Test {
    12   public partial class FormView : ViewBase {
     12  public partial class FormView : ViewBase<object> {
    1313    private int[] array;
    1414    public FormView() {
  • branches/Mainform refactoring/HeuristicLab.MainForm.Test/3.2/MenuItems/ExitToolStripMenuItem.cs

    r2426 r2433  
    2323
    2424    public override void Execute() {
    25       MainFormManager.Instance.Close();
     25      MainFormManager.MainForm.Close();
    2626    }
    2727  }
  • branches/Mainform refactoring/HeuristicLab.MainForm.Test/3.2/MenuItems/NewEditorToolStripMenuItem.cs

    r2426 r2433  
    2323
    2424    public override void Execute() {
    25       new NewEditorAction().Execute(MainFormManager.Instance);
     25      new NewEditorAction().Execute(MainFormManager.MainForm);
    2626    }
    2727  }
  • branches/Mainform refactoring/HeuristicLab.MainForm.Test/3.2/MenuItems/NewFormToolStripMenuItem.cs

    r2426 r2433  
    2424
    2525    public override void Execute() {
    26       new NewFormAction().Execute(MainFormManager.Instance);
     26      new NewFormAction().Execute(MainFormManager.MainForm);
    2727    }
    2828  }
  • branches/Mainform refactoring/HeuristicLab.MainForm.Test/3.2/MenuItems/OpenToolStripMenuItem.cs

    r2426 r2433  
    3030
    3131    public override void Execute() {
    32       new OpenAction().Execute(MainFormManager.Instance);
     32      new OpenAction().Execute(MainFormManager.MainForm);
    3333    }
    3434  }
  • branches/Mainform refactoring/HeuristicLab.MainForm.Test/3.2/MenuItems/SaveToolStripMenuItem.cs

    r2426 r2433  
    3131
    3232    public override void Execute() {
    33       new SaveAction().Execute(MainFormManager.Instance);
     33      new SaveAction().Execute(MainFormManager.MainForm);
    3434    }
    3535  }
  • branches/Mainform refactoring/HeuristicLab.MainForm.WindowsForms/3.2/DockForm.cs

    r2426 r2433  
    5555          Control control = (Control)View;
    5656          control.Dock = DockStyle.Fill;
     57          this.Size = control.Size;
    5758          viewPanel.Controls.Add(control);
    5859          View.CaptionChanged += new EventHandler(View_CaptionChanged);
  • branches/Mainform refactoring/HeuristicLab.MainForm.WindowsForms/3.2/ViewBase.Designer.cs

    r2426 r2433  
    2323
    2424namespace HeuristicLab.MainForm.WindowsForms {
    25   partial class ViewBase {
     25  partial class ViewBase<T> {
    2626    /// <summary>
    2727    /// Required designer variable.
  • branches/Mainform refactoring/HeuristicLab.MainForm.WindowsForms/3.2/ViewBase.cs

    r2426 r2433  
    3030
    3131namespace HeuristicLab.MainForm.WindowsForms {
    32   public partial class ViewBase : UserControl,IView {
     32  public partial class ViewBase<T> : UserControl, IView<T> {
     33    protected T item;
    3334    public ViewBase() {
    3435      InitializeComponent();
     36      item = default(T);
     37    }
     38    public virtual void View(T item) {
    3539    }
    3640
  • branches/Mainform refactoring/HeuristicLab.MainForm/3.2/HeuristicLab.MainForm-3.2.csproj

    r2426 r2433  
    9292    <Compile Include="Properties\AssemblyInfo.cs" />
    9393    <Compile Include="UserInterfaceItemBase.cs" />
     94    <Compile Include="DefaultView.cs" />
    9495  </ItemGroup>
    9596  <ItemGroup>
  • branches/Mainform refactoring/HeuristicLab.MainForm/3.2/Interfaces/IView.cs

    r2426 r2433  
    3333    void OnClosed(object sender, EventArgs e);
    3434  }
     35
     36  public interface IView<T> : IView {
     37    void View(T item);
     38  }
    3539}
  • branches/Mainform refactoring/HeuristicLab.MainForm/3.2/MainFormManager.cs

    r2426 r2433  
    1 using System;
     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;
    222using System.Collections.Generic;
    323using System.Linq;
    424using System.Text;
     25using HeuristicLab.PluginInfrastructure;
    526
    627namespace HeuristicLab.MainForm {
    7   public class MainFormManager {
    8     private MainFormManager() {
    9       MainFormManager.mainform = null;
     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>();
    1038    }
    1139
    12     private static IMainForm mainform;
    1340    public static void RegisterMainForm(IMainForm mainform) {
    14       if (MainFormManager.mainform == null)
    15         MainFormManager.mainform = mainform;
    16       else
    17         throw new ArgumentException("A mainform was already associated with the mainform manager.");
     41      lock (locker) {
     42        if (MainFormManager.mainform == null) {
     43          MainFormManager.mainform = mainform;
     44
     45          DiscoveryService ds = new DiscoveryService();
     46          Type[] types = ds.GetTypes(typeof(IView));
     47
     48          foreach (Type t in types.Where(t => !t.IsAbstract && !t.IsInterface && !t.IsGenericType)) {
     49            foreach (Type viewableType in GetViewableType(t)) {
     50              if (viewableType != null) {
     51                if (!views.ContainsKey(viewableType))
     52                  views[viewableType] = new List<Type>();
     53                views[viewableType].Add(t);
     54
     55                object[] attributes = t.GetCustomAttributes(typeof(DefaultView), false);
     56                if (attributes != null && attributes.Length == 1) {
     57                  if (defaultViews.ContainsKey(viewableType))
     58                    throw new ArgumentException("DefaultView for type " + viewableType + " is " + defaultViews[viewableType] +
     59                      ". Can't register additional DefaultView " + t + ".");
     60                  defaultViews[viewableType] = t;
     61                }
     62              }
     63            }
     64          }
     65        } else
     66          throw new ArgumentException("A mainform was already associated with the mainform manager.");
     67      }
    1868    }
    1969
    20     public static IMainForm Instance {
     70    private static IEnumerable<Type> GetViewableType(Type t) {
     71      foreach (Type interfaceType in t.GetInterfaces().Where(i => i.Namespace == "HeuristicLab.MainForm" && i.Name.StartsWith("IView"))) {
     72        if (interfaceType.IsGenericType && !interfaceType.IsGenericTypeDefinition)
     73          yield return interfaceType.GetGenericArguments()[0];
     74      }
     75    }
     76
     77    public static IMainForm MainForm {
    2178      get { return mainform; }
    2279    }
    2380
    24     public static T GetInstance<T>() where T : IMainForm {
     81    public static T GetMainForm<T>() where T : IMainForm {
    2582      return (T)mainform;
     83    }
     84
     85    public static IEnumerable<Type> GetViewTypes(Type viewableType) {
     86      List<Type> viewsForType;
     87      if (!views.ContainsKey(viewableType))
     88        viewsForType = new List<Type>();
     89      else
     90        viewsForType = views[viewableType];
     91
     92      if (viewableType.BaseType != null)
     93        viewsForType.AddRange(GetViewTypes(viewableType.BaseType));
     94      return viewsForType.Distinct();
     95    }
     96
     97    public static bool ViewCanViewObject(IView view, object o) {
     98      return GetViewTypes(o.GetType()).Contains(view.GetType());
     99    }
     100
     101    public static Type GetDefaultViewType(Type viewableType) {
     102      if (!defaultViews.ContainsKey(viewableType))
     103        return null;
     104      return defaultViews[viewableType];
     105    }
     106
     107    public static IView<T> CreateDefaultView<T>(T objectToView) {
     108      Type t = GetDefaultViewType(objectToView.GetType());
     109      if (t == null)
     110        return null;
     111      else
     112        return (IView<T>) Activator.CreateInstance(t);
    26113    }
    27114  }
Note: See TracChangeset for help on using the changeset viewer.