using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using UAF.Application; using System.Globalization; using System.Reflection; using UAF.WPF.Themes; namespace DataFileAnalyzer { /// /// File library status to color /// public class StatusToColorConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (!(value is FileUpdaterStatus)) return ThemeManager.GetGlobalThemeResource("WhiteBrush"); FileUpdaterStatus strValue = (FileUpdaterStatus)value; if (strValue == FileUpdaterStatus.STARTING) return ThemeManager.GetGlobalThemeResource("OrangeBrush"); if (strValue == FileUpdaterStatus.INITIALIZING) return ThemeManager.GetGlobalThemeResource("OrangeBrush"); if (strValue == FileUpdaterStatus.RUNNING) return ThemeManager.GetGlobalThemeResource("YellowBrush"); if (strValue == FileUpdaterStatus.READY) return ThemeManager.GetGlobalThemeResource("GreenBrush"); if (strValue == FileUpdaterStatus.PAUSED) return ThemeManager.GetGlobalThemeResource("RedBrush"); if (strValue == FileUpdaterStatus.STOPPED) return ThemeManager.GetGlobalThemeResource("RedBrush"); return ThemeManager.GetGlobalThemeResource("WhiteBrush"); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotSupportedException(); } } /// /// List of DataFileError to string /// public class DataFileErrorListToStringConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { List errorList = value as List; string result = ""; foreach (DataFileError error in errorList) { result += ((result.Length != 0) ? ", " : "") + error; } return result; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotSupportedException(); } } /// /// DataFile to a string containing the errors and warnings of this file /// public class DataFileToErrorListConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { DataFile dataFile = value as DataFile; string result = null; if (dataFile != null && (dataFile.Warnings.Count != 0 || dataFile.Errors.Count != 0)) { if (dataFile.Errors.Count != 0) { result = ""; bool needComa = false; foreach (DataFileError error in dataFile.Errors) { result += (needComa ? ", " : "") + error; needComa = true; } } else if (dataFile.Warnings.Count != 0) { result = ""; bool needComa = false; foreach (DataFileError error in dataFile.Warnings) { result += (needComa ? ", " : "") + error; needComa = true; } } } return result; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotSupportedException(); } } /// /// Get the pause button state depending on the status /// public class StatusToPauseButtonTextConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { string strValue = value as string; return (strValue == "PAUSED") ? "Resume" : "Pause"; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotSupportedException(); } } /// /// The main view of the DataFileAnalyzer /// public partial class DataFileAnalyzerView : UserControl { public DataFileAnalyzerModel Model { get { return DataContext as DataFileAnalyzerModel; } } /// /// Constructor /// public DataFileAnalyzerView() { InitializeComponent(); } /// /// Save file library state /// private void SaveFileListClick(object sender, RoutedEventArgs e) { Model.Library.Save(); } /// /// Pause the file library update /// private void PauseUpdateClick(object sender, RoutedEventArgs e) { if (Model.Library.Paused) { Model.Library.Resume(); } else { Model.Library.Pause(); Model.RefreshFileList(); } } /// /// Double click on a referenced file: select this file in the file list /// private void OnReferencedFileDoubleClick(object sender, MouseButtonEventArgs e) { DataFileDependency dependency = ((ListViewItem)sender).Content as DataFileDependency; DataFile parentFile = Model.Library.GetFile(dependency.Key); if (parentFile != null) { Model.FilterString = parentFile.NormalizedPath; ScrollToFile(parentFile); uiFileList.SelectedItem = parentFile; uiFileList.Focus(); } } /// /// Double click on a referencing file: select this file in the file list /// private void OnReferencingFileDoubleClick(object sender, MouseButtonEventArgs e) { DataFileDependency dependency = ((ListViewItem)sender).Content as DataFileDependency; DataFile childFile = Model.Library.GetFile(dependency.Key); if (childFile != null) { Model.FilterString = childFile.NormalizedPath; ScrollToFile(childFile); uiFileList.SelectedItem = childFile; uiFileList.Focus(); } } /// /// Scroll to a file in the file list /// private void ScrollToFile(DataFile _file) { // This doesn't work well when virtualization is activated //uiFileList.ScrollIntoView(_file); VirtualizingStackPanel vsp = (VirtualizingStackPanel)typeof(ItemsControl).InvokeMember("_itemsHost", BindingFlags.Instance | BindingFlags.GetField | BindingFlags.NonPublic, null, uiFileList, null); int index = Model.CurrentDataFiles.IndexOf(_file); double scrollHeight = vsp.ScrollOwner.ScrollableHeight; double offset = index * scrollHeight / uiFileList.Items.Count; vsp.SetVerticalOffset(offset); } private void FileList_GotFocus(object sender, RoutedEventArgs e) { if (Model != null) Model.ActiveView = DataFileAnalyzerModel.FocusedView.FileView; } private void FileList_LostFocus(object sender, RoutedEventArgs e) { if (Model != null) Model.ActiveView = DataFileAnalyzerModel.FocusedView.Other; } private void FileList_PreviewMouseDown(object sender, MouseButtonEventArgs e) { uiFileList.Focus(); } private void ReferencedFileList_GotFocus(object sender, RoutedEventArgs e) { if (Model != null) Model.ActiveView = DataFileAnalyzerModel.FocusedView.ReferencedByMeView; } private void ReferencedFileList_LostFocus(object sender, RoutedEventArgs e) { if (Model != null) Model.ActiveView = DataFileAnalyzerModel.FocusedView.Other; } private void ReferencedFileList_PreviewMouseDown(object sender, MouseButtonEventArgs e) { uiReferencedFileList.Focus(); } private void ReferencingFileList_GotFocus(object sender, RoutedEventArgs e) { if (Model != null) Model.ActiveView = DataFileAnalyzerModel.FocusedView.ReferencingMeView; } private void ReferencingFileList_LostFocus(object sender, RoutedEventArgs e) { if (Model != null) Model.ActiveView = DataFileAnalyzerModel.FocusedView.Other; } private void ReferencingFileList_PreviewMouseDown(object sender, MouseButtonEventArgs e) { uiReferencingFileList.Focus(); } } }