Changeset 2696 for trunk/sources/HeuristicLab.MainForm
- Timestamp:
- 01/28/10 16:15:21 (15 years ago)
- 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 26 26 27 27 namespace 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; 30 34 } 31 35 32 36 public abstract string Name { get; } 33 public abstract int Position { get; }34 37 35 38 public virtual System.Drawing.Image Image { … … 43 46 public abstract void Execute(); 44 47 45 p ublic virtual voidActiveViewChanged(object sender, EventArgs e) {48 protected virtual void OnActiveViewChanged(object sender, EventArgs e) { 46 49 } 47 50 48 p ublic virtual void ViewChanged(object sender, EventArgs e) {51 protected virtual void OnMainFormChanged(object sender, EventArgs e) { 49 52 } 50 53 51 p ublic virtual void MainFormChanged(object sender, EventArgs e) {54 protected virtual void OnViewChanged(object sender, EventArgs e) { 52 55 } 53 56 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; 55 64 } 56 65 } -
trunk/sources/HeuristicLab.MainForm/3.2/ContentAttribute.cs
r2540 r2696 19 19 */ 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; … … 50 51 public static bool CanViewType(Type viewType, Type content) { 51 52 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)); 53 54 } 54 55 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) { 67 57 ContentAttribute[] attributes = (ContentAttribute[])viewType.GetCustomAttributes(typeof(ContentAttribute), false); 68 58 return from a in attributes -
trunk/sources/HeuristicLab.MainForm/3.2/HeuristicLab.MainForm-3.2.csproj
r2548 r2696 81 81 </ItemGroup> 82 82 <ItemGroup> 83 <Compile Include="Interfaces\IActionUserInterfaceItem.cs" /> 84 <Compile Include="Interfaces\IPositionableUserInterfaceItem.cs" /> 85 <Compile Include="PositionableUserInterfaceItem.cs" /> 86 <Compile Include="TypeExtension.cs" /> 83 87 <Compile Include="ViewShownEventArgs.cs" /> 84 88 <Compile Include="ViewEventArgs.cs" /> 85 <Compile Include="ToolBarSeparatorItem Base.cs" />89 <Compile Include="ToolBarSeparatorItem.cs" /> 86 90 <Compile Include="Interfaces\IToolBarSeparatorItem.cs" /> 87 91 <Compile Include="Interfaces\IMenuItem.cs" /> … … 89 93 <Compile Include="Interfaces\IToolBarItem.cs" /> 90 94 <Compile Include="MainFormManager.cs" /> 91 <Compile Include="MenuItem Base.cs" />92 <Compile Include="MenuSeparatorItem Base.cs" />93 <Compile Include="ToolBarItem Base.cs" />95 <Compile Include="MenuItem.cs" /> 96 <Compile Include="MenuSeparatorItem.cs" /> 97 <Compile Include="ToolBarItem.cs" /> 94 98 <Compile Include="HeuristicLabMainFormPlugin.cs" /> 95 99 <Compile Include="Interfaces\IMainForm.cs" /> … … 97 101 <Compile Include="Interfaces\IView.cs" /> 98 102 <Compile Include="Properties\AssemblyInfo.cs" /> 99 <Compile Include=" UserInterfaceItemBase.cs" />103 <Compile Include="ActionUserInterfaceItem.cs" /> 100 104 <Compile Include="ContentAttribute.cs" /> 101 105 </ItemGroup> -
trunk/sources/HeuristicLab.MainForm/3.2/Interfaces/IMainForm.cs
r2548 r2696 25 25 using System.Text; 26 26 using System.Drawing; 27 using System.ComponentModel; 27 28 28 29 namespace HeuristicLab.MainForm { … … 35 36 event EventHandler ActiveViewChanged; 36 37 event EventHandler Changed; 37 38 38 39 event EventHandler<ViewEventArgs> ViewClosed; 39 40 event EventHandler<ViewShownEventArgs> ViewShown; … … 41 42 42 43 Type UserInterfaceItemType { get; } 43 void ShowView(IView view);44 void HideView(IView view);45 void CloseView(IView view);46 44 void CloseAllViews(); 47 45 void Close(); -
trunk/sources/HeuristicLab.MainForm/3.2/Interfaces/IMenuItem.cs
r2458 r2696 27 27 28 28 namespace HeuristicLab.MainForm { 29 public interface IMenuItem : IUserInterfaceItem { 30 IEnumerable<string> Structure { get; } 29 public interface IMenuItem : IActionUserInterfaceItem { 31 30 } 32 31 } -
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 22 using System; 2 23 using System.Collections.Generic; 3 24 using System.Linq; … … 5 26 6 27 namespace HeuristicLab.MainForm { 7 public interface IMenuSeparatorItem : I MenuItem {28 public interface IMenuSeparatorItem : IPositionableUserInterfaceItem { 8 29 } 9 30 } -
trunk/sources/HeuristicLab.MainForm/3.2/Interfaces/IToolBarItem.cs
r2458 r2696 26 26 27 27 namespace HeuristicLab.MainForm { 28 public interface IToolBarItem : IUserInterfaceItem { 29 IEnumerable<string> Structure { get; } 28 public interface IToolBarItem : IActionUserInterfaceItem { 30 29 } 31 30 } -
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 22 using System; 2 23 using System.Collections.Generic; 3 24 using System.Linq; … … 5 26 6 27 namespace HeuristicLab.MainForm { 7 public interface IToolBarSeparatorItem : I ToolBarItem {28 public interface IToolBarSeparatorItem : IPositionableUserInterfaceItem { 8 29 } 9 30 } -
trunk/sources/HeuristicLab.MainForm/3.2/Interfaces/IUserInterfaceItem.cs
r2541 r2696 28 28 namespace HeuristicLab.MainForm { 29 29 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);41 30 } 42 31 } -
trunk/sources/HeuristicLab.MainForm/3.2/Interfaces/IView.cs
r2466 r2696 27 27 namespace HeuristicLab.MainForm { 28 28 public interface IView { 29 29 30 string Caption { get; set; } 30 31 event EventHandler CaptionChanged; 31 32 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(); 34 37 } 35 38 } -
trunk/sources/HeuristicLab.MainForm/3.2/MainFormManager.cs
r2591 r2696 19 19 */ 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; … … 24 25 using System.Text; 25 26 using HeuristicLab.PluginInfrastructure; 27 using System.Diagnostics; 26 28 27 29 namespace HeuristicLab.MainForm { 30 class StringDict<T> : Dictionary<string, T> { 31 } 28 32 public static class MainFormManager { 29 33 private static object locker; … … 36 40 views = new HashSet<Type>(); 37 41 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)); 38 82 } 39 83 40 84 public static void RegisterMainForm(IMainForm mainform) { 41 85 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; 58 104 } 59 } else 60 throw new ArgumentException("A mainform was already associated with the mainform manager."); 105 } 61 106 } 62 107 } … … 87 132 //check base classes for default view 88 133 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 } 90 139 type = type.BaseType; 91 140 } 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 96 143 List<Type> temp = (from t in defaultViews.Keys 97 where t.Is AssignableFrom(contentType) && t.IsInterface144 where t.IsInterface && contentType.IsAssignableTo(t) 98 145 select t).ToList(); 99 146 if (temp.Count == 1) … … 109 156 if (t == null) 110 157 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); 113 164 } 114 165 … … 116 167 if (!typeof(IView).IsAssignableFrom(viewType)) 117 168 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 118 172 return (IView)Activator.CreateInstance(viewType); 119 173 } … … 122 176 if (!typeof(IView).IsAssignableFrom(viewType)) 123 177 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; 125 202 } 126 203 } -
trunk/sources/HeuristicLab.MainForm/3.2/MenuItem.cs
r2689 r2696 26 26 27 27 namespace 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{ 32 29 } 33 30 } -
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 22 using System; 2 23 using System.Collections.Generic; 3 24 using System.Linq; … … 5 26 6 27 namespace 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 { 43 29 } 44 30 } -
trunk/sources/HeuristicLab.MainForm/3.2/ToolBarItem.cs
r2689 r2696 26 26 27 27 namespace 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 { 32 29 } 33 30 } -
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 22 using System; 2 23 using System.Collections.Generic; 3 24 using System.Linq; … … 5 26 6 27 namespace 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 { 45 29 } 46 30 }
Note: See TracChangeset
for help on using the changeset viewer.