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

📄 xmlview.cs

📁 c#源代码
💻 CS
📖 第 1 页 / 共 2 页
字号:
//
// SharpDevelop Xml Editor
//
// Copyright (C) 2005 Matthew Ward
//
// 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
//
// Matthew Ward (mrward@users.sourceforge.net)

using ICSharpCode.Core.Properties;
using ICSharpCode.Core.Services;
using ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.Gui.Pads;
using ICSharpCode.SharpDevelop.Services;
using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Document;
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Schema;

namespace ICSharpCode.XmlEditor
{
	/// <summary>
	/// Wrapper class for the XmlEditor used when displaying the xml file.
	/// </summary>
	public class XmlView : AbstractViewContent, IEditable, IClipboardHandler, IParseInformationListener, IMementoCapable, IPrintable, ITextEditorControlProvider, IPositionable
	{
		/// <summary>
		/// The language handled by this view.
		/// </summary>
		public static readonly string Language = "XML";

		delegate void RefreshDelegate(AbstractMargin margin);

		XmlEditorControl xmlEditor = new XmlEditorControl();
		FileSystemWatcher watcher;
		bool wasChangedExternally;
		MessageViewCategory category;
		
		StringParserService stringParserService = (StringParserService)ServiceManager.Services.GetService(typeof(StringParserService));
		
		public XmlView()
		{
			xmlEditor.Dock = DockStyle.Fill;
			
			xmlEditor.SchemaCompletionDataItems = XmlSchemaManager.SchemaCompletionDataItems;
			xmlEditor.Document.DocumentChanged += new DocumentEventHandler(DocumentChanged);
		
			xmlEditor.ActiveTextAreaControl.Caret.CaretModeChanged += new EventHandler(CaretModeChanged);
			xmlEditor.ActiveTextAreaControl.Enter += new EventHandler(CaretUpdate);
			((Form)ICSharpCode.SharpDevelop.Gui.WorkbenchSingleton.Workbench).Activated += new EventHandler(GotFocusEvent);
			
			// Listen for changes to the xml editor properties.
			XmlEditorAddInOptions.PropertyChanged += new PropertyEventHandler(PropertyChanged); 
			XmlSchemaManager.UserSchemaAdded += new EventHandler(UserSchemaAdded);
			XmlSchemaManager.UserSchemaRemoved += new EventHandler(UserSchemaRemoved);
		}
		
		/// <summary>
		/// Loads the string content into the view.
		/// </summary>
		public void LoadContent(string content)
		{
			StringParserService stringParserService = (StringParserService)ServiceManager.Services.GetService(typeof(StringParserService));
			xmlEditor.Document.TextContent = stringParserService.Parse(content);
			xmlEditor.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy(XmlView.Language);
			UpdateFolding();
		}
		
		/// <summary>
		/// Can create content for the 'XML' language.
		/// </summary>
		public static bool IsLanguageHandled(string language)
		{
			return language == XmlView.Language;
		}
		
		/// <summary>
		/// Returns whether the view can handle the specified file.
		/// </summary>
		public static bool IsFileNameHandled(string fileName)
		{
			return IsXmlFileExtension(Path.GetExtension(fileName));
		}
		
		/// <summary>
		/// Gets the known xml file extensions.
		/// </summary>
		public static string[] GetXmlFileExtensions()
		{
			IHighlightingStrategy strategy = HighlightingManager.Manager.FindHighlighter(XmlView.Language);
			if (strategy != null) {
				return strategy.Extensions;
			}
			
			return new string[0];
		}		
		
		/// <summary>
		/// Validates the xml against known schemas.
		/// </summary>
		public void ValidateXml()
		{
			ClearTasks();
			Category.ClearText();
			OutputWindowWriteLine(stringParserService.Parse("${res:MainWindow.XmlValidationMessages.ValidationStarted}"));

			try {
				StringReader stringReader = new StringReader(xmlEditor.Document.TextContent);
				XmlTextReader xmlReader = new XmlTextReader(stringReader);
				xmlReader.XmlResolver = null;
				XmlValidatingReader reader = new XmlValidatingReader(xmlReader);
				reader.XmlResolver = null;
				
				foreach (XmlSchemaCompletionData schemaData in XmlSchemaManager.SchemaCompletionDataItems) {
					reader.Schemas.Add(schemaData.Schema);
				}
				
				XmlDocument doc = new XmlDocument();
				doc.Load(reader);
				                   
				OutputWindowWriteLine(String.Empty);
				OutputWindowWriteLine(stringParserService.Parse("${res:MainWindow.XmlValidationMessages.ValidationSuccess}"));
						
			} catch (XmlSchemaException ex) {
				DisplayValidationError(xmlEditor.FileName, ex.Message, ex.LinePosition - 1, ex.LineNumber - 1);
			} catch (XmlException ex) {
				DisplayValidationError(xmlEditor.FileName, ex.Message, ex.LinePosition - 1, ex.LineNumber - 1);
			}
			
			// Show tasks.
			if (HasTasks && ShowTaskListAfterBuild) {
				ShowTasks();
			}		
		}
		
		/// <summary>
		/// Gets the name of a new view.
		/// </summary>
		public override string UntitledName {
			get {
				return base.UntitledName;
			}
			set {
				base.UntitledName = value;
				xmlEditor.FileName = value;
				SetDefaultSchema(Path.GetExtension(xmlEditor.FileName));
			}
		}		
		
		public override void Dispose()
		{
			((Form)ICSharpCode.SharpDevelop.Gui.WorkbenchSingleton.Workbench).Activated -= new EventHandler(GotFocusEvent);
			
			XmlEditorAddInOptions.PropertyChanged -= new PropertyEventHandler(PropertyChanged);
			XmlSchemaManager.UserSchemaAdded -= new EventHandler(UserSchemaAdded);
			XmlSchemaManager.UserSchemaRemoved -= new EventHandler(UserSchemaRemoved);

			xmlEditor.Dispose();
		}		
		
		/// <summary>
		/// Sets the filename associated with the view.
		/// </summary>
		public override string FileName {
			set {
				if (Path.GetExtension(FileName) != Path.GetExtension(value)) {
					if (xmlEditor.Document.HighlightingStrategy != null) {
						xmlEditor.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategyForFile(value);
						xmlEditor.Refresh();
					}
				}
				base.FileName  = value;
				base.TitleName = Path.GetFileName(value);
				
				SetDefaultSchema(Path.GetExtension(xmlEditor.FileName));
			}
		}		
		
		#region IEditable interface
		
		public IClipboardHandler ClipboardHandler {
			get {
				return this;
			}
		}
		
		public bool EnableUndo {
			get {
				return xmlEditor.EnableUndo;
			}
		}
		
		public bool EnableRedo {
			get {
				return xmlEditor.EnableUndo;
			}
		}
		
		public string Text {
			get {
				return xmlEditor.Document.TextContent;
			}
			set {
				xmlEditor.Document.TextContent = value;
			}
		}
		
		public void Redo()
		{
			xmlEditor.Redo();
		}
		
		public void Undo()
		{
			xmlEditor.Undo();
		}
		
		#endregion
		
		#region AbstractViewContent implementation
		
		public override Control Control {
			get {
				return xmlEditor;
			}
		}
		
		public override void Load(string fileName)
		{
			xmlEditor.IsReadOnly = IsFileReadOnly(fileName);
			xmlEditor.LoadFile(fileName);
			FileName  = fileName;
			TitleName = Path.GetFileName(fileName);
			IsDirty     = false;
			UpdateFolding();
			SetWatcher();	
		}
		
		public override void Save(string fileName)
		{
			OnSaving(EventArgs.Empty);

			if (watcher != null) {
				watcher.EnableRaisingEvents = false;
			}

			xmlEditor.SaveFile(fileName);
			FileName = fileName;
			TitleName = Path.GetFileName(fileName);
			IsDirty = false;
			
			SetWatcher();
			OnSaved(new SaveEventArgs(true));
		}
		
		
		#endregion
		
		#region IClipboardHandler interface

		public bool EnableCut {
			get {
				return xmlEditor.ActiveTextAreaControl.TextArea.ClipboardHandler.EnableCut;
			}
		}
		
		public bool EnableCopy {
			get {
				return xmlEditor.ActiveTextAreaControl.TextArea.ClipboardHandler.EnableCopy;
			}
		}
		
		public bool EnablePaste {
			get {
				return xmlEditor.ActiveTextAreaControl.TextArea.ClipboardHandler.EnablePaste;
			}
		}
		
		public bool EnableDelete {
			get {
				return xmlEditor.ActiveTextAreaControl.TextArea.ClipboardHandler.EnableDelete;
			}
		}
		
		public bool EnableSelectAll {
			get {
				return xmlEditor.ActiveTextAreaControl.TextArea.ClipboardHandler.EnableSelectAll;
			}
		}
		
		public void SelectAll(object sender, System.EventArgs e)
		{
			xmlEditor.ActiveTextAreaControl.TextArea.ClipboardHandler.SelectAll(sender, e);
		}
		
		public void Delete(object sender, System.EventArgs e)
		{
			xmlEditor.ActiveTextAreaControl.TextArea.ClipboardHandler.Delete(sender, e);
		}
		
		public void Paste(object sender, System.EventArgs e)
		{
			xmlEditor.ActiveTextAreaControl.TextArea.ClipboardHandler.Paste(sender, e);
		}
		
		public void Copy(object sender, System.EventArgs e)
		{
			xmlEditor.ActiveTextAreaControl.TextArea.ClipboardHandler.Copy(sender, e);
		}
		
		public void Cut(object sender, System.EventArgs e)
		{
			xmlEditor.ActiveTextAreaControl.TextArea.ClipboardHandler.Cut(sender, e);
		}
		
		#endregion
			
		#region IParseInformationListener interface
		
		public void ParseInformationUpdated(IParseInformation parseInfo)
		{
			UpdateFolding();
		}
		

⌨️ 快捷键说明

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