Work on project, document managers, controllers and UI

This commit is contained in:
2014-09-08 21:31:47 +03:00
parent e338ae31ca
commit fd166fe814
19 changed files with 698 additions and 148 deletions

View File

@ -9,6 +9,8 @@ using RainmeterStudio.UI.Dialogs;
using RainmeterStudio.UI.ViewModel;
using RainmeterStudio.Core.Model;
using System.IO;
using Microsoft.Win32;
using RainmeterStudio.Core.Utils;
namespace RainmeterStudio.UI.Controller
{
@ -34,10 +36,20 @@ namespace RainmeterStudio.UI.Controller
public Command DocumentOpenCommand { get; private set; }
public Command DocumentSaveCommand { get; private set; }
public Command DocumentSaveAsCommand { get; private set; }
public Command DocumentSaveACopyCommand { get; private set; }
public Command DocumentSaveAllCommand { get; private set; }
public Command DocumentCloseCommand { get; private set; }
#endregion
#region Events
/// <summary>
/// Triggered when a document is opened
/// </summary>
@ -56,15 +68,77 @@ namespace RainmeterStudio.UI.Controller
remove { DocumentManager.DocumentClosed -= value; }
}
/// <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());
}
}
public MainWindow OwnerWindow { get; set; }
#endregion
/// <summary>
/// Initializes a document controller
/// </summary>
/// <param name="documentManager"></param>
/// <param name="projectManager"></param>
public DocumentController(DocumentManager documentManager, ProjectManager projectManager)
{
DocumentManager = documentManager;
ProjectManager = projectManager;
DocumentCreateCommand = new Command("DocumentCreateCommand", () => Create(), () => ProjectManager.ActiveProject != null);
ProjectManager.ActiveProjectChanged += new EventHandler((obj, e) => DocumentCreateCommand.NotifyCanExecuteChanged());
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);
ProjectManager.ActiveProjectChanged += new EventHandler((obj, e) =>
{
DocumentCreateCommand.NotifyCanExecuteChanged();
DocumentSaveAllCommand.NotifyCanExecuteChanged();
});
ActiveDocumentEditorChanged += new EventHandler((obj, e) =>
{
DocumentSaveCommand.NotifyCanExecuteChanged();
DocumentSaveAsCommand.NotifyCanExecuteChanged();
DocumentSaveACopyCommand.NotifyCanExecuteChanged();
DocumentCloseCommand.NotifyCanExecuteChanged();
});
}
private bool HasActiveDocumentEditor()
{
return ActiveDocumentEditor != null;
}
#region Document operations
@ -83,13 +157,13 @@ namespace RainmeterStudio.UI.Controller
return;
var format = dialog.SelectedTemplate;
// Call manager
var editor = DocumentManager.Create(format.Template);
// Set the reference
var name = dialog.SelectedName;
string folder = OwnerWindow.ProjectPanel.ActiveItem.StoragePath;
if (!Directory.Exists(folder))
folder = Path.GetDirectoryName(folder);
@ -105,23 +179,105 @@ namespace RainmeterStudio.UI.Controller
}
/// <summary>
/// Saves the document opened in specified editor
/// Shows an 'open document' dialog, and opens a document
/// </summary>
/// <param name="editor">Editor</param>
public void Save(IDocumentEditor editor)
public void Open()
{
if (!editor.AttachedDocument.Reference.IsOnStorage())
{
SaveAs(editor);
return;
}
// 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;
// TODO
bool? res = dialog.ShowDialog();
if (res.HasValue && res.Value)
{
// Open file
DocumentManager.Open(dialog.FileName);
}
}
public void SaveAs(IDocumentEditor editor)
/// <summary>
/// Saves the active document
/// </summary>
public bool Save()
{
// TODO
return Save(ActiveDocumentEditor);
}
/// <summary>
/// Saves the active document
/// </summary>
public bool Save(IDocumentEditor editor)
{
if (editor.AttachedDocument.Reference.IsOnStorage())
{
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;
}
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);
}
}
/// <summary>
/// Saves all opened documents
/// </summary>
public void SaveAll()
{
foreach (var editor in DocumentManager.Editors)
{
if (!Save(editor))
return;
}
}
/// <summary>
@ -135,18 +291,14 @@ namespace RainmeterStudio.UI.Controller
// Show the 'are you sure' prompt if necesary
if (editor.AttachedDocument.IsDirty)
{
bool? res = CloseUnsavedDialog.ShowDialog(OwnerWindow, editor.AttachedDocument);
if (res.HasValue)
switch(CloseUnsavedDialog.ShowDialog(OwnerWindow, editor.AttachedDocument))
{
// Save
if (res.Value)
{
Save(editor);
}
}
else
{
return false;
case CloseUnsavedDialogResult.Save:
Save();
break;
case CloseUnsavedDialogResult.Cancel:
return false;
}
}
@ -155,6 +307,56 @@ namespace RainmeterStudio.UI.Controller
return true;
}
/// <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()
{
// Show the 'are you sure' prompt if necesary
if (Close(ActiveDocumentEditor))
{
ActiveDocumentEditor = null;
return true;
}
return false;
}
/// <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;
}
#endregion
/// <summary>

View File

@ -82,10 +82,21 @@ namespace RainmeterStudio.UI.Controller
#region Commands
/// <summary>
/// Create project command
/// </summary>
public Command ProjectCreateCommand { get; private set; }
/// <summary>
/// Open project command
/// </summary>
public Command ProjectOpenCommand { get; private set; }
/// <summary>
/// Close project command
/// </summary>
public Command ProjectCloseCommand { get; private set; }
#endregion
/// <summary>
@ -96,8 +107,11 @@ namespace RainmeterStudio.UI.Controller
{
Manager = manager;
ProjectCreateCommand = new Command("ProjectCreateCommand", () => CreateProject());
ProjectOpenCommand = new Command("ProjectOpenCommand", () => OpenProject());
// Initialize commands
ProjectCreateCommand = new Command("ProjectCreate", CreateProject);
ProjectOpenCommand = new Command("ProjectOpen", OpenProject);
ProjectCloseCommand = new Command("ProjectClose", CloseProject, () => ActiveProject != null);
ActiveProjectChanged += new EventHandler((sender, e) => ProjectCloseCommand.NotifyCanExecuteChanged());
}
/// <summary>
@ -126,7 +140,7 @@ namespace RainmeterStudio.UI.Controller
/// Displays an 'open file' dialog and opens an existing project
/// </summary>
/// <param name="path"></param>
public void OpenProject(string path = null)
public void OpenProject()
{
// Open dialog
OpenFileDialog dialog = new OpenFileDialog();
@ -151,6 +165,7 @@ namespace RainmeterStudio.UI.Controller
/// </summary>
public void CloseProject()
{
Manager.Close();
}
}
}