PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/Tools/SequenceAssembler/Dialog/OpenFileDialog .xaml.cs

#
C# | 530 lines | 306 code | 69 blank | 155 comment | 47 complexity | 653390fdd852ffcb08c4f5c3178438c9 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, CPL-1.0
  1. // *********************************************************
  2. //
  3. // Copyright (c) Microsoft. All rights reserved.
  4. // This code is licensed under the Apache License, Version 2.0.
  5. // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
  6. // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
  7. // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
  8. // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
  9. //
  10. // *********************************************************
  11. namespace SequenceAssembler
  12. {
  13. #region -- Using Directives --
  14. using System;
  15. using System.Collections;
  16. using System.Collections.Generic;
  17. using System.Collections.ObjectModel;
  18. using System.IO;
  19. using System.Linq;
  20. using System.Windows;
  21. using System.Windows.Controls;
  22. using Bio;
  23. using Bio.IO;
  24. using SequenceAssembler.Properties;
  25. #endregion
  26. /// <summary>
  27. /// Interaction logic for OpenFileDialog.xaml. Open file dialogue will
  28. /// allow the user to select files containing DNA\Protein sequences.
  29. /// The file types that are supported by the Bio can
  30. /// be retrieved by querying Framework Abstraction classes.
  31. /// </summary>
  32. public partial class OpenFileDialog : Window
  33. {
  34. #region -- Private members --
  35. /// <summary>
  36. /// Describes the List of the extensions without the '.' character
  37. /// </summary>
  38. private Collection<string[]> extensionsList;
  39. /// <summary>
  40. /// Describes the Title of the format type.
  41. /// </summary>
  42. private Collection<string> fileTypeHeaders;
  43. /// <summary>
  44. /// Describes the Supported File Types by the Sequence Assembler
  45. /// </summary>
  46. private Collection<string> fileType;
  47. /// <summary>
  48. /// Describes Molecule Type
  49. /// </summary>
  50. private IAlphabet molecule;
  51. /// <summary>
  52. /// Describes the selected filenames.
  53. /// </summary>
  54. private Collection<string> fileNames;
  55. /// <summary>
  56. /// Describes the files info of the uploaded file.
  57. /// </summary>
  58. private Collection<FileSystemInfo> fileInfo;
  59. /// <summary>
  60. /// Describes the open file dialog being escaped
  61. /// </summary>
  62. private bool dialogEscaped;
  63. /// <summary>
  64. /// Describes the selected parser.
  65. /// </summary>
  66. private string selectedParserName;
  67. #endregion
  68. #region -- Constructor --
  69. /// <summary>
  70. /// Initializes the Opendialog.
  71. /// </summary>
  72. /// <param name="types">Supported file Types</param>
  73. /// <param name="info">Collection of the files and the sequences parsed from them</param>
  74. /// <param name="showFileBrowserAtStartup">Indicates whether to show file browse dialog by default</param>
  75. public OpenFileDialog(Collection<string> types, Collection<FileSystemInfo> info, bool showFileBrowserAtStartup = false)
  76. {
  77. this.InitializeComponent();
  78. this.SetTabIndex();
  79. this.fileType = types;
  80. this.fileInfo = info;
  81. this.fileNames = new Collection<string>();
  82. this.extensionsList = new Collection<string[]>();
  83. this.fileTypeHeaders = new Collection<string>();
  84. this.ExtractExtensions();
  85. this.btnImport.Click += new RoutedEventHandler(this.OnImportButtonClick);
  86. this.btnCancel.Click += new RoutedEventHandler(this.OnCancelButtonClick);
  87. this.btnImportCancel.Click += new RoutedEventHandler(this.OnCancelAnimationButtonClick);
  88. this.btnBrowse.Click += new RoutedEventHandler(this.OnBrowseButtonClick);
  89. this.comboMoleculeType.SelectionChanged += new SelectionChangedEventHandler(this.ComboMoleculeTypeSelectionChanged);
  90. this.cmbParserType.SelectionChanged += new SelectionChangedEventHandler(this.OnParserSelectionChanged);
  91. this.PreviewKeyUp += new System.Windows.Input.KeyEventHandler(this.OnOpenFileDialogKeyUp);
  92. this.Closed += new EventHandler(this.OnOpenFileDialogClosed);
  93. this.btnBrowse.Focus();
  94. this.Owner = Application.Current.MainWindow;
  95. // Populate the Moleculetype combo box.
  96. this.comboMoleculeType.Items.Add(Resource.AUTO_DETECT_TEXT);
  97. IEnumerable<IAlphabet> moleculeTypes = Alphabets.All.OrderBy(alpha => alpha.Name);
  98. foreach (IAlphabet alpha in moleculeTypes)
  99. {
  100. this.comboMoleculeType.Items.Add(alpha.Name);
  101. }
  102. this.comboMoleculeType.SelectedIndex = 0;
  103. ComboBoxItem defaultItem = new ComboBoxItem();
  104. defaultItem.Content = Resource.MANUAL_CHOICE;
  105. this.cmbParserType.Items.Add(defaultItem);
  106. foreach (string parserName in SequenceParsers.All.Select(P => P.Name))
  107. {
  108. ComboBoxItem item = new ComboBoxItem();
  109. item.Content = parserName;
  110. item.Tag = parserName;
  111. this.cmbParserType.Items.Add(item);
  112. }
  113. this.cmbParserType.SelectedIndex = 0;
  114. if (showFileBrowserAtStartup)
  115. {
  116. this.OnBrowseButtonClick(this, null);
  117. }
  118. }
  119. #endregion
  120. #region -- Public Events --
  121. /// <summary>
  122. /// Event to close the Pop up, It informs the
  123. /// Controller that the pop is closed and to
  124. /// close the Gray background.
  125. /// </summary>
  126. public event EventHandler ClosePopup;
  127. /// <summary>
  128. /// Event to cancel the import of files, It informs the
  129. /// Controller to cancel the import of files.
  130. /// </summary>
  131. public event EventHandler CancelImport;
  132. /// <summary>
  133. /// Event to start import files, it informs the Controller
  134. /// to start import the given files and molecule type.
  135. /// </summary>
  136. public event EventHandler<ImportFileEventArgs> ImportFile;
  137. #endregion
  138. #region -- Public Properties --
  139. /// <summary>
  140. /// Gets the different File Types
  141. /// </summary>
  142. public Collection<string> FileTypes
  143. {
  144. get
  145. {
  146. return this.fileType;
  147. }
  148. }
  149. #endregion
  150. #region -- Public Methods --
  151. /// <summary>
  152. /// Hides the animation and shows the
  153. /// import and cancel button
  154. /// </summary>
  155. public void OnCancelImport()
  156. {
  157. buttonPanel.Visibility = Visibility.Visible;
  158. animationPanel.Visibility = Visibility.Collapsed;
  159. }
  160. #endregion
  161. #region -- Private Methods --
  162. /// <summary>
  163. /// This method sets Tab index for the children controls.
  164. /// </summary>
  165. private void SetTabIndex()
  166. {
  167. this.btnBrowse.TabIndex = 0;
  168. this.comboMoleculeType.TabIndex = 1;
  169. this.cmbParserType.TabIndex = 2;
  170. this.btnImport.TabIndex = 3;
  171. this.btnCancel.TabIndex = 4;
  172. }
  173. /// <summary>
  174. /// This event is fired when the dialog is closed.
  175. /// </summary>
  176. /// <param name="sender">Open File Dialog</param>
  177. /// <param name="e">Event Data</param>
  178. private void OnOpenFileDialogClosed(object sender, EventArgs e)
  179. {
  180. //// Raise the event to controller, inform closing of the pop up
  181. if (this.ClosePopup != null)
  182. {
  183. this.ClosePopup(sender, e);
  184. }
  185. }
  186. /// <summary>
  187. /// Extracts the extensions from the supported file types
  188. /// It creates the array of the extensions without the '.'
  189. /// character from the comma separated string
  190. /// </summary>
  191. private void ExtractExtensions()
  192. {
  193. const int SourceIndex = 1;
  194. const int DestinationIndex = 0;
  195. foreach (string type in this.FileTypes)
  196. {
  197. //// Split the comma separated values into the array.
  198. string[] filterTypes = type.Split(',');
  199. //// Add the Format type header in the headers list.
  200. this.fileTypeHeaders.Add(filterTypes[0]);
  201. string[] filters = new string[filterTypes.Length - 1];
  202. //// remove the Format type header from the array of the extensions
  203. Array.Copy(filterTypes, SourceIndex, filters, DestinationIndex, filterTypes.Length - 1);
  204. int i = 0;
  205. //// replace the '.' character
  206. foreach (string filter in filters)
  207. {
  208. filters[i] = filter.Replace(".", string.Empty);
  209. i++;
  210. }
  211. //// create the extension list.
  212. this.extensionsList.Add(filters);
  213. }
  214. }
  215. /// <summary>
  216. /// This event close the dialog on escape button pressed,
  217. /// it would be a cancel action.
  218. /// </summary>
  219. /// <param name="sender">OpenFileDialog Instance</param>
  220. /// <param name="e">Event data</param>
  221. private void OnOpenFileDialogKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
  222. {
  223. if (e.Key == System.Windows.Input.Key.Escape && !this.dialogEscaped)
  224. {
  225. //// Raise the event to controller, inform closing of the pop up
  226. if (this.ClosePopup != null)
  227. {
  228. this.ClosePopup(sender, e);
  229. }
  230. //// Close the pop up.
  231. this.Close();
  232. }
  233. else
  234. {
  235. this.dialogEscaped = false;
  236. }
  237. }
  238. /// <summary>
  239. /// On import button click would inform the controller to import files,
  240. /// would also pass the list of filenames and the molecule type as event args.
  241. /// </summary>
  242. /// <param name="sender">Framework Element</param>
  243. /// <param name="e">Routed event args</param>
  244. private void OnImportButtonClick(object sender, RoutedEventArgs e)
  245. {
  246. //// Creates the collection of the File names.
  247. foreach (string file in this.fileNames)
  248. {
  249. FileInfo info = new FileInfo(file);
  250. if (this.fileInfo == null)
  251. {
  252. this.fileInfo = new Collection<FileSystemInfo>();
  253. }
  254. this.fileInfo.Add(info);
  255. }
  256. ImportFileEventArgs importArgs = new ImportFileEventArgs(this.fileNames, this.molecule, this.fileInfo, this.selectedParserName);
  257. buttonPanel.Visibility = Visibility.Collapsed;
  258. animationPanel.Visibility = Visibility.Visible;
  259. if (this.ImportFile != null)
  260. {
  261. this.ImportFile(sender, importArgs);
  262. }
  263. }
  264. /// <summary>
  265. /// On cancel button click would close the Importing dialog and would
  266. /// inform the controller
  267. /// </summary>
  268. /// <param name="sender">Framework Element</param>
  269. /// <param name="e">Routed Event args</param>
  270. private void OnCancelButtonClick(object sender, RoutedEventArgs e)
  271. {
  272. //// Raise the event to controller, inform closing of the pop up
  273. if (this.ClosePopup != null)
  274. {
  275. this.ClosePopup(sender, e);
  276. }
  277. //// Close the pop up.
  278. this.Close();
  279. }
  280. /// <summary>
  281. /// On cancel button click of Importing of files would inform
  282. /// the controller to cancel the import of files through events
  283. /// </summary>
  284. /// <param name="sender">Framework Element</param>
  285. /// <param name="e">Routed events args</param>
  286. private void OnCancelAnimationButtonClick(object sender, RoutedEventArgs e)
  287. {
  288. //// Raise the event
  289. if (this.CancelImport != null)
  290. {
  291. this.CancelImport(sender, e);
  292. }
  293. }
  294. /// <summary>
  295. /// Handles the click on the Browse button,Launches the Windows Open File dialog
  296. /// with custom File formats filters being set to the dialog.
  297. /// On selection of files shows the paths of the selected files on the screen.
  298. /// Gives option to import the files.
  299. /// </summary>
  300. /// <param name="sender">Framework Element</param>
  301. /// <param name="e">Routed event args</param>
  302. private void OnBrowseButtonClick(object sender, RoutedEventArgs e)
  303. {
  304. //// Launch the FileDialog
  305. this.LaunchWindowFileDialog();
  306. }
  307. /// <summary>
  308. /// Launches the File Dialog, creates the selected filenames list,
  309. /// also validates the selected file name for import.
  310. /// </summary>
  311. /// <param name="fileDialog">OpenFiledialog instance to be launched</param>
  312. private void LaunchWindowFileDialog()
  313. {
  314. //// Create and launch the Windows File Dialog, Set various validations
  315. using (System.Windows.Forms.OpenFileDialog fileDialog = new System.Windows.Forms.OpenFileDialog())
  316. {
  317. fileDialog.Multiselect = true;
  318. fileDialog.CheckFileExists = true;
  319. fileDialog.CheckPathExists = true;
  320. fileDialog.Filter = Resource.AllFilesFilter;
  321. // On SuccessFull selection of the files.
  322. if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  323. {
  324. // Reset the file name collection
  325. this.fileNames = new Collection<string>();
  326. this.fileNameList.Items.Clear();
  327. string autoParser = null;
  328. string prevParser = null;
  329. bool chooseParserManually = false;
  330. //// Validate the file type and create a list of file names to be displayed on the screen.
  331. foreach (string file in fileDialog.FileNames)
  332. {
  333. if (!this.CheckDuplicateFile(file))
  334. {
  335. MessageDialogBox.Show(Resource.DUPLICATE_FILE, Resource.CAPTION, MessageDialogButton.OK);
  336. return;
  337. }
  338. ISequenceParser parser = SequenceParsers.FindParserByFileName(file);
  339. autoParser = parser == null ? null : parser.Name;
  340. if (fileDialog.FileNames[0].Equals(file))
  341. {
  342. prevParser = autoParser;
  343. }
  344. if (autoParser == null || prevParser == null || !autoParser.Equals(prevParser))
  345. {
  346. chooseParserManually = true;
  347. }
  348. prevParser = autoParser;
  349. }
  350. //// Creates the collection of the File names.
  351. foreach (string file in fileDialog.FileNames)
  352. {
  353. if (!this.fileNames.Contains(file))
  354. {
  355. this.fileNames.Add(file);
  356. this.fileNameList.Items.Add(new ListViewItem { Content = new FileInfo(file).Name, ToolTip = file });
  357. }
  358. }
  359. //// Enables the Molecule type.
  360. this.comboMoleculeType.IsEnabled = true;
  361. this.comboMoleculeType.SelectedIndex = 0;
  362. //// Enables the Parser selection.
  363. this.cmbParserType.IsEnabled = true;
  364. if (chooseParserManually == true)
  365. {
  366. this.cmbParserType.SelectedIndex = 0;
  367. this.btnImport.IsEnabled = false;
  368. }
  369. else
  370. {
  371. foreach (ComboBoxItem item in this.cmbParserType.Items)
  372. {
  373. string parser = item.Content as string;
  374. if (parser != null && autoParser != null)
  375. {
  376. if (parser.Equals(autoParser))
  377. {
  378. this.cmbParserType.SelectedItem = item;
  379. break;
  380. }
  381. }
  382. }
  383. this.btnImport.IsEnabled = true;
  384. }
  385. this.btnImport.Focus();
  386. }
  387. else
  388. {
  389. this.btnBrowse.Focus();
  390. this.dialogEscaped = true;
  391. }
  392. }
  393. }
  394. /// <summary>
  395. /// This method checks the file selected for import is
  396. /// duplicate or a new file
  397. /// </summary>
  398. /// <param name="file">File Name to be imported</param>
  399. /// <returns>Status whether the file is duplicate or not</returns>
  400. private bool CheckDuplicateFile(string file)
  401. {
  402. FileInfo loadFileInfo = new FileInfo(file);
  403. if (this.fileInfo != null && this.fileInfo.Count > 0)
  404. {
  405. foreach (FileInfo info in this.fileInfo)
  406. {
  407. if (info.FullName == loadFileInfo.FullName || info.LastWriteTime == loadFileInfo.LastWriteTime)
  408. {
  409. return false;
  410. }
  411. }
  412. }
  413. return true;
  414. }
  415. /// <summary>
  416. /// Handles the selection changed on the Combo box to select Molecule type.
  417. /// This would set the molecule type to be used while importingthe given files.
  418. /// </summary>
  419. /// <param name="sender">Framework element</param>
  420. /// <param name="e">selection changed event args</param>
  421. private void ComboMoleculeTypeSelectionChanged(object sender, SelectionChangedEventArgs e)
  422. {
  423. switch (comboMoleculeType.SelectedIndex)
  424. {
  425. case 0: this.molecule = null;
  426. break;
  427. default:
  428. foreach (IAlphabet alpha in Alphabets.All)
  429. {
  430. if (comboMoleculeType.SelectedValue != null && alpha.Name.Equals(comboMoleculeType.SelectedValue.ToString()))
  431. {
  432. this.molecule = alpha;
  433. break;
  434. }
  435. }
  436. break;
  437. }
  438. }
  439. /// <summary>
  440. /// Handles the selection changed on the Combo box to select Parser type.
  441. /// This would set the Parser type to be used while parsing the given files.
  442. /// </summary>
  443. /// <param name="sender">Framework element</param>
  444. /// <param name="e">selection changed event args</param>
  445. private void OnParserSelectionChanged(object sender, SelectionChangedEventArgs e)
  446. {
  447. if (this.cmbParserType.SelectedIndex == 0)
  448. {
  449. this.btnImport.IsEnabled = false;
  450. this.selectedParserName = null;
  451. }
  452. else
  453. {
  454. this.btnImport.IsEnabled = true;
  455. ComboBoxItem item = this.cmbParserType.SelectedItem as ComboBoxItem;
  456. this.selectedParserName = item.Content as string;
  457. }
  458. }
  459. #endregion
  460. }
  461. }