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-09-12 10:26:52 +00:00
|
|
|
|
using RainmeterStudio.Core.Editor;
|
2014-08-12 13:33:13 +00:00
|
|
|
|
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-09-08 18:31:47 +00:00
|
|
|
|
using Microsoft.Win32;
|
|
|
|
|
using RainmeterStudio.Core.Utils;
|
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-09-08 18:31:47 +00:00
|
|
|
|
public Command DocumentSaveCommand { get; private set; }
|
2014-08-30 07:24:01 +00:00
|
|
|
|
|
2014-09-08 18:31:47 +00:00
|
|
|
|
public Command DocumentSaveAsCommand { get; private set; }
|
|
|
|
|
|
|
|
|
|
public Command DocumentSaveACopyCommand { get; private set; }
|
|
|
|
|
|
|
|
|
|
public Command DocumentSaveAllCommand { get; private set; }
|
|
|
|
|
|
|
|
|
|
public Command DocumentCloseCommand { get; private set; }
|
2014-08-30 07:24:01 +00:00
|
|
|
|
|
2014-07-26 07:12:56 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2014-09-08 18:31:47 +00:00
|
|
|
|
#region Events
|
|
|
|
|
|
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-09-08 18:31:47 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Triggered when the active document editor changes.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event EventHandler ActiveDocumentEditorChanged;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Properties
|
|
|
|
|
|
|
|
|
|
private IDocumentEditor _activeDocumentEditor = null;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the active document editor.
|
|
|
|
|
/// This must be set by the main window when active document changes.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IDocumentEditor ActiveDocumentEditor
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _activeDocumentEditor;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_activeDocumentEditor = value;
|
|
|
|
|
|
|
|
|
|
if (ActiveDocumentEditorChanged != null)
|
|
|
|
|
ActiveDocumentEditorChanged(this, new EventArgs());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-16 12:40:08 +00:00
|
|
|
|
public MainWindow OwnerWindow { get; set; }
|
2014-07-26 07:12:56 +00:00
|
|
|
|
|
2014-09-08 18:31:47 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a document controller
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="documentManager"></param>
|
|
|
|
|
/// <param name="projectManager"></param>
|
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-09-08 18:31:47 +00:00
|
|
|
|
DocumentCreateCommand = new Command("DocumentCreate", Create, () => ProjectManager.ActiveProject != null);
|
|
|
|
|
DocumentOpenCommand = new Command("DocumentOpen", Open);
|
|
|
|
|
DocumentSaveCommand = new Command("DocumentSave", () => Save(), HasActiveDocumentEditor);
|
|
|
|
|
DocumentSaveAsCommand = new Command("DocumentSaveAs", () => SaveAs(), HasActiveDocumentEditor);
|
|
|
|
|
DocumentSaveACopyCommand = new Command("DocumentSaveACopy", () => SaveACopy(), HasActiveDocumentEditor);
|
|
|
|
|
DocumentSaveAllCommand = new Command("DocumentSaveAll", SaveAll, () => ProjectManager.ActiveProject != null);
|
|
|
|
|
DocumentCloseCommand = new Command("DocumentClose", () => Close(), HasActiveDocumentEditor);
|
|
|
|
|
|
2014-09-13 08:17:24 +00:00
|
|
|
|
ProjectManager.ActiveProjectChanged += new EventHandler((sender, e) =>
|
2014-09-08 18:31:47 +00:00
|
|
|
|
{
|
|
|
|
|
DocumentCreateCommand.NotifyCanExecuteChanged();
|
|
|
|
|
DocumentSaveAllCommand.NotifyCanExecuteChanged();
|
|
|
|
|
});
|
|
|
|
|
|
2014-09-13 08:17:24 +00:00
|
|
|
|
ActiveDocumentEditorChanged += new EventHandler((sender, e) =>
|
2014-09-08 18:31:47 +00:00
|
|
|
|
{
|
|
|
|
|
DocumentSaveCommand.NotifyCanExecuteChanged();
|
|
|
|
|
DocumentSaveAsCommand.NotifyCanExecuteChanged();
|
|
|
|
|
DocumentSaveACopyCommand.NotifyCanExecuteChanged();
|
|
|
|
|
DocumentCloseCommand.NotifyCanExecuteChanged();
|
|
|
|
|
});
|
|
|
|
|
}
|
2014-09-13 08:17:24 +00:00
|
|
|
|
|
2014-09-08 18:31:47 +00:00
|
|
|
|
private bool HasActiveDocumentEditor()
|
|
|
|
|
{
|
|
|
|
|
return ActiveDocumentEditor != null;
|
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-09-08 18:31:47 +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);
|
2014-09-08 18:31:47 +00:00
|
|
|
|
|
2014-08-16 12:40:08 +00:00
|
|
|
|
// Set the reference
|
|
|
|
|
var name = dialog.SelectedName;
|
2014-09-08 18:31:47 +00:00
|
|
|
|
|
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);
|
|
|
|
|
|
2014-09-12 16:20:21 +00:00
|
|
|
|
var reference = new Reference(name, Path.Combine(folder, name), Reference.ReferenceTargetKind.File);
|
2014-08-16 12:40:08 +00:00
|
|
|
|
editor.AttachedDocument.Reference = reference;
|
|
|
|
|
|
|
|
|
|
// Save document
|
|
|
|
|
DocumentManager.Save(editor.AttachedDocument);
|
|
|
|
|
|
|
|
|
|
// Add to parent
|
|
|
|
|
OwnerWindow.ProjectPanel.ActiveItem.Add(reference);
|
2014-09-12 16:20:21 +00:00
|
|
|
|
ProjectManager.SaveActiveProject();
|
2014-07-26 07:12:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-30 07:24:01 +00:00
|
|
|
|
/// <summary>
|
2014-09-08 18:31:47 +00:00
|
|
|
|
/// Shows an 'open document' dialog, and opens a document
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Open()
|
|
|
|
|
{
|
|
|
|
|
// Show open dialog
|
|
|
|
|
OpenFileDialog dialog = new OpenFileDialog();
|
|
|
|
|
dialog.Title = Resources.Strings.Dialog_OpenDocument_Title;
|
|
|
|
|
dialog.Filter = Resources.Strings.Dialog_FileType_AllFiles + "|*.*";
|
|
|
|
|
dialog.InitialDirectory = Properties.Settings.Default.Project_SavedLocation;
|
|
|
|
|
|
|
|
|
|
bool? res = dialog.ShowDialog();
|
|
|
|
|
if (res.HasValue && res.Value)
|
|
|
|
|
{
|
|
|
|
|
// Open file
|
|
|
|
|
DocumentManager.Open(dialog.FileName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Saves the active document
|
2014-08-30 07:24:01 +00:00
|
|
|
|
/// </summary>
|
2014-09-08 18:31:47 +00:00
|
|
|
|
public bool Save()
|
2014-07-26 07:12:56 +00:00
|
|
|
|
{
|
2014-09-08 18:31:47 +00:00
|
|
|
|
return Save(ActiveDocumentEditor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Saves the active document
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool Save(IDocumentEditor editor)
|
|
|
|
|
{
|
2014-09-12 15:22:24 +00:00
|
|
|
|
if (editor.AttachedDocument.Reference != null)
|
2014-08-30 07:24:01 +00:00
|
|
|
|
{
|
2014-09-08 18:31:47 +00:00
|
|
|
|
DocumentManager.Save(editor.AttachedDocument);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return SaveAs(editor);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Displays a 'save as' dialog, and saves active document
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool SaveAs()
|
|
|
|
|
{
|
|
|
|
|
return SaveAs(ActiveDocumentEditor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Displays a 'save as' dialog, and saves active document
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool SaveAs(IDocumentEditor editor)
|
|
|
|
|
{
|
|
|
|
|
// Show save dialog
|
|
|
|
|
SaveFileDialog dialog = new SaveFileDialog();
|
|
|
|
|
dialog.Title = Resources.Strings.Dialog_SaveDocument_Title;
|
|
|
|
|
dialog.Filter = Resources.Strings.Dialog_FileType_AllFiles + "|*.*";
|
|
|
|
|
dialog.FileName = editor.AttachedDocument.Reference.StoragePath;
|
|
|
|
|
|
|
|
|
|
bool? res = dialog.ShowDialog();
|
|
|
|
|
if (res.HasValue && res.Value)
|
|
|
|
|
{
|
|
|
|
|
DocumentManager.SaveAs(dialog.FileName, editor.AttachedDocument);
|
|
|
|
|
return true;
|
2014-08-30 07:24:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-08 18:31:47 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Displays a 'save' dialog, and saves a copy of the active document
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void SaveACopy()
|
|
|
|
|
{
|
|
|
|
|
// Show save dialog
|
|
|
|
|
SaveFileDialog dialog = new SaveFileDialog();
|
|
|
|
|
dialog.Title = Resources.Strings.Dialog_SaveDocument_Title;
|
|
|
|
|
dialog.Filter = Resources.Strings.Dialog_FileType_AllFiles + "|*.*";
|
|
|
|
|
dialog.FileName = ActiveDocumentEditor.AttachedDocument.Reference.StoragePath;
|
|
|
|
|
|
|
|
|
|
bool? res = dialog.ShowDialog();
|
|
|
|
|
if (res.HasValue && res.Value)
|
|
|
|
|
{
|
|
|
|
|
DocumentManager.SaveACopy(dialog.FileName, ActiveDocumentEditor.AttachedDocument);
|
|
|
|
|
}
|
2014-07-26 07:12:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-08 18:31:47 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Saves all opened documents
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void SaveAll()
|
2014-08-30 07:24:01 +00:00
|
|
|
|
{
|
2014-09-08 18:31:47 +00:00
|
|
|
|
foreach (var editor in DocumentManager.Editors)
|
|
|
|
|
{
|
|
|
|
|
if (!Save(editor))
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-08-30 07:24:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
2014-09-08 18:31:47 +00:00
|
|
|
|
switch(CloseUnsavedDialog.ShowDialog(OwnerWindow, editor.AttachedDocument))
|
2014-08-30 07:24:01 +00:00
|
|
|
|
{
|
2014-09-08 18:31:47 +00:00
|
|
|
|
case CloseUnsavedDialogResult.Save:
|
|
|
|
|
Save();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case CloseUnsavedDialogResult.Cancel:
|
|
|
|
|
return false;
|
2014-08-30 07:24:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close
|
|
|
|
|
DocumentManager.Close(editor);
|
2014-09-13 08:17:24 +00:00
|
|
|
|
|
|
|
|
|
// Update ActiveDocument
|
|
|
|
|
if (editor == ActiveDocumentEditor)
|
|
|
|
|
ActiveDocumentEditor = null;
|
|
|
|
|
|
2014-08-30 07:24:01 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-08 18:31:47 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Closes the active document.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>True if closed successfully</returns>
|
|
|
|
|
/// <remarks>Shows the 'are you sure' prompt if there are unsaved edits.</remarks>
|
|
|
|
|
public bool Close()
|
|
|
|
|
{
|
2014-09-13 08:17:24 +00:00
|
|
|
|
return Close(ActiveDocumentEditor);
|
2014-09-08 18:31:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Closes all the opened documents
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>True if closed successfully, false if user hit 'cancel'.</returns>
|
|
|
|
|
public bool CloseAll()
|
|
|
|
|
{
|
|
|
|
|
// Get dirty documents
|
|
|
|
|
var unsaved = DocumentManager.Editors
|
|
|
|
|
.Select(editor => editor.AttachedDocument)
|
|
|
|
|
.Where(document => document.IsDirty);
|
|
|
|
|
|
|
|
|
|
// There are unsaved documents? Display save dialog
|
|
|
|
|
if (unsaved.Any())
|
|
|
|
|
{
|
|
|
|
|
switch (CloseUnsavedDialog.ShowDialog(OwnerWindow, unsaved))
|
|
|
|
|
{
|
|
|
|
|
case CloseUnsavedDialogResult.Save:
|
|
|
|
|
SaveAll();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case CloseUnsavedDialogResult.Cancel:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close all documents
|
|
|
|
|
// To array is used because DocumentManager.Editors is modified when closing a document.
|
|
|
|
|
DocumentManager.Editors.ToArray().ForEach(DocumentManager.Close);
|
|
|
|
|
|
|
|
|
|
// Done
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-30 07:24:01 +00:00
|
|
|
|
#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
|
|
|
|
}
|
|
|
|
|
}
|