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

259 lines
7.5 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;
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;
using RainmeterStudio.UI.Controller;
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>
public IDocumentTemplate SelectedTemplate
2014-07-26 10:49:11 +00:00
{
get
{
return listTemplates.SelectedItem as IDocumentTemplate;
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>
/// Gets or sets the path
/// </summary>
public string SelectedPath
{
get
{
return textPath.Text;
}
set
{
textPath.Text = value;
_pathUserSet = true;
}
}
#endregion
#region Private fields
private bool _pathUserSet = false;
private bool _ignoreNextChange = false;
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
string location = Settings.Default.CreateProjectDialog_SavedLocation;
// 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;
2014-08-16 08:05:26 +00:00
res &= (listTemplates.SelectedItem != null);
2014-07-26 10:49:11 +00:00
res &= !String.IsNullOrWhiteSpace(textPath.Text);
2014-08-16 08:05:26 +00:00
res &= PathHelper.IsPathValid(textPath.Text);
buttonCreate.IsEnabled = res;
2014-07-26 10:49:11 +00:00
}
private void UpdatePath()
{
if (!_pathUserSet)
{
// Start with location
string path = textLocation.Text;
try
{
// Combine with project directory
if (checkCreateDirectory.IsChecked.HasValue && checkCreateDirectory.IsChecked.Value)
path = System.IO.Path.Combine(path, textName.Text);
// Combine with project file name
path = System.IO.Path.Combine(path, textName.Text + ".rsproj");
// Set new value
_ignoreNextChange = true;
textPath.Text = path;
}
catch (ArgumentException)
{
}
}
}
private void textName_TextChanged(object sender, TextChangedEventArgs e)
{
UpdatePath();
}
private void textLocation_TextChanged(object sender, TextChangedEventArgs e)
{
UpdatePath();
}
private void textPath_TextChanged(object sender, TextChangedEventArgs e)
{
if (_ignoreNextChange)
{
_ignoreNextChange = false;
}
else
{
_pathUserSet = true;
2014-08-16 08:05:26 +00:00
try
{
textLocation.Text = System.IO.Path.GetDirectoryName(textPath.Text);
}
catch { }
2014-07-26 10:49:11 +00:00
}
2014-08-16 08:05:26 +00:00
Validate();
2014-07-26 10:49:11 +00:00
}
private void checkCreateDirectory_CheckChanged(object sender, RoutedEventArgs e)
{
UpdatePath();
}
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)
{
Settings.Default.CreateProjectDialog_SavedLocation = SelectedLocation;
}
}
}
2014-07-26 10:49:11 +00:00
}
}