forked from killswitch1111/powerguivsx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReferenceNode.cs
More file actions
327 lines (275 loc) · 10.8 KB
/
Copy pathReferenceNode.cs
File metadata and controls
327 lines (275 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Apache License, Version 2.0. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Apache License, Version 2.0, please send an email to
* vspython@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Apache License, Version 2.0.
*
* You must not remove this notice, or any other, from this software.
*
* ***************************************************************************/
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Text;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;
using OleConstants = Microsoft.VisualStudio.OLE.Interop.Constants;
using VsCommands2K = Microsoft.VisualStudio.VSConstants.VSStd2KCmdID;
namespace Microsoft.VisualStudioTools.Project
{
internal abstract class ReferenceNode : HierarchyNode
{
internal delegate void CannotAddReferenceErrorMessage();
#region ctors
/// <summary>
/// constructor for the ReferenceNode
/// </summary>
protected ReferenceNode(ProjectNode root, ProjectElement element)
: base(root, element)
{
this.ExcludeNodeFromScc = true;
}
/// <summary>
/// constructor for the ReferenceNode
/// </summary>
internal ReferenceNode(ProjectNode root)
: base(root)
{
this.ExcludeNodeFromScc = true;
}
#endregion
#region overridden properties
public override int MenuCommandId
{
get { return VsMenus.IDM_VS_CTXT_REFERENCE; }
}
public override Guid ItemTypeGuid
{
get { return Guid.Empty; }
}
public override string Url
{
get
{
return String.Empty;
}
}
public override string Caption
{
get
{
return String.Empty;
}
}
#endregion
#region overridden methods
protected override NodeProperties CreatePropertiesObject()
{
return new ReferenceNodeProperties(this);
}
/// <summary>
/// Get an instance of the automation object for ReferenceNode
/// </summary>
/// <returns>An instance of Automation.OAReferenceItem type if succeeded</returns>
public override object GetAutomationObject()
{
if (this.ProjectMgr == null || this.ProjectMgr.IsClosed)
{
return null;
}
return new Automation.OAReferenceItem(this.ProjectMgr.GetAutomationObject() as Automation.OAProject, this);
}
/// <summary>
/// Disable inline editing of Caption of a ReferendeNode
/// </summary>
/// <returns>null</returns>
public override string GetEditLabel()
{
return null;
}
public override object GetIconHandle(bool open)
{
int offset = (this.CanShowDefaultIcon() ? (int)ProjectNode.ImageName.Reference : (int)ProjectNode.ImageName.DanglingReference);
return this.ProjectMgr.ImageHandler.GetIconHandle(offset);
}
/// <summary>
/// Not supported.
/// </summary>
internal override int ExcludeFromProject()
{
return (int)OleConstants.OLECMDERR_E_NOTSUPPORTED;
}
public override void Remove(bool removeFromStorage) {
ReferenceContainerNode parent = Parent as ReferenceContainerNode;
base.Remove(removeFromStorage);
if (parent != null) {
parent.FireChildRemoved(this);
}
}
/// <summary>
/// References node cannot be dragged.
/// </summary>
/// <returns>A stringbuilder.</returns>
protected internal override string PrepareSelectedNodesForClipBoard()
{
return null;
}
internal override int QueryStatusOnNode(Guid cmdGroup, uint cmd, IntPtr pCmdText, ref QueryStatusResult result)
{
if (cmdGroup == VsMenus.guidStandardCommandSet2K)
{
if ((VsCommands2K)cmd == VsCommands2K.QUICKOBJECTSEARCH)
{
result |= QueryStatusResult.SUPPORTED | QueryStatusResult.ENABLED;
return VSConstants.S_OK;
}
}
else
{
return (int)OleConstants.OLECMDERR_E_UNKNOWNGROUP;
}
return base.QueryStatusOnNode(cmdGroup, cmd, pCmdText, ref result);
}
internal override int ExecCommandOnNode(Guid cmdGroup, uint cmd, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
{
if (cmdGroup == VsMenus.guidStandardCommandSet2K)
{
if ((VsCommands2K)cmd == VsCommands2K.QUICKOBJECTSEARCH)
{
return this.ShowObjectBrowser();
}
}
return base.ExecCommandOnNode(cmdGroup, cmd, nCmdexecopt, pvaIn, pvaOut);
}
#endregion
#region methods
/// <summary>
/// Links a reference node to the project and hierarchy.
/// </summary>
public virtual void AddReference()
{
ReferenceContainerNode referencesFolder = this.ProjectMgr.GetReferenceContainer() as ReferenceContainerNode;
Utilities.CheckNotNull(referencesFolder, "Could not find the References node");
CannotAddReferenceErrorMessage referenceErrorMessageHandler = null;
if (!this.CanAddReference(out referenceErrorMessageHandler))
{
if (referenceErrorMessageHandler != null)
{
referenceErrorMessageHandler.DynamicInvoke(new object[] { });
}
return;
}
// Link the node to the project file.
this.BindReferenceData();
// At this point force the item to be refreshed
this.ItemNode.RefreshProperties();
referencesFolder.AddChild(this);
return;
}
/// <summary>
/// Refreshes a reference by re-resolving it and redrawing the icon.
/// </summary>
internal virtual void RefreshReference()
{
this.ResolveReference();
ProjectMgr.ReDrawNode(this, UIHierarchyElement.Icon);
}
/// <summary>
/// Resolves references.
/// </summary>
protected virtual void ResolveReference()
{
}
/// <summary>
/// Validates that a reference can be added.
/// </summary>
/// <param name="errorHandler">A CannotAddReferenceErrorMessage delegate to show the error message.</param>
/// <returns>true if the reference can be added.</returns>
protected virtual bool CanAddReference(out CannotAddReferenceErrorMessage errorHandler)
{
// When this method is called this refererence has not yet been added to the hierarchy, only instantiated.
errorHandler = null;
if (this.IsAlreadyAdded())
{
return false;
}
return true;
}
/// <summary>
/// Checks if a reference is already added. The method parses all references and compares the Url.
/// </summary>
/// <returns>true if the assembly has already been added.</returns>
protected virtual bool IsAlreadyAdded()
{
ReferenceContainerNode referencesFolder = this.ProjectMgr.GetReferenceContainer() as ReferenceContainerNode;
Utilities.CheckNotNull(referencesFolder, "Could not find the References node");
for (HierarchyNode n = referencesFolder.FirstChild; n != null; n = n.NextSibling)
{
ReferenceNode refererenceNode = n as ReferenceNode;
if (null != refererenceNode)
{
// We check if the Url of the assemblies is the same.
if (CommonUtils.IsSamePath(refererenceNode.Url, this.Url))
{
return true;
}
}
}
return false;
}
/// <summary>
/// Shows the Object Browser
/// </summary>
/// <returns></returns>
protected virtual int ShowObjectBrowser()
{
if (!File.Exists(this.Url))
{
return (int)OleConstants.OLECMDERR_E_NOTSUPPORTED;
}
// Request unmanaged code permission in order to be able to creaet the unmanaged memory representing the guid.
new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
Guid guid = VSConstants.guidCOMPLUSLibrary;
IntPtr ptr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(guid.ToByteArray().Length);
System.Runtime.InteropServices.Marshal.StructureToPtr(guid, ptr, false);
int returnValue = VSConstants.S_OK;
try
{
VSOBJECTINFO[] objInfo = new VSOBJECTINFO[1];
objInfo[0].pguidLib = ptr;
objInfo[0].pszLibName = this.Url;
IVsObjBrowser objBrowser = this.ProjectMgr.Site.GetService(typeof(SVsObjBrowser)) as IVsObjBrowser;
ErrorHandler.ThrowOnFailure(objBrowser.NavigateTo(objInfo, 0));
}
catch (COMException e)
{
Trace.WriteLine("Exception" + e.ErrorCode);
returnValue = e.ErrorCode;
}
finally
{
if (ptr != IntPtr.Zero)
{
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(ptr);
}
}
return returnValue;
}
internal override bool CanDeleteItem(__VSDELETEITEMOPERATION deleteOperation)
{
if (deleteOperation == __VSDELETEITEMOPERATION.DELITEMOP_RemoveFromProject)
{
return true;
}
return false;
}
protected abstract void BindReferenceData();
#endregion
}
}