155 lines
4.6 KiB
C#
155 lines
4.6 KiB
C#
using log4net;
|
|
using log4net.Repository.Hierarchy;
|
|
using Microsoft.Office.Interop.Word;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace AIProofread.Model
|
|
{
|
|
/// <summary>
|
|
/// 文档列表
|
|
/// </summary>
|
|
public class DocumentList
|
|
{
|
|
public ILog Logger = LogHelper.GetLogger(typeof(DocumentList));
|
|
public List<DocumentInfo> documentList = new List<DocumentInfo>();
|
|
public DocumentInfo ActiveDocument { get; set; }
|
|
public int Count => documentList.Count;
|
|
|
|
public void Add(DocumentInfo documentInfo)
|
|
{
|
|
documentList.Add(documentInfo);
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
documentList.ForEach(doc =>{ doc.Dispose(); });
|
|
|
|
documentList.Clear();
|
|
}
|
|
|
|
public DocumentInfo Get(int index)
|
|
{
|
|
return documentList[index];
|
|
}
|
|
public DocumentInfo GetById(int id)
|
|
{
|
|
return Count > 0 ? documentList.FirstOrDefault(x => x.Id == id) : null;
|
|
}
|
|
public DocumentInfo Get(string uniqueId)
|
|
{
|
|
return Count > 0 ? documentList.FirstOrDefault(x => x.UniqueId == uniqueId) : null;
|
|
}
|
|
public DocumentInfo Get(Document doc)
|
|
{
|
|
return Count > 0 && doc != null ? documentList.FirstOrDefault(x => x.CurrentDocument == doc) : null;
|
|
}
|
|
|
|
public DocumentInfo GetActive()
|
|
{
|
|
// documentList.FirstOrDefault(x => x.IsActive)
|
|
return Count > 0 ? ActiveDocument : null;
|
|
}
|
|
|
|
public bool Contains(DocumentInfo documentInfo)
|
|
{
|
|
return Count > 0 && documentList.Contains(documentInfo);
|
|
}
|
|
|
|
public bool Contains(string uniqueId)
|
|
{
|
|
return Count > 0 && documentList.Any(x => x.UniqueId == uniqueId);
|
|
}
|
|
|
|
// 通过文档判断是否包含
|
|
public bool Contains(Document originDocument)
|
|
{
|
|
return Count > 0 && originDocument != null && documentList.Any(x => x.CurrentDocument == originDocument);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 移除文档
|
|
/// </summary>
|
|
/// <param name="documentInfo"></param>
|
|
/// <returns></returns>
|
|
public bool Remove(DocumentInfo documentInfo)
|
|
{
|
|
return documentList.Remove(documentInfo);
|
|
}
|
|
public bool Remove(Document originDocument)
|
|
{
|
|
if (Count > 0 && originDocument != null)
|
|
{
|
|
documentList.RemoveAll(x =>
|
|
{
|
|
if (x.CurrentDocument == originDocument)
|
|
{
|
|
x.Close();
|
|
return true;
|
|
}
|
|
return false;
|
|
});
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取文档的索引
|
|
/// </summary>
|
|
/// <param name="item"></param>
|
|
/// <returns></returns>
|
|
public int IndexOf(DocumentInfo item)
|
|
{
|
|
return documentList.IndexOf(item);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置当前激活的文档
|
|
/// </summary>
|
|
/// <param name="originDocument"></param>
|
|
public DocumentInfo SetActiveDocument(Document originDocument)
|
|
{
|
|
Logger.Debug("SetActiveDocument(113): " + originDocument.Name);
|
|
if (originDocument == null) return null;
|
|
var document = InitDocument(originDocument);
|
|
if (document == null)
|
|
{
|
|
Logger.Error("Document not exists SetActiveDocument");
|
|
return null;
|
|
}
|
|
if (ActiveDocument == document) return document;
|
|
|
|
if (Globals.ThisAddIn.IsWPS)
|
|
{
|
|
// WPS 只有一个窗口 所以需要先关闭之前文档的面板
|
|
ActiveDocument?.Deactive();
|
|
}
|
|
// 如果存在,则设置激活
|
|
ActiveDocument = document;
|
|
//document.IsActive = true;
|
|
document.Active();
|
|
return document;
|
|
}
|
|
|
|
internal void HideAllPane()
|
|
{
|
|
documentList.ForEach(d => d.HidePane());
|
|
}
|
|
|
|
public DocumentInfo InitDocument(Document originDocument)
|
|
{
|
|
var document = Get(originDocument);
|
|
|
|
// 如果不存在,则添加
|
|
if (document == null)
|
|
{
|
|
Logger.Debug("Document not exists,InitDocument: " + originDocument.Name);
|
|
document = new DocumentInfo(originDocument);
|
|
Add(document);
|
|
}
|
|
return document;
|
|
}
|
|
}
|
|
}
|