Changeset 2837


Ignore:
Timestamp:
02/19/10 11:45:51 (11 years ago)
Author:
mkommend
Message:

added ViewAttribute and adapted ViewContextMenuStrip (ticket #857)

Location:
trunk/sources
Files:
3 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.MainForm.WindowsForms/3.2/ViewContextMenuStrip.Designer.cs

    r2545 r2837  
    2828      // ViewContextMenuStrip
    2929      //
    30       this.ItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.ViewContextMenuStrip_ItemClicked);
    3130      this.ResumeLayout(false);
    32 
    3331    }
    3432
  • trunk/sources/HeuristicLab.MainForm.WindowsForms/3.2/ViewContextMenuStrip.cs

    r2696 r2837  
    88
    99namespace HeuristicLab.MainForm.WindowsForms {
    10   public partial class ViewContextMenuStrip : ContextMenuStrip {
    11     private object item;
     10  public sealed partial class ViewContextMenuStrip : ContextMenuStrip {
    1211    public ViewContextMenuStrip() {
    1312      InitializeComponent();
     13      this.menuItems = new Dictionary<Type, ToolStripMenuItem>();
    1414    }
    1515
    16     public ViewContextMenuStrip(object item) :this() {
    17       if (item != null) {
    18         this.item = item;
    19         IEnumerable<Type> types = MainFormManager.GetViewTypes(item.GetType());
    20         if (types != null) {
    21           foreach (Type t in types) {
    22             ToolStripMenuItem menuItem = new ToolStripMenuItem() {
    23               Text = t.Name,
    24               Tag = t
    25             };
    26             this.Items.Add(menuItem);
    27           }
     16    public ViewContextMenuStrip(object item)
     17      : this() {
     18      this.Item = item;
     19    }
     20
     21    private object item;
     22    public object Item {
     23      get { return this.item; }
     24      set {
     25        if (this.item != value) {
     26          this.item = value;
     27          this.RefreshMenuItems();
    2828        }
    2929      }
    3030    }
    3131
    32     private void ViewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
    33       if (item != null) {
    34         Type viewType = e.ClickedItem.Tag as Type;
    35         IView view = MainFormManager.CreateView(viewType, this.item);
    36         view.Show();
     32    private Dictionary<Type, ToolStripMenuItem> menuItems;
     33    public IEnumerable<KeyValuePair<Type, ToolStripMenuItem>> MenuItems {
     34      get { return this.menuItems; }
     35    }
     36
     37    private void RefreshMenuItems() {
     38      this.Items.Clear();
     39      this.menuItems.Clear();
     40
     41      if (this.item != null) {
     42        ToolStripMenuItem menuItem;
     43        IEnumerable<Type> types = MainFormManager.GetViewTypes(item.GetType());
     44        foreach (Type t in types) {
     45          menuItem = new ToolStripMenuItem();
     46          menuItem.Tag = t;
     47          menuItem.Text = ViewAttribute.GetViewName(t);
     48
     49          this.menuItems.Add(t, menuItem);
     50          this.Items.Add(menuItem);
     51        }
    3752      }
    3853    }
    39 
    4054  }
    4155}
  • trunk/sources/HeuristicLab.MainForm/3.2/HeuristicLab.MainForm-3.2.csproj

    r2754 r2837  
    8282  <ItemGroup>
    8383    <None Include="HeuristicLabMainFormPlugin.cs.frame" />
     84    <Compile Include="ViewAttribute.cs" />
    8485    <Compile Include="Interfaces\IContentView.cs" />
    8586    <Compile Include="Interfaces\IActionUserInterfaceItem.cs" />
  • trunk/sources/HeuristicLab.MainForm/3.2/ViewAttribute.cs

    r2835 r2837  
    2727
    2828namespace HeuristicLab.MainForm {
    29   [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
    30   public sealed class ContentAttribute : Attribute {
    31     public ContentAttribute(Type contentType) {
    32       this.contentType = contentType;
     29  [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
     30  public sealed class ViewAttribute : Attribute {
     31    public ViewAttribute(string name) {
     32      this.name = name;
    3333    }
    3434
    35     public ContentAttribute(Type contentType, bool isDefaultView)
    36       : this(contentType) {
    37       this.isDefaultView = isDefaultView;
     35    private string name;
     36    public string Name {
     37      get { return this.name; }
     38      set { this.name = value; }
    3839    }
    3940
    40     private bool isDefaultView;
    41     public bool IsDefaultView {
    42       get { return this.isDefaultView; }
    43       set { this.isDefaultView = value; }
    44     }
    45 
    46     private Type contentType;
    47     public Type ContentType {
    48       get { return this.contentType; }
    49     }
    50 
    51     public static bool HasContentAttribute(MemberInfo viewType) {
    52       ContentAttribute[] attributes = (ContentAttribute[])viewType.GetCustomAttributes(typeof(ContentAttribute), false);
     41    public static bool HasViewAttribute(MemberInfo viewType) {
     42      ViewAttribute[] attributes = (ViewAttribute[])viewType.GetCustomAttributes(typeof(ViewAttribute), false);
    5343      return attributes.Length != 0;
    5444    }
    5545
    56     public static bool CanViewType(MemberInfo viewType, Type content) {
    57       ContentAttribute[] attributes = (ContentAttribute[])viewType.GetCustomAttributes(typeof(ContentAttribute), false);
    58       return attributes.Any(a => content.IsAssignableTo(a.contentType));
    59     }
    60 
    61     internal static IEnumerable<Type> GetDefaultViewableTypes(Type viewType) {
    62       ContentAttribute[] attributes = (ContentAttribute[])viewType.GetCustomAttributes(typeof(ContentAttribute), false);
    63       return from a in attributes
    64              where a.isDefaultView
    65              select a.contentType;
    66     }
    67 
    68     internal static IEnumerable<Type> GetViewableTypes(Type viewType) {
    69       ContentAttribute[] attributes = (ContentAttribute[])viewType.GetCustomAttributes(typeof(ContentAttribute), false);
    70       return from a in attributes
    71              select a.contentType;
     46    public static string GetViewName(MemberInfo viewType) {
     47      ViewAttribute[] attributes = (ViewAttribute[])viewType.GetCustomAttributes(typeof(ViewAttribute), false);
     48      if (attributes.Length == 1)
     49        return attributes[0].Name;
     50      return viewType.Name;
    7251    }
    7352  }
Note: See TracChangeset for help on using the changeset viewer.