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

📄 projectbrowsernode.cs

📁 c#精彩编程百例(源代码)
💻 CS
字号:
//  ProjectBrowserNode.cs 
//  Copyright (C) 2000 Mike Krueger
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;
using System.Drawing;

using SharpDevelop.Tool.Function;
using SharpDevelop.Internal.Project;

namespace SharpDevelop.Gui.Navigation.ProjectBrowser {

	public enum ProjectNodeType {
		Project,
		Folder,
		ResourceFile,
		TextFile,
		NonEditable, // with this, you can prevent to set auto image&behaviour to this node
		ResourceFolder,
		ReferenceFolder,
		Reference
	}
	
	public class ProjectBrowserNode : TreeNode 
	{
		string referencename = null; // filename for references.
		string resourcepath  = null; // path for resources (resource paths can't be altered)   

		string text          = "";   // real text of the node
		
		ProjectNodeType type;
		ISdProject        project;
		
		public ISdProject Project {
			get {
				return project;
			}
		}
		
		public string GetNameWithExtension(string name)
		{
			string oldextension = Path.GetExtension(text);
			string newextension = Path.GetExtension(name);
			if (newextension == "")
				return name + oldextension;
			return name;
		}
		
		public string Name {
			get {
				return text;
			}
			set {
				if (type != ProjectNodeType.Reference) {
					string oldextension = Path.GetExtension(text);
					text = GetNameWithExtension(value);
					string newextension = Path.GetExtension(text);
					if (oldextension != newextension) {
						SetTypeTo(text);
						SetIcon();
					}
					Text = Path.GetFileNameWithoutExtension(text);
				} else {
					text = value;
					Text = text;
				}
			}
		}
		
		public ProjectNodeType ProjectNodeType {
			get {
				return type;
			}
		}
		public ProjectBrowserNode Root {
			get {
				if (type == ProjectNodeType.Project) 
					return this;
				if (Parent == null)
					return null;
				return ((ProjectBrowserNode)Parent).Root;
			}
		}
		
		public string FileName {
			get {
				if (type == ProjectNodeType.Reference) {
					return referencename;
				}
				if (type == ProjectNodeType.ResourceFile) 
					return resourcepath + "\\" + text;
				
				if (type == ProjectNodeType.Project) 
					return project.WorkingDirectory;
				
				Debug.Assert(Parent != null, "ProjectBrowserNode.FileName : Parent can't be null");
				return ((ProjectBrowserNode)Parent).FileName + "\\" + text;
			}
		}
		
		public ProjectBrowserNode(ISdProject project, string text)
		{
			SetTypeTo(text);
			this.project = project;
			Init();
			this.Name    = text;
		}
		
		public ProjectBrowserNode(ISdProject project, ProjectNodeType type, string text)
		{
			this.type    = type;
			this.project = project;
			this.text    = text;
			Init();
			this.Name    = text;
		}
		
		void SetTypeTo(string file)
		{
			if (type == ProjectNodeType.Reference)
				return;
			switch (FSTypeUtility.GetFileType(file)) {
				case FileType.Resource:
					type = ProjectNodeType.ResourceFile;
					break;
				default:
					type = ProjectNodeType.TextFile;
					break;
			}
		}
		
		void Init()
		{
			SetIcon();
			if (type == ProjectNodeType.Reference) {
				referencename = text;
				foreach (string reference in project.References) {
					if (Path.GetFileName(reference) == text) {
						referencename = reference;
					}
				}
			}
			if (type == ProjectNodeType.ResourceFile) {
				foreach (string resource in project.Files) {
					if (Path.GetFileName(resource) == text) 
						resourcepath = Path.GetDirectoryName(resource);
				}
				Debug.Assert(resourcepath != null, "ProjectBrowserNode.FileName : Resource " + text + " not found in project");
			}
		}
		
		void SetIcon()
		{
			ForeColor  = Color.Black;
			switch (type) {
				case ProjectNodeType.Project:
					ImageIndex  = SelectedImageIndex = 0;
					break;
				case ProjectNodeType.Folder:
					ImageIndex = SelectedImageIndex = 1;
					break;
				case ProjectNodeType.ResourceFile:
					ImageIndex    = SelectedImageIndex = 7 + FileUtility.GetImageIndexFor(".resources");
					break;
				case ProjectNodeType.ReferenceFolder:
					ImageIndex    = SelectedImageIndex = 3;
					break;
				case ProjectNodeType.Reference:
					ImageIndex    = SelectedImageIndex = 6;
					break;
				case ProjectNodeType.ResourceFolder:
					ImageIndex    = SelectedImageIndex = 5;
					break;
				case ProjectNodeType.TextFile:
					ImageIndex    = SelectedImageIndex = 7 + FileUtility.GetImageIndexFor(text);
				break;
			}
		}
	}
}

⌨️ 快捷键说明

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