📄 vbnetdesignerdisplaybindingwrapper.cs
字号:
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Mike Krger" email="mike@icsharpcode.net"/>
// <version value="$version"/>
// </file>
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Collections;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Design;
using System.Reflection;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Xml;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.Internal.Project;
using ICSharpCode.SharpDevelop.Internal.Undo;
using ICSharpCode.SharpDevelop.Gui.Components;
using ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor;
using ICSharpCode.Core.Properties;
using ICSharpCode.Core.AddIns;
using ICSharpCode.Core.Services;
using ICSharpCode.SharpDevelop.Services;
using SharpDevelop.Internal.Parser;
using ICSharpCode.SharpDevelop.FormDesigner.Services;
using ICSharpCode.SharpDevelop.FormDesigner.Hosts;
using ICSharpCode.SharpDevelop.FormDesigner.Util;
using ICSharpCode.Core.AddIns.Codons;
using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Document;
using ICSharpCode.SharpRefactory.Parser.VB;
using ICSharpCode.SharpRefactory.Parser.AST.VB;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using Microsoft.VisualBasic;
namespace ICSharpCode.SharpDevelop.FormDesigner
{
public class VBNetDesignerDisplayBindingWrapper : FormDesignerDisplayBindingBase, ISecondaryViewContent
{
protected bool failedDesignerInitialize;
IViewContent viewContent;
IClass c;
IMethod initializeComponents;
ITextEditorControlProvider textAreaControlProvider;
string compilationErrors = String.Empty;
public override string FileName {
get {
string fileName = textAreaControlProvider.TextEditorControl.FileName;
return fileName == null ? viewContent.UntitledName : fileName;
}
}
public override System.Windows.Forms.Control Control {
get {
return base.designPanel;
}
}
public override bool IsDirty {
get {
if (viewContent == null) {
return false;
}
return viewContent.IsDirty;
}
set {
if (viewContent != null) {
viewContent.IsDirty = value;
}
}
}
IDocument Document {
get {
return textAreaControlProvider.TextEditorControl.Document;
}
}
public VBNetDesignerDisplayBindingWrapper(IViewContent viewContent)
{
this.viewContent = viewContent;
this.textAreaControlProvider = viewContent as ITextEditorControlProvider;
Reparse(FileName, textAreaControlProvider.TextEditorControl.Document.TextContent);
InitializeComponents();
}
void InitializeComponents()
{
failedDesignerInitialize = false;
undoHandler.Reset();
Reload();
UpdateSelectableObjects();
if (designPanel != null) {
base.designPanel.Disable();
}
}
protected override void CreateDesignerHost()
{
base.CreateDesignerHost();
host.AddService(typeof(CodeDomProvider), new VBCodeProvider());
}
public override void Reload()
{
try {
Initialize();
} catch (Exception ex) {
Console.WriteLine("Initialization exception : " + ex);
}
bool dirty = viewContent.IsDirty;
if (host != null) {
base.host.SetRootFullName(c.FullyQualifiedName);
}
try {
// host.DesignerLoader.BeginLoad(host);
// parse source file
Lexer lexer = new Lexer(new ICSharpCode.SharpRefactory.Parser.VB.StringReader(textAreaControlProvider.TextEditorControl.Document.TextContent));
Parser p = new Parser();
p.Parse(lexer);
failedDesignerInitialize = p.Errors.count != 0;
if (failedDesignerInitialize) {
compilationErrors = p.Errors.ErrorOutput;
return;
}
// host.DesignerLoader.BeginLoad(host);
CodeDomDesignerSerializetionManager serializationManager = (CodeDomDesignerSerializetionManager)host.GetService(typeof(IDesignerSerializationManager));
serializationManager.Initialize();
CodeDOMVisitor codeDOMVisitor = new CodeDOMVisitor();
codeDOMVisitor.Visit(p.compilationUnit, null);
Type baseType = typeof(System.Windows.Forms.Form);
CodeTypeDeclaration type = null;
foreach (CodeNamespace codeNamespace in codeDOMVisitor.codeCompileUnit.Namespaces) {
foreach (CodeTypeDeclaration t in codeNamespace.Types) {
if (t.BaseTypes.Count > 0 ) {
baseType = host.GetType(t.BaseTypes[0].BaseType);
type = t;
goto typeFound;
}
}
}
typeFound:
if (type == null) {
throw new Exception("No de-serializable class found.");
}
CodeDomSerializer rootSerializer = serializationManager.GetRootSerializer(baseType);
if (rootSerializer == null) {
throw new Exception("No root serializer found");
}
// // output generated CodeDOM to the console :
// Microsoft.VisualBasic.VBCodeProvider provider = new Microsoft.VisualBasic.VBCodeProvider();
// System.CodeDom.Compiler.ICodeGenerator generator = provider.CreateGenerator();
// generator.GenerateCodeFromCompileUnit(codeDOMVisitor.codeCompileUnit, Console.Out, null);
// foreach (CodeNamespace codeNamespace in codeDOMVisitor.codeCompileUnit.Namespaces) {
// if (codeNamespace.Types.Count > 0) {
// Console.WriteLine("Try to deserialize type : " + codeNamespace.Types[0].Name);
DesignerResourceService designerResourceService = (DesignerResourceService)host.GetService(typeof(System.ComponentModel.Design.IResourceService));
if (designerResourceService != null) {
designerResourceService.SerializationStarted(false);
}
// designerResourceService.NameSpace = codeNamespace.Name;
// designerResourceService.RootType = codeNamespace.Types[0].Name;
// designerResourceService.LoadResources();
try {
rootSerializer.Deserialize(serializationManager, type);
} catch (Exception e) {
Console.WriteLine(e);
StringParserService stringParserService = (StringParserService)ServiceManager.Services.GetService(typeof(StringParserService));
compilationErrors = stringParserService.Parse("${res:ICSharpCode.SharpDevelop.FormDesigner.CantDeserializeFormError}");
failedDesignerInitialize = true;
return;
}
serializationManager.OnSerializationComplete();
if (designerResourceService != null) {
designerResourceService.SerializationEnded(false);
}
designPanel.SetRootDesigner();
designPanel.Enable();
// break;
// }
// }
failedDesignerInitialize = false;
undoHandler.Reset();
// host.DesignerLoader.EndLoad();
} catch (Exception ex) {
compilationErrors = ex.ToString();
failedDesignerInitialize = true;
}
viewContent.IsDirty = dirty;
}
#region MergeForm
string GetInitializeComponentsString(IDocument doc, IMethod initializeComponents)
{
LineSegment beginLine = doc.GetLineSegment(initializeComponents.Region.BeginLine - 1);
LineSegment endLine = doc.GetLineSegment(initializeComponents.BodyRegion.EndLine - 1);
int startOffset = beginLine.Offset + initializeComponents.Region.BeginColumn - 1;
int endOffset = endLine.Offset + initializeComponents.BodyRegion.EndColumn - 1;
string initializeComponentsString = doc.GetText(startOffset, endOffset - startOffset);
return initializeComponentsString;
}
ArrayList GetUsedFields(IDocument doc, IClass c, IMethod initializeComponents)
{
string InitializeComponentsString = GetInitializeComponentsString(doc, initializeComponents);
ArrayList fields = new ArrayList();
foreach (IField field in c.Fields) {
// if (field.IsPrivate) {
if (InitializeComponentsString.IndexOf(String.Concat("Me.", field.Name, " ")) >= 0) {
fields.Add(field);
}
// }
}
return fields;
}
void DeleteFormFields(IDocument doc)
{
ArrayList fields = GetUsedFields(doc, c, initializeComponents);
for (int i = fields.Count - 1; i >= 0; --i) {
IField field = (IField)fields[i];
LineSegment fieldLine = doc.GetLineSegment(field.Region.BeginLine - 1);
doc.Remove(fieldLine.Offset, fieldLine.TotalLength);
}
}
protected virtual void MergeFormChanges()
{
if (this.failedDesignerInitialize) {
return;
}
bool dirty = viewContent.IsDirty;
IParserService parserService = (IParserService)ICSharpCode.Core.Services.ServiceManager.Services.GetService(typeof(IParserService));
// generate file and get initialize components string
string currentForm = GetDataAs("VB.NET");
DesignerResourceService designerResourceService = (DesignerResourceService)host.GetService(typeof(System.ComponentModel.Design.IResourceService));
if (designerResourceService != null) {
this.resources = new Hashtable();
if (designerResourceService.Resources != null && designerResourceService.Resources.Count != 0) {
foreach(DictionaryEntry entry in designerResourceService.Resources)
this.resources[entry.Key] = new DesignerResourceService.ResourceStorage((DesignerResourceService.ResourceStorage)entry.Value);
}
}
IParseInformation generatedInfo = parserService.ParseFile(FileName, currentForm, false);
ICompilationUnit cu = (ICompilationUnit)generatedInfo.BestCompilationUnit;
if (cu.Classes == null || cu.Classes.Count == 0) {
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -