OSDN Git Service

Ver.1.0.0.185 Task187 Interface based design
[simplebackup/SBTGitRepository.git] / Main / Views / MainWindow.xaml.cs
1 //-----------------------------------------------------------------------
2 // <copyright file="MainWindow.xaml.cs" company="Takayoshi Matsuyama">
3 //     Copyright (c) Takayoshi Matsuyama. All rights reserved.
4 // </copyright>
5 //-----------------------------------------------------------------------
6
7 // This file is part of Simple Backup.
8 //
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.
13 //
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.
18 //
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/>.
21
22 using System;
23 using System.ComponentModel;
24 using System.Linq;
25 using System.Windows;
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;
34
35 namespace SimpleBackup.Views
36 {
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;
43
44     /// <summary>
45     /// Code-Behind of MainWindow.xaml.
46     /// </summary>
47     public partial class MainWindow : Window, IMainWindow
48     {
49         /// <summary>
50         /// Initializes a new instance of the <see cref="MainWindow"/> class.
51         /// </summary>
52         public MainWindow()
53         {
54             this.InitializeComponent();
55
56             this.DataContextChanged += (sender, args) =>
57                 {
58                     var viewModel = this.DataContext as IMainWindowViewModel;
59                     if (viewModel != null)
60                     {
61                         viewModel.FocusOnGrid += this.OnFocusOnGridRequired;
62                         viewModel.FocusOnGridRow += this.OnFocusOnGridRowRequired;
63                     }
64                     else
65                     {
66                         throw new InvalidOperationException("The data context is not an IMainWindowViewModel object.");
67                     }
68                 };
69
70             this.BackupInfoDataGrid.Loaded += (sender, args) =>
71                 {
72                     if (this.BackupInfoDataGrid.Items.Count != 0)
73                     {
74                         this.FocusOnGridRow(0);
75                     }
76                 };
77         }
78
79         /// <summary>
80         /// Raises the Closing event.
81         /// </summary>
82         /// <param name="e">A <see cref="T:System.ComponentModel.CancelEventArgs" /> that contains the event data.</param>
83         protected override void OnClosing(CancelEventArgs e)
84         {
85             if ((BackupSettingService.Instance.CurrentSetting != null) &&
86                 BackupSettingService.Instance.CurrentSetting.Modified)
87             {
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)
94                 {
95                     if (BackupSettingService.Instance.OverWriteCurrntBackupSetting() == false)
96                     {
97                         e.Cancel = true;
98                     }
99                 }
100                 else if (confirmationResult == MessageBoxResult.Cancel)
101                 {
102                     e.Cancel = true;
103                 }
104             }
105         }
106
107         private void OnFocusOnGridRequired(object sender, EventArgs eventArgs)
108         {
109             this.BackupInfoDataGrid.Focus();
110         }
111
112         private void OnFocusOnGridRowRequired(object sender, FocusOnGridRowEventArgs args)
113         {
114             this.FocusOnGridRow(args.Index);
115         }
116
117         private void FocusOnGridRow(int index)
118         {
119             this.BackupInfoDataGrid.Focus();
120             var dataGridRow = this.BackupInfoDataGrid.ItemContainerGenerator.ContainerFromIndex(index) as DataGridRow;
121             if (dataGridRow != null)
122             {
123                 dataGridRow.Focus();
124                 this.SelectBackupInfoDataGridItem(index);
125
126                 var dataGridCellsPanel = dataGridRow.GetVisualChild<DataGridCellsPanel>();
127                 if (dataGridCellsPanel != null)
128                 {
129                     if (dataGridCellsPanel.Children.Count != 0)
130                     {
131                         var dataGridCell = dataGridCellsPanel.Children[0] as DataGridCell;
132                         if (dataGridCell != null)
133                         {
134                             dataGridCell.Focus();
135                         }
136                     }
137                 }
138             }
139         }
140
141         private void SelectBackupInfoDataGridItem(int index)
142         {
143             this.BackupInfoDataGrid.SelectedIndex = index;
144         }
145
146         private void OnDataGridMouseDoubleClick(object sender, MouseButtonEventArgs e)
147         {
148             var dataGrid = (DataGrid)sender;
149             var currentItemviewModel = dataGrid.CurrentItem as BackupInfoViewModel;
150             if (currentItemviewModel == null)
151             {
152                 return;
153             }
154
155             DataGridCellInfo currentCellInfo = dataGrid.CurrentCell;
156             var currentColumnHeaderContentPresenter = currentCellInfo.Column.Header as ContentPresenter;
157             if (currentColumnHeaderContentPresenter != null)
158             {
159                 var sourceColumnHeaderViewModel =
160                     currentColumnHeaderContentPresenter.Content as SourceColumnHeaderViewModel;
161                 if (sourceColumnHeaderViewModel != null)
162                 {
163                     currentItemviewModel.ExecuteDoubleClickAction(BackupInfoPathType.Source);
164                 }
165                 else
166                 {
167                     var destinationColumnHeaderViewModel =
168                         currentColumnHeaderContentPresenter.Content as DestinationColumnHeaderViewModel;
169                     if (destinationColumnHeaderViewModel != null)
170                     {
171                         currentItemviewModel.ExecuteDoubleClickAction(BackupInfoPathType.Destination);
172                     }
173                 }
174             }
175         }
176
177         private void OnCellContentPreviewKeyDown(object sender, KeyEventArgs e)
178         {
179             Type sourceType = sender.GetType();
180
181             var sourceDataGridCell = ((DependencyObject)sender).SearchUpParentWithSelf<DataGridCell>();
182             if (sourceDataGridCell == null)
183             {
184                 return;
185             }
186
187             var sourceDataGridRow = sourceDataGridCell.SearchUpParentWithSelf<DataGridRow>();
188             if (sourceDataGridRow == null)
189             {
190                 return;
191             }
192
193             var dataGrid = sourceDataGridRow.SearchUpParentWithSelf<DataGrid>();
194             if (dataGrid == null)
195             {
196                 return;
197             }
198
199             int sourceRowIndex = dataGrid.Items.IndexOf(sourceDataGridRow.Item);
200             int targetRowIndex = -1;
201             switch (e.Key)
202             {
203                 case Key.Up:
204                     if (sourceRowIndex != 0)
205                     {
206                         targetRowIndex = sourceRowIndex - 1;
207                     }
208
209                     break;
210                 case Key.Down:
211                     if (sourceRowIndex < dataGrid.Items.Count - 1)
212                     {
213                         targetRowIndex = sourceRowIndex + 1;
214                     }
215
216                     break;
217                 case Key.PageUp:
218                     break;
219                 case Key.PageDown:
220                     break;
221             }
222
223             if (targetRowIndex < 0)
224             {
225                 return;
226             }
227
228             var targetDataGridRow = dataGrid.ItemContainerGenerator.ContainerFromIndex(targetRowIndex) as DataGridRow;
229             if (targetDataGridRow == null)
230             {
231                 return;
232             }
233
234             var targetCellsPresenter = targetDataGridRow.GetVisualChild<DataGridCellsPresenter>();
235             if (targetCellsPresenter == null)
236             {
237                 return;
238             }
239
240             var targetDataGridCell =
241                 (DataGridCell)targetCellsPresenter.ItemContainerGenerator.ContainerFromIndex(sourceDataGridCell.Column.DisplayIndex);
242             if (targetDataGridCell == null)
243             {
244                 return;
245             }
246
247             var targetElement = targetDataGridCell.GetVisualChild(sourceType) as UIElement;
248             if (targetElement == null)
249             {
250                 return;
251             }
252
253             sourceDataGridRow.IsSelected = false;
254             targetDataGridRow.IsSelected = true;
255             targetElement.Focus();
256
257             e.Handled = true;
258         }
259
260         private void OnMainWindowPreviewKeyDown(object sender, KeyEventArgs e)
261         {
262             if (e.Key == Key.F2)
263             {
264                 this.StartEditCurrentCell();
265                 e.Handled = true;
266             }
267             else if (e.Key == Key.Escape)
268             {
269                 var viewModel = this.DataContext as IMainWindowViewModel;
270                 if (viewModel != null)
271                 {
272                     viewModel.OnEscKeyPressed();
273                 }
274
275                 e.Handled = true;
276             }
277             else if ((Keyboard.Modifiers & ModifierKeys.Control) > 0)
278             {
279                 if (e.Key == Key.S)
280                 {
281                     var viewModel = this.DataContext as IMainWindowViewModel;
282                     if (viewModel != null)
283                     {
284                         if (viewModel.SaveBackupSettingRelayCommand.CanExecute(this))
285                         {
286                             viewModel.SaveBackupSettingRelayCommand.Execute(this);
287                             e.Handled = true;
288                         }
289                     }
290                 }
291             }
292         }
293
294         private void StartEditCurrentCell()
295         {
296             var currentItemviewModel = this.BackupInfoDataGrid.CurrentItem as BackupInfoViewModel;
297             if (currentItemviewModel == null)
298             {
299                 return;
300             }
301
302             DataGridCellInfo currentCellInfo = this.BackupInfoDataGrid.CurrentCell;
303             var currentColumnHeaderContentPresenter = currentCellInfo.Column.Header as ContentPresenter;
304             if (currentColumnHeaderContentPresenter != null)
305             {
306                 if (currentColumnHeaderContentPresenter.Content is SourceColumnHeaderViewModel)
307                 {
308                     currentItemviewModel.EditSourceRelayCommand.Execute(null);
309                     this.FocusOnTextBoxInSelectedDataGridCell();
310                 }
311                 else if (currentColumnHeaderContentPresenter.Content is DestinationColumnHeaderViewModel)
312                 {
313                     currentItemviewModel.EditDestinationRelayCommand.Execute(null);
314                     this.FocusOnTextBoxInSelectedDataGridCell();
315                 }
316                 else if (currentColumnHeaderContentPresenter.Content is LabelColumnHeaderViewModel)
317                 {
318                     this.FocusOnTextBoxInSelectedDataGridCell();
319                 }
320             }
321         }
322
323         private void FocusOnTextBoxInSelectedDataGridCell()
324         {
325             DataGridCellInfo cellInfo = this.BackupInfoDataGrid.SelectedCells.FirstOrDefault();
326             var cellContentPresenter = cellInfo.Column.GetCellContent(cellInfo.Item) as ContentPresenter;
327             if (cellContentPresenter != null)
328             {
329                 var textBox = cellContentPresenter.GetVisualChild<TextBox>();
330                 if (textBox != null)
331                 {
332                     textBox.Focus();
333                     textBox.Select(textBox.Text.Length, 0);
334                 }
335             }
336         }
337
338         private void OnZipEncodingSelectionChanged(object sender, SelectionChangedEventArgs e)
339         {
340             // Move focus setting from the ComboBox by setting focus to the main window.
341             this.Focus();
342         }
343     }
344 }