rainmeter-studio/RainmeterStudio/UI/Dialogs/CreateProjectDialog.xaml.cs

230 lines
7.0 KiB
C#
Raw Normal View History

2014-07-26 10:49:11 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
2014-08-16 10:55:07 +00:00
using Microsoft.Win32;
2014-07-26 10:49:11 +00:00
using RainmeterStudio.Business;
using RainmeterStudio.Core.Documents;
using RainmeterStudio.Core.Model;
2014-08-16 08:05:26 +00:00
using RainmeterStudio.Core.Utils;
using RainmeterStudio.Properties;
2014-08-16 10:55:07 +00:00
using RainmeterStudio.Resources;
using RainmeterStudio.UI.Controller;
2014-08-16 12:40:08 +00:00
using RainmeterStudio.UI.ViewModel;
2014-07-26 10:49:11 +00:00
namespace RainmeterStudio.UI.Dialogs
{
/// <summary>
/// Interaction logic for CreateProjectDialog.xaml
/// </summary>
public partial class CreateProjectDialog : Window
{
#region Properties
/// <summary>
/// Gets or sets the currently selected file format
/// </summary>
2014-08-16 11:35:54 +00:00
public IProjectTemplate SelectedTemplate
2014-07-26 10:49:11 +00:00
{
get
{
2014-08-16 12:40:08 +00:00
var item = listTemplates.SelectedItem as ProjectTemplateViewModel;
if (item != null)
return item.Template;
return null;
2014-07-26 10:49:11 +00:00
}
set
{
listTemplates.SelectedItem = value;
}
}
/// <summary>
/// Gets or sets the path
/// </summary>
public string SelectedName
{
get
{
return textName.Text;
}
set
{
textName.Text = value;
}
}
/// <summary>
/// Gets or sets the path
/// </summary>
public string SelectedLocation
{
get
{
return textLocation.Text;
}
set
{
textLocation.Text = value;
}
}
/// <summary>
2014-08-16 10:55:07 +00:00
/// Gets the selected path
2014-07-26 10:49:11 +00:00
/// </summary>
public string SelectedPath
{
get
{
2014-08-16 10:55:07 +00:00
string path = SelectedLocation;
if (checkCreateDirectory.IsChecked.HasValue && checkCreateDirectory.IsChecked.Value)
path = System.IO.Path.Combine(path, SelectedName);
return System.IO.Path.Combine(path, SelectedName + ".rsproj");
2014-07-26 10:49:11 +00:00
}
}
#endregion
#region Private fields
2014-08-16 08:05:26 +00:00
private ProjectController _projectController;
2014-07-26 10:49:11 +00:00
#endregion
2014-08-16 08:05:26 +00:00
/// <summary>
/// Initializes the create project dialog
/// </summary>
/// <param name="projectController">Project controller</param>
public CreateProjectDialog(ProjectController projectController)
2014-07-26 10:49:11 +00:00
{
InitializeComponent();
2014-08-16 08:05:26 +00:00
_projectController = projectController;
// Add event handlers
2014-07-26 10:49:11 +00:00
textLocation.AddHandler(TextBoxBase.TextChangedEvent, new TextChangedEventHandler(textLocation_TextChanged));
2014-08-16 08:05:26 +00:00
// Populate controls
listTemplates.ItemsSource = projectController.ProjectTemplates.OrderBy(x => x.DisplayText);
2014-08-16 08:05:26 +00:00
textLocation.ItemsSource = GetRecentLocations().OrderBy(x => x);
textLocation.Text = GetLocation();
Validate();
// Focus on name textbox
textName.Focus();
2014-07-26 10:49:11 +00:00
}
2014-08-16 08:05:26 +00:00
private string GetLocation()
2014-07-26 10:49:11 +00:00
{
2014-08-16 08:05:26 +00:00
// Get setting
2014-08-16 11:35:54 +00:00
string location = Settings.Default.Project_SavedLocation;
2014-08-16 08:05:26 +00:00
// No location provided, use default
if (String.IsNullOrEmpty(location))
return System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Rainmeter Studio Projects");
return location;
2014-07-26 10:49:11 +00:00
}
2014-08-16 08:05:26 +00:00
private IEnumerable<string> GetRecentLocations()
2014-07-26 10:49:11 +00:00
{
2014-08-16 08:05:26 +00:00
return Settings.Default.CreateProjectDialog_RecentLocations
.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
2014-07-26 10:49:11 +00:00
}
2014-08-16 08:05:26 +00:00
private void Validate()
2014-07-26 10:49:11 +00:00
{
bool res = true;
res &= (listTemplates.SelectedItem != null);
res &= PathHelper.IsFileNameValid(textName.Text);
res &= PathHelper.IsPathValid(textLocation.Text);
2014-08-16 08:05:26 +00:00
buttonCreate.IsEnabled = res;
2014-07-26 10:49:11 +00:00
}
private void textName_TextChanged(object sender, TextChangedEventArgs e)
{
2014-08-16 10:55:07 +00:00
Validate();
2014-07-26 10:49:11 +00:00
}
private void textLocation_TextChanged(object sender, TextChangedEventArgs e)
{
2014-08-16 08:05:26 +00:00
Validate();
2014-07-26 10:49:11 +00:00
}
private void listTemplates_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
2014-08-16 08:05:26 +00:00
Validate();
}
private void buttonCreate_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
Close();
}
private void buttonCancel_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
Close();
}
private void Window_Closed(object sender, EventArgs e)
{
// Save settings
if (DialogResult.HasValue && DialogResult.Value)
{
// Save recent locations
IEnumerable<string> recentLocations = GetRecentLocations();
if (!recentLocations.Contains(SelectedLocation))
{
if (recentLocations.Count() > 5)
recentLocations = recentLocations.Skip(1);
recentLocations = recentLocations.Append(SelectedLocation);
}
Settings.Default.CreateProjectDialog_RecentLocations = recentLocations.Aggregate((first, second) => first + "|" + second);
// Save location
if (checkLocationDefault.IsChecked.HasValue && checkLocationDefault.IsChecked.Value)
{
2014-08-16 11:35:54 +00:00
Settings.Default.Project_SavedLocation = SelectedLocation;
2014-08-16 08:05:26 +00:00
}
}
}
2014-08-16 10:55:07 +00:00
private void buttonBrowse_Click(object sender, RoutedEventArgs e)
{
// Show dialog
SaveFileDialog dialog = new SaveFileDialog();
dialog.Title = Strings.CreateProjectDialog_Browse_Title;
dialog.AddExtension = true;
dialog.Filter = Strings.Dialog_FileType_Project + "|*.rsproj|" + Strings.Dialog_FileType_AllFiles + "|*.*";
dialog.InitialDirectory = SelectedLocation;
dialog.FileName = SelectedName;
bool? res = dialog.ShowDialog();
if (res.HasValue && res.Value)
{
SelectedName = System.IO.Path.GetFileNameWithoutExtension(dialog.FileName);
SelectedLocation = System.IO.Path.GetDirectoryName(dialog.FileName);
}
}
2014-07-26 10:49:11 +00:00
}
}