Ejemplo n.º 1
0
        private void saveMenuItem_Click(object sender, RoutedEventArgs e)
        {
            ComboBoxItem selectedLanguage = (ComboBoxItem)languageComboBox.SelectedItem;

            SnippetXML xml = new SnippetXML(new HeaderInfo().SetHeaderData(
                                                titleTextBox.Text,
                                                authorTextBox.Text,
                                                descriptionTextBox.Text,
                                                GetSelectedRadioButton()
                                                ),
                                            new SnippetInfo()
                                            .SetDeclarations(literalsDataGrid.ItemsSource.Cast <Literal>())
                                            .SetCode(GetRichTextBoxText())
                                            .SetLanguage(selectedLanguage.Tag.ToString())
                                            );

            try {
                var dialog = new Microsoft.Win32.SaveFileDialog();
                dialog.DefaultExt = ".snippet";
                dialog.Filter     = "Code Snippets (.snippet)|*.snippet";

                if (dialog.ShowDialog() == true)
                {
                    xml.Xml.Save(dialog.FileName);
                }
            }
            catch (Exception ex) {
                MessageBox.Show("Error saving file:" + Environment.NewLine + ex.Message.ToString());
            }
        }
Ejemplo n.º 2
0
        private void openMenuItem_Click(object sender, RoutedEventArgs e)
        {
            //Open the XML Document, and attempt to populate the GUI with its values
            try {
                var dialog = new Microsoft.Win32.OpenFileDialog {
                    Filter = "Code Snippets (.snippet)|*.snippet"
                };
                if (dialog.ShowDialog() == true)
                {
                    SnippetXML xdoc       = new SnippetXML(XDocument.Load(dialog.FileName));
                    HeaderInfo headerInfo = xdoc.GetHeaderDataFromFile();
                    titleTextBox.Text       = headerInfo.Title;
                    authorTextBox.Text      = headerInfo.Author;
                    descriptionTextBox.Text = headerInfo.Description;
                    if (GetSelectedRadioButton() == "Expansion")
                    {
                        expansionRadioButton.IsChecked = true;
                    }
                    else
                    {
                        surroundsWithRadioButton.IsChecked = true;
                    }

                    SnippetInfo snippetInfo = xdoc.GetSnippetInfoFromFile();

                    foreach (ComboBoxItem item in languageComboBox.Items)
                    {
                        if (item.Tag.ToString() == snippetInfo.Language)
                        {
                            languageComboBox.SelectedItem = item;
                            break;
                        }
                    }

                    codeRichTextBox.Document.Blocks.Clear();
                    codeRichTextBox.Document.Blocks.Add(new Paragraph(new Run(snippetInfo.Code)));
                    literalsDataGrid.ItemsSource = snippetInfo.Literals;
                }
            }
            catch (System.IO.IOException ex) {
                MessageBox.Show("Error loading file:" + Environment.NewLine + ex.Message.ToString());
            }
            catch (System.Xml.XmlException ex) {
                MessageBox.Show("Error parsing XML in file. " + Environment.NewLine + ex.Message.ToString());
            }
            catch (NullReferenceException ex) {
                MessageBox.Show("There was a problem locating the necessary elements within the XML." + Environment.NewLine + ex.Message.ToString());
            }
            catch (Exception ex) {
                MessageBox.Show("An Unknown Error occurred." + Environment.NewLine + ex.Message.ToString());
            }
        }