mirror of
https://github.com/ckaczor/FeedCenter.git
synced 2026-02-09 09:42:36 -05:00
More UI updates
This commit is contained in:
@@ -9,417 +9,416 @@ using System.Xml;
|
||||
using FeedCenter.Data;
|
||||
using Microsoft.Win32;
|
||||
|
||||
namespace FeedCenter.Options
|
||||
namespace FeedCenter.Options;
|
||||
|
||||
public partial class FeedsOptionsPanel
|
||||
{
|
||||
public partial class FeedsOptionsPanel
|
||||
private CollectionViewSource _collectionViewSource;
|
||||
|
||||
public FeedsOptionsPanel(Window parentWindow) : base(parentWindow)
|
||||
{
|
||||
private CollectionViewSource _collectionViewSource;
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
public FeedsOptionsPanel(Window parentWindow) : base(parentWindow)
|
||||
public override string CategoryName => Properties.Resources.optionCategoryFeeds;
|
||||
|
||||
public override void LoadPanel()
|
||||
{
|
||||
base.LoadPanel();
|
||||
|
||||
var collectionViewSource = new CollectionViewSource { Source = Database.Entities.Categories };
|
||||
collectionViewSource.SortDescriptions.Add(new SortDescription("SortKey", ListSortDirection.Ascending));
|
||||
collectionViewSource.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
|
||||
collectionViewSource.IsLiveSortingRequested = true;
|
||||
|
||||
CategoryListBox.ItemsSource = collectionViewSource.View;
|
||||
CategoryListBox.SelectedIndex = 0;
|
||||
}
|
||||
|
||||
private void SetFeedButtonStates()
|
||||
{
|
||||
AddFeedButton.IsEnabled = true;
|
||||
EditFeedButton.IsEnabled = FeedListBox.SelectedItems.Count == 1;
|
||||
DeleteFeedButton.IsEnabled = FeedListBox.SelectedItems.Count > 0;
|
||||
}
|
||||
|
||||
private void AddFeed()
|
||||
{
|
||||
var feed = Feed.Create();
|
||||
|
||||
var category = (Category) CategoryListBox.SelectedItem;
|
||||
|
||||
feed.CategoryId = category.Id;
|
||||
|
||||
var feedWindow = new FeedWindow();
|
||||
|
||||
var result = feedWindow.Display(feed, Window.GetWindow(this));
|
||||
|
||||
if (!result.HasValue || !result.Value)
|
||||
return;
|
||||
|
||||
Database.Entities.SaveChanges(() => Database.Entities.Feeds.Add(feed));
|
||||
|
||||
FeedListBox.SelectedItem = feed;
|
||||
|
||||
SetFeedButtonStates();
|
||||
}
|
||||
|
||||
private void EditSelectedFeed()
|
||||
{
|
||||
if (FeedListBox.SelectedItem == null)
|
||||
return;
|
||||
|
||||
var feed = (Feed) FeedListBox.SelectedItem;
|
||||
|
||||
var feedWindow = new FeedWindow();
|
||||
|
||||
feedWindow.Display(feed, Window.GetWindow(this));
|
||||
}
|
||||
|
||||
private void DeleteSelectedFeeds()
|
||||
{
|
||||
if (MessageBox.Show(ParentWindow, Properties.Resources.ConfirmDeleteFeeds, Properties.Resources.ConfirmDeleteTitle, MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.No)
|
||||
return;
|
||||
|
||||
var selectedItems = new Feed[FeedListBox.SelectedItems.Count];
|
||||
|
||||
FeedListBox.SelectedItems.CopyTo(selectedItems, 0);
|
||||
|
||||
foreach (var feed in selectedItems)
|
||||
Database.Entities.SaveChanges(() => Database.Entities.Feeds.Remove(feed));
|
||||
|
||||
SetFeedButtonStates();
|
||||
}
|
||||
|
||||
private void HandleAddFeedButtonClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
AddFeed();
|
||||
}
|
||||
|
||||
private void HandleEditFeedButtonClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
EditSelectedFeed();
|
||||
}
|
||||
|
||||
private void HandleDeleteFeedButtonClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
DeleteSelectedFeeds();
|
||||
}
|
||||
|
||||
private void HandleImportButtonClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
ImportFeeds();
|
||||
}
|
||||
|
||||
private void HandleExportButtonClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
ExportFeeds();
|
||||
}
|
||||
|
||||
private static void ExportFeeds()
|
||||
{
|
||||
var saveFileDialog = new SaveFileDialog
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
FileName = Properties.Resources.ApplicationName,
|
||||
Filter = Properties.Resources.ImportExportFilter,
|
||||
FilterIndex = 0,
|
||||
OverwritePrompt = true
|
||||
};
|
||||
|
||||
public override string CategoryName => Properties.Resources.optionCategoryFeeds;
|
||||
var result = saveFileDialog.ShowDialog();
|
||||
|
||||
public override void LoadPanel()
|
||||
if (!result.GetValueOrDefault(false))
|
||||
return;
|
||||
|
||||
var writerSettings = new XmlWriterSettings
|
||||
{
|
||||
base.LoadPanel();
|
||||
Indent = true,
|
||||
CheckCharacters = true,
|
||||
ConformanceLevel = ConformanceLevel.Document
|
||||
};
|
||||
|
||||
var collectionViewSource = new CollectionViewSource { Source = Database.Entities.Categories };
|
||||
collectionViewSource.SortDescriptions.Add(new SortDescription("SortKey", ListSortDirection.Ascending));
|
||||
collectionViewSource.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
|
||||
collectionViewSource.IsLiveSortingRequested = true;
|
||||
var xmlWriter = XmlWriter.Create(saveFileDialog.FileName, writerSettings);
|
||||
|
||||
CategoryListBox.ItemsSource = collectionViewSource.View;
|
||||
CategoryListBox.SelectedIndex = 0;
|
||||
}
|
||||
xmlWriter.WriteStartElement("opml");
|
||||
xmlWriter.WriteStartElement("body");
|
||||
|
||||
private void SetFeedButtonStates()
|
||||
foreach (var feed in Database.Entities.Feeds.OrderBy(feed => feed.Name))
|
||||
{
|
||||
AddFeedButton.IsEnabled = true;
|
||||
EditFeedButton.IsEnabled = FeedListBox.SelectedItems.Count == 1;
|
||||
DeleteFeedButton.IsEnabled = FeedListBox.SelectedItems.Count > 0;
|
||||
}
|
||||
xmlWriter.WriteStartElement("outline");
|
||||
|
||||
private void AddFeed()
|
||||
{
|
||||
var feed = Feed.Create();
|
||||
|
||||
var category = (Category) CategoryListBox.SelectedItem;
|
||||
|
||||
feed.CategoryId = category.Id;
|
||||
|
||||
var feedWindow = new FeedWindow();
|
||||
|
||||
var result = feedWindow.Display(feed, Window.GetWindow(this));
|
||||
|
||||
if (!result.HasValue || !result.Value)
|
||||
return;
|
||||
|
||||
Database.Entities.SaveChanges(() => Database.Entities.Feeds.Add(feed));
|
||||
|
||||
FeedListBox.SelectedItem = feed;
|
||||
|
||||
SetFeedButtonStates();
|
||||
}
|
||||
|
||||
private void EditSelectedFeed()
|
||||
{
|
||||
if (FeedListBox.SelectedItem == null)
|
||||
return;
|
||||
|
||||
var feed = (Feed) FeedListBox.SelectedItem;
|
||||
|
||||
var feedWindow = new FeedWindow();
|
||||
|
||||
feedWindow.Display(feed, Window.GetWindow(this));
|
||||
}
|
||||
|
||||
private void DeleteSelectedFeeds()
|
||||
{
|
||||
if (MessageBox.Show(ParentWindow, Properties.Resources.ConfirmDeleteFeeds, Properties.Resources.ConfirmDeleteTitle, MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.No)
|
||||
return;
|
||||
|
||||
var selectedItems = new Feed[FeedListBox.SelectedItems.Count];
|
||||
|
||||
FeedListBox.SelectedItems.CopyTo(selectedItems, 0);
|
||||
|
||||
foreach (var feed in selectedItems)
|
||||
Database.Entities.SaveChanges(() => Database.Entities.Feeds.Remove(feed));
|
||||
|
||||
SetFeedButtonStates();
|
||||
}
|
||||
|
||||
private void HandleAddFeedButtonClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
AddFeed();
|
||||
}
|
||||
|
||||
private void HandleEditFeedButtonClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
EditSelectedFeed();
|
||||
}
|
||||
|
||||
private void HandleDeleteFeedButtonClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
DeleteSelectedFeeds();
|
||||
}
|
||||
|
||||
private void HandleImportButtonClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
ImportFeeds();
|
||||
}
|
||||
|
||||
private void HandleExportButtonClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
ExportFeeds();
|
||||
}
|
||||
|
||||
private static void ExportFeeds()
|
||||
{
|
||||
var saveFileDialog = new SaveFileDialog
|
||||
{
|
||||
FileName = Properties.Resources.ApplicationName,
|
||||
Filter = Properties.Resources.ImportExportFilter,
|
||||
FilterIndex = 0,
|
||||
OverwritePrompt = true
|
||||
};
|
||||
|
||||
var result = saveFileDialog.ShowDialog();
|
||||
|
||||
if (!result.GetValueOrDefault(false))
|
||||
return;
|
||||
|
||||
var writerSettings = new XmlWriterSettings
|
||||
{
|
||||
Indent = true,
|
||||
CheckCharacters = true,
|
||||
ConformanceLevel = ConformanceLevel.Document
|
||||
};
|
||||
|
||||
var xmlWriter = XmlWriter.Create(saveFileDialog.FileName, writerSettings);
|
||||
|
||||
xmlWriter.WriteStartElement("opml");
|
||||
xmlWriter.WriteStartElement("body");
|
||||
|
||||
foreach (var feed in Database.Entities.Feeds.OrderBy(feed => feed.Name))
|
||||
{
|
||||
xmlWriter.WriteStartElement("outline");
|
||||
|
||||
xmlWriter.WriteAttributeString("title", feed.Title);
|
||||
xmlWriter.WriteAttributeString("htmlUrl", feed.Link);
|
||||
xmlWriter.WriteAttributeString("xmlUrl", feed.Source);
|
||||
|
||||
xmlWriter.WriteEndElement();
|
||||
}
|
||||
xmlWriter.WriteAttributeString("title", feed.Title);
|
||||
xmlWriter.WriteAttributeString("htmlUrl", feed.Link);
|
||||
xmlWriter.WriteAttributeString("xmlUrl", feed.Source);
|
||||
|
||||
xmlWriter.WriteEndElement();
|
||||
|
||||
xmlWriter.WriteEndElement();
|
||||
|
||||
xmlWriter.Flush();
|
||||
xmlWriter.Close();
|
||||
}
|
||||
|
||||
private static void ImportFeeds()
|
||||
xmlWriter.WriteEndElement();
|
||||
|
||||
xmlWriter.WriteEndElement();
|
||||
|
||||
xmlWriter.Flush();
|
||||
xmlWriter.Close();
|
||||
}
|
||||
|
||||
private static void ImportFeeds()
|
||||
{
|
||||
var openFileDialog = new OpenFileDialog
|
||||
{
|
||||
var openFileDialog = new OpenFileDialog
|
||||
Filter = Properties.Resources.ImportExportFilter,
|
||||
FilterIndex = 0
|
||||
};
|
||||
|
||||
var result = openFileDialog.ShowDialog();
|
||||
|
||||
if (!result.GetValueOrDefault(false))
|
||||
return;
|
||||
|
||||
var xmlReaderSettings = new XmlReaderSettings { IgnoreWhitespace = true };
|
||||
|
||||
var xmlReader = XmlReader.Create(openFileDialog.FileName, xmlReaderSettings);
|
||||
|
||||
try
|
||||
{
|
||||
xmlReader.Read();
|
||||
|
||||
xmlReader.ReadStartElement("opml");
|
||||
xmlReader.ReadStartElement("body");
|
||||
|
||||
while (xmlReader.NodeType != XmlNodeType.EndElement)
|
||||
{
|
||||
Filter = Properties.Resources.ImportExportFilter,
|
||||
FilterIndex = 0
|
||||
};
|
||||
var feed = Feed.Create();
|
||||
feed.CategoryId = Database.Entities.Categories.First(c => c.IsDefault).Id;
|
||||
|
||||
var result = openFileDialog.ShowDialog();
|
||||
|
||||
if (!result.GetValueOrDefault(false))
|
||||
return;
|
||||
|
||||
var xmlReaderSettings = new XmlReaderSettings { IgnoreWhitespace = true };
|
||||
|
||||
var xmlReader = XmlReader.Create(openFileDialog.FileName, xmlReaderSettings);
|
||||
|
||||
try
|
||||
{
|
||||
xmlReader.Read();
|
||||
|
||||
xmlReader.ReadStartElement("opml");
|
||||
xmlReader.ReadStartElement("body");
|
||||
|
||||
while (xmlReader.NodeType != XmlNodeType.EndElement)
|
||||
while (xmlReader.MoveToNextAttribute())
|
||||
{
|
||||
var feed = Feed.Create();
|
||||
feed.CategoryId = Database.Entities.Categories.First(c => c.IsDefault).Id;
|
||||
|
||||
while (xmlReader.MoveToNextAttribute())
|
||||
switch (xmlReader.Name.ToLower())
|
||||
{
|
||||
switch (xmlReader.Name.ToLower())
|
||||
{
|
||||
case "title":
|
||||
feed.Title = xmlReader.Value;
|
||||
break;
|
||||
case "title":
|
||||
feed.Title = xmlReader.Value;
|
||||
break;
|
||||
|
||||
// ReSharper disable once StringLiteralTypo
|
||||
case "htmlurl":
|
||||
feed.Link = xmlReader.Value;
|
||||
break;
|
||||
// ReSharper disable once StringLiteralTypo
|
||||
case "htmlurl":
|
||||
feed.Link = xmlReader.Value;
|
||||
break;
|
||||
|
||||
// ReSharper disable once StringLiteralTypo
|
||||
case "xmlurl":
|
||||
feed.Source = xmlReader.Value;
|
||||
break;
|
||||
// ReSharper disable once StringLiteralTypo
|
||||
case "xmlurl":
|
||||
feed.Source = xmlReader.Value;
|
||||
break;
|
||||
|
||||
case "text":
|
||||
feed.Name = xmlReader.Value;
|
||||
break;
|
||||
}
|
||||
case "text":
|
||||
feed.Name = xmlReader.Value;
|
||||
break;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(feed.Name))
|
||||
feed.Name = feed.Title;
|
||||
|
||||
Database.Entities.Feeds.Add(feed);
|
||||
|
||||
xmlReader.MoveToElement();
|
||||
|
||||
xmlReader.Skip();
|
||||
}
|
||||
|
||||
xmlReader.ReadEndElement();
|
||||
if (string.IsNullOrEmpty(feed.Name))
|
||||
feed.Name = feed.Title;
|
||||
|
||||
xmlReader.ReadEndElement();
|
||||
}
|
||||
finally
|
||||
{
|
||||
xmlReader.Close();
|
||||
}
|
||||
}
|
||||
Database.Entities.Feeds.Add(feed);
|
||||
|
||||
private void SetCategoryButtonStates()
|
||||
{
|
||||
AddCategoryButton.IsEnabled = true;
|
||||
xmlReader.MoveToElement();
|
||||
|
||||
var selectedId = ((Category) CategoryListBox.SelectedItem).Id;
|
||||
|
||||
EditCategoryButton.IsEnabled = CategoryListBox.SelectedItem != null &&
|
||||
selectedId != Database.Entities.DefaultCategory.Id;
|
||||
DeleteCategoryButton.IsEnabled = CategoryListBox.SelectedItem != null &&
|
||||
selectedId != Database.Entities.DefaultCategory.Id;
|
||||
}
|
||||
|
||||
private void AddCategory()
|
||||
{
|
||||
var category = new Category();
|
||||
|
||||
var categoryWindow = new CategoryWindow();
|
||||
|
||||
var result = categoryWindow.Display(category, Window.GetWindow(this));
|
||||
|
||||
if (!result.HasValue || !result.Value)
|
||||
return;
|
||||
|
||||
Database.Entities.SaveChanges(() => Database.Entities.Categories.Add(category));
|
||||
|
||||
CategoryListBox.SelectedItem = category;
|
||||
|
||||
SetCategoryButtonStates();
|
||||
}
|
||||
|
||||
private void EditSelectedCategory()
|
||||
{
|
||||
if (CategoryListBox.SelectedItem == null)
|
||||
return;
|
||||
|
||||
var category = (Category) CategoryListBox.SelectedItem;
|
||||
|
||||
var categoryWindow = new CategoryWindow();
|
||||
|
||||
categoryWindow.Display(category, Window.GetWindow(this));
|
||||
}
|
||||
|
||||
private void DeleteSelectedCategory()
|
||||
{
|
||||
var category = (Category) CategoryListBox.SelectedItem;
|
||||
|
||||
if (MessageBox.Show(ParentWindow, string.Format(Properties.Resources.ConfirmDeleteCategory, category.Name), Properties.Resources.ConfirmDeleteTitle, MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.No)
|
||||
return;
|
||||
|
||||
var defaultCategory = Database.Entities.DefaultCategory;
|
||||
|
||||
foreach (var feed in Database.Entities.Feeds.Where(f => f.CategoryId == category.Id))
|
||||
Database.Entities.SaveChanges(() => feed.CategoryId = defaultCategory.Id);
|
||||
|
||||
var index = CategoryListBox.SelectedIndex;
|
||||
|
||||
if (index == CategoryListBox.Items.Count - 1)
|
||||
CategoryListBox.SelectedIndex = index - 1;
|
||||
else
|
||||
CategoryListBox.SelectedIndex = index + 1;
|
||||
|
||||
Database.Entities.SaveChanges(() => Database.Entities.Categories.Remove(category));
|
||||
|
||||
SetCategoryButtonStates();
|
||||
}
|
||||
|
||||
private void HandleAddCategoryButtonClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
AddCategory();
|
||||
}
|
||||
|
||||
private void HandleEditCategoryButtonClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
EditSelectedCategory();
|
||||
}
|
||||
|
||||
private void HandleDeleteCategoryButtonClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
DeleteSelectedCategory();
|
||||
}
|
||||
|
||||
private void HandleCategoryListBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
if (_collectionViewSource == null)
|
||||
{
|
||||
_collectionViewSource = new CollectionViewSource { Source = Database.Entities.Feeds };
|
||||
_collectionViewSource.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
|
||||
_collectionViewSource.Filter += HandleCollectionViewSourceFilter;
|
||||
|
||||
FeedListBox.ItemsSource = _collectionViewSource.View;
|
||||
xmlReader.Skip();
|
||||
}
|
||||
|
||||
_collectionViewSource.View.Refresh();
|
||||
xmlReader.ReadEndElement();
|
||||
|
||||
if (FeedListBox.Items.Count > 0)
|
||||
FeedListBox.SelectedIndex = 0;
|
||||
|
||||
SetFeedButtonStates();
|
||||
SetCategoryButtonStates();
|
||||
xmlReader.ReadEndElement();
|
||||
}
|
||||
|
||||
private void HandleCollectionViewSourceFilter(object sender, FilterEventArgs e)
|
||||
finally
|
||||
{
|
||||
var selectedCategory = (Category) CategoryListBox.SelectedItem;
|
||||
|
||||
var feed = (Feed) e.Item;
|
||||
|
||||
e.Accepted = feed.CategoryId == selectedCategory.Id;
|
||||
}
|
||||
|
||||
private void CategoryListBox_Drop(object sender, DragEventArgs e)
|
||||
{
|
||||
var feedList = (List<Feed>) e.Data.GetData(typeof(List<Feed>));
|
||||
|
||||
var category = (Category) ((DataGridRow) sender).Item;
|
||||
|
||||
foreach (var feed in feedList!)
|
||||
Database.Entities.SaveChanges(() => feed.CategoryId = category.Id);
|
||||
|
||||
_collectionViewSource.View.Refresh();
|
||||
|
||||
var dataGridRow = (DataGridRow) sender;
|
||||
|
||||
dataGridRow.FontWeight = FontWeights.Normal;
|
||||
}
|
||||
|
||||
private void HandleListBoxItemPreviewMouseMove(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (e.LeftButton != MouseButtonState.Pressed)
|
||||
return;
|
||||
|
||||
var selectedItems = FeedListBox.SelectedItems.Cast<Feed>().ToList();
|
||||
|
||||
DragDrop.DoDragDrop(FeedListBox, selectedItems, DragDropEffects.Move);
|
||||
}
|
||||
|
||||
private void CategoryListBox_DragEnter(object sender, DragEventArgs e)
|
||||
{
|
||||
var dataGridRow = (DataGridRow) sender;
|
||||
|
||||
dataGridRow.FontWeight = FontWeights.Bold;
|
||||
}
|
||||
|
||||
private void CategoryListBox_DragLeave(object sender, DragEventArgs e)
|
||||
{
|
||||
var dataGridRow = (DataGridRow) sender;
|
||||
|
||||
dataGridRow.FontWeight = FontWeights.Normal;
|
||||
}
|
||||
|
||||
private void HandleListBoxItemMouseDoubleClick(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
EditSelectedFeed();
|
||||
}
|
||||
|
||||
private void HandleMultipleEditClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var bulkFeedWindow = new BulkFeedWindow();
|
||||
bulkFeedWindow.Display(Window.GetWindow(this));
|
||||
}
|
||||
|
||||
private void HandleFeedListPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
// Get the object that was clicked on
|
||||
var originalSource = (DependencyObject) e.OriginalSource;
|
||||
|
||||
// Look for a row that contains the object
|
||||
var dataGridRow = (DataGridRow) FeedListBox.ContainerFromElement(originalSource);
|
||||
|
||||
// If the selection already contains this row then ignore it
|
||||
if (dataGridRow != null && FeedListBox.SelectedItems.Contains(dataGridRow.Item))
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
private void CategoryListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
if (!EditCategoryButton.IsEnabled)
|
||||
return;
|
||||
|
||||
EditSelectedCategory();
|
||||
}
|
||||
|
||||
private void FeedListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
SetFeedButtonStates();
|
||||
xmlReader.Close();
|
||||
}
|
||||
}
|
||||
|
||||
private void SetCategoryButtonStates()
|
||||
{
|
||||
AddCategoryButton.IsEnabled = true;
|
||||
|
||||
var selectedId = ((Category) CategoryListBox.SelectedItem).Id;
|
||||
|
||||
EditCategoryButton.IsEnabled = CategoryListBox.SelectedItem != null &&
|
||||
selectedId != Database.Entities.DefaultCategory.Id;
|
||||
DeleteCategoryButton.IsEnabled = CategoryListBox.SelectedItem != null &&
|
||||
selectedId != Database.Entities.DefaultCategory.Id;
|
||||
}
|
||||
|
||||
private void AddCategory()
|
||||
{
|
||||
var category = new Category();
|
||||
|
||||
var categoryWindow = new CategoryWindow();
|
||||
|
||||
var result = categoryWindow.Display(category, Window.GetWindow(this));
|
||||
|
||||
if (!result.HasValue || !result.Value)
|
||||
return;
|
||||
|
||||
Database.Entities.SaveChanges(() => Database.Entities.Categories.Add(category));
|
||||
|
||||
CategoryListBox.SelectedItem = category;
|
||||
|
||||
SetCategoryButtonStates();
|
||||
}
|
||||
|
||||
private void EditSelectedCategory()
|
||||
{
|
||||
if (CategoryListBox.SelectedItem == null)
|
||||
return;
|
||||
|
||||
var category = (Category) CategoryListBox.SelectedItem;
|
||||
|
||||
var categoryWindow = new CategoryWindow();
|
||||
|
||||
categoryWindow.Display(category, Window.GetWindow(this));
|
||||
}
|
||||
|
||||
private void DeleteSelectedCategory()
|
||||
{
|
||||
var category = (Category) CategoryListBox.SelectedItem;
|
||||
|
||||
if (MessageBox.Show(ParentWindow, string.Format(Properties.Resources.ConfirmDeleteCategory, category.Name), Properties.Resources.ConfirmDeleteTitle, MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.No)
|
||||
return;
|
||||
|
||||
var defaultCategory = Database.Entities.DefaultCategory;
|
||||
|
||||
foreach (var feed in Database.Entities.Feeds.Where(f => f.CategoryId == category.Id))
|
||||
Database.Entities.SaveChanges(() => feed.CategoryId = defaultCategory.Id);
|
||||
|
||||
var index = CategoryListBox.SelectedIndex;
|
||||
|
||||
if (index == CategoryListBox.Items.Count - 1)
|
||||
CategoryListBox.SelectedIndex = index - 1;
|
||||
else
|
||||
CategoryListBox.SelectedIndex = index + 1;
|
||||
|
||||
Database.Entities.SaveChanges(() => Database.Entities.Categories.Remove(category));
|
||||
|
||||
SetCategoryButtonStates();
|
||||
}
|
||||
|
||||
private void HandleAddCategoryButtonClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
AddCategory();
|
||||
}
|
||||
|
||||
private void HandleEditCategoryButtonClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
EditSelectedCategory();
|
||||
}
|
||||
|
||||
private void HandleDeleteCategoryButtonClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
DeleteSelectedCategory();
|
||||
}
|
||||
|
||||
private void HandleCategoryListBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
if (_collectionViewSource == null)
|
||||
{
|
||||
_collectionViewSource = new CollectionViewSource { Source = Database.Entities.Feeds };
|
||||
_collectionViewSource.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
|
||||
_collectionViewSource.Filter += HandleCollectionViewSourceFilter;
|
||||
|
||||
FeedListBox.ItemsSource = _collectionViewSource.View;
|
||||
}
|
||||
|
||||
_collectionViewSource.View.Refresh();
|
||||
|
||||
if (FeedListBox.Items.Count > 0)
|
||||
FeedListBox.SelectedIndex = 0;
|
||||
|
||||
SetFeedButtonStates();
|
||||
SetCategoryButtonStates();
|
||||
}
|
||||
|
||||
private void HandleCollectionViewSourceFilter(object sender, FilterEventArgs e)
|
||||
{
|
||||
var selectedCategory = (Category) CategoryListBox.SelectedItem;
|
||||
|
||||
var feed = (Feed) e.Item;
|
||||
|
||||
e.Accepted = feed.CategoryId == selectedCategory.Id;
|
||||
}
|
||||
|
||||
private void CategoryListBox_Drop(object sender, DragEventArgs e)
|
||||
{
|
||||
var feedList = (List<Feed>) e.Data.GetData(typeof(List<Feed>));
|
||||
|
||||
var category = (Category) ((DataGridRow) sender).Item;
|
||||
|
||||
foreach (var feed in feedList!)
|
||||
Database.Entities.SaveChanges(() => feed.CategoryId = category.Id);
|
||||
|
||||
_collectionViewSource.View.Refresh();
|
||||
|
||||
var dataGridRow = (DataGridRow) sender;
|
||||
|
||||
dataGridRow.FontWeight = FontWeights.Normal;
|
||||
}
|
||||
|
||||
private void HandleListBoxItemPreviewMouseMove(object sender, MouseEventArgs e)
|
||||
{
|
||||
if (e.LeftButton != MouseButtonState.Pressed)
|
||||
return;
|
||||
|
||||
var selectedItems = FeedListBox.SelectedItems.Cast<Feed>().ToList();
|
||||
|
||||
DragDrop.DoDragDrop(FeedListBox, selectedItems, DragDropEffects.Move);
|
||||
}
|
||||
|
||||
private void CategoryListBox_DragEnter(object sender, DragEventArgs e)
|
||||
{
|
||||
var dataGridRow = (DataGridRow) sender;
|
||||
|
||||
dataGridRow.FontWeight = FontWeights.Bold;
|
||||
}
|
||||
|
||||
private void CategoryListBox_DragLeave(object sender, DragEventArgs e)
|
||||
{
|
||||
var dataGridRow = (DataGridRow) sender;
|
||||
|
||||
dataGridRow.FontWeight = FontWeights.Normal;
|
||||
}
|
||||
|
||||
private void HandleListBoxItemMouseDoubleClick(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
EditSelectedFeed();
|
||||
}
|
||||
|
||||
private void HandleMultipleEditClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var bulkFeedWindow = new BulkFeedWindow();
|
||||
bulkFeedWindow.Display(Window.GetWindow(this));
|
||||
}
|
||||
|
||||
private void HandleFeedListPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
// Get the object that was clicked on
|
||||
var originalSource = (DependencyObject) e.OriginalSource;
|
||||
|
||||
// Look for a row that contains the object
|
||||
var dataGridRow = (DataGridRow) FeedListBox.ContainerFromElement(originalSource);
|
||||
|
||||
// If the selection already contains this row then ignore it
|
||||
if (dataGridRow != null && FeedListBox.SelectedItems.Contains(dataGridRow.Item))
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
private void CategoryListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
if (!EditCategoryButton.IsEnabled)
|
||||
return;
|
||||
|
||||
EditSelectedCategory();
|
||||
}
|
||||
|
||||
private void FeedListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
SetFeedButtonStates();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user