1
/***************************************************************************
3 Copyright (c) Microsoft Corporation. All rights reserved.
4 This code is licensed under the Visual Studio SDK license terms.
5 THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6 ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7 IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8 PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
10 ***************************************************************************/
16 namespace Microsoft
.VisualStudio
.Shell
19 /// Used to provide registration information to the XML Chooser
20 /// for a custom XML designer.
22 [AttributeUsage(AttributeTargets
.Class
, AllowMultiple
= true, Inherited
= true)]
23 public sealed class ProvideXmlEditorChooserDesignerViewAttribute
: RegistrationAttribute
25 const string XmlChooserFactory
= "XmlChooserFactory";
26 const string XmlChooserEditorExtensionsKeyPath
= @"Editors\{32CC8DFA-2D70-49b2-94CD-22D57349B778}\Extensions";
27 const string XmlEditorFactoryGuid
= "{FA3CD31E-987B-443A-9B81-186104E8DAC1}";
31 private string extension
;
32 private Guid defaultLogicalView
;
33 private int xmlChooserPriority
;
36 /// Constructor for ProvideXmlEditorChooserDesignerViewAttribute.
38 /// <param name="name">The registry keyName for your XML editor. For example "RESX", "Silverlight", "Workflow", etc...</param>
39 /// <param name="extension">The file extension for your custom XML type (e.g. "xaml", "resx", "xsd").</param>
40 /// <param name="defaultLogicalViewEditorFactory">A Type, Guid, or String object representing the editor factory for the default logical view.</param>
41 /// <param name="xmlChooserPriority">The priority of the extension in the XML Chooser. This value must be greater than the extension's priority value for the XML designer's EditorFactory.</param>
42 public ProvideXmlEditorChooserDesignerViewAttribute(string name
, string extension
, object defaultLogicalViewEditorFactory
, int xmlChooserPriority
)
44 if (string.IsNullOrWhiteSpace(name
))
46 throw new ArgumentException("Editor description cannot be null or empty.", "editorDescription");
48 if (string.IsNullOrWhiteSpace(extension
))
50 throw new ArgumentException("Extension cannot be null or empty.", "extension");
52 if (defaultLogicalViewEditorFactory
== null)
54 throw new ArgumentNullException("defaultLogicalViewEditorFactory");
58 this.extension
= extension
;
59 this.defaultLogicalView
= TryGetGuidFromObject(defaultLogicalViewEditorFactory
);
60 this.xmlChooserPriority
= xmlChooserPriority
;
62 this.CodeLogicalViewEditor
= XmlEditorFactoryGuid
;
63 this.DebuggingLogicalViewEditor
= XmlEditorFactoryGuid
;
64 this.DesignerLogicalViewEditor
= XmlEditorFactoryGuid
;
65 this.TextLogicalViewEditor
= XmlEditorFactoryGuid
;
68 public override void Register(RegistrationContext context
)
72 throw new ArgumentNullException("context");
75 using (Key xmlChooserExtensions
= context
.CreateKey(XmlChooserEditorExtensionsKeyPath
))
77 xmlChooserExtensions
.SetValue(extension
, xmlChooserPriority
);
80 using (Key key
= context
.CreateKey(GetKeyName()))
82 key
.SetValue("DefaultLogicalView", defaultLogicalView
.ToString("B").ToUpperInvariant());
83 key
.SetValue("Extension", extension
);
85 if (!string.IsNullOrWhiteSpace(Namespace
))
87 key
.SetValue("Namespace", Namespace
);
90 if (MatchExtensionAndNamespace
)
92 key
.SetValue("Match", "both");
95 if (IsDataSet
.HasValue
)
97 key
.SetValue("IsDataSet", Convert
.ToInt32(IsDataSet
.Value
));
100 SetLogicalViewMapping(key
, VSConstants
.LOGVIEWID_Debugging
, DebuggingLogicalViewEditor
);
101 SetLogicalViewMapping(key
, VSConstants
.LOGVIEWID_Code
, CodeLogicalViewEditor
);
102 SetLogicalViewMapping(key
, VSConstants
.LOGVIEWID_Designer
, DesignerLogicalViewEditor
);
103 SetLogicalViewMapping(key
, VSConstants
.LOGVIEWID_TextView
, TextLogicalViewEditor
);
107 private void SetLogicalViewMapping(Key key
, Guid logicalView
, object editorFactory
)
109 if (editorFactory
!= null)
111 key
.SetValue(logicalView
.ToString("B").ToUpperInvariant(), TryGetGuidFromObject(editorFactory
).ToString("B").ToUpperInvariant());
115 private Guid
TryGetGuidFromObject(object guidObject
)
117 // figure out what type of object they passed in and get the GUID from it
118 if (guidObject
is string)
119 return new Guid((string)guidObject
);
120 else if (guidObject
is Type
)
121 return ((Type
)guidObject
).GUID
;
122 else if (guidObject
is Guid
)
123 return (Guid
)guidObject
;
125 throw new ArgumentException("Could not determine Guid from supplied object.", "guidObject");
128 public override void Unregister(RegistrationContext context
)
132 throw new ArgumentNullException("context");
135 context
.RemoveKey(GetKeyName());
137 context
.RemoveValue(XmlChooserEditorExtensionsKeyPath
, extension
);
138 context
.RemoveKeyIfEmpty(XmlChooserEditorExtensionsKeyPath
);
141 private string GetKeyName()
143 return Path
.Combine(XmlChooserFactory
, name
);
147 /// The XML Namespace used in documents that this editor supports.
149 public string Namespace { get; set; }
152 /// Boolean value indicating whether the XML chooser should match on both the file extension
153 /// and the Namespace. If false, the XML chooser will match on either the extension or the
156 public bool MatchExtensionAndNamespace { get; set; }
159 /// Special value used only by the DataSet designer.
161 public bool? IsDataSet { get; set; }
164 /// The editor factory to associate with the debugging logical view
166 public object DebuggingLogicalViewEditor { get; set; }
169 /// The editor factory to associate with the code logical view
171 public object CodeLogicalViewEditor { get; set; }
174 /// The editor factory to associate with the designer logical view
176 public object DesignerLogicalViewEditor { get; set; }
179 /// The editor factory to associate with the text logical view
181 public object TextLogicalViewEditor { get; set; }