Work on documents, separated UI from Main

This commit is contained in:
2014-07-29 23:35:59 +03:00
parent 09224d9af7
commit 473f23378f
21 changed files with 316 additions and 226 deletions

View File

@ -2,13 +2,18 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using RainmeterStudio.Documents;
using RainmeterStudio.Model;
using RainmeterStudio.Model.Events;
using RainmeterStudio.Utils;
namespace RainmeterStudio.Business
{
/// <summary>
/// Document manager
/// </summary>
public class DocumentManager
{
#region Events
@ -62,6 +67,61 @@ namespace RainmeterStudio.Business
{
}
/// <summary>
/// Registers all classes with the auto register flag
/// </summary>
/// <remarks>We love linq</remarks>
public void PerformAutoRegister()
{
// Get all assemblies
AppDomain.CurrentDomain.GetAssemblies()
// Get all types
.SelectMany(assembly => assembly.GetTypes())
// Select only the classes
.Where(type => type.IsClass)
// That have the AutoRegister attribute
.Where(type => type.GetCustomAttributes(typeof(AutoRegisterAttribute), false).Length > 0)
// That implement any of the types that can be registered
.Where((type) =>
{
bool res = false;
res |= typeof(IDocumentEditorFactory).IsAssignableFrom(type);
res |= typeof(IDocumentStorage).IsAssignableFrom(type);
res |= typeof(DocumentTemplate).IsAssignableFrom(type);
return res;
})
// Obtain their default constructor
.Select(type => type.GetConstructor(new Type[0]))
// Invoke the default constructor
.Select(constructor => constructor.Invoke(new object[0]))
// Register
.ForEach(obj =>
{
// Try to register factory
var factory = obj as IDocumentEditorFactory;
if (factory != null)
RegisterEditorFactory(factory);
// Try to register as storage
var storage = obj as IDocumentStorage;
if (storage != null)
RegisterStorage(storage);
// Try to register as document template
var doctemplate = obj as DocumentTemplate;
if (doctemplate != null)
RegisterTemplate(doctemplate);
});
}
/// <summary>
/// Registers a document editor factory
/// </summary>
@ -117,6 +177,7 @@ namespace RainmeterStudio.Business
/// <summary>
/// Opens the specified document
/// </summary>
/// <param name="path">The path to the file to open</param>
public IDocumentEditor Open(string path)
{
// Try to open
@ -132,6 +193,10 @@ namespace RainmeterStudio.Business
return editor;
}
/// <summary>
/// Saves a document
/// </summary>
/// <param name="document">The document</param>
public void Save(IDocument document)
{
// Find a storage
@ -147,6 +212,11 @@ namespace RainmeterStudio.Business
document.IsDirty = false;
}
/// <summary>
/// Saves the document as
/// </summary>
/// <param name="path">Path</param>
/// <param name="document">Document</param>
public void SaveAs(string path, IDocument document)
{
// Find a storage
@ -163,6 +233,11 @@ namespace RainmeterStudio.Business
document.IsDirty = false;
}
/// <summary>
/// Saves a copy of the document
/// </summary>
/// <param name="path">Path</param>
/// <param name="document">Document</param>
public void SaveACopy(string path, IDocument document)
{
// Find a storage
@ -172,6 +247,20 @@ namespace RainmeterStudio.Business
storage.Write(path, document);
}
/// <summary>
/// Closes a document editor
/// </summary>
/// <param name="editor"></param>
public void Close(IDocumentEditor editor)
{
// Remove from list of opened editors
_editors.Remove(editor);
// Close event
if (DocumentClosed != null)
DocumentClosed(this, new DocumentClosedEventArgs(editor));
}
#endregion
#region Private functions