0
|
1 using System;
|
|
2 using System.ComponentModel;
|
|
3 using System.ComponentModel.Design;
|
|
4 using System.ComponentModel.Design.Data;
|
|
5 using System.Drawing;
|
|
6 using System.Reflection;
|
|
7 using System.Windows.Forms;
|
|
8 using System.Windows.Forms.Design;
|
|
9
|
|
10 namespace BLToolkit.ComponentModel.Design
|
|
11 {
|
|
12 [DesignTimeVisible(false)]
|
|
13 [ToolboxItem(false)]
|
|
14 public partial class TypePicker : UserControl
|
|
15 {
|
|
16 public TypePicker()
|
|
17 {
|
|
18 InitializeComponent();
|
|
19
|
|
20 if (!_size.IsEmpty)
|
|
21 Size = _size;
|
|
22 }
|
|
23
|
|
24 ITypeResolutionService _typeResolutionService;
|
|
25 IWindowsFormsEditorService _windowsFormsEditorService;
|
|
26 IServiceProvider _serviceProvider;
|
|
27
|
|
28 Type _resultType;
|
|
29 Predicate<Type> _filter;
|
|
30
|
|
31 static Size _size;
|
|
32
|
|
33 private T GetService<T>()
|
|
34 {
|
|
35 return (T)_serviceProvider.GetService(typeof(T));
|
|
36 }
|
|
37
|
|
38 public Type PickType(IServiceProvider serviceProvider, Type type, Predicate<Type> filter)
|
|
39 {
|
|
40 _resultType = type;
|
|
41 _filter = filter;
|
|
42
|
|
43 _serviceProvider = serviceProvider;
|
|
44 _typeResolutionService = GetService<ITypeResolutionService>();
|
|
45 _windowsFormsEditorService = GetService<IWindowsFormsEditorService>();
|
|
46
|
|
47 InitUI ();
|
|
48 AddTypes();
|
|
49
|
|
50 if (_windowsFormsEditorService != null)
|
|
51 _windowsFormsEditorService.DropDownControl(this);
|
|
52
|
|
53 return _resultType;
|
|
54 }
|
|
55
|
|
56 private void InitUI()
|
|
57 {
|
|
58 IUIService uiService = GetService<IUIService>();
|
|
59
|
|
60 if (uiService != null)
|
|
61 {
|
|
62 object color = uiService.Styles["VsColorPanelHyperLink"];
|
|
63
|
|
64 if (color is Color)
|
|
65 addNewLinkLabel.LinkColor = (Color)color;
|
|
66
|
|
67 color = uiService.Styles["VsColorPanelHyperLinkPressed"];
|
|
68
|
|
69 if (color is Color)
|
|
70 addNewLinkLabel.ActiveLinkColor = (Color)color;
|
|
71 }
|
|
72
|
|
73 // Add None node.
|
|
74 //
|
|
75 TreeNode node = new TypeNode("None");
|
|
76
|
|
77 treeView.Nodes.Add(node);
|
|
78 treeView.SelectedNode = node;
|
|
79 }
|
|
80
|
|
81 private TypeNode GetTypeNode(DataSourceDescriptor ds)
|
|
82 {
|
|
83 Type type = null;
|
|
84
|
|
85 if (_typeResolutionService != null)
|
|
86 type = _typeResolutionService.GetType(ds.TypeName);
|
|
87
|
|
88 try
|
|
89 {
|
|
90 if (type == null)
|
|
91 type = Type.GetType(ds.TypeName, true);
|
|
92 }
|
|
93 catch
|
|
94 {
|
|
95 return null;
|
|
96 }
|
|
97
|
|
98 if (_filter != null && _filter(type) == false)
|
|
99 return null;
|
|
100
|
|
101 return new TypeNode(ds.Name, type);
|
|
102 }
|
|
103
|
|
104 private void AddGroup(DataSourceGroup group)
|
|
105 {
|
|
106 TreeNode groupNode = null;
|
|
107
|
|
108 foreach (DataSourceDescriptor d in group.DataSources)
|
|
109 {
|
|
110 if (d == null)
|
|
111 continue;
|
|
112
|
|
113 TypeNode node = GetTypeNode(d);
|
|
114
|
|
115 if (node == null)
|
|
116 continue;
|
|
117
|
|
118 if (group.IsDefault)
|
|
119 {
|
|
120 treeView.Nodes.Add(node);
|
|
121 }
|
|
122 else
|
|
123 {
|
|
124 if (groupNode == null)
|
|
125 treeView.Nodes.Add(groupNode = new TreeNode(group.Name, 2, 2));
|
|
126
|
|
127 groupNode.Nodes.Add(node);
|
|
128 }
|
|
129
|
|
130 if (_resultType == node.Type)
|
|
131 treeView.SelectedNode = node;
|
|
132 }
|
|
133 }
|
|
134
|
|
135 private void AddTypes()
|
|
136 {
|
|
137 DataSourceProviderService dspService = GetService<DataSourceProviderService>();
|
|
138
|
|
139 if (dspService == null || !dspService.SupportsAddNewDataSource)
|
|
140 return;
|
|
141
|
|
142 DataSourceGroupCollection dataSources = null;
|
|
143
|
|
144 try
|
|
145 {
|
|
146 dataSources = dspService.GetDataSources();
|
|
147 }
|
|
148 catch (Exception ex)
|
|
149 {
|
|
150 IUIService ui = GetService<IUIService>();
|
|
151
|
|
152 string message =
|
|
153 "Cant retrieve Data Source Collection: " + ex.Message +
|
|
154 "\nCheck the 'Properties\\DataSources' folder of your project.";
|
|
155
|
|
156 if (ui != null)
|
|
157 ui.ShowError(ex, message);
|
|
158 else
|
|
159 MessageBox.Show(this, message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
160 }
|
|
161
|
|
162 if (dataSources == null)
|
|
163 return;
|
|
164
|
|
165 foreach (DataSourceGroup group in dataSources)
|
|
166 if (group != null)
|
|
167 AddGroup(group);
|
|
168 }
|
|
169
|
|
170 private void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
|
|
171 {
|
|
172 if (e.Node is TypeNode)
|
|
173 {
|
|
174 _resultType = ((TypeNode)e.Node).Type;
|
|
175 _windowsFormsEditorService.CloseDropDown();
|
|
176 }
|
|
177 }
|
|
178
|
|
179 private void addNewLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
|
|
180 {
|
|
181 using (GetTypeDialog dlg = new GetTypeDialog(_serviceProvider, typeof (object), _filter))
|
|
182 {
|
|
183 IUIService uiService = GetService<IUIService>();
|
|
184 IWin32Window owner = uiService == null? null: uiService.GetDialogOwnerWindow();
|
|
185 DialogResult result = dlg.ShowDialog(owner);
|
|
186
|
|
187 if (result == DialogResult.OK && dlg.ResultType != null)
|
|
188 {
|
|
189 _resultType = dlg.ResultType;
|
|
190
|
|
191 SaveType(_resultType);
|
|
192
|
|
193 _windowsFormsEditorService.CloseDropDown();
|
|
194 }
|
|
195 }
|
|
196 }
|
|
197
|
|
198 private void TypePicker_Resize(object sender, EventArgs e)
|
|
199 {
|
|
200 _size = Size;
|
|
201 }
|
|
202
|
|
203 private void SaveType(Type type)
|
|
204 {
|
|
205 DataSourceProviderService dspService = GetService<DataSourceProviderService>();
|
|
206
|
|
207 if (dspService == null || !dspService.SupportsAddNewDataSource)
|
|
208 return;
|
|
209
|
|
210 try
|
|
211 {
|
|
212 const string vs9TypeName = "Microsoft.VSDesigner.VSDesignerPackage.IGenericObjectDataSourcesService, Microsoft.VSDesigner, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
|
|
213 const string vs8TypeName = "Microsoft.VSDesigner.VSDesignerPackage.IGenericObjectDataSourcesService, Microsoft.VSDesigner, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
|
|
214
|
|
215 Type serviceType = Type.GetType(vs9TypeName) ?? Type.GetType(vs8TypeName);
|
|
216
|
|
217 if (serviceType == null)
|
|
218 return;
|
|
219
|
|
220 object service = _serviceProvider.GetService(serviceType);
|
|
221
|
|
222 if (service == null)
|
|
223 return;
|
|
224
|
|
225 MethodInfo mi = serviceType.GetMethod("AddGenericObjectDataSource");
|
|
226
|
|
227 mi.Invoke(service, new object[] { _serviceProvider, null, type });
|
|
228 }
|
|
229 catch (Exception ex)
|
|
230 {
|
|
231 IUIService ui = GetService<IUIService>();
|
|
232
|
|
233 if (ui != null)
|
|
234 ui.ShowError(ex);
|
|
235 else
|
|
236 MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
237 }
|
|
238 }
|
|
239
|
|
240 internal class TypeNode : TreeNode
|
|
241 {
|
|
242 public TypeNode(string name)
|
|
243 : base(name, 0, 0)
|
|
244 {
|
|
245 _isSelectable = true;
|
|
246 }
|
|
247
|
|
248 public TypeNode(string name, Type type)
|
|
249 : this(name, type, true)
|
|
250 {
|
|
251 }
|
|
252
|
|
253 public TypeNode(string name, Type type, bool isSelectable)
|
|
254 : base(name, 3, 3)
|
|
255 {
|
|
256 _type = type;
|
|
257 _isSelectable = isSelectable;
|
|
258 }
|
|
259
|
|
260 private bool _isSelectable;
|
|
261 public bool IsSelectable
|
|
262 {
|
|
263 get { return _isSelectable; }
|
|
264 set { _isSelectable = value; }
|
|
265 }
|
|
266
|
|
267 private readonly Type _type;
|
|
268 public Type Type
|
|
269 {
|
|
270 get { return _type; }
|
|
271 }
|
|
272 }
|
|
273
|
|
274 class NewLink : LinkLabel
|
|
275 {
|
|
276 protected override bool IsInputKey(Keys key)
|
|
277 {
|
|
278 return key == Keys.Return? true: base.IsInputKey(key);
|
|
279 }
|
|
280 }
|
|
281 }
|
|
282 }
|