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.

500 lines
16 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Mesnac.Docking;
using ICSharpCode.Core;
namespace Mesnac.Gui.Workbench
{
using Mesnac.Core.Service;
using Mesnac.PlugIn.Pad;
using Mesnac.PlugIn.View;
using Mesnac.PlugIn.Workbench;
public class WorkbenchLayout : IWorkbenchLayout
{
#region 定义变量
private int _leftWidth = 200; //左面板的宽度
private int _rightWidth = 200; //右面板的宽度
private int _bottomHeight = 80; //底部面板的高度
private IWorkbench _workbench; //默认工作台
private const string _layoutFile = "LayoutConfigFile.xml";
private DockPanel _dockPanel;
private DockContent _lastActiveContent;
private Dictionary<string, PadContentWrapper> _contentHash = new Dictionary<string, PadContentWrapper>();
private bool[] _padVisibilitys = null;
#endregion
#region 定义属性
/// <summary>
/// 布局管理容器中的内容集合
/// </summary>
public Dictionary<string, DockContent> ContentHash
{
get
{
Dictionary<string, DockContent> dic = new Dictionary<string, DockContent>();
foreach (string key in this._contentHash.Keys)
{
dic.Add(key, this._contentHash[key]);
}
return dic;
}
}
#endregion
#region 定义外部方法
[System.Runtime.InteropServices.DllImport("User32.dll")]
public static extern bool LockWindowUpdate(IntPtr hWnd);
#endregion
#region 默认构造方法
public WorkbenchLayout(IWorkbench wb) {
this._workbench = wb;
InitMethod();
}
private void InitMethod()
{
Form wbForm = this._workbench as Form;
foreach (Control c in wbForm.Controls)
{
if (c is Mesnac.Docking.DockPanel)
{
this._dockPanel = c as Mesnac.Docking.DockPanel;
this._dockPanel.DockLeftPortion = this._leftWidth;
this._dockPanel.DockRightPortion = this._rightWidth;
this._dockPanel.DockBottomPortion = this._bottomHeight;
break;
}
}
if (this._dockPanel == null)
{
this._dockPanel = new DockPanel();
this._dockPanel.Dock = DockStyle.Fill;
wbForm.Controls.Add(this._dockPanel);
}
}
#endregion
#region 实现IWorkbenchLayout成员
public object Owner
{
get
{
return this._workbench;
}
set
{
this._workbench = value as IWorkbench;
}
}
public object ActiveConent
{
get {
DockContent activeConetnt;
if (this._dockPanel == null)
{
activeConetnt = this._lastActiveContent;
}
else
{
activeConetnt = this._dockPanel.ActiveContent as DockContent ?? this._lastActiveContent;
}
this._lastActiveContent = activeConetnt;
if (activeConetnt == null)
{
return null;
}
//此处加载工作窗口
//
if (activeConetnt is PadContentWrapper)
{
return (activeConetnt as PadContentWrapper).PadContent;
}
return activeConetnt;
}
}
/// <summary>
/// 初始化工作区
/// </summary>
public void Attach()
{
//附加主菜单
if (this._dockPanel == null)
{
this.InitMethod();
}
MesnacServiceManager.Instance.LoggingService.Info("显示工作区面板...");
this.ShowPads();
MesnacServiceManager.Instance.LoggingService.Info("重绘所有组件...");
this.RedrawAllComponents();
MesnacServiceManager.Instance.LoggingService.Info("加载布局配置...");
this.LoadConfiguration();
(this._workbench as Form).ResumeLayout(false);
}
public void Detach()
{
this.StoreConfiguration();
this.DetachPadContents(true);
this.DetachViewContents(true);
try
{
if (this._dockPanel != null)
{
(this._workbench as Form).Controls.Remove(this._dockPanel);
this._dockPanel.Dispose();
this._dockPanel = null;
}
}
catch (Exception e)
{
MesnacServiceManager.Instance.LoggingService.Error(e.Message);
}
if (this._contentHash != null)
{
this._contentHash.Clear();
}
(this._workbench as Form).Controls.Clear();
}
/// <summary>
/// 设置全屏的面板状态
/// </summary>
/// <param name="flag"></param>
public void SetFullScreenPads(bool flag)
{
if (flag)
{
if (this._padVisibilitys != null)
{
for (int i = 0; i < this._padVisibilitys.Length; i++)
{
if (this._padVisibilitys[i])
{
if ((this._dockPanel.Contents[i] as DockContent).ShowHint != DockState.Float && (this._dockPanel.Contents[i] as DockContent).ShowHint != DockState.Document)
{
(this._dockPanel.Contents[i] as DockContent).Show();
}
}
}
}
}
else
{
this._padVisibilitys = new bool[this._dockPanel.Contents.Count];
for (int i = 0; i < this._dockPanel.Contents.Count; i++)
{
this._padVisibilitys[i] = (this._dockPanel.Contents[i] as DockContent).Visible;
if ((this._dockPanel.Contents[i] as DockContent).ShowHint != DockState.Float && (this._dockPanel.Contents[i] as DockContent).ShowHint != DockState.Document)
{
(this._dockPanel.Contents[i] as DockContent).Hide();
}
}
}
}
public void ShowPad(Mesnac.PlugIn.Pad.PadDescriptor content)
{
this.ShowPad(content, false);
}
public void ShowPad(Mesnac.PlugIn.Pad.PadDescriptor content, bool bActivateIt)
{
if (content == null)
{
return;
}
if (!this._contentHash.ContainsKey(content.Class))
{
DockContent newContent = this.CreateContent(content);
newContent.Show(this._dockPanel);
}
else
{
this._contentHash[content.Class].Show();
if (bActivateIt)
{
this._contentHash[content.Class].PanelPane.Activate();
}
}
}
/// <summary>
/// 显示视图窗口
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
public IWorkbenchWindow ShowView(IViewContent content)
{
if (content.MyOwner is WorkspaceWindow)
{
WorkspaceWindow oldWindow = content.MyOwner as WorkspaceWindow;
if (!oldWindow.IsDisposed)
{
if (oldWindow.Visible)
{
oldWindow.SelectWindow();
}
else
{
oldWindow.Show(this._dockPanel);
}
return oldWindow;
}
}
WorkspaceWindow workspaceWindow = new WorkspaceWindow(content);
if (this._dockPanel != null)
{
workspaceWindow.Show(this._dockPanel);
}
return workspaceWindow;
}
/// <summary>
/// 显示视图窗口
/// </summary>
/// <param name="content">要显示的视图内容</param>
/// <param name="dockState">要显示的视图状态</param>
/// <returns></returns>
public IWorkbenchWindow ShowView(IViewContent content, DockState dockState)
{
if (content.MyOwner is WorkspaceWindow)
{
WorkspaceWindow oldWindow = content.MyOwner as WorkspaceWindow;
if (!oldWindow.IsDisposed)
{
oldWindow.ShowHint = dockState;
oldWindow.Show(this._dockPanel);
return oldWindow;
}
}
WorkspaceWindow workspaceWindow = new WorkspaceWindow(content);
workspaceWindow.ShowHint = dockState;
if (this._dockPanel != null)
{
workspaceWindow.Show(this._dockPanel);
}
return workspaceWindow;
}
/// <summary>
/// 重绘组件
/// </summary>
public void RedrawAllComponents()
{
foreach (PadDescriptor padDescriptor in this._workbench.PadContentCollection)
{
DockContent c = this._contentHash[padDescriptor.Class];
if (c != null)
{
c.Text = StringParser.Parse(padDescriptor.Title);
}
}
//RedrawMainMenu();
}
/// <summary>
/// 加载布局配置
/// </summary>
public void LoadConfiguration()
{
if (this._dockPanel != null)
{
string configPath = Path.Combine(PropertyService.ConfigDirectory, "layouts", _layoutFile);
if (!File.Exists(configPath))
return;
LockWindowUpdate((this._workbench as Form).Handle);
try
{
this._dockPanel.LoadFromXml(configPath, new DeserializeDockContent(this.GetContentFromPersistString));
MesnacServiceManager.Instance.LoggingService.Info("布局配置加载成功...");
}
catch (Exception ex)
{
MesnacServiceManager.Instance.LoggingService.Error(ex.Message);
}
finally
{
LockWindowUpdate(IntPtr.Zero);
}
}
}
/// <summary>
/// 保存布局配置
/// </summary>
public void StoreConfiguration()
{
try
{
if (this._dockPanel != null)
{
string configPath = Path.Combine(PropertyService.ConfigDirectory, "layouts");
if (!Directory.Exists(configPath))
{
Directory.CreateDirectory(configPath);
}
MesnacServiceManager.Instance.LoggingService.Info("保存布局配置...");
this._dockPanel.SaveAsXml(Path.Combine(configPath, _layoutFile));
}
}
catch (Exception e)
{
MesnacServiceManager.Instance.LoggingService.Error(e.Message);
}
}
#endregion
#region 辅助方法
/// <summary>
/// 解析布局配置文件
/// </summary>
/// <param name="persistString"></param>
/// <returns></returns>
public IDockContent GetContentFromPersistString(string persistString)
{
//文档
if (persistString.StartsWith("ViewContent|"))
{
string filePath = persistString.Substring("ViewContent|".Length);
if (File.Exists(filePath))
{
}
}
//面板
foreach (PadDescriptor pad in this._workbench.PadContentCollection)
{
if (pad.Class == persistString)
{
return this.CreateContent(pad);
}
}
return null;
}
public void ShowPads()
{
foreach (PadDescriptor content in this._workbench.PadContentCollection)
{
if (!this._contentHash.ContainsKey(content.Class))
{
this.ShowPad(content);
}
}
foreach (PadContentWrapper content in this._contentHash.Values)
{
content.AllowInitialize();
}
}
private void ShowViewContents()
{
foreach (IViewContent content in WorkbenchSingleton.Workbench.ViewContentCollection)
{
this.ShowView(content);
}
}
private void DetachPadContents(bool dispose)
{
foreach (PadContentWrapper padContentWrapper in this._contentHash.Values)
{
padContentWrapper._allowInitialize = false;
}
foreach (PadDescriptor content in this._workbench.PadContentCollection)
{
try
{
if (this._contentHash.ContainsKey(content.Class))
{
PadContentWrapper padContentWrapper = this._contentHash[content.Class];
padContentWrapper.DockPanel = null;
if (dispose)
{
padContentWrapper.DetachContent();
padContentWrapper.Dispose();
}
}
}
catch (Exception ex)
{
MesnacServiceManager.Instance.LoggingService.Error(ex.Message);
}
}
this._workbench.PadContentCollection.Clear();
}
private void DetachViewContents(bool dispose)
{
foreach (IViewContent viewContent in WorkbenchSingleton.Workbench.ViewContentCollection)
{
try
{
if (viewContent == null) { WorkbenchSingleton.Workbench.ViewContentCollection.Remove(viewContent); }
if (viewContent.MyOwner is WorkspaceWindow)
{
WorkspaceWindow f = viewContent.MyOwner as WorkspaceWindow;
f.DockPanel = null;
if (dispose)
{
viewContent.MyOwner = null;
f.DetachContent();
f.Dispose();
}
}
}
catch (Exception e) { MessageService.ShowError(e.Message); }
}
}
private PadContentWrapper CreateContent(PadDescriptor content)
{
if (this._contentHash.ContainsKey(content.Class))
{
return this._contentHash[content.Class];
}
//ICSharpCode.Core.Properties properties = ICSharpCode.Core.PropertyService.Get("Workspace.ViewMementos", new ICSharpCode.Core.Properties()) as ICSharpCode.Core.Properties;
PadContentWrapper newContent = new PadContentWrapper(content);
if (content.Icon != null)
{
newContent.Icon = MesnacServiceManager.Instance.IconService.GetIcon(content.Icon);
}
newContent.Text = ICSharpCode.Core.StringParser.Parse(content.Title);
_contentHash[content.Class] = newContent;
return newContent;
}
#endregion
}
}