1 //-----------------------------------------------------------------------
2 // <copyright file="MainWindow.xaml.cs" company="Takayoshi Matsuyama">
3 // Copyright (c) Takayoshi Matsuyama. All rights reserved.
5 //-----------------------------------------------------------------------
7 // This file is part of Simple Backup.
9 // Simple Backup is free software: you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 3 of the License, or
12 // (at your option) any later version.
14 // Simple Backup is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with Foobar. If not, see <http://www.gnu.org/licenses/>.
23 using System.ComponentModel;
26 using System.Windows.Controls;
27 using System.Windows.Controls.Primitives;
28 using System.Windows.Input;
29 using SimpleBackup.Core.Utilities;
30 using SimpleBackup.Core.ViewModels;
31 using SimpleBackup.Core.Views;
32 using SimpleBackup.Services;
33 using SimpleBackup.ViewModels;
35 namespace SimpleBackup.Views
37 using Application = System.Windows.Application;
38 using DataGrid = System.Windows.Controls.DataGrid;
39 using DataGridCell = System.Windows.Controls.DataGridCell;
40 using KeyEventArgs = System.Windows.Input.KeyEventArgs;
41 using MessageBox = System.Windows.MessageBox;
42 using TextBox = System.Windows.Controls.TextBox;
45 /// Code-Behind of MainWindow.xaml.
47 public partial class MainWindow : Window, IMainWindow
50 /// Initializes a new instance of the <see cref="MainWindow"/> class.
54 this.InitializeComponent();
56 this.DataContextChanged += (sender, args) =>
58 var viewModel = this.DataContext as IMainWindowViewModel;
59 if (viewModel != null)
61 viewModel.FocusOnGrid += this.OnFocusOnGridRequired;
62 viewModel.FocusOnGridRow += this.OnFocusOnGridRowRequired;
66 throw new InvalidOperationException("The data context is not an IMainWindowViewModel object.");
70 this.BackupInfoDataGrid.Loaded += (sender, args) =>
72 if (this.BackupInfoDataGrid.Items.Count != 0)
74 this.FocusOnGridRow(0);
80 /// Raises the Closing event.
82 /// <param name="e">A <see cref="T:System.ComponentModel.CancelEventArgs" /> that contains the event data.</param>
83 protected override void OnClosing(CancelEventArgs e)
85 if ((BackupSettingService.Instance.CurrentSetting != null) &&
86 BackupSettingService.Instance.CurrentSetting.Modified)
88 MessageBoxResult confirmationResult = MessageBox.Show(
89 Application.Current.MainWindow,
90 SimpleBackup.Resources.Resources.Views_SaveConfirmationMessage,
91 SimpleBackup.Resources.Resources.Views_ConfirmationDialogTitle,
92 MessageBoxButton.YesNoCancel);
93 if (confirmationResult == MessageBoxResult.Yes)
95 if (BackupSettingService.Instance.OverWriteCurrntBackupSetting() == false)
100 else if (confirmationResult == MessageBoxResult.Cancel)
107 private void OnFocusOnGridRequired(object sender, EventArgs eventArgs)
109 this.BackupInfoDataGrid.Focus();
112 private void OnFocusOnGridRowRequired(object sender, FocusOnGridRowEventArgs args)
114 this.FocusOnGridRow(args.Index);
117 private void FocusOnGridRow(int index)
119 this.BackupInfoDataGrid.Focus();
120 var dataGridRow = this.BackupInfoDataGrid.ItemContainerGenerator.ContainerFromIndex(index) as DataGridRow;
121 if (dataGridRow != null)
124 this.SelectBackupInfoDataGridItem(index);
126 var dataGridCellsPanel = dataGridRow.GetVisualChild<DataGridCellsPanel>();
127 if (dataGridCellsPanel != null)
129 if (dataGridCellsPanel.Children.Count != 0)
131 var dataGridCell = dataGridCellsPanel.Children[0] as DataGridCell;
132 if (dataGridCell != null)
134 dataGridCell.Focus();
141 private void SelectBackupInfoDataGridItem(int index)
143 this.BackupInfoDataGrid.SelectedIndex = index;
146 private void OnDataGridMouseDoubleClick(object sender, MouseButtonEventArgs e)
148 var dataGrid = (DataGrid)sender;
149 var currentItemviewModel = dataGrid.CurrentItem as BackupInfoViewModel;
150 if (currentItemviewModel == null)
155 DataGridCellInfo currentCellInfo = dataGrid.CurrentCell;
156 var currentColumnHeaderContentPresenter = currentCellInfo.Column.Header as ContentPresenter;
157 if (currentColumnHeaderContentPresenter != null)
159 var sourceColumnHeaderViewModel =
160 currentColumnHeaderContentPresenter.Content as SourceColumnHeaderViewModel;
161 if (sourceColumnHeaderViewModel != null)
163 currentItemviewModel.ExecuteDoubleClickAction(BackupInfoPathType.Source);
167 var destinationColumnHeaderViewModel =
168 currentColumnHeaderContentPresenter.Content as DestinationColumnHeaderViewModel;
169 if (destinationColumnHeaderViewModel != null)
171 currentItemviewModel.ExecuteDoubleClickAction(BackupInfoPathType.Destination);
177 private void OnCellContentPreviewKeyDown(object sender, KeyEventArgs e)
179 Type sourceType = sender.GetType();
181 var sourceDataGridCell = ((DependencyObject)sender).SearchUpParentWithSelf<DataGridCell>();
182 if (sourceDataGridCell == null)
187 var sourceDataGridRow = sourceDataGridCell.SearchUpParentWithSelf<DataGridRow>();
188 if (sourceDataGridRow == null)
193 var dataGrid = sourceDataGridRow.SearchUpParentWithSelf<DataGrid>();
194 if (dataGrid == null)
199 int sourceRowIndex = dataGrid.Items.IndexOf(sourceDataGridRow.Item);
200 int targetRowIndex = -1;
204 if (sourceRowIndex != 0)
206 targetRowIndex = sourceRowIndex - 1;
211 if (sourceRowIndex < dataGrid.Items.Count - 1)
213 targetRowIndex = sourceRowIndex + 1;
223 if (targetRowIndex < 0)
228 var targetDataGridRow = dataGrid.ItemContainerGenerator.ContainerFromIndex(targetRowIndex) as DataGridRow;
229 if (targetDataGridRow == null)
234 var targetCellsPresenter = targetDataGridRow.GetVisualChild<DataGridCellsPresenter>();
235 if (targetCellsPresenter == null)
240 var targetDataGridCell =
241 (DataGridCell)targetCellsPresenter.ItemContainerGenerator.ContainerFromIndex(sourceDataGridCell.Column.DisplayIndex);
242 if (targetDataGridCell == null)
247 var targetElement = targetDataGridCell.GetVisualChild(sourceType) as UIElement;
248 if (targetElement == null)
253 sourceDataGridRow.IsSelected = false;
254 targetDataGridRow.IsSelected = true;
255 targetElement.Focus();
260 private void OnMainWindowPreviewKeyDown(object sender, KeyEventArgs e)
264 this.StartEditCurrentCell();
267 else if (e.Key == Key.Escape)
269 var viewModel = this.DataContext as IMainWindowViewModel;
270 if (viewModel != null)
272 viewModel.OnEscKeyPressed();
277 else if ((Keyboard.Modifiers & ModifierKeys.Control) > 0)
281 var viewModel = this.DataContext as IMainWindowViewModel;
282 if (viewModel != null)
284 if (viewModel.SaveBackupSettingRelayCommand.CanExecute(this))
286 viewModel.SaveBackupSettingRelayCommand.Execute(this);
294 private void StartEditCurrentCell()
296 var currentItemviewModel = this.BackupInfoDataGrid.CurrentItem as BackupInfoViewModel;
297 if (currentItemviewModel == null)
302 DataGridCellInfo currentCellInfo = this.BackupInfoDataGrid.CurrentCell;
303 var currentColumnHeaderContentPresenter = currentCellInfo.Column.Header as ContentPresenter;
304 if (currentColumnHeaderContentPresenter != null)
306 if (currentColumnHeaderContentPresenter.Content is SourceColumnHeaderViewModel)
308 currentItemviewModel.EditSourceRelayCommand.Execute(null);
309 this.FocusOnTextBoxInSelectedDataGridCell();
311 else if (currentColumnHeaderContentPresenter.Content is DestinationColumnHeaderViewModel)
313 currentItemviewModel.EditDestinationRelayCommand.Execute(null);
314 this.FocusOnTextBoxInSelectedDataGridCell();
316 else if (currentColumnHeaderContentPresenter.Content is LabelColumnHeaderViewModel)
318 this.FocusOnTextBoxInSelectedDataGridCell();
323 private void FocusOnTextBoxInSelectedDataGridCell()
325 DataGridCellInfo cellInfo = this.BackupInfoDataGrid.SelectedCells.FirstOrDefault();
326 var cellContentPresenter = cellInfo.Column.GetCellContent(cellInfo.Item) as ContentPresenter;
327 if (cellContentPresenter != null)
329 var textBox = cellContentPresenter.GetVisualChild<TextBox>();
333 textBox.Select(textBox.Text.Length, 0);
338 private void OnZipEncodingSelectionChanged(object sender, SelectionChangedEventArgs e)
340 // Move focus setting from the ComboBox by setting focus to the main window.