You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
7.9 KiB
C#

using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Drawing;
using System.Drawing.Design;
using System.Data;
using System.Windows.Forms;
using System.IO;
using Loader;
namespace Host
{
public enum LoaderType
{
BasicDesignerLoader = 1,
CodeDomDesignerLoader = 2,
NoLoader = 3
}
public enum GraphType
{
Curve=16,
Meter=18,
Report=23
}
/// <summary>
/// Manages numerous HostSurfaces. Any services added to HostSurfaceManager
/// will be accessible to all HostSurfaces
/// </summary>
public class HostSurfaceManager : DesignSurfaceManager
{
//public BasicHostLoader basicHostLoader = null;
public MesnacHostLoader basicHostLoader = null;
public HostSurfaceManager() : base()
{
this.AddService(typeof(INameCreationService), new NameCreationService());
this.ActiveDesignSurfaceChanged += new ActiveDesignSurfaceChangedEventHandler(HostSurfaceManager_ActiveDesignSurfaceChanged);
}
protected override DesignSurface CreateDesignSurfaceCore(IServiceProvider parentProvider)
{
return new HostSurface(parentProvider);
}
/// <summary>
/// Gets a new HostSurface and loads it with the appropriate type of
/// root component.
/// </summary>
private HostControl GetNewHost(Type rootComponentType)
{
HostSurface hostSurface = (HostSurface)this.CreateDesignSurface(this.ServiceContainer);
hostSurface.UseSnapLines();
if (rootComponentType == typeof(Form))
hostSurface.BeginLoad(typeof(Form));
else if (rootComponentType == typeof(UserControl))
hostSurface.BeginLoad(typeof(UserControl));
else if (rootComponentType == typeof(Component))
hostSurface.BeginLoad(typeof(Component));
else
throw new Exception("Undefined Host Type: " + rootComponentType.ToString());
hostSurface.Initialize();
this.ActiveDesignSurface = hostSurface;
return new HostControl(hostSurface);
}
/// <summary>
/// Gets a new HostSurface and loads it with the appropriate type of
/// root component. Uses the appropriate Loader to load the HostSurface.
/// </summary>
public HostControl GetNewHost(Type rootComponentType, LoaderType loaderType)
{
if (loaderType == LoaderType.NoLoader)
return GetNewHost(rootComponentType);
HostSurface hostSurface = (HostSurface)this.CreateDesignSurface(this.ServiceContainer);
hostSurface.UseSnapLines();
IDesignerHost host = (IDesignerHost)hostSurface.GetService(typeof(IDesignerHost));
switch (loaderType)
{
case LoaderType.BasicDesignerLoader :
//BasicHostLoader basicHostLoader = new BasicHostLoader(rootComponentType);
MesnacHostLoader basicHostLoader = new MesnacHostLoader(rootComponentType);
hostSurface.BeginLoad(basicHostLoader);
hostSurface.Loader = basicHostLoader;
break;
case LoaderType.CodeDomDesignerLoader:
CodeDomHostLoader codeDomHostLoader = new CodeDomHostLoader();
hostSurface.BeginLoad(codeDomHostLoader);
hostSurface.Loader = codeDomHostLoader;
break;
default:
throw new Exception("Loader is not defined: " + loaderType.ToString());
}
hostSurface.Initialize();
return new HostControl(hostSurface);
}
public HostControl GetNewHost(Type rootComponentType, LoaderType loaderType, string rootName)
{
if (loaderType == LoaderType.NoLoader)
return GetNewHost(rootComponentType);
HostSurface hostSurface = (HostSurface)this.CreateDesignSurface(this.ServiceContainer);
hostSurface.UseSnapLines();
IDesignerHost host = (IDesignerHost)hostSurface.GetService(typeof(IDesignerHost));
switch (loaderType)
{
case LoaderType.BasicDesignerLoader:
//BasicHostLoader basicHostLoader = new BasicHostLoader(rootComponentType);
MesnacHostLoader basicHostLoader = new MesnacHostLoader(rootComponentType, rootName);
hostSurface.BeginLoad(basicHostLoader);
hostSurface.Loader = basicHostLoader;
break;
case LoaderType.CodeDomDesignerLoader:
CodeDomHostLoader codeDomHostLoader = new CodeDomHostLoader();
hostSurface.BeginLoad(codeDomHostLoader);
hostSurface.Loader = codeDomHostLoader;
break;
default:
throw new Exception("Loader is not defined: " + loaderType.ToString());
}
hostSurface.Initialize();
return new HostControl(hostSurface);
}
/// <summary>
/// Opens an Xml file and loads it up using BasicHostLoader (inherits from
/// BasicDesignerLoader)
/// </summary>
public HostControl GetNewHost(string fileName)
{
if (fileName == null || !File.Exists(fileName))
MessageBox.Show("<22>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ: " + fileName);
LoaderType loaderType = LoaderType.NoLoader;
if (fileName.EndsWith("xml"))
loaderType = LoaderType.BasicDesignerLoader;
if (loaderType == LoaderType.NoLoader || loaderType == LoaderType.CodeDomDesignerLoader)
{
throw new Exception("File cannot be opened. Please check the type or extension of the file. Supported format is Xml");
}
HostSurface hostSurface = (HostSurface)this.CreateDesignSurface(this.ServiceContainer);
hostSurface.UseSnapLines();
IDesignerHost host = (IDesignerHost)hostSurface.GetService(typeof(IDesignerHost));
//basicHostLoader = new BasicHostLoader(fileName,false);
basicHostLoader = new MesnacHostLoader(fileName, false);
hostSurface.BeginLoad(basicHostLoader);
hostSurface.Loader = basicHostLoader;
hostSurface.Initialize();
return new HostControl(hostSurface);
}
//<2F><><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD>ļ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>õĺ<C3B5><C4BA><EFBFBD>
public HostControl LoadNewHost(string fileName)
{
if (fileName == null || !File.Exists(fileName))
MessageBox.Show("<22>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ: " + fileName);
LoaderType loaderType = LoaderType.NoLoader;
if (fileName.EndsWith("xml"))
loaderType = LoaderType.BasicDesignerLoader;
if (loaderType == LoaderType.NoLoader || loaderType == LoaderType.CodeDomDesignerLoader)
{
throw new Exception("<22>ļ<EFBFBD><C4BC>򲻿<EFBFBD><F2B2BBBF><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD>Ϻ<EFBFBD>׺<EFBFBD><D7BA><EFBFBD><EFBFBD>xml<6D><6C>");
}
HostSurface hostSurface = (HostSurface)this.CreateDesignSurface(this.ServiceContainer);
hostSurface.UseSnapLines();
IDesignerHost host = (IDesignerHost)hostSurface.GetService(typeof(IDesignerHost));
//basicHostLoader = new BasicHostLoader(fileName, true);
basicHostLoader = new MesnacHostLoader(fileName, true);
hostSurface.BeginLoad(basicHostLoader);
hostSurface.Loader = basicHostLoader;
hostSurface.Initialize();
return new HostControl(hostSurface);
}
public void AddService(Type type, object serviceInstance)
{
if (this.ServiceContainer.GetService(type) != null)
{
this.ServiceContainer.RemoveService(type);
}
this.ServiceContainer.AddService(type, serviceInstance);
}
/// <summary>
/// Uses the OutputWindow service and writes out to it.
/// </summary>
void HostSurfaceManager_ActiveDesignSurfaceChanged(object sender, ActiveDesignSurfaceChangedEventArgs e)
{
//ToolWindows.OutputWindow o = this.GetService(typeof(ToolWindows.OutputWindow)) as ToolWindows.OutputWindow;
//o.RichTextBox.Text += "New host added.\n";
}
}// class
}// namespace