added some development tools
[windows-sources.git] / developer / VSSDK / VisualStudioIntegration / Common / Source / CSharp / RegistrationAttributes / ProvideXmlEditorChooserDesignerViewAttribute.cs
blob7d8a7f1b685f2db6c7c89b02473c9953451337ec
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 ***************************************************************************/
12 using System;
13 using System.Text;
14 using System.IO;
16 namespace Microsoft.VisualStudio.Shell
18 /// <summary>
19 /// Used to provide registration information to the XML Chooser
20 /// for a custom XML designer.
21 /// </summary>
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}";
30 private string name;
31 private string extension;
32 private Guid defaultLogicalView;
33 private int xmlChooserPriority;
35 /// <summary>
36 /// Constructor for ProvideXmlEditorChooserDesignerViewAttribute.
37 /// </summary>
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");
57 this.name = name;
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)
70 if (context == null)
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;
124 else
125 throw new ArgumentException("Could not determine Guid from supplied object.", "guidObject");
128 public override void Unregister(RegistrationContext context)
130 if (context == null)
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);
146 /// <summary>
147 /// The XML Namespace used in documents that this editor supports.
148 /// </summary>
149 public string Namespace { get; set; }
151 /// <summary>
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
154 /// Namespace.
155 /// </summary>
156 public bool MatchExtensionAndNamespace { get; set; }
158 /// <summary>
159 /// Special value used only by the DataSet designer.
160 /// </summary>
161 public bool? IsDataSet { get; set; }
163 /// <summary>
164 /// The editor factory to associate with the debugging logical view
165 /// </summary>
166 public object DebuggingLogicalViewEditor { get; set; }
168 /// <summary>
169 /// The editor factory to associate with the code logical view
170 /// </summary>
171 public object CodeLogicalViewEditor { get; set; }
173 /// <summary>
174 /// The editor factory to associate with the designer logical view
175 /// </summary>
176 public object DesignerLogicalViewEditor { get; set; }
178 /// <summary>
179 /// The editor factory to associate with the text logical view
180 /// </summary>
181 public object TextLogicalViewEditor { get; set; }