Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.MainForm.WindowsForms/3.2/ViewHost.cs @ 3361

Last change on this file since 3361 was 3361, checked in by mkommend, 14 years ago

added operator graph tab page to EngineAlgorithmView (ticket #973)

File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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;
23using System.Collections.Generic;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.MainForm;
27
28namespace HeuristicLab.MainForm.WindowsForms {
29  public sealed partial class ViewHost : UserControl {
30    private Dictionary<Type, IView> cachedViews;
31    public ViewHost() {
32      InitializeComponent();
33      viewType = null;
34      content = null;
35      cachedViews = new Dictionary<Type, IView>();
36      Initialize();
37    }
38    public ViewHost(bool readOnly)
39      : this() {
40      this.ReadOnly = readOnly;
41    }
42
43    private Type viewType;
44    public Type ViewType {
45      get { return this.viewType; }
46      set {
47        if (viewType != value) {
48          if (value != null && !ViewCanShowContent(value, content))
49            throw new ArgumentException(string.Format("View \"{0}\" cannot display content \"{1}\".",
50                                                      value, content.GetType()));
51          viewType = value;
52          UpdateView();
53        }
54      }
55    }
56
57    private object content;
58    public object Content {
59      get { return content; }
60      set {
61        if (value != content) {
62          content = value;
63          viewContextMenuStrip.Item = content;
64          cachedViews.Clear();
65          this.viewsLabel.Enabled = value != null;
66          Initialize();
67        }
68      }
69    }
70
71    public new bool Enabled {
72      get { return base.Enabled; }
73      set {
74        this.SuspendRepaint();
75        base.Enabled = value;
76        if (Content != null && value)
77          this.viewsLabel.Enabled = value;
78        this.ResumeRepaint(true);
79      }
80    }
81    private bool readOnly;
82    public bool ReadOnly {
83      get { return this.readOnly; }
84      set {
85        if (InvokeRequired) {
86          Action<bool> action = delegate(bool b) { this.ReadOnly = b; };
87          Invoke(action, value);
88        } else {
89          if (value != readOnly) {
90            readOnly = value;
91            OnReadOnlyChanged();
92          }
93        }
94      }
95    }
96
97    public event EventHandler ReadOnlyChanged;
98    private void OnReadOnlyChanged() {
99      if (InvokeRequired)
100        Invoke((MethodInvoker)OnReadOnlyChanged);
101      else {
102        EventHandler handler = ReadOnlyChanged;
103        if (handler != null)
104          handler(this, EventArgs.Empty);
105        foreach (IView view in cachedViews.Values)
106          view.ReadOnly = this.readOnly;
107      }
108    }
109
110    private void Initialize() {
111      viewsLabel.Visible = false;
112      viewPanel.Visible = false;
113      if (viewPanel.Controls.Count > 0) viewPanel.Controls[0].Dispose();
114      viewPanel.Controls.Clear();
115
116      if (Content != null) {
117        if (viewContextMenuStrip.Items.Count == 0)
118          messageLabel.Visible = true;
119        else
120          viewsLabel.Visible = true;
121
122        if (!ViewCanShowContent(viewType, Content)) {
123          viewType = MainFormManager.GetDefaultViewType(Content.GetType());
124          if ((viewType == null) && (viewContextMenuStrip.Items.Count > 0))  // create first available view if default view is not available
125            viewType = (Type)viewContextMenuStrip.Items[0].Tag;
126        }
127        UpdateView();
128      }
129    }
130
131    private void UpdateView() {
132      if (viewPanel.Controls.Count > 0) viewPanel.Controls[0].Dispose();
133      viewPanel.Controls.Clear();
134
135      if (viewType == null || content == null)
136        return;
137
138      if (!ViewCanShowContent(viewType, content))
139        throw new InvalidOperationException(string.Format("View \"{0}\" cannot display content \"{1}\".",
140                                                          viewType, Content.GetType()));
141
142      UpdateActiveMenuItem();
143      cachedViews.Clear();
144      Control view = (Control)MainFormManager.CreateView(viewType, Content, ReadOnly);
145      cachedViews.Add(viewType, ((IView)view));
146      view.Dock = DockStyle.Fill;
147      viewPanel.Controls.Add(view);
148      viewPanel.Visible = true;
149    }
150
151    private void UpdateActiveMenuItem() {
152      foreach (KeyValuePair<Type, ToolStripMenuItem> item in viewContextMenuStrip.MenuItems) {
153        if (item.Key == viewType) {
154          item.Value.Checked = true;
155          item.Value.Enabled = false;
156        } else {
157          item.Value.Checked = false;
158          item.Value.Enabled = true;
159        }
160      }
161    }
162
163    private bool ViewCanShowContent(Type viewType, object content) {
164      if (content == null) // every view can display null
165        return true;
166      if (viewType == null)
167        return false;
168      return ContentAttribute.CanViewType(viewType, Content.GetType()) && viewContextMenuStrip.MenuItems.Any(item => item.Key == viewType);
169    }
170
171    private void viewsLabel_DoubleClick(object sender, EventArgs e) {
172      MainFormManager.CreateView(viewType, Content, ReadOnly).Show();
173    }
174
175    private void viewContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
176      Type viewType = (Type)e.ClickedItem.Tag;
177      ViewType = viewType;
178    }
179  }
180}
Note: See TracBrowser for help on using the repository browser.