2014-07-26 07:12:56 +00:00
|
|
|
|
using System;
|
2014-08-14 07:06:20 +00:00
|
|
|
|
using System.Collections.Generic;
|
2014-08-12 13:33:13 +00:00
|
|
|
|
using System.Windows;
|
2014-08-14 07:06:20 +00:00
|
|
|
|
using System.Linq;
|
2014-07-26 07:12:56 +00:00
|
|
|
|
using RainmeterStudio.Business;
|
2014-08-12 13:33:13 +00:00
|
|
|
|
using RainmeterStudio.Core.Documents;
|
|
|
|
|
using RainmeterStudio.Core.Model.Events;
|
2014-07-26 07:12:56 +00:00
|
|
|
|
using RainmeterStudio.UI.Dialogs;
|
2014-08-14 07:06:20 +00:00
|
|
|
|
using RainmeterStudio.UI.ViewModel;
|
2014-08-15 21:39:31 +00:00
|
|
|
|
using RainmeterStudio.Core.Model;
|
2014-08-16 12:40:08 +00:00
|
|
|
|
using System.IO;
|
2014-07-26 07:12:56 +00:00
|
|
|
|
|
|
|
|
|
namespace RainmeterStudio.UI.Controller
|
|
|
|
|
{
|
|
|
|
|
public class DocumentController
|
|
|
|
|
{
|
2014-07-29 16:42:52 +00:00
|
|
|
|
#region Managers
|
2014-07-26 07:12:56 +00:00
|
|
|
|
|
2014-07-29 16:42:52 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the document manager
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected DocumentManager DocumentManager { get; private set; }
|
2014-07-26 10:49:11 +00:00
|
|
|
|
|
2014-07-29 16:42:52 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the project manager
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected ProjectManager ProjectManager { get; private set; }
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Commands
|
|
|
|
|
|
|
|
|
|
public Command DocumentCreateCommand { get; private set; }
|
2014-07-26 07:12:56 +00:00
|
|
|
|
|
2014-08-16 11:35:54 +00:00
|
|
|
|
public Command DocumentOpenCommand { get; private set; }
|
|
|
|
|
|
2014-08-30 07:24:01 +00:00
|
|
|
|
|
|
|
|
|
|
2014-07-26 07:12:56 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2014-07-29 20:35:59 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Triggered when a document is opened
|
|
|
|
|
/// </summary>
|
2014-07-26 07:12:56 +00:00
|
|
|
|
public event EventHandler<DocumentOpenedEventArgs> DocumentOpened
|
|
|
|
|
{
|
2014-07-29 20:35:59 +00:00
|
|
|
|
add { DocumentManager.DocumentOpened += value; }
|
|
|
|
|
remove { DocumentManager.DocumentOpened -= value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Triggered when a document is closed
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event EventHandler<DocumentClosedEventArgs> DocumentClosed
|
|
|
|
|
{
|
|
|
|
|
add { DocumentManager.DocumentClosed += value; }
|
|
|
|
|
remove { DocumentManager.DocumentClosed -= value; }
|
2014-07-26 07:12:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-16 12:40:08 +00:00
|
|
|
|
public MainWindow OwnerWindow { get; set; }
|
2014-07-26 07:12:56 +00:00
|
|
|
|
|
2014-07-29 16:42:52 +00:00
|
|
|
|
public DocumentController(DocumentManager documentManager, ProjectManager projectManager)
|
2014-07-26 07:12:56 +00:00
|
|
|
|
{
|
2014-07-29 16:42:52 +00:00
|
|
|
|
DocumentManager = documentManager;
|
|
|
|
|
ProjectManager = projectManager;
|
|
|
|
|
|
2014-08-16 11:35:54 +00:00
|
|
|
|
DocumentCreateCommand = new Command("DocumentCreateCommand", () => Create(), () => ProjectManager.ActiveProject != null);
|
|
|
|
|
ProjectManager.ActiveProjectChanged += new EventHandler((obj, e) => DocumentCreateCommand.NotifyCanExecuteChanged());
|
2014-07-26 07:12:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-30 07:24:01 +00:00
|
|
|
|
#region Document operations
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Shows the new item dialog, and creates a new document
|
|
|
|
|
/// </summary>
|
2014-08-16 11:35:54 +00:00
|
|
|
|
public void Create()
|
2014-07-26 07:12:56 +00:00
|
|
|
|
{
|
|
|
|
|
// Show dialog
|
2014-08-16 11:35:54 +00:00
|
|
|
|
var dialog = new CreateDocumentDialog(this);
|
|
|
|
|
dialog.Owner = OwnerWindow;
|
2014-07-26 07:12:56 +00:00
|
|
|
|
bool? res = dialog.ShowDialog();
|
|
|
|
|
|
|
|
|
|
if (!res.HasValue || !res.Value)
|
|
|
|
|
return;
|
|
|
|
|
|
2014-07-29 20:35:59 +00:00
|
|
|
|
var format = dialog.SelectedTemplate;
|
2014-08-16 12:40:08 +00:00
|
|
|
|
|
2014-07-26 07:12:56 +00:00
|
|
|
|
// Call manager
|
2014-08-16 12:40:08 +00:00
|
|
|
|
var editor = DocumentManager.Create(format.Template);
|
|
|
|
|
|
|
|
|
|
// Set the reference
|
|
|
|
|
var name = dialog.SelectedName;
|
|
|
|
|
|
2014-08-31 11:41:24 +00:00
|
|
|
|
string folder = OwnerWindow.ProjectPanel.ActiveItem.StoragePath;
|
2014-08-16 12:40:08 +00:00
|
|
|
|
if (!Directory.Exists(folder))
|
|
|
|
|
folder = Path.GetDirectoryName(folder);
|
|
|
|
|
|
|
|
|
|
var reference = new Reference(name, Path.Combine(folder, name));
|
|
|
|
|
editor.AttachedDocument.Reference = reference;
|
|
|
|
|
|
|
|
|
|
// Save document
|
|
|
|
|
DocumentManager.Save(editor.AttachedDocument);
|
|
|
|
|
|
|
|
|
|
// Add to parent
|
|
|
|
|
OwnerWindow.ProjectPanel.ActiveItem.Add(reference);
|
2014-07-26 07:12:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-30 07:24:01 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Saves the document opened in specified editor
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="editor">Editor</param>
|
|
|
|
|
public void Save(IDocumentEditor editor)
|
2014-07-26 07:12:56 +00:00
|
|
|
|
{
|
2014-08-30 07:24:01 +00:00
|
|
|
|
if (!editor.AttachedDocument.Reference.IsOnStorage())
|
|
|
|
|
{
|
|
|
|
|
SaveAs(editor);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO
|
2014-07-26 07:12:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-30 07:24:01 +00:00
|
|
|
|
public void SaveAs(IDocumentEditor editor)
|
|
|
|
|
{
|
|
|
|
|
// TODO
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Closes an active document.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="editor">The document editor attached</param>
|
|
|
|
|
/// <returns>True if closed successfully</returns>
|
|
|
|
|
/// <remarks>Shows the 'are you sure' prompt if there are unsaved edits.</remarks>
|
|
|
|
|
public bool Close(IDocumentEditor editor)
|
|
|
|
|
{
|
|
|
|
|
// Show the 'are you sure' prompt if necesary
|
|
|
|
|
if (editor.AttachedDocument.IsDirty)
|
|
|
|
|
{
|
|
|
|
|
bool? res = CloseUnsavedDialog.ShowDialog(OwnerWindow, editor.AttachedDocument);
|
|
|
|
|
if (res.HasValue)
|
|
|
|
|
{
|
|
|
|
|
// Save
|
|
|
|
|
if (res.Value)
|
|
|
|
|
{
|
|
|
|
|
Save(editor);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close
|
|
|
|
|
DocumentManager.Close(editor);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2014-08-14 07:06:20 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a list of document templates view models
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IEnumerable<DocumentTemplateViewModel> DocumentTemplates
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return DocumentManager.DocumentTemplates.Select(t => new DocumentTemplateViewModel(t));
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-26 07:12:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|