⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 configurationguihelper.cs

📁 SharpDevelop2.0.0 c#开发免费工具
💻 CS
📖 第 1 页 / 共 2 页
字号:
// <file>
//     <copyright see="prj:///doc/copyright.txt"/>
//     <license see="prj:///doc/license.txt"/>
//     <owner name="Daniel Grunwald" email="daniel@danielgrunwald.de"/>
//     <version>$Revision: 1203 $</version>
// </file>

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop.Gui;

namespace ICSharpCode.SharpDevelop.Project
{
	/// <summary>
	/// Class that helps connecting configuration GUI controls to MsBuild properties.
	/// </summary>
	public class ConfigurationGuiHelper : ICanBeDirty
	{
		MSBuildProject project;
		Dictionary<string, Control> controlDictionary;
		List<ConfigurationGuiBinding> bindings = new List<ConfigurationGuiBinding>();
		
		public ConfigurationGuiHelper(MSBuildProject project, Dictionary<string, Control> controlDictionary)
		{
			this.project = project;
			this.controlDictionary = controlDictionary;
			this.configuration = project.Configuration;
			this.platform = project.Platform;
		}
		
		public MSBuildProject Project {
			get {
				return project;
			}
		}
		
		internal Dictionary<string, Control> ControlDictionary {
			get {
				return controlDictionary;
			}
		}
		
		#region Manage bindings
		public T GetProperty<T>(string property, T defaultValue, out PropertyStorageLocations location)
		{
			return project.GetProperty(configuration, platform, property, defaultValue, out location);
		}
		
		public void SetProperty<T>(string property, T value, PropertyStorageLocations location)
		{
			project.SetProperty(configuration, platform, property, value, location);
		}
		
		/// <summary>
		/// Initializes the Property and Project properties on the binding and calls the load method on it.
		/// Registers the binding so that Save is called on it when Save is called on the ConfigurationGuiHelper.
		/// </summary>
		public void AddBinding(string property, ConfigurationGuiBinding binding)
		{
			binding.Property = property;
			binding.Helper = this;
			binding.Load();
			bindings.Add(binding);
		}
		
		public void Load()
		{
			if (Loading != null) {
				Loading(this, EventArgs.Empty);
			}
			foreach (ConfigurationGuiBinding binding in bindings) {
				binding.Load();
			}
			if (Loaded != null) {
				Loaded(this, EventArgs.Empty);
			}
			IsDirty = false;
		}
		
		public bool Save()
		{
			foreach (ConfigurationGuiBinding binding in bindings) {
				if (!binding.Save()) {
					return false;
				}
			}
			if (Saved != null) {
				Saved(this, EventArgs.Empty);
			}
			IsDirty = false;
			return true;
		}
		
		/// <summary>
		/// This event is raised when another configuration is beginning to load.
		/// </summary>
		public event EventHandler Loading;
		
		/// <summary>
		/// This event is raised when another configuration has been loaded.
		/// </summary>
		public event EventHandler Loaded;
		
		/// <summary>
		/// This event is raised after the configuration has been saved.
		/// </summary>
		public event EventHandler Saved;
		
		void ControlValueChanged(object sender, EventArgs e)
		{
			IsDirty = true;
		}
		
		bool dirty;
		
		public bool IsDirty {
			get {
				return dirty;
			}
			set {
				if (dirty != value) {
					dirty = value;
					if (DirtyChanged != null) {
						DirtyChanged(this, EventArgs.Empty);
					}
				}
			}
		}
		
		public event EventHandler DirtyChanged;
		
		string configuration;
		
		public string Configuration {
			get {
				return configuration;
			}
			set {
				configuration = value;
			}
		}
		
		string platform;

		public string Platform {
			get {
				return platform;
			}
			set {
				platform = value;
			}
		}
		
		#region Bind bool to CheckBox
		public ConfigurationGuiBinding BindBoolean(string control, string property, bool defaultValue)
		{
			return BindBoolean(controlDictionary[control], property, defaultValue);
		}
		
		public ConfigurationGuiBinding BindBoolean(Control control, string property, bool defaultValue)
		{
			CheckBox checkBox = control as CheckBox;
			if (checkBox != null) {
				CheckBoxBinding binding = new CheckBoxBinding(checkBox, defaultValue);
				AddBinding(property, binding);
				checkBox.CheckedChanged += ControlValueChanged;
				return binding;
			} else {
				throw new ApplicationException("Cannot bind " + control.GetType().Name + " to bool property.");
			}
		}
		
		class CheckBoxBinding : ConfigurationGuiBinding
		{
			CheckBox control;
			bool defaultValue;
			
			public CheckBoxBinding(CheckBox control, bool defaultValue)
			{
				this.control = control;
				this.defaultValue = defaultValue;
			}
			
			public override void Load()
			{
				control.Checked = Get(defaultValue);
			}
			
			public override bool Save()
			{
				string oldValue = Get("True");
				if (oldValue == "true" || oldValue == "false") {
					// keep value in lower case
					Set(control.Checked.ToString().ToLowerInvariant());
				} else {
					Set(control.Checked.ToString());
				}
				return true;
			}
		}
		#endregion
		
		#region Bind string to TextBox or ComboBox
		public ConfigurationGuiBinding BindString(string control, string property)
		{
			return BindString(controlDictionary[control], property);
		}
		
		public ConfigurationGuiBinding BindString(Control control, string property)
		{
			if (control is TextBoxBase || control is ComboBox) {
				SimpleTextBinding binding = new SimpleTextBinding(control);
				AddBinding(property, binding);
				control.TextChanged += ControlValueChanged;
				if (control is ComboBox) {
					control.KeyDown += ComboBoxKeyDown;
				}
				return binding;
			} else {
				throw new ApplicationException("Cannot bind " + control.GetType().Name + " to string property.");
			}
		}
		
		void ComboBoxKeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyData == (Keys.Control | Keys.S)) {
				e.Handled = true;
				new ICSharpCode.SharpDevelop.Commands.SaveFile().Run();
			}
		}
		
		class SimpleTextBinding : ConfigurationGuiBinding
		{
			Control control;
			
			public SimpleTextBinding(Control control)
			{
				this.control = control;
			}
			
			public override void Load()
			{
				control.Text = Get("");
			}
			
			public override bool Save()
			{
				Set(control.Text);
				return true;
			}
		}
		#endregion
		
		#region Bind int to NumericUpDown
		public ConfigurationGuiBinding BindInt(string control, string property, int defaultValue)
		{
			return BindInt(controlDictionary[control], property, defaultValue);
		}

		public ConfigurationGuiBinding BindInt(Control control, string property, int defaultValue)
		{
			if (control is NumericUpDown) {
				SimpleIntBinding binding = new SimpleIntBinding((NumericUpDown)control, defaultValue);
				AddBinding(property, binding);
				control.TextChanged += ControlValueChanged;
				return binding;
			} else {
				throw new ApplicationException("Cannot bind " + control.GetType().Name + " to int property.");
			}
		}

		class SimpleIntBinding : ConfigurationGuiBinding
		{
			NumericUpDown control;
			int           defaultValue;
			
			public SimpleIntBinding(NumericUpDown control, int defaultValue)
			{
				this.control = control;
				this.defaultValue = defaultValue;
			}
			
			public override void Load()
			{
				int val;
				if (!int.TryParse(Get(defaultValue.ToString(NumberFormatInfo.InvariantInfo)), NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out val)) {
					val = defaultValue;
				}
				control.Text = val.ToString();
			}
			
			public override bool Save()
			{
				string txt = control.Text.Trim();
				NumberStyles style = NumberStyles.Integer;
				int val;
				val = int.Parse(txt, style, NumberFormatInfo.InvariantInfo);
				Set(val.ToString(NumberFormatInfo.InvariantInfo));
				return true;
			}
		}
		#endregion
		
		#region Bind hex number to TextBox
		public ConfigurationGuiBinding BindHexadecimal(TextBoxBase textBox, string property, int defaultValue)
		{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -