Exemplo n.º 1
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var name = (sender as Control).Name;

            if (sender == ButtonOk)
            {
                var parser = new TableParser();

                if (TableMode == "Grid Table")
                {
                    TableHtml = parser.ToGridTableMarkdown(TableData);
                }
                else if (TableMode == "HTML Table")
                {
                    TableHtml = parser.ToTableHtml(TableData);
                }
                else
                {
                    TableHtml = parser.ToPipeTableMarkdown(TableData);
                }

                Cancelled    = false;
                DialogResult = true;
                Close();
            }
            else if (sender == ButtonCancel)
            {
                Cancelled = true;
                Close();
            }
            else if (sender == ButtonPasteHtml)
            {
                CreateTableFromClipboardHtml();
            }
            else if (sender == ButtonImportCsv)
            {
                var form = new TableEditorCsvImport();
                form.Owner = this;
                form.ShowDialog();

                if (form.IsCancelled)
                {
                    return;
                }

                var parser = new TableParser();

                bool   deleteCsvFile = false;
                string csvFile       = form.CsvFilename;

                if (form.ImportFromClipboard)
                {
                    string csvText = ClipboardHelper.GetText();
                    csvFile = Path.GetTempFileName();
                    csvFile = Path.ChangeExtension(csvFile, "csv");
                    File.WriteAllText(csvFile, csvText);
                    deleteCsvFile = true;
                }


                var data = parser.ParseCsvFileToData(csvFile, form.CsvSeparator);
                if (data == null || data.Count < 1)
                {
                    AppModel.Window.ShowStatusError($"Couldn\'t open file {csvFile} or the file is empty.");
                    return;
                }

                TableData = data;
                DataGridTableEditor.TableSource = TableData;

                if (deleteCsvFile)
                {
                    File.Delete(csvFile);
                }
            }

            var focusedTextBox = FocusManager.GetFocusedElement(this) as TextBox;

            if (focusedTextBox == null)
            {
                return;
            }

            if ("MenuInsertColumnRight" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.AddColumn(pos.Row, pos.Column, ColumnInsertLocation.Right);
            }
            else if ("MenuInsertColumnLeft" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.AddColumn(pos.Row, pos.Column, ColumnInsertLocation.Left);
            }
            else if ("MenuDeleteColumn" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.DeleteColumn(pos.Row, pos.Column);
            }
            else if ("MenuInsertRowBelow" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.AddRow(pos.Row, pos.Column, RowInsertLocation.Below);
            }
            else if ("MenuInsertRowAbove" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.AddRow(pos.Row, pos.Column, RowInsertLocation.Above);
            }
            else if ("MenuDeleteRow" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.DeleteRow(pos.Row, pos.Column);
            }
        }
Exemplo n.º 2
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var name = (sender as Control).Name;

            if (sender == ButtonOk)
            {
                var parser = new TableParser();

                if (TableMode == "Grid Table")
                {
                    TableHtml = parser.ToGridTableMarkdown(TableData);
                }
                else if (TableMode == "HTML Table")
                {
                    TableHtml = parser.ToTableHtml(TableData);
                }
                else
                {
                    TableHtml = parser.ToPipeTableMarkdown(TableData);
                }

                Cancelled    = false;
                DialogResult = true;
                Close();
            }
            else if (sender == ButtonCancel)
            {
                Cancelled = true;
                Close();
            }
            else if (sender == ButtonPasteHtml)
            {
                CreateTableFromClipboardHtml();
            }
            else if (sender == ButtonImportCsv)
            {
                var fd = new OpenFileDialog
                {
                    DefaultExt = ".csv",
                    Filter     = "Csv files (*.csv)|*.csv|" +
                                 "All files (*.*)|*.*",
                    CheckFileExists  = true,
                    RestoreDirectory = true,
                    Multiselect      = false,
                    Title            = "Open CsvFile"
                };
                if (!string.IsNullOrEmpty(mmApp.Configuration.LastFolder))
                {
                    fd.InitialDirectory = mmApp.Configuration.LastFolder;
                }

                var res = fd.ShowDialog();
                if (res == null || !res.Value)
                {
                    return;
                }

                var parser = new TableParser();
                var data   = parser.ParseCsvFileToData(fd.FileName);
                if (data == null || data.Count < 1)
                {
                    AppModel.Window.ShowStatusError($"Couldn\'t open file {fd.FileName} or the file is empty.");
                    return;
                }

                TableData = data;
                DataGridTableEditor.TableSource = TableData;
            }

            var focusedTextBox = FocusManager.GetFocusedElement(this) as TextBox;

            if (focusedTextBox == null)
            {
                return;
            }

            if ("MenuInsertColumnRight" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.AddColumn(pos.Row, pos.Column, ColumnInsertLocation.Right);
            }
            else if ("MenuInsertColumnLeft" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.AddColumn(pos.Row, pos.Column, ColumnInsertLocation.Left);
            }
            else if ("MenuDeleteColumn" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.DeleteColumn(pos.Row, pos.Column);
            }
            else if ("MenuInsertRowBelow" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.AddRow(pos.Row, pos.Column, RowInsertLocation.Below);
            }
            else if ("MenuInsertRowAbove" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.AddRow(pos.Row, pos.Column, RowInsertLocation.Above);
            }
            else if ("MenuDeleteRow" == name)
            {
                var pos = focusedTextBox.Tag as TablePosition;
                DataGridTableEditor.DeleteRow(pos.Row, pos.Column);
            }
        }