Changeset 4299 for trunk/sources
- Timestamp:
- 08/24/10 14:24:30 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/ViewHost.cs
r4158 r4299 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using System.Reflection;26 25 using System.Windows.Forms; 27 26 using HeuristicLab.Common; … … 32 31 public ViewHost() { 33 32 InitializeComponent(); 34 cachedViews = new Dictionary<Type, IContentView>();35 33 startDragAndDrop = false; 36 34 viewContextMenuStrip.IgnoredViewTypes = new List<Type>() { typeof(ViewHost) }; … … 41 39 messageLabel.Visible = false; 42 40 viewsLabel.Visible = false; 43 }44 45 private Dictionary<Type, IContentView> cachedViews;46 public IEnumerable<IContentView> Views {47 get { return cachedViews.Values; }48 41 } 49 42 … … 56 49 DeregisterActiveViewEvents(); 57 50 View view = activeView as View; 58 if (view != null) 51 if (view != null) { 59 52 view.OnHidden(EventArgs.Empty); 60 if (ActiveViewControl != null) 61 ActiveViewControl.Visible = false; 53 Controls.Remove(view); 54 view.Dispose(); 55 } 62 56 } 63 57 activeView = value; … … 66 60 RegisterActiveViewEvents(); 67 61 View view = activeView as View; 68 if (view != null) 62 if (view != null) { 69 63 view.OnShown(new ViewShownEventArgs(view, false)); 70 if (ActiveViewControl != null) { 71 ActiveViewControl.Visible = true; 72 ActiveViewControl.BringToFront(); 64 Controls.Add(view); 73 65 } 74 66 } else viewType = null; … … 98 90 get { return base.Enabled; } 99 91 set { 100 this.SuspendRepaint();101 92 base.Enabled = value; 102 93 this.viewsLabel.Enabled = value; 103 this.ResumeRepaint(true);104 }105 }106 107 public void ClearCache() {108 foreach (var cachedView in cachedViews.ToArray()) {109 if (cachedView.Value != activeView) {110 Control c = cachedView.Value as Control;111 if (c != null) {112 this.Controls.Remove(c);113 c.Dispose();114 }115 cachedViews.Remove(cachedView.Key);116 }117 94 } 118 95 } … … 120 97 protected override void OnContentChanged() { 121 98 viewContextMenuStrip.Item = Content; 122 //remove cached views which cannot show the content123 foreach (Type type in cachedViews.Keys.ToList()) {124 if (!ViewCanShowContent(type, Content)) {125 Control c = cachedViews[type] as Control;126 if (c != null) {127 this.Controls.Remove(c);128 c.Dispose();129 }130 cachedViews.Remove(type);131 }132 }133 134 99 //change ViewType if view of ViewType can not show content or is null 135 if (Content != null && !ViewCanShowContent(viewType, Content)) { 136 Type defaultViewType = MainFormManager.GetDefaultViewType(Content.GetType()); 137 if (defaultViewType != null) 138 ViewType = defaultViewType; 139 else if (viewContextMenuStrip.Items.Count > 0) // create first available view if no default view is available 140 ViewType = (Type)viewContextMenuStrip.Items[0].Tag; 141 else 142 ViewType = null; 143 } 144 145 foreach (IContentView view in cachedViews.Values) 146 view.Content = this.Content; 147 148 if (Content != null && viewType != null) 149 ActiveView = cachedViews[viewType]; 150 else 151 ActiveView = null; 152 100 if (Content != null) { 101 if (!ViewCanShowContent(viewType, Content)) { 102 Type defaultViewType = MainFormManager.GetDefaultViewType(Content.GetType()); 103 if (defaultViewType != null) 104 ViewType = defaultViewType; 105 else if (viewContextMenuStrip.Items.Count > 0) // create first available view if no default view is available 106 ViewType = (Type)viewContextMenuStrip.Items[0].Tag; 107 else 108 ViewType = null; 109 } 110 if (ActiveView != null) { 111 ActiveView.Content = Content; 112 if (ActiveViewControl != null) ActiveViewControl.Visible = true; 113 } 114 } else if (ActiveViewControl != null) 115 ActiveViewControl.Visible = false; 116 117 UpdateLabels(); 118 } 119 120 private void UpdateLabels() { 153 121 if (Content != null && viewContextMenuStrip.Items.Count > 0) { 154 122 messageLabel.Visible = false; … … 169 137 viewType, Content.GetType())); 170 138 IContentView view; 171 if (!cachedViews.ContainsKey(ViewType)) { 172 view = MainFormManager.CreateView(viewType); 173 view.ReadOnly = this.ReadOnly; 174 view.Locked = this.Locked; 175 ActiveView = view; //necessary to allow the views to change the status of the viewhost 176 view.Content = Content; 177 cachedViews.Add(viewType, view); 178 Control c = view as Control; 179 if (c != null) 180 this.Controls.Add(c); 181 } else 182 ActiveView = cachedViews[viewType]; 139 view = MainFormManager.CreateView(viewType); 140 view.ReadOnly = this.ReadOnly; 141 view.Locked = this.Locked; 142 ActiveView = view; //necessary to allow the views to change the status of the viewhost 143 view.Content = Content; 144 183 145 UpdateActiveMenuItem(); 184 146 } … … 231 193 232 194 #region forwarding of view events 233 protected override void PropagateStateChanges(Control control, Type type, System.Reflection.PropertyInfo propertyInfo) {234 if (!type.GetProperties().Contains(propertyInfo))235 throw new ArgumentException("The specified type " + type + "implement the property " + propertyInfo.Name + ".");236 if (!type.IsAssignableFrom(this.GetType()))237 throw new ArgumentException("The specified type " + type + "must be the same or a base class / interface of this object.");238 if (!propertyInfo.CanWrite)239 throw new ArgumentException("The specified property " + propertyInfo.Name + " must have a setter.");240 241 if (activeView != null) {242 Type controlType = activeView.GetType();243 PropertyInfo controlPropertyInfo = controlType.GetProperty(propertyInfo.Name, propertyInfo.PropertyType);244 if (type.IsAssignableFrom(controlType) && controlPropertyInfo != null) {245 var thisValue = propertyInfo.GetValue(this, null);246 controlPropertyInfo.SetValue(activeView, thisValue, null);247 }248 }249 }250 251 195 internal protected override void OnShown(ViewShownEventArgs e) { 252 196 base.OnShown(e); … … 263 207 internal protected override void OnClosing(FormClosingEventArgs e) { 264 208 base.OnClosing(e); 265 foreach (View view in this.Views.OfType<View>()) 209 View view = ActiveView as View; 210 if (view != null) 266 211 view.OnClosing(e); 267 212 } 268 213 internal protected override void OnClosed(FormClosedEventArgs e) { 269 214 base.OnClosed(e); 270 foreach (View view in this.Views.OfType<View>()) 215 View view = ActiveView as View; 216 if (view != null) 271 217 view.OnClosed(e); 272 218 }
Note: See TracChangeset
for help on using the changeset viewer.