public static void EnsureSequenceIsRemembered(string key, HapticSequence sequence) { key = HapticResources.CleanName(key); if (!LoadedSequences.ContainsKey(key) && sequence != null) { LoadedSequences.Add(key, new SequenceImportData(sequence, key)); } }
/// <summary> /// This method loads new sequences from a file. /// </summary> private void OnLoadFile() { string filterString = "All Supported Formats|" + string.Join(";", SequenceParsers.All.Select(parser => parser.SupportedFileTypes.Replace(',', ';').Replace(".", "*."))) + "|" + string.Join("|", SequenceParsers.All.Select(parser => string.Format("{0}|{1}", parser.Name, parser.SupportedFileTypes.Replace(',', ';').Replace(".", "*.")))); OpenFileDialog openFileDialog = new OpenFileDialog { CheckFileExists = true, Filter = filterString }; // Prompt the user for the filename if (openFileDialog.ShowDialog() == true) { // See if we can auto-locate the parser ISequenceParser parser = SequenceParsers.FindParserByFileName(openFileDialog.FileName); if (parser == null) { // Use the extension string fileExtension = Path.GetExtension(openFileDialog.FileName); parser = SequenceParsers.All.FirstOrDefault(sp => sp.SupportedFileTypes.Contains(fileExtension)); } // Cannot parse this file. if (parser == null) { MessageBox.Show(string.Format("Cannot locate a sequence parser for {0}", openFileDialog.FileName), "Cannot Parse File"); return; } // Parse the file - open it read-only as we will not be writing the sequences back out. try { foreach (var sequence in parser.Parse()) { LoadedSequences.Add(new SequenceViewModel(this, sequence)); } } catch (Exception ex) { ShowError("Cannot Parse File", "Failed to open " + openFileDialog.FileName, ex); } finally { parser.Close(); } } }