PageRenderTime 33ms CodeModel.GetById 25ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/ILSpy/Options/OptionsDialog.xaml.cs

http://github.com/icsharpcode/ILSpy
C# | 119 lines | 83 code | 13 blank | 23 comment | 10 complexity | aa544a53251b9d21e46c5d878ad6af9f MD5 | raw file
  1// Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
  2// 
  3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4// software and associated documentation files (the "Software"), to deal in the Software
  5// without restriction, including without limitation the rights to use, copy, modify, merge,
  6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7// to whom the Software is furnished to do so, subject to the following conditions:
  8// 
  9// The above copyright notice and this permission notice shall be included in all copies or
 10// substantial portions of the Software.
 11// 
 12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 17// DEALINGS IN THE SOFTWARE.
 18
 19using System;
 20using System.ComponentModel.Composition;
 21using System.Linq;
 22using System.Windows;
 23using System.Windows.Controls;
 24using System.Xml.Linq;
 25using ICSharpCode.ILSpy.Properties;
 26
 27namespace ICSharpCode.ILSpy.Options
 28{
 29	/// <summary>
 30	/// Interaction logic for OptionsDialog.xaml
 31	/// </summary>
 32	public partial class OptionsDialog : Window
 33	{
 34		
 35		readonly Lazy<UIElement, IOptionsMetadata>[] optionPages;
 36		
 37		public OptionsDialog()
 38		{
 39			InitializeComponent();
 40			// These used to have [ImportMany(..., RequiredCreationPolicy = CreationPolicy.NonShared)], so they use their own
 41			// ExportProvider instance.
 42			// FIXME: Ideally, the export provider should be disposed when it's no longer needed.
 43			var ep = App.ExportProviderFactory.CreateExportProvider();
 44			this.optionPages = ep.GetExports<UIElement, IOptionsMetadata>("OptionPages").ToArray();
 45			ILSpySettings settings = ILSpySettings.Load();
 46			foreach (var optionPage in optionPages.OrderBy(p => p.Metadata.Order)) {
 47				TabItem tabItem = new TabItem();
 48				tabItem.Header = MainWindow.GetResourceString( optionPage.Metadata.Title);
 49				tabItem.Content = optionPage.Value;
 50				tabControl.Items.Add(tabItem);
 51				
 52				IOptionPage page = optionPage.Value as IOptionPage;
 53				if (page != null)
 54					page.Load(settings);
 55			}
 56		}
 57		
 58		void OKButton_Click(object sender, RoutedEventArgs e)
 59		{
 60			ILSpySettings.Update(
 61				delegate (XElement root) {
 62					foreach (var optionPage in optionPages) {
 63						IOptionPage page = optionPage.Value as IOptionPage;
 64						if (page != null)
 65							page.Save(root);
 66					}
 67				});
 68			this.DialogResult = true;
 69			Close();
 70		}
 71
 72		private void DefaultsButton_Click(object sender, RoutedEventArgs e)
 73		{
 74			if (MessageBox.Show(Properties.Resources.ResetToDefaultsConfirmationMessage, "ILSpy", MessageBoxButton.YesNo) == MessageBoxResult.Yes) {
 75				var page = ((TabItem)tabControl.SelectedItem).Content as IOptionPage;
 76				if (page != null)
 77					page.LoadDefaults();
 78			}
 79		}
 80	}
 81
 82	public interface IOptionsMetadata
 83	{
 84		string Title { get; }
 85		int Order { get; }
 86	}
 87	
 88	public interface IOptionPage
 89	{
 90		void Load(ILSpySettings settings);
 91		void Save(XElement root);
 92		void LoadDefaults();
 93	}
 94	
 95	[MetadataAttribute]
 96	[AttributeUsage(AttributeTargets.Class, AllowMultiple=false)]
 97	public class ExportOptionPageAttribute : ExportAttribute
 98	{
 99		public ExportOptionPageAttribute() : base("OptionPages", typeof(UIElement))
100		{ }
101		
102		public string Title { get; set; }
103		
104		public int Order { get; set; }
105	}
106	
107	[ExportMainMenuCommand(Menu = nameof(Resources._View), Header = nameof(Resources._Options),  MenuCategory = nameof(Resources.Options) ,MenuOrder = 999)]
108	sealed class ShowOptionsCommand : SimpleCommand
109	{
110		public override void Execute(object parameter)
111		{
112			OptionsDialog dlg = new OptionsDialog();
113			dlg.Owner = MainWindow.Instance;
114			if (dlg.ShowDialog() == true) {
115				new RefreshCommand().Execute(parameter);
116			}
117		}
118	}
119}