Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/28/10 16:15:21 (15 years ago)
Author:
mkommend
Message:

first version of redesigned MainForm (ticket #857)

Location:
trunk/sources/HeuristicLab.MainForm/3.2
Files:
4 added
10 edited
5 copied

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.MainForm/3.2/ActionUserInterfaceItem.cs

    r2687 r2696  
    2626
    2727namespace HeuristicLab.MainForm {
    28   public abstract class UserInterfaceItemBase : IUserInterfaceItem{
    29     protected UserInterfaceItemBase() {
     28  public abstract class ActionUserInterfaceItem : PositionableUserInterfaceItem, IActionUserInterfaceItem {
     29    protected ActionUserInterfaceItem() {
     30      MainFormManager.MainForm.ActiveViewChanged += this.OnActiveViewChanged;
     31      MainFormManager.MainForm.Changed += this.OnMainFormChanged;
     32      MainFormManager.MainForm.ViewShown += this.OnViewShown;
     33      MainFormManager.MainForm.ViewClosed += this.OnViewClosed;
    3034    }
    3135
    3236    public abstract string Name { get; }
    33     public abstract int Position { get; }
    3437
    3538    public virtual System.Drawing.Image Image {
     
    4346    public abstract void Execute();
    4447
    45     public virtual void ActiveViewChanged(object sender, EventArgs e) {
     48    protected virtual void OnActiveViewChanged(object sender, EventArgs e) {
    4649    }
    4750
    48     public virtual void ViewChanged(object sender, EventArgs e) {
     51    protected virtual void OnMainFormChanged(object sender, EventArgs e) {
    4952    }
    5053
    51     public virtual void MainFormChanged(object sender, EventArgs e) {
     54    protected virtual void OnViewChanged(object sender, EventArgs e) {
    5255    }
    5356
    54     public virtual void MainFormInitialized(object sender, EventArgs e) {
     57    private void OnViewShown(object sender, ViewShownEventArgs e) {
     58      if (e.FirstTimeShown)
     59        e.View.Changed += this.OnViewChanged;
     60    }
     61
     62    private void OnViewClosed(object sender, ViewEventArgs e) {
     63      e.View.Changed -= this.OnViewChanged;
    5564    }
    5665  }
  • trunk/sources/HeuristicLab.MainForm/3.2/ContentAttribute.cs

    r2540 r2696  
    1919 */
    2020#endregion
     21
    2122using System;
    2223using System.Collections.Generic;
     
    5051    public static bool CanViewType(Type viewType, Type content) {
    5152      ContentAttribute[] attributes = (ContentAttribute[])viewType.GetCustomAttributes(typeof(ContentAttribute), false);
    52       return attributes.Any(a => a.type.IsAssignableFrom(content));
     53      return attributes.Any(a => content.IsAssignableTo(a.type));
    5354    }
    5455
    55     public static IEnumerable<Type> GetViewableTypes(Type viewType) {
    56       ContentAttribute[] attributes = (ContentAttribute[])viewType.GetCustomAttributes(typeof(ContentAttribute), false);
    57       return from a in attributes
    58              select a.type;
    59     }
    60 
    61     public static bool IsDefaultViewForType(Type viewType, Type content) {
    62       ContentAttribute[] attributes = (ContentAttribute[])viewType.GetCustomAttributes(typeof(ContentAttribute), false);
    63       return attributes.Any(a => a.isDefaultView && a.type == content);
    64     }
    65 
    66     public static IEnumerable<Type> GetDefaultViewableTypes(Type viewType) {
     56    internal static IEnumerable<Type> GetDefaultViewableTypes(Type viewType) {
    6757      ContentAttribute[] attributes = (ContentAttribute[])viewType.GetCustomAttributes(typeof(ContentAttribute), false);
    6858      return from a in attributes
  • trunk/sources/HeuristicLab.MainForm/3.2/HeuristicLab.MainForm-3.2.csproj

    r2548 r2696  
    8181  </ItemGroup>
    8282  <ItemGroup>
     83    <Compile Include="Interfaces\IActionUserInterfaceItem.cs" />
     84    <Compile Include="Interfaces\IPositionableUserInterfaceItem.cs" />
     85    <Compile Include="PositionableUserInterfaceItem.cs" />
     86    <Compile Include="TypeExtension.cs" />
    8387    <Compile Include="ViewShownEventArgs.cs" />
    8488    <Compile Include="ViewEventArgs.cs" />
    85     <Compile Include="ToolBarSeparatorItemBase.cs" />
     89    <Compile Include="ToolBarSeparatorItem.cs" />
    8690    <Compile Include="Interfaces\IToolBarSeparatorItem.cs" />
    8791    <Compile Include="Interfaces\IMenuItem.cs" />
     
    8993    <Compile Include="Interfaces\IToolBarItem.cs" />
    9094    <Compile Include="MainFormManager.cs" />
    91     <Compile Include="MenuItemBase.cs" />
    92     <Compile Include="MenuSeparatorItemBase.cs" />
    93     <Compile Include="ToolBarItemBase.cs" />
     95    <Compile Include="MenuItem.cs" />
     96    <Compile Include="MenuSeparatorItem.cs" />
     97    <Compile Include="ToolBarItem.cs" />
    9498    <Compile Include="HeuristicLabMainFormPlugin.cs" />
    9599    <Compile Include="Interfaces\IMainForm.cs" />
     
    97101    <Compile Include="Interfaces\IView.cs" />
    98102    <Compile Include="Properties\AssemblyInfo.cs" />
    99     <Compile Include="UserInterfaceItemBase.cs" />
     103    <Compile Include="ActionUserInterfaceItem.cs" />
    100104    <Compile Include="ContentAttribute.cs" />
    101105  </ItemGroup>
  • trunk/sources/HeuristicLab.MainForm/3.2/Interfaces/IMainForm.cs

    r2548 r2696  
    2525using System.Text;
    2626using System.Drawing;
     27using System.ComponentModel;
    2728
    2829namespace HeuristicLab.MainForm {
     
    3536    event EventHandler ActiveViewChanged;
    3637    event EventHandler Changed;
    37    
     38
    3839    event EventHandler<ViewEventArgs> ViewClosed;
    3940    event EventHandler<ViewShownEventArgs> ViewShown;
     
    4142
    4243    Type UserInterfaceItemType { get; }
    43     void ShowView(IView view);
    44     void HideView(IView view);
    45     void CloseView(IView view);
    4644    void CloseAllViews();
    4745    void Close();
  • trunk/sources/HeuristicLab.MainForm/3.2/Interfaces/IMenuItem.cs

    r2458 r2696  
    2727
    2828namespace HeuristicLab.MainForm {
    29   public interface IMenuItem : IUserInterfaceItem {
    30     IEnumerable<string> Structure { get; }
     29  public interface IMenuItem : IActionUserInterfaceItem {
    3130  }
    3231}
  • trunk/sources/HeuristicLab.MainForm/3.2/Interfaces/IMenuSeparatorItem.cs

    r2514 r2696  
    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
     21
     22using System;
    223using System.Collections.Generic;
    324using System.Linq;
     
    526
    627namespace HeuristicLab.MainForm {
    7   public interface IMenuSeparatorItem : IMenuItem {
     28  public interface IMenuSeparatorItem : IPositionableUserInterfaceItem {
    829  }
    930}
  • trunk/sources/HeuristicLab.MainForm/3.2/Interfaces/IToolBarItem.cs

    r2458 r2696  
    2626
    2727namespace HeuristicLab.MainForm {
    28   public interface IToolBarItem : IUserInterfaceItem {
    29     IEnumerable<string> Structure { get; }
     28  public interface IToolBarItem : IActionUserInterfaceItem {
    3029  }
    3130}
  • trunk/sources/HeuristicLab.MainForm/3.2/Interfaces/IToolBarSeparatorItem.cs

    r2514 r2696  
    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
     21
     22using System;
    223using System.Collections.Generic;
    324using System.Linq;
     
    526
    627namespace HeuristicLab.MainForm {
    7   public interface IToolBarSeparatorItem : IToolBarItem {
     28  public interface IToolBarSeparatorItem : IPositionableUserInterfaceItem {
    829  }
    930}
  • trunk/sources/HeuristicLab.MainForm/3.2/Interfaces/IUserInterfaceItem.cs

    r2541 r2696  
    2828namespace HeuristicLab.MainForm {
    2929  public interface IUserInterfaceItem {
    30     string Name { get; }
    31     int Position { get; }
    32     Image Image { get; }
    33     string ToolTipText { get; }
    34 
    35     void Execute();
    36 
    37     void ActiveViewChanged(object sender, EventArgs e);
    38     void ViewChanged(object sender, EventArgs e);
    39     void MainFormChanged(object sender, EventArgs e);
    40     void MainFormInitialized(object sender, EventArgs e);
    4130  }
    4231}
  • trunk/sources/HeuristicLab.MainForm/3.2/Interfaces/IView.cs

    r2466 r2696  
    2727namespace HeuristicLab.MainForm {
    2828  public interface IView {
     29
    2930    string Caption { get; set; }
    3031    event EventHandler CaptionChanged;
    3132    event EventHandler Changed;
    32     void OnClosing(object sender, CancelEventArgs e);
    33     void OnClosed(object sender, EventArgs e);
     33
     34    void Show();
     35    void Hide();
     36    void Close();
    3437  }
    3538}
  • trunk/sources/HeuristicLab.MainForm/3.2/MainFormManager.cs

    r2591 r2696  
    1919 */
    2020#endregion
     21
    2122using System;
    2223using System.Collections.Generic;
     
    2425using System.Text;
    2526using HeuristicLab.PluginInfrastructure;
     27using System.Diagnostics;
    2628
    2729namespace HeuristicLab.MainForm {
     30  class StringDict<T> : Dictionary<string, T> {
     31  }
    2832  public static class MainFormManager {
    2933    private static object locker;
     
    3640      views = new HashSet<Type>();
    3741      defaultViews = new Dictionary<Type, Type>();
     42
     43      Type listString = typeof(List<string>);
     44      Type list = typeof(List<>);
     45      Type ilist = typeof(IList<>);
     46      Type stringStringDict = typeof(Dictionary<string, string>);
     47      Type dict = typeof(Dictionary<,>);
     48      Type stringDict = typeof(StringDict<>);
     49      Type stringIntDict = typeof(StringDict<int>);
     50      Type stringStringDict2 = typeof(StringDict<string>);
     51
     52      bool result;
     53      result = listString.IsAssignableTo(list);
     54      Debug.Assert(result);
     55
     56      Debug.Assert(stringStringDict2.IsAssignableTo(stringDict));
     57      Debug.Assert(stringStringDict2.IsAssignableTo(stringStringDict));
     58      Debug.Assert(stringStringDict2.IsAssignableTo(dict));
     59      Debug.Assert(!stringStringDict2.IsAssignableTo(stringIntDict));
     60      Debug.Assert(!stringIntDict.IsAssignableTo(stringStringDict));
     61
     62      result = list.IsAssignableTo(ilist);
     63      Debug.Assert(result);
     64      result = listString.IsAssignableTo(ilist);
     65      Debug.Assert(result);
     66      result = list.IsAssignableTo(listString);
     67      Debug.Assert(!result);
     68      result = ilist.IsAssignableTo(listString);
     69      Debug.Assert(!result);
     70      result = ilist.IsAssignableTo(list);
     71      Debug.Assert(!result);
     72
     73      Type stackedListList = list.MakeGenericType(typeof(List<>));
     74      Type stackedListListint = typeof(List<List<int>>);
     75      Type istackedListListint = typeof(IList<List<int>>);
     76      Type stackedListIListint = typeof(List<IList<int>>);
     77      Debug.Assert(stackedListListint.IsAssignableTo(list));
     78      Debug.Assert(stackedListListint.IsAssignableTo(ilist));
     79      Debug.Assert(!stackedListListint.IsAssignableTo(listString));
     80      Debug.Assert(stackedListListint.IsAssignableTo(istackedListListint));
     81      Debug.Assert(!stackedListListint.IsAssignableTo(stackedListIListint));
    3882    }
    3983
    4084    public static void RegisterMainForm(IMainForm mainform) {
    4185      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             }
     86        if (MainFormManager.mainform != null)
     87          throw new ArgumentException("A mainform was already associated with the mainform manager.");
     88        if (mainform == null)
     89          throw new ArgumentException("Could not associate null as a mainform in the mainform manager.");
     90
     91        MainFormManager.mainform = mainform;
     92        IEnumerable<Type> types =
     93          from t in ApplicationManager.Manager.GetTypes(typeof(IView))
     94          where !t.IsAbstract && !t.IsInterface && ContentAttribute.HasContentAttribute(t)
     95          select t;
     96
     97        foreach (Type viewType in types) {
     98          views.Add(viewType);
     99          foreach (Type contentType in ContentAttribute.GetDefaultViewableTypes(viewType)) {
     100            if (defaultViews.ContainsKey(contentType))
     101              throw new ArgumentException("DefaultView for type " + contentType + " is " + defaultViews[contentType] +
     102                ". Can't register additional DefaultView " + viewType + ".");
     103            defaultViews[contentType] = viewType;
    58104          }
    59         } else
    60           throw new ArgumentException("A mainform was already associated with the mainform manager.");
     105        }
    61106      }
    62107    }
     
    87132      //check base classes for default view
    88133      Type type = contentType;
    89       while (type.BaseType != null && !defaultViews.ContainsKey(type)) {
     134      while (type != null) {
     135        foreach (Type defaultViewType in defaultViews.Keys) {
     136          if (type == defaultViewType || type.CheckGenericTypes(defaultViewType))
     137            return defaultViews[defaultViewType];
     138        }
    90139        type = type.BaseType;
    91140      }
    92       if (defaultViews.ContainsKey(type))
    93         return defaultViews[type];
    94 
    95       //check if exact one implemented interface has a default view
     141
     142      //check if exactly one implemented interface has a default view
    96143      List<Type> temp = (from t in defaultViews.Keys
    97                          where t.IsAssignableFrom(contentType) && t.IsInterface
     144                         where t.IsInterface && contentType.IsAssignableTo(t)
    98145                         select t).ToList();
    99146      if (temp.Count == 1)
     
    109156      if (t == null)
    110157        return null;
    111       else
    112         return (IView)Activator.CreateInstance(t, objectToView);
     158
     159      Type viewType = TransformGenericTypeDefinition(t, objectToView);
     160      if (viewType == null)
     161        return null;
     162
     163      return (IView)Activator.CreateInstance(viewType, objectToView);
    113164    }
    114165
     
    116167      if (!typeof(IView).IsAssignableFrom(viewType))
    117168        throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is not of type IView.");
     169      if (viewType.IsGenericTypeDefinition)
     170        throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is a generic type definition.");
     171
    118172      return (IView)Activator.CreateInstance(viewType);
    119173    }
     
    122176      if (!typeof(IView).IsAssignableFrom(viewType))
    123177        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);
     178
     179      Type t = TransformGenericTypeDefinition(viewType, objectToView);
     180      if (t == null)
     181        return null;
     182
     183      return (IView)Activator.CreateInstance(t, objectToView);
     184    }
     185
     186    private static Type TransformGenericTypeDefinition(Type type, object objectToView) {
     187      if (!type.IsGenericTypeDefinition)
     188        return type;
     189
     190      Type[] typeGenericArguments = type.GetGenericArguments();
     191      Type[] objectGenericArguments = objectToView.GetType().GetGenericArguments();
     192
     193      for (int i = 0; i < typeGenericArguments.Length; i++) {
     194        foreach (Type typeConstraint in typeGenericArguments[i].GetGenericParameterConstraints()) {
     195          if (!typeConstraint.IsAssignableFrom(objectGenericArguments[i]))
     196            return null;
     197        }
     198      }
     199
     200      Type t = type.MakeGenericType(objectToView.GetType().GetGenericArguments());
     201      return t;
    125202    }
    126203  }
  • trunk/sources/HeuristicLab.MainForm/3.2/MenuItem.cs

    r2689 r2696  
    2626
    2727namespace HeuristicLab.MainForm {
    28   public abstract class MenuItemBase : UserInterfaceItemBase, IMenuItem{
    29     public virtual IEnumerable<string> Structure {
    30       get { return Enumerable.Empty<string>(); }
    31     }   
     28  public abstract class MenuItem : ActionUserInterfaceItem, IMenuItem{
    3229  }
    3330}
  • trunk/sources/HeuristicLab.MainForm/3.2/MenuSeparatorItem.cs

    r2689 r2696  
    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
     21
     22using System;
    223using System.Collections.Generic;
    324using System.Linq;
     
    526
    627namespace HeuristicLab.MainForm {
    7   public abstract class MenuSeparatorItemBase : IMenuSeparatorItem {
    8     public abstract IEnumerable<string> Structure { get; }
    9     public abstract int Position { get; }
    10 
    11 
    12     public string Name {
    13       get { return string.Empty; }
    14     }
    15 
    16     public System.Drawing.Image Image {
    17       get { return null; }
    18     }
    19 
    20     public string ToolTipText {
    21       get { return string.Empty; }
    22     }
    23 
    24     public void Execute() {
    25       throw new NotImplementedException();
    26     }
    27 
    28     public void ActiveViewChanged(object sender, EventArgs e) {
    29       throw new NotImplementedException();
    30     }
    31 
    32     public void ViewChanged(object sender, EventArgs e) {
    33       throw new NotImplementedException();
    34     }
    35 
    36     public void MainFormChanged(object sender, EventArgs e) {
    37       throw new NotImplementedException();
    38     }
    39 
    40     public void MainFormInitialized(object sender, EventArgs e) {
    41       throw new NotImplementedException();
    42     }
     28  public abstract class MenuSeparatorItem : PositionableUserInterfaceItem, IMenuSeparatorItem {
    4329  }
    4430}
  • trunk/sources/HeuristicLab.MainForm/3.2/ToolBarItem.cs

    r2689 r2696  
    2626
    2727namespace HeuristicLab.MainForm {
    28   public abstract class ToolBarItemBase : UserInterfaceItemBase, IToolBarItem {
    29     public virtual IEnumerable<string> Structure {
    30       get { return Enumerable.Empty<string>(); }
    31     }   
     28  public abstract class ToolBarItem : ActionUserInterfaceItem, IToolBarItem {
    3229  }
    3330}
  • trunk/sources/HeuristicLab.MainForm/3.2/ToolBarSeparatorItem.cs

    r2689 r2696  
    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
     21
     22using System;
    223using System.Collections.Generic;
    324using System.Linq;
     
    526
    627namespace HeuristicLab.MainForm {
    7   public abstract class ToolBarSeparatorItemBase : IToolBarSeparatorItem {
    8     public abstract int Position { get; }
    9 
    10     public IEnumerable<string> Structure {
    11       get { return new List<string>(); }
    12     }
    13 
    14     public string Name {
    15       get { return string.Empty; }
    16     }
    17 
    18     public System.Drawing.Image Image {
    19       get { return null; }
    20     }
    21 
    22     public string ToolTipText {
    23       get { return string.Empty; }
    24     }
    25 
    26     public void Execute() {
    27       throw new NotImplementedException();
    28     }
    29 
    30     public void ActiveViewChanged(object sender, EventArgs e) {
    31       throw new NotImplementedException();
    32     }
    33 
    34     public void ViewChanged(object sender, EventArgs e) {
    35       throw new NotImplementedException();
    36     }
    37 
    38     public void MainFormChanged(object sender, EventArgs e) {
    39       throw new NotImplementedException();
    40     }
    41 
    42     public void MainFormInitialized(object sender, EventArgs e) {
    43       throw new NotImplementedException();
    44     }
     28  public abstract class ToolBarSeparatorItem : PositionableUserInterfaceItem,IToolBarSeparatorItem {
    4529  }
    4630}
Note: See TracChangeset for help on using the changeset viewer.