Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/07/10 14:36:22 (14 years ago)
Author:
mkommend
Message:
  • refactored ViewHost and various views to use fewer nested controls
  • added UnitTests for ContentViews to ensure proper using of the ContentAttribute
  • fixed some views which could not handle null as Content

(ticket #972)

Location:
trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3
Files:
8 added
2 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/HeuristicLab.MainForm.WindowsForms-3.3.csproj

    r3832 r4011  
    164164      <DependentUpon>ViewHost.cs</DependentUpon>
    165165    </Compile>
    166     <Compile Include="ViewHostPanel.cs">
    167       <SubType>Component</SubType>
    168     </Compile>
    169     <Compile Include="ViewHostPanel.Designer.cs">
    170       <DependentUpon>ViewHostPanel.cs</DependentUpon>
    171     </Compile>
    172166  </ItemGroup>
    173167  <ItemGroup>
  • trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/HeuristicLabMainFormWindowsFormsPlugin.cs.frame

    r3702 r4011  
    3232  [PluginDependency("HeuristicLab.MainForm", "3.3")]
    3333  [PluginDependency("HeuristicLab.WinFormsUI", "2.3.1")]
    34   public class HeuristicLabMainFormTestPlugin : PluginBase {
     34  public class HeuristicLabMainFormPlugin : PluginBase {
    3535  }
    3636}
  • trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/ViewHost.Designer.cs

    r3658 r4011  
    4646    private void InitializeComponent() {
    4747      this.components = new System.ComponentModel.Container();
    48       this.viewPanel = new HeuristicLab.MainForm.WindowsForms.ViewHostPanel();
    4948      this.messageLabel = new System.Windows.Forms.Label();
    5049      this.viewsLabel = new System.Windows.Forms.Label();
     
    5251      this.toolTip = new System.Windows.Forms.ToolTip(this.components);
    5352      this.SuspendLayout();
    54       //
    55       // viewPanel
    56       //
    57       this.viewPanel.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
    58                   | System.Windows.Forms.AnchorStyles.Left)
    59                   | System.Windows.Forms.AnchorStyles.Right)));
    60       this.viewPanel.Location = new System.Drawing.Point(0, 0);
    61       this.viewPanel.Name = "viewPanel";
    62       this.viewPanel.Size = new System.Drawing.Size(205, 184);
    63       this.viewPanel.TabIndex = 1;
    64       this.viewPanel.Resize += new System.EventHandler(this.viewPanel_Resize);
    6553      //
    6654      // messageLabel
     
    8068      this.viewsLabel.Image = HeuristicLab.Common.Resources.VS2008ImageLibrary.Windows;
    8169      this.viewsLabel.Location = new System.Drawing.Point(211, 0);
     70      this.viewsLabel.Margin = new System.Windows.Forms.Padding(3);
    8271      this.viewsLabel.Name = "viewsLabel";
    8372      this.viewsLabel.Size = new System.Drawing.Size(16, 16);
     
    10089      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    10190      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    102       this.Controls.Add(this.viewPanel);
    10391      this.Controls.Add(this.viewsLabel);
    10492      this.Controls.Add(this.messageLabel);
     
    10694      this.Size = new System.Drawing.Size(227, 184);
    10795      this.ResumeLayout(false);
    108 
    10996    }
    11097
    11198    #endregion
    112 
    113     private ViewHostPanel viewPanel;
    11499    private System.Windows.Forms.Label viewsLabel;
    115100    private System.Windows.Forms.Label messageLabel;
  • trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/ViewHost.cs

    r3924 r4011  
    3333      InitializeComponent();
    3434      cachedViews = new Dictionary<Type, IContentView>();
    35       viewType = null;
    3635      startDragAndDrop = false;
    3736      viewContextMenuStrip.IgnoredViewTypes = new List<Type>() { typeof(ViewHost) };
     37
     38      viewType = null;
    3839      activeView = null;
    3940      Content = null;
    40       OnContentChanged();
    41     }
    42 
    43     private bool viewShown;
     41      messageLabel.Visible = false;
     42      viewsLabel.Visible = false;
     43    }
     44
    4445    private Dictionary<Type, IContentView> cachedViews;
    4546    public IEnumerable<IContentView> Views {
     
    5758            if (view != null)
    5859              view.OnHidden(EventArgs.Empty);
     60            if (ActiveViewControl != null)
     61              ActiveViewControl.Visible = false;
    5962          }
    6063          activeView = value;
    6164          if (activeView != null) {
    62             ViewType = activeView.GetType();
     65            viewType = activeView.GetType();
    6366            RegisterActiveViewEvents();
    6467            View view = activeView as View;
    6568            if (view != null)
    6669              view.OnShown(new ViewShownEventArgs(view, false));
     70            if (ActiveViewControl != null) {
     71              ActiveViewControl.Visible = true;
     72              ActiveViewControl.BringToFront();
     73            }
    6774          }
    68           ActiveViewChanged();
    69         }
    70       }
    71     }
     75          OnActiveViewChanged();
     76        }
     77      }
     78    }
     79
     80    private Control ActiveViewControl {
     81      get { return ActiveView as Control; }
     82    }
     83
    7284    private Type viewType;
    7385    public Type ViewType {
     
    7587      set {
    7688        if (viewType != value) {
    77           if (value != null && !ViewCanShowContent(value, Content))
     89          if (value != null && Content != null && !ViewCanShowContent(value, Content))
    7890            throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".",
    7991                                                      value, Content.GetType()));
     
    96108    protected override void OnContentChanged() {
    97109      viewContextMenuStrip.Item = Content;
    98 
    99       if (Content != null) {
    100         if (viewContextMenuStrip.Items.Count == 0) {
    101           messageLabel.Visible = true;
    102           viewsLabel.Visible = false;
    103           viewPanel.Visible = false;
    104         } else {
    105           messageLabel.Visible = false;
    106           viewsLabel.Visible = true;
    107           viewPanel.Visible = true;
    108         }
    109 
     110      //remove cached views which cannot show the content
     111      foreach (Type type in cachedViews.Keys.ToList()) {
     112        if (!ViewCanShowContent(type, Content)) {
     113          Control c = cachedViews[type] as Control;
     114          if (c != null) {
     115            this.Controls.Remove(c);
     116            c.Dispose();
     117          }
     118          cachedViews.Remove(type);
     119        }
     120      }
     121
     122      //change ViewType if view of ViewType can not show content or is null
     123      if (Content != null && !ViewCanShowContent(viewType, Content)) {
    110124        Type defaultViewType = MainFormManager.GetDefaultViewType(Content.GetType());
    111         if (ViewType == null || !ViewCanShowContent(viewType, Content)) {
    112           cachedViews.Clear();
    113           if (defaultViewType == null) {
    114             if (viewContextMenuStrip.Items.Count > 0)  // create first available view if default view is not available
    115               ViewType = (Type)viewContextMenuStrip.Items[0].Tag;
    116           } else ViewType = defaultViewType;
    117         } else {
    118           foreach (Type type in cachedViews.Keys.ToList()) {
    119             if (!ViewCanShowContent(type, Content)) {
    120               Control c = cachedViews[type] as Control;
    121               if (c != null)
    122                 c.Dispose();
    123               cachedViews.Remove(type);
    124             }
    125           }
    126         }
    127         UpdateActiveMenuItem();
    128         foreach (IContentView view in cachedViews.Values)
    129           view.Content = this.Content;
     125        if (defaultViewType != null)
     126          ViewType = defaultViewType;
     127        else if (viewContextMenuStrip.Items.Count > 0)  // create first available view if no default view is available
     128          ViewType = (Type)viewContextMenuStrip.Items[0].Tag;
     129        else
     130          ViewType = null;
     131      }
     132
     133      foreach (IContentView view in cachedViews.Values)
     134        view.Content = this.Content;
     135
     136      if (Content != null && viewType != null)
     137        ActiveView = cachedViews[viewType];
     138      else
     139        ActiveView = null;
     140
     141      if (Content != null && viewContextMenuStrip.Items.Count > 0) {
     142        messageLabel.Visible = false;
     143        viewsLabel.Visible = true;
     144      } else if (Content != null) {
     145        messageLabel.Visible = true;
     146        viewsLabel.Visible = false;
    130147      } else {
    131148        messageLabel.Visible = false;
    132149        viewsLabel.Visible = false;
    133         viewPanel.Visible = false;
    134150      }
    135151    }
    136152
    137153    private void OnViewTypeChanged() {
    138       for (int i = viewPanel.Controls.Count - 1; i > 0; i--)
    139         viewPanel.Controls[i].Dispose();
    140       viewPanel.Controls.Clear();
    141 
    142       if (viewType == null || Content == null)
    143         return;
    144       if (!ViewCanShowContent(viewType, Content))
    145         throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
    146                                                           viewType, Content.GetType()));
    147       if (viewPanel.Height <= 10 || viewPanel.Width <= 10) {
    148         viewShown = false;
    149         return;
    150       }
    151 
    152       viewShown = true;
    153       UpdateActiveMenuItem();
    154       IContentView view;
    155       if (cachedViews.ContainsKey(ViewType))
    156         view = cachedViews[ViewType];
    157       else {
    158         view = MainFormManager.CreateView(viewType);
    159         view.ReadOnly = this.ReadOnly;
    160         view.Locked = this.Locked;
    161         view.Content = Content;
    162         cachedViews.Add(viewType, view);
    163       }
    164       this.ActiveView = view;
    165       Control control = (Control)view;
    166       control.Dock = DockStyle.Fill;
    167       viewPanel.Controls.Add(control);
    168       viewPanel.Visible = true;
    169     }
    170 
    171     private void viewPanel_Resize(object sender, EventArgs e) {
    172       if (!viewShown)
    173         this.OnViewTypeChanged();
     154      if (viewType != null) {
     155        if (!ViewCanShowContent(viewType, Content))
     156          throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
     157                                                            viewType, Content.GetType()));
     158        IContentView view;
     159        if (!cachedViews.ContainsKey(ViewType)) {
     160          view = MainFormManager.CreateView(viewType);
     161          view.ReadOnly = this.ReadOnly;
     162          view.Locked = this.Locked;
     163          view.Content = Content;
     164          cachedViews.Add(viewType, view);
     165          Control c = view as Control;
     166          if (c != null)
     167            this.Controls.Add(c);
     168        }
     169        UpdateActiveMenuItem();
     170        ActiveView = cachedViews[viewType];
     171      }
     172    }
     173
     174    private void OnActiveViewChanged() {
     175      this.SuspendRepaint();
     176      if (activeView != null) {
     177        UpdateActiveMenuItem();
     178        this.ActiveViewControl.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
     179        this.ActiveViewControl.Size = new System.Drawing.Size(this.Width - this.viewsLabel.Width - this.viewsLabel.Margin.Left - this.viewsLabel.Margin.Right, this.Height);
     180        this.ActiveViewChanged();
     181      }
     182      this.ResumeRepaint(true);
     183    }
     184
     185    protected override void OnSizeChanged(EventArgs e) {
     186      //mkommend: solution to resizing issues. taken from http://support.microsoft.com/kb/953934
     187      //not implemented with a panel to reduce the number of nested controls
     188      if (this.Handle != null)
     189        this.BeginInvoke((Action<EventArgs>)OnSizeChangedHelper, e);
     190    }
     191    private void OnSizeChangedHelper(EventArgs e) {
     192      base.OnSizeChanged(e);
     193      this.viewsLabel.Location = new System.Drawing.Point(this.Width - this.viewsLabel.Margin.Right - this.viewsLabel.Width, this.viewsLabel.Margin.Top);
    174194    }
    175195
     
    194214      }
    195215    }
     216
    196217    #region forwarding of view events
    197218    protected override void OnReadOnlyChanged() {
Note: See TracChangeset for help on using the changeset viewer.