using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Linq;
using Microsoft.Win32;
using RainmeterStudio.Business;
using RainmeterStudio.Core.Model;
using RainmeterStudio.UI.Dialogs;
using RainmeterStudio.UI.ViewModel;
using RainmeterStudio.Properties;
namespace RainmeterStudio.UI.Controller
{
public class ProjectController
{
#region Properties
///
/// Gets the project manager
///
protected ProjectManager Manager { get; private set; }
///
/// Gets or sets the owner window. Used for creating dialogs.
///
public Window OwnerWindow { get; set; }
///
/// Gets the active project
///
public Project ActiveProject
{
get
{
return Manager.ActiveProject;
}
}
///
/// Gets the active project path
///
public string ActiveProjectPath
{
get
{
return Manager.ActiveProject.Path;
}
}
///
/// Gets the project templates
///
public IEnumerable ProjectTemplates
{
get
{
return Manager.ProjectTemplates.Select(pt => new ProjectTemplateViewModel(pt));
}
}
#endregion
#region Callbacks
///
/// Called when a project is opened or the active project closes.
///
public event EventHandler ActiveProjectChanged
{
add
{
Manager.ActiveProjectChanged += value;
}
remove
{
Manager.ActiveProjectChanged -= value;
}
}
#endregion
#region Commands
///
/// Create project command
///
public Command ProjectCreateCommand { get; private set; }
///
/// Open project command
///
public Command ProjectOpenCommand { get; private set; }
///
/// Close project command
///
public Command ProjectCloseCommand { get; private set; }
#endregion
///
/// Initializes the project controller
///
/// Project manager
public ProjectController(ProjectManager manager)
{
Manager = manager;
// 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());
}
///
/// Displays the 'create project' dialog and creates a new project
///
public void CreateProject()
{
// Create dialog
var dialog = new CreateProjectDialog(this);
dialog.Owner = OwnerWindow;
// Display
bool? res = dialog.ShowDialog();
if (!res.HasValue || !res.Value)
return;
string selectedName = dialog.SelectedName;
string selectedPath = dialog.SelectedPath;
IProjectTemplate selectedTemplate = dialog.SelectedTemplate;
// Call manager
Manager.CreateProject(selectedName, selectedPath, selectedTemplate);
}
///
/// Displays an 'open file' dialog and opens an existing project
///
///
public void OpenProject()
{
// Open dialog
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = Resources.Strings.Dialog_FileType_Project + "|*.rsproj|"
+ Resources.Strings.Dialog_FileType_AllFiles + "|*.*";
dialog.Title = Resources.Strings.Dialog_OpenProject_Title;
dialog.Multiselect = false;
dialog.InitialDirectory = Settings.Default.Project_SavedLocation;
// Show dialog
bool? res = dialog.ShowDialog(OwnerWindow);
if (!res.HasValue || !res.Value)
return;
// Call manager
string filename = dialog.FileName;
Manager.OpenProject(filename);
}
///
/// Closes the active project
///
public void CloseProject()
{
Manager.Close();
}
}
}