OLD | NEW |
| (Empty) |
1 /// Copyright (c) Microsoft Corporation. All rights reserved. | |
2 | |
3 using System; | |
4 using System.Collections; | |
5 using System.Collections.Generic; | |
6 using System.Diagnostics.CodeAnalysis; | |
7 using System.Runtime.InteropServices; | |
8 using Microsoft.VisualStudio.Shell.Interop; | |
9 using VSLangProj; | |
10 using ErrorHandler = Microsoft.VisualStudio.ErrorHandler; | |
11 | |
12 namespace Microsoft.VisualStudio.Project.Automation | |
13 { | |
14 /// <summary> | |
15 /// Represents the automation object for the equivalent ReferenceContain
erNode object | |
16 /// </summary> | |
17 [SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrec
tSuffix")] | |
18 [ComVisible(true)] | |
19 public class OAReferences : ConnectionPointContainer, | |
20 IEventSource<_di
spReferencesEvents>, | |
21 References, | |
22 ReferencesEvents | |
23 { | |
24 private ReferenceContainerNode container; | |
25 public OAReferences(ReferenceContainerNode containerNode) | |
26 { | |
27 container = containerNode; | |
28 AddEventSource<_dispReferencesEvents>(this as IEventSour
ce<_dispReferencesEvents>); | |
29 container.OnChildAdded += new EventHandler<HierarchyNode
EventArgs>(OnReferenceAdded); | |
30 container.OnChildRemoved += new EventHandler<HierarchyNo
deEventArgs>(OnReferenceRemoved); | |
31 } | |
32 | |
33 #region Private Members | |
34 private Reference AddFromSelectorData(VSCOMPONENTSELECTORDATA se
lector) | |
35 { | |
36 ReferenceNode refNode = container.AddReferenceFromSelect
orData(selector); | |
37 if(null == refNode) | |
38 { | |
39 return null; | |
40 } | |
41 | |
42 return refNode.Object as Reference; | |
43 } | |
44 | |
45 private Reference FindByName(string stringIndex) | |
46 { | |
47 foreach(Reference refNode in this) | |
48 { | |
49 if(0 == string.Compare(refNode.Name, stringIndex
, StringComparison.Ordinal)) | |
50 { | |
51 return refNode; | |
52 } | |
53 } | |
54 return null; | |
55 } | |
56 #endregion | |
57 | |
58 #region References Members | |
59 | |
60 public Reference Add(string bstrPath) | |
61 { | |
62 if(string.IsNullOrEmpty(bstrPath)) | |
63 { | |
64 return null; | |
65 } | |
66 VSCOMPONENTSELECTORDATA selector = new VSCOMPONENTSELECT
ORDATA(); | |
67 selector.type = VSCOMPONENTTYPE.VSCOMPONENTTYPE_File; | |
68 selector.bstrFile = bstrPath; | |
69 | |
70 return AddFromSelectorData(selector); | |
71 } | |
72 | |
73 public Reference AddActiveX(string bstrTypeLibGuid, int lMajorVe
r, int lMinorVer, int lLocaleId, string bstrWrapperTool) | |
74 { | |
75 VSCOMPONENTSELECTORDATA selector = new VSCOMPONENTSELECT
ORDATA(); | |
76 selector.type = VSCOMPONENTTYPE.VSCOMPONENTTYPE_Com2; | |
77 selector.guidTypeLibrary = new Guid(bstrTypeLibGuid); | |
78 selector.lcidTypeLibrary = (uint)lLocaleId; | |
79 selector.wTypeLibraryMajorVersion = (ushort)lMajorVer; | |
80 selector.wTypeLibraryMinorVersion = (ushort)lMinorVer; | |
81 | |
82 return AddFromSelectorData(selector); | |
83 } | |
84 | |
85 public Reference AddProject(EnvDTE.Project project) | |
86 { | |
87 if(null == project) | |
88 { | |
89 return null; | |
90 } | |
91 // Get the soulution. | |
92 IVsSolution solution = container.ProjectMgr.Site.GetServ
ice(typeof(SVsSolution)) as IVsSolution; | |
93 if(null == solution) | |
94 { | |
95 return null; | |
96 } | |
97 | |
98 // Get the hierarchy for this project. | |
99 IVsHierarchy projectHierarchy; | |
100 ErrorHandler.ThrowOnFailure(solution.GetProjectOfUniqueN
ame(project.UniqueName, out projectHierarchy)); | |
101 | |
102 // Create the selector data. | |
103 VSCOMPONENTSELECTORDATA selector = new VSCOMPONENTSELECT
ORDATA(); | |
104 selector.type = VSCOMPONENTTYPE.VSCOMPONENTTYPE_Project; | |
105 | |
106 // Get the project reference string. | |
107 ErrorHandler.ThrowOnFailure(solution.GetProjrefOfProject
(projectHierarchy, out selector.bstrProjRef)); | |
108 | |
109 selector.bstrTitle = project.Name; | |
110 selector.bstrFile = System.IO.Path.GetDirectoryName(proj
ect.FullName); | |
111 | |
112 return AddFromSelectorData(selector); | |
113 } | |
114 | |
115 public EnvDTE.Project ContainingProject | |
116 { | |
117 get | |
118 { | |
119 return container.ProjectMgr.GetAutomationObject(
) as EnvDTE.Project; | |
120 } | |
121 } | |
122 | |
123 public int Count | |
124 { | |
125 get | |
126 { | |
127 return container.EnumReferences().Count; | |
128 } | |
129 } | |
130 | |
131 public EnvDTE.DTE DTE | |
132 { | |
133 get | |
134 { | |
135 return container.ProjectMgr.Site.GetService(type
of(EnvDTE.DTE)) as EnvDTE.DTE; | |
136 } | |
137 } | |
138 | |
139 public Reference Find(string bstrIdentity) | |
140 { | |
141 if(string.IsNullOrEmpty(bstrIdentity)) | |
142 { | |
143 return null; | |
144 } | |
145 foreach(Reference refNode in this) | |
146 { | |
147 if(null != refNode) | |
148 { | |
149 if(0 == string.Compare(bstrIdentity, ref
Node.Identity, StringComparison.Ordinal)) | |
150 { | |
151 return refNode; | |
152 } | |
153 } | |
154 } | |
155 return null; | |
156 } | |
157 | |
158 public IEnumerator GetEnumerator() | |
159 { | |
160 List<Reference> references = new List<Reference>(); | |
161 IEnumerator baseEnum = container.EnumReferences().GetEnu
merator(); | |
162 if(null == baseEnum) | |
163 { | |
164 return references.GetEnumerator(); | |
165 } | |
166 while(baseEnum.MoveNext()) | |
167 { | |
168 ReferenceNode refNode = baseEnum.Current as Refe
renceNode; | |
169 if(null == refNode) | |
170 { | |
171 continue; | |
172 } | |
173 Reference reference = refNode.Object as Referenc
e; | |
174 if(null != reference) | |
175 { | |
176 references.Add(reference); | |
177 } | |
178 } | |
179 return references.GetEnumerator(); | |
180 } | |
181 | |
182 public Reference Item(object index) | |
183 { | |
184 string stringIndex = index as string; | |
185 if(null != stringIndex) | |
186 { | |
187 return FindByName(stringIndex); | |
188 } | |
189 // Note that this cast will throw if the index is not co
nvertible to int. | |
190 int intIndex = (int)index; | |
191 IList<ReferenceNode> refs = container.EnumReferences(); | |
192 if(null == refs) | |
193 { | |
194 throw new ArgumentOutOfRangeException("index"); | |
195 } | |
196 if((intIndex <= 0) || (intIndex > refs.Count)) | |
197 { | |
198 throw new ArgumentOutOfRangeException("index"); | |
199 } | |
200 // Let the implementation of IList<> throw in case of in
dex not correct. | |
201 return refs[intIndex - 1].Object as Reference; | |
202 } | |
203 | |
204 public object Parent | |
205 { | |
206 get | |
207 { | |
208 return container.Parent.Object; | |
209 } | |
210 } | |
211 | |
212 #endregion | |
213 | |
214 #region _dispReferencesEvents_Event Members | |
215 public event _dispReferencesEvents_ReferenceAddedEventHandler Re
ferenceAdded; | |
216 public event _dispReferencesEvents_ReferenceChangedEventHandler
ReferenceChanged; | |
217 public event _dispReferencesEvents_ReferenceRemovedEventHandler
ReferenceRemoved; | |
218 #endregion | |
219 | |
220 #region Callbacks for the HierarchyNode events | |
221 private void OnReferenceAdded(object sender, HierarchyNodeEventA
rgs args) | |
222 { | |
223 // Validate the parameters. | |
224 if((container != sender as ReferenceContainerNode) || | |
225 (null == args) || (null == args.Child)) | |
226 { | |
227 return; | |
228 } | |
229 | |
230 // Check if there is any sink for this event. | |
231 if(null == ReferenceAdded) | |
232 { | |
233 return; | |
234 } | |
235 | |
236 // Check that the removed item implements the Reference
interface. | |
237 Reference reference = args.Child.Object as Reference; | |
238 if(null != reference) | |
239 { | |
240 ReferenceAdded(reference); | |
241 } | |
242 } | |
243 | |
244 private void OnReferenceRemoved(object sender, HierarchyNodeEven
tArgs args) | |
245 { | |
246 // Validate the parameters. | |
247 if((container != sender as ReferenceContainerNode) || | |
248 (null == args) || (null == args.Child)) | |
249 { | |
250 return; | |
251 } | |
252 | |
253 // Check if there is any sink for this event. | |
254 if(null == ReferenceRemoved) | |
255 { | |
256 return; | |
257 } | |
258 | |
259 // Check that the removed item implements the Reference
interface. | |
260 Reference reference = args.Child.Object as Reference; | |
261 if(null != reference) | |
262 { | |
263 ReferenceRemoved(reference); | |
264 } | |
265 } | |
266 #endregion | |
267 | |
268 #region IEventSource<_dispReferencesEvents> Members | |
269 void IEventSource<_dispReferencesEvents>.OnSinkAdded(_dispRefere
ncesEvents sink) | |
270 { | |
271 ReferenceAdded += new _dispReferencesEvents_ReferenceAdd
edEventHandler(sink.ReferenceAdded); | |
272 ReferenceChanged += new _dispReferencesEvents_ReferenceC
hangedEventHandler(sink.ReferenceChanged); | |
273 ReferenceRemoved += new _dispReferencesEvents_ReferenceR
emovedEventHandler(sink.ReferenceRemoved); | |
274 } | |
275 | |
276 void IEventSource<_dispReferencesEvents>.OnSinkRemoved(_dispRefe
rencesEvents sink) | |
277 { | |
278 ReferenceAdded -= new _dispReferencesEvents_ReferenceAdd
edEventHandler(sink.ReferenceAdded); | |
279 ReferenceChanged -= new _dispReferencesEvents_ReferenceC
hangedEventHandler(sink.ReferenceChanged); | |
280 ReferenceRemoved -= new _dispReferencesEvents_ReferenceR
emovedEventHandler(sink.ReferenceRemoved); | |
281 } | |
282 #endregion | |
283 } | |
284 } | |
OLD | NEW |