OLD | NEW |
| (Empty) |
1 /// Copyright (c) Microsoft Corporation. All rights reserved. | |
2 | |
3 using System; | |
4 using System.Diagnostics; | |
5 using System.Globalization; | |
6 using System.IO; | |
7 using System.Runtime.InteropServices; | |
8 using System.Security.Permissions; | |
9 using System.Text; | |
10 using Microsoft.VisualStudio; | |
11 using Microsoft.VisualStudio.Shell; | |
12 using Microsoft.VisualStudio.Shell.Interop; | |
13 using OleConstants = Microsoft.VisualStudio.OLE.Interop.Constants; | |
14 using VsCommands2K = Microsoft.VisualStudio.VSConstants.VSStd2KCmdID; | |
15 | |
16 namespace Microsoft.VisualStudio.Project | |
17 { | |
18 [CLSCompliant(false), ComVisible(true)] | |
19 public abstract class ReferenceNode : HierarchyNode | |
20 { | |
21 protected delegate void CannotAddReferenceErrorMessage(); | |
22 | |
23 #region ctors | |
24 /// <summary> | |
25 /// constructor for the ReferenceNode | |
26 /// </summary> | |
27 protected ReferenceNode(ProjectNode root, ProjectElement element
) | |
28 : base(root, element) | |
29 { | |
30 this.ExcludeNodeFromScc = true; | |
31 } | |
32 | |
33 /// <summary> | |
34 /// constructor for the ReferenceNode | |
35 /// </summary> | |
36 protected ReferenceNode(ProjectNode root) | |
37 : base(root) | |
38 { | |
39 this.ExcludeNodeFromScc = true; | |
40 } | |
41 | |
42 #endregion | |
43 | |
44 #region overridden properties | |
45 public override int MenuCommandId | |
46 { | |
47 get { return VsMenus.IDM_VS_CTXT_REFERENCE; } | |
48 } | |
49 | |
50 public override Guid ItemTypeGuid | |
51 { | |
52 get { return Guid.Empty; } | |
53 } | |
54 | |
55 public override string Url | |
56 { | |
57 get | |
58 { | |
59 return String.Empty; | |
60 } | |
61 } | |
62 | |
63 public override string Caption | |
64 { | |
65 get | |
66 { | |
67 return String.Empty; | |
68 } | |
69 } | |
70 #endregion | |
71 | |
72 #region overridden methods | |
73 protected override NodeProperties CreatePropertiesObject() | |
74 { | |
75 return new ReferenceNodeProperties(this); | |
76 } | |
77 | |
78 /// <summary> | |
79 /// Get an instance of the automation object for ReferenceNode | |
80 /// </summary> | |
81 /// <returns>An instance of Automation.OAReferenceItem type if s
ucceeded</returns> | |
82 public override object GetAutomationObject() | |
83 { | |
84 if(this.ProjectMgr == null || this.ProjectMgr.IsClosed) | |
85 { | |
86 return null; | |
87 } | |
88 | |
89 return new Automation.OAReferenceItem(this.ProjectMgr.Ge
tAutomationObject() as Automation.OAProject, this); | |
90 } | |
91 | |
92 /// <summary> | |
93 /// Disable inline editing of Caption of a ReferendeNode | |
94 /// </summary> | |
95 /// <returns>null</returns> | |
96 public override string GetEditLabel() | |
97 { | |
98 return null; | |
99 } | |
100 | |
101 | |
102 public override object GetIconHandle(bool open) | |
103 { | |
104 int offset = (this.CanShowDefaultIcon() ? (int)ProjectNo
de.ImageName.Reference : (int)ProjectNode.ImageName.DanglingReference); | |
105 return this.ProjectMgr.ImageHandler.GetIconHandle(offset
); | |
106 } | |
107 | |
108 /// <summary> | |
109 /// This method is called by the interface method GetMkDocument
to specify the item moniker. | |
110 /// </summary> | |
111 /// <returns>The moniker for this item</returns> | |
112 public override string GetMkDocument() | |
113 { | |
114 return this.Url; | |
115 } | |
116 | |
117 /// <summary> | |
118 /// Not supported. | |
119 /// </summary> | |
120 protected override int ExcludeFromProject() | |
121 { | |
122 return (int)OleConstants.OLECMDERR_E_NOTSUPPORTED; | |
123 } | |
124 | |
125 /// <summary> | |
126 /// References node cannot be dragged. | |
127 /// </summary> | |
128 /// <returns>A stringbuilder.</returns> | |
129 protected internal override StringBuilder PrepareSelectedNodesFo
rClipBoard() | |
130 { | |
131 return null; | |
132 } | |
133 | |
134 protected override int QueryStatusOnNode(Guid cmdGroup, uint cmd
, IntPtr pCmdText, ref QueryStatusResult result) | |
135 { | |
136 if(cmdGroup == VsMenus.guidStandardCommandSet2K) | |
137 { | |
138 if((VsCommands2K)cmd == VsCommands2K.QUICKOBJECT
SEARCH) | |
139 { | |
140 result |= QueryStatusResult.SUPPORTED |
QueryStatusResult.ENABLED; | |
141 return VSConstants.S_OK; | |
142 } | |
143 } | |
144 else | |
145 { | |
146 return (int)OleConstants.OLECMDERR_E_UNKNOWNGROU
P; | |
147 } | |
148 return base.QueryStatusOnNode(cmdGroup, cmd, pCmdText, r
ef result); | |
149 } | |
150 | |
151 protected override int ExecCommandOnNode(Guid cmdGroup, uint cmd
, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut) | |
152 { | |
153 if(cmdGroup == VsMenus.guidStandardCommandSet2K) | |
154 { | |
155 if((VsCommands2K)cmd == VsCommands2K.QUICKOBJECT
SEARCH) | |
156 { | |
157 return this.ShowObjectBrowser(); | |
158 } | |
159 } | |
160 | |
161 return base.ExecCommandOnNode(cmdGroup, cmd, nCmdexecopt
, pvaIn, pvaOut); | |
162 } | |
163 | |
164 #endregion | |
165 | |
166 #region methods | |
167 | |
168 | |
169 /// <summary> | |
170 /// Links a reference node to the project and hierarchy. | |
171 /// </summary> | |
172 public virtual void AddReference() | |
173 { | |
174 ReferenceContainerNode referencesFolder = this.ProjectMg
r.FindChild(ReferenceContainerNode.ReferencesNodeVirtualName) as ReferenceContai
nerNode; | |
175 Debug.Assert(referencesFolder != null, "Could not find t
he References node"); | |
176 | |
177 CannotAddReferenceErrorMessage referenceErrorMessageHand
ler = null; | |
178 | |
179 if(!this.CanAddReference(out referenceErrorMessageHandle
r)) | |
180 { | |
181 if(referenceErrorMessageHandler != null) | |
182 { | |
183 referenceErrorMessageHandler.DynamicInvo
ke(new object[] { }); | |
184 } | |
185 return; | |
186 } | |
187 | |
188 // Link the node to the project file. | |
189 this.BindReferenceData(); | |
190 | |
191 // At this point force the item to be refreshed | |
192 this.ItemNode.RefreshProperties(); | |
193 | |
194 referencesFolder.AddChild(this); | |
195 | |
196 return; | |
197 } | |
198 | |
199 /// <summary> | |
200 /// Refreshes a reference by re-resolving it and redrawing the i
con. | |
201 /// </summary> | |
202 internal virtual void RefreshReference() | |
203 { | |
204 this.ResolveReference(); | |
205 this.ReDraw(UIHierarchyElement.Icon); | |
206 } | |
207 | |
208 /// <summary> | |
209 /// Resolves references. | |
210 /// </summary> | |
211 protected virtual void ResolveReference() | |
212 { | |
213 | |
214 } | |
215 | |
216 /// <summary> | |
217 /// Validates that a reference can be added. | |
218 /// </summary> | |
219 /// <param name="errorHandler">A CannotAddReferenceErrorMessage
delegate to show the error message.</param> | |
220 /// <returns>true if the reference can be added.</returns> | |
221 protected virtual bool CanAddReference(out CannotAddReferenceErr
orMessage errorHandler) | |
222 { | |
223 // When this method is called this refererence has not y
et been added to the hierarchy, only instantiated. | |
224 errorHandler = null; | |
225 if(this.IsAlreadyAdded()) | |
226 { | |
227 return false; | |
228 } | |
229 | |
230 return true; | |
231 } | |
232 | |
233 | |
234 /// <summary> | |
235 /// Checks if a reference is already added. The method parses al
l references and compares the Url. | |
236 /// </summary> | |
237 /// <returns>true if the assembly has already been added.</retur
ns> | |
238 protected virtual bool IsAlreadyAdded() | |
239 { | |
240 ReferenceContainerNode referencesFolder = this.ProjectMg
r.FindChild(ReferenceContainerNode.ReferencesNodeVirtualName) as ReferenceContai
nerNode; | |
241 Debug.Assert(referencesFolder != null, "Could not find t
he References node"); | |
242 | |
243 for(HierarchyNode n = referencesFolder.FirstChild; n !=
null; n = n.NextSibling) | |
244 { | |
245 ReferenceNode refererenceNode = n as ReferenceNo
de; | |
246 if(null != refererenceNode) | |
247 { | |
248 // We check if the Url of the assemblies
is the same. | |
249 if(NativeMethods.IsSamePath(refererenceN
ode.Url, this.Url)) | |
250 { | |
251 return true; | |
252 } | |
253 } | |
254 } | |
255 | |
256 return false; | |
257 } | |
258 | |
259 | |
260 /// <summary> | |
261 /// Shows the Object Browser | |
262 /// </summary> | |
263 /// <returns></returns> | |
264 protected virtual int ShowObjectBrowser() | |
265 { | |
266 if(String.IsNullOrEmpty(this.Url) || !File.Exists(this.U
rl)) | |
267 { | |
268 return (int)OleConstants.OLECMDERR_E_NOTSUPPORTE
D; | |
269 } | |
270 | |
271 // Request unmanaged code permission in order to be able
to creaet the unmanaged memory representing the guid. | |
272 new SecurityPermission(SecurityPermissionFlag.UnmanagedC
ode).Demand(); | |
273 | |
274 Guid guid = VSConstants.guidCOMPLUSLibrary; | |
275 IntPtr ptr = System.Runtime.InteropServices.Marshal.Allo
cCoTaskMem(guid.ToByteArray().Length); | |
276 | |
277 System.Runtime.InteropServices.Marshal.StructureToPtr(gu
id, ptr, false); | |
278 int returnValue = VSConstants.S_OK; | |
279 try | |
280 { | |
281 VSOBJECTINFO[] objInfo = new VSOBJECTINFO[1]; | |
282 | |
283 objInfo[0].pguidLib = ptr; | |
284 objInfo[0].pszLibName = this.Url; | |
285 | |
286 IVsObjBrowser objBrowser = this.ProjectMgr.Site.
GetService(typeof(SVsObjBrowser)) as IVsObjBrowser; | |
287 | |
288 ErrorHandler.ThrowOnFailure(objBrowser.NavigateT
o(objInfo, 0)); | |
289 } | |
290 catch(COMException e) | |
291 { | |
292 Trace.WriteLine("Exception" + e.ErrorCode); | |
293 returnValue = e.ErrorCode; | |
294 } | |
295 finally | |
296 { | |
297 if(ptr != IntPtr.Zero) | |
298 { | |
299 System.Runtime.InteropServices.Marshal.F
reeCoTaskMem(ptr); | |
300 } | |
301 } | |
302 | |
303 return returnValue; | |
304 } | |
305 | |
306 protected override bool CanDeleteItem(__VSDELETEITEMOPERATION de
leteOperation) | |
307 { | |
308 if(deleteOperation == __VSDELETEITEMOPERATION.DELITEMOP_
RemoveFromProject) | |
309 { | |
310 return true; | |
311 } | |
312 return false; | |
313 } | |
314 | |
315 protected abstract void BindReferenceData(); | |
316 | |
317 #endregion | |
318 } | |
319 } | |
OLD | NEW |