Esempio n. 1
0
 //Handles a click on File->Open
 private void openFileClick(object sender, RoutedEventArgs e)
 {
     if(IsOpened)
     {
         var openChoice = MessageBox.Show("Are you sure you want to open a new file? Any unsaved changes to the current file will be lost!", "Open a file", MessageBoxButton.OKCancel);
         if(openChoice == MessageBoxResult.Cancel) { return; }
     }
     OpenFileDialog openDialog = new OpenFileDialog();
     openDialog.Filter = "BibTeX files (*.bib)|*.bib|All files (*.*)|*.*"; //http://www.wpf-tutorial.com/dialogs/the-openfiledialog/
     openDialog.ShowDialog();
     if(openDialog.FileName == "") { return; } //https://msdn.microsoft.com/en-us/library/aa984409(v=vs.71).aspx FileName gets set if the user clicks Cancel, too.
     try
     {
         using (StreamReader openFile = new StreamReader(openDialog.FileName)) //As per lecture slides
         {
             BibEntity.entityList.Clear(); //Clear the list, because we're commited to opening a new file now
             string[] itemArray = openFile.ReadToEnd().Split(new char[] {'@'}, StringSplitOptions.RemoveEmptyEntries);
             foreach (string item in itemArray)
             {
                 string entityString = Regex.Match(item, "(BOOK|MASTERSTHESIS|PHDTHESIS|ARTICLE)").Groups[1].Value;
                 string itemTitle = Regex.Match(item, @"title\s*=\s*{(.+)}").Groups[1].Value; //Wanted to write a regex that would catch all Unicode chars (. doesn't) except '}', but I'm not sure if that's possible
                 string itemAuthor = Regex.Match(item, @"author\s*=\s*{(.+)}").Groups[1].Value;
                 int itemYear;
                 try { itemYear = Int32.Parse(Regex.Match(item, @"year\s*=\s*{(.+)}").Groups[1].Value); }
                 catch (Exception exc) { MessageBox.Show("Invalid year! Item \"" + itemTitle + "\" not parsed! " + exc.Message); continue; }
                 DateTime itemEntryDate;
                 try { itemEntryDate = DateTime.Parse(Regex.Match(item, @"timestamp\s*=\s*{(.+)}").Groups[1].Value); } //https://msdn.microsoft.com/en-us/library/1k1skd40(v=vs.110).aspx
                 catch(Exception exc) { MessageBox.Show("Something went wrong! Item \"" + itemTitle + "\" not parsed! " + exc.Message); continue; }
                 string itemUser = Regex.Match(item, @"owner\s*=\s*{(.+)}").Groups[1].Value;
                 BibEntity itemEntity;
                 switch(entityString)
                 {
                     case "BOOK":
                         string itemPublisher = Regex.Match(item, @"publisher\s*=\s*{(.+)}").Groups[1].Value;
                         string itemPublisherAddr = Regex.Match(item, @"address\s*=\s*{(.+)}").Groups[1].Value;
                         itemEntity = new Book(itemTitle, itemAuthor, itemYear, itemPublisher, itemPublisherAddr, EntityType.BOOK, itemEntryDate);
                         itemEntity.User = itemUser;
                         BibEntity.entityList.Add(itemEntity);
                         break;
                     case "MASTERSTHESIS":
                         string itemSchool = Regex.Match(item, @"school\s*=\s*{(.+)}").Groups[1].Value;
                         itemEntity = new Thesis(itemTitle, itemAuthor, itemYear, ThesisType.MASTERSTHESIS, itemSchool, EntityType.THESIS, itemEntryDate);
                         itemEntity.User = itemUser;
                         BibEntity.entityList.Add(itemEntity);
                         break;
                     case "PHDTHESIS":
                         itemSchool = Regex.Match(item, @"school\s*=\s*{(.+)}").Groups[1].Value;
                         itemEntity = new Thesis(itemTitle, itemAuthor, itemYear, ThesisType.PHDTHESIS, itemSchool, EntityType.THESIS, itemEntryDate);
                         itemEntity.User = itemUser;
                         BibEntity.entityList.Add(itemEntity);
                         break;
                     case "ARTICLE":
                         string itemJournal = Regex.Match(item, @"journal\s*=\s*{(.+)}").Groups[1].Value;
                         int itemVolume;
                         try { itemVolume = Int32.Parse(Regex.Match(item, @"volume\s*=\s*{(.+)}").Groups[1].Value); }
                         catch(Exception exc) { MessageBox.Show("Invalid volume! Item \"" + itemTitle + "\" not parsed! " + exc.Message); continue; }
                         itemEntity = new Article(itemTitle, itemAuthor, itemYear, itemJournal, itemVolume, EntityType.ARTICLE, itemEntryDate);
                         itemEntity.User = itemUser;
                         BibEntity.entityList.Add(itemEntity);
                         break;
                     default:    //In case of an unparsable entity type
                         MessageBox.Show("Unknown entity type! Item \"" + itemTitle + "\" not parsed!");
                         break;
                 }
             }
         }
         entryList.Items.Refresh();
         IsOpened = true;
         setControls(true);
         this.Title = WindowTitle + " (" + openDialog.FileName + ")";
     }
     catch(Exception exc) { MessageBox.Show("Something went wrong! " + exc.Message); return; }
 }
Esempio n. 2
0
 //Handles a click on the OK button
 private void btnOKHandle(object sender, RoutedEventArgs e)
 {
     if(validateFields())
     {
         if (IsNew)
         {
             BibEntity newEntity;
             switch (entryType)
             {
                 case EntityType.BOOK:
                     newEntity = new Book(titleField.Text, authorField.Text, parsedYear,
                                          publisherField.Text, publisherAddrField.Text, EntityType.BOOK,
                                          new DateTime(System.DateTime.Now.Year, System.DateTime.Now.Month, System.DateTime.Now.Day));
                     BibEntity.entityList.Add(newEntity);
                     break;
                 case EntityType.THESIS:
                     newEntity = new Thesis(titleField.Text, authorField.Text, parsedYear, //v--- I love this cast within a cast
                                            (ThesisType)((ComboBoxItem)thesisTypeBox.SelectedItem).Tag, schoolField.Text, EntityType.THESIS,
                                            new DateTime(System.DateTime.Now.Year, System.DateTime.Now.Month, System.DateTime.Now.Day));
                     BibEntity.entityList.Add(newEntity);
                     break;
                 case EntityType.ARTICLE:
                     newEntity = new Article(titleField.Text, authorField.Text, parsedYear,
                                             journalField.Text, parsedVolume, EntityType.ARTICLE,
                                             new DateTime(System.DateTime.Now.Year, System.DateTime.Now.Month, System.DateTime.Now.Day));
                     BibEntity.entityList.Add(newEntity);
                     break;
             }
         } else
         {
             editEntity.Title = titleField.Text;
             editEntity.Authors = new Author(authorField.Text);
             editEntity.Year = parsedYear;
             switch(entryType)
             {
                 case EntityType.BOOK:
                     editEntity.Publisher = publisherField.Text;
                     editEntity.PublisherAddr = publisherAddrField.Text;
                     break;
                 case EntityType.THESIS:
                     editEntity.ThesisEntityType = (ThesisType)((ComboBoxItem)thesisTypeBox.SelectedItem).Tag; //This cast is so awesome
                     editEntity.School = schoolField.Text;
                     break;
                 case EntityType.ARTICLE:
                     editEntity.Journal = journalField.Text;
                     editEntity.Volume = parsedVolume;
                     break;
             }
         }
         this.Close();
     }
 }