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

📄 managerform.cs

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

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using ICSharpCode.Core;

namespace ICSharpCode.AddInManager
{
	public class ManagerForm : System.Windows.Forms.Form
	{
		#region Form Initialization
		static ManagerForm instance;
		
		public static ManagerForm Instance {
			get {
				return instance;
			}
		}
		
		public static void ShowForm()
		{
			if (instance == null) {
				instance = new ManagerForm();
				#if !STANDALONE
				instance.Owner = ICSharpCode.SharpDevelop.Gui.WorkbenchSingleton.MainForm;
				#endif
				instance.Show();
			} else {
				instance.Activate();
			}
		}
		
		public ManagerForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			#if !STANDALONE
			ICSharpCode.SharpDevelop.Gui.FormLocationHelper.Apply(this, "AddInManager.WindowBounds", true);
			#endif
			
			#if STANDALONE
			actionFlowLayoutPanel.BackgroundImage = new Bitmap(typeof(ManagerForm).Assembly.GetManifestResourceStream("ICSharpCode.AddInManager.WizardBackground.png"));
			#else
			actionFlowLayoutPanel.BackgroundImage = ResourceService.GetBitmap("GeneralWizardBackground");
			#endif
			
			installButton.Text = ResourceService.GetString("AddInManager.InstallButton");
			uninstallButton.Text = ResourceService.GetString("AddInManager.ActionUninstall");
			closeButton.Text = ResourceService.GetString("Global.CloseButtonText");
			showPreinstalledAddInsCheckBox.Text = ResourceService.GetString("AddInManager.ShowPreinstalledAddIns");
			this.Text = ResourceService.GetString("AddInManager.Title");
			CreateAddInList();
		}
		
		void OnSplitContainerPanel1Paint(object sender, PaintEventArgs e)
		{
			if (visibleAddInCount == 0) {
				Rectangle rect = splitContainer.Panel1.ClientRectangle;
				rect.Offset(16, 16);
				rect.Inflate(-32, -32);
				e.Graphics.DrawString(ResourceService.GetString("AddInManager.NoAddInsInstalled"),
				                      Font, SystemBrushes.WindowText, rect);
			}
		}
		
		void CreateAddInList()
		{
			Stack<AddInControl> stack = new Stack<AddInControl>();
			int index = 0;
			AddInControl addInControl;
			
			List<AddIn> addInList = new List<AddIn>(AddInTree.AddIns);
			addInList.Sort(delegate(AddIn a, AddIn b) {
			               	return a.Name.CompareTo(b.Name);
			               });
			foreach (AddIn addIn in addInList) {
				string identity = addIn.Manifest.PrimaryIdentity;
				if (identity == null || addIn.Properties["addInManagerHidden"] == "true")
					continue;
				addInControl = new AddInControl(addIn);
				addInControl.Dock = DockStyle.Top;
				addInControl.TabIndex = index++;
				stack.Push(addInControl);
				addInControl.Enter += OnControlEnter;
				addInControl.Click += OnControlClick;
			}
			while (stack.Count > 0) {
				splitContainer.Panel1.Controls.Add(stack.Pop());
			}
			ShowPreinstalledAddInsCheckBoxCheckedChanged(null, null);
			#if SHOWALLADDINS
			showPreinstalledAddInsCheckBox.Visible = false;
			showPreinstalledAddInsCheckBox.Checked = true;
			#endif
			splitContainer.Panel2Collapsed = true;
		}
		
		void RefreshAddInList()
		{
			List<AddIn> oldSelected = selected;
			foreach (Control ctl in splitContainer.Panel1.Controls) {
				ctl.Dispose();
			}
			splitContainer.Panel1.Controls.Clear();
			CreateAddInList();
			if (oldSelected != null) {
				foreach (AddInControl ctl in splitContainer.Panel1.Controls) {
					if (oldSelected.Contains(ctl.AddIn))
						ctl.Selected = true;
				}
			}
			UpdateActionBox();
		}
		#endregion
		
		#region AddInList-Management
		int visibleAddInCount = 0;
		
		void ShowPreinstalledAddInsCheckBoxCheckedChanged(object sender, EventArgs e)
		{
			visibleAddInCount = 0;
			foreach (AddInControl ctl in splitContainer.Panel1.Controls) {
				ctl.Selected = false;
				bool visible;
				if (showPreinstalledAddInsCheckBox.Checked) {
					visible = true;
				} else {
					if (ctl == oldFocus)
						oldFocus = null;
					visible = !FileUtility.IsBaseDirectory(FileUtility.ApplicationRootPath, ctl.AddIn.FileName);
				}
				if (visible)
					visibleAddInCount += 1;
				ctl.Visible = visible;
			}
			UpdateActionBox();
		}
		
		void OnControlClick(object sender, EventArgs e)
		{
			// clicking again on already focused item:
			// remove selection of other items / or with Ctrl: toggle selection
			if (((Control)sender).Focused)
				OnControlEnter(sender, e);
		}
		
		AddInControl oldFocus;
		bool ignoreFocusChange;
		
		void OnControlEnter(object sender, EventArgs e)
		{
			if (ignoreFocusChange)
				return;
			bool ctrl = (ModifierKeys & Keys.Control) == Keys.Control;
			if ((ModifierKeys & Keys.Shift) == Keys.Shift && sender != oldFocus) {
				bool sel = false;
				foreach (AddInControl ctl in splitContainer.Panel1.Controls) {
					if (!ctl.Visible) continue;
					if (ctl == sender || ctl == oldFocus) {
						sel = !sel;
						ctl.Selected = true;
					} else {
						if (sel || !ctrl) {
							ctl.Selected = sel;
						}
					}
				}
			} else if (ctrl) {
				foreach (AddInControl ctl in splitContainer.Panel1.Controls) {
					if (ctl == sender)
						ctl.Selected = !ctl.Selected;
				}
				oldFocus = (AddInControl)sender;
			} else {
				foreach (AddInControl ctl in splitContainer.Panel1.Controls) {
					ctl.Selected = ctl == sender;
				}
				oldFocus = (AddInControl)sender;
			}
			UpdateActionBox();
		}
		#endregion
		
		#region UpdateActionBox
		List<AddIn> selected;
		AddInAction selectedAction;
		
		void UpdateActionBox()
		{
			ignoreFocusChange = true;
			selected = new List<AddIn>();
			foreach (AddInControl ctl in splitContainer.Panel1.Controls) {
				if (ctl.Selected)
					selected.Add(ctl.AddIn);
			}
			splitContainer.Panel2Collapsed = selected.Count == 0;
			if (selected.Count > 0) {
				dependencyTable.Visible = false;
				runActionButton.Visible = true;
				uninstallButton.Visible = true;
				
				bool allEnabled      = true;
				bool allDisabled     = true;
				bool allInstalling   = true;
				bool allUninstalling = true;
				bool allUpdating     = true;
				bool allUninstallable = true;
				bool hasErrors = false;
				foreach (AddIn addIn in selected) {
					allEnabled      &= addIn.Action == AddInAction.Enable;
					if (addIn.Action == AddInAction.DependencyError || addIn.Action == AddInAction.InstalledTwice)
						hasErrors = true;
					else
						allDisabled     &= addIn.Action == AddInAction.Disable;
					allUpdating     &= addIn.Action == AddInAction.Update;
					allInstalling   &= addIn.Action == AddInAction.Install;
					allUninstalling &= addIn.Action == AddInAction.Uninstall;
					if (allUninstallable) {
						if (FileUtility.IsBaseDirectory(FileUtility.ApplicationRootPath, addIn.FileName)) {
							allUninstallable = false;
						}
					}
				}
				if (allEnabled) {
					selectedAction = AddInAction.Disable;
					actionGroupBox.Text = runActionButton.Text = ResourceService.GetString("AddInManager.ActionDisable");
					actionDescription.Text = ResourceService.GetString("AddInManager.DescriptionDisable");
					runActionButton.Enabled = ShowDependencies(selected, ShowDependencyMode.Disable);
					uninstallButton.Enabled = allUninstallable && runActionButton.Enabled;
				} else if (allDisabled) {
					selectedAction = AddInAction.Enable;
					actionGroupBox.Text = runActionButton.Text = ResourceService.GetString("AddInManager.ActionEnable");
					actionDescription.Text = ResourceService.GetString("AddInManager.DescriptionEnable");
					runActionButton.Enabled = ShowDependencies(selected, ShowDependencyMode.Enable);
					if (hasErrors)
						runActionButton.Enabled = false;
					uninstallButton.Enabled = allUninstallable;
				} else if (allInstalling) {
					selectedAction = AddInAction.Uninstall;
					actionGroupBox.Text = runActionButton.Text = ResourceService.GetString("AddInManager.ActionCancelInstallation");
					actionDescription.Text = ResourceService.GetString("AddInManager.DescriptionCancelInstall");
					runActionButton.Enabled = ShowDependencies(selected, ShowDependencyMode.Disable);
					uninstallButton.Visible = false;
				} else if (allUninstalling) {
					selectedAction = AddInAction.Enable;
					actionGroupBox.Text = runActionButton.Text = ResourceService.GetString("AddInManager.ActionCancelDeinstallation");
					actionDescription.Text = ResourceService.GetString("AddInManager.DescriptionCancelDeinstallation");
					runActionButton.Enabled = ShowDependencies(selected, ShowDependencyMode.Enable);
					uninstallButton.Visible = false;
				} else if (allUpdating) {
					selectedAction = AddInAction.InstalledTwice;
					actionGroupBox.Text = runActionButton.Text = ResourceService.GetString("AddInManager.ActionCancelUpdate");
					actionDescription.Text = ResourceService.GetString("AddInManager.DescriptionCancelUpdate");
					runActionButton.Enabled = ShowDependencies(selected, ShowDependencyMode.CancelUpdate);
					uninstallButton.Visible = false;
				} else {
					actionGroupBox.Text = "";
					actionDescription.Text = ResourceService.GetString("AddInManager.DescriptionInconsistentSelection");
					runActionButton.Visible = false;
					uninstallButton.Visible = false;
				}
			}
			ignoreFocusChange = false;
		}
		
		enum ShowDependencyMode {
			Disable,
			Enable,
			CancelUpdate
		}
		
		bool ShowDependencies(IList<AddIn> addIns, ShowDependencyMode mode)
		{
			List<AddInReference> dependencies = new List<AddInReference>(); // only used with enable=true
			List<KeyValuePair<AddIn, AddInReference>> dependenciesToSel = new List<KeyValuePair<AddIn, AddInReference>>();
			Dictionary<string, Version> addInDict = new Dictionary<string, Version>();
			Dictionary<string, Version> modifiedAddIns = new Dictionary<string, Version>();
			
			// add available addins
			foreach (AddIn addIn in AddInTree.AddIns) {
				if (addIn.Action != AddInAction.Enable && addIn.Action != AddInAction.Install)
					continue;
				if (addIns.Contains(addIn))
					continue;
				foreach (KeyValuePair<string, Version> pair in addIn.Manifest.Identities) {
					addInDict[pair.Key] = pair.Value;
				}
			}
			
			// create list of modified addin names
			foreach (AddIn addIn in addIns) {
				foreach (KeyValuePair<string, Version> pair in addIn.Manifest.Identities) {
					modifiedAddIns[pair.Key] = pair.Value;
				}
			}

⌨️ 快捷键说明

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