Information of a *.sql file: -FullName -DisplayName -isChanged -TabPage -SqlTabPages
Inheritance: IDisposable
Esempio n. 1
0
        /// <summary>
        /// Event TextChanged
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void textBoxUndo_TextChanged(object sender, EventArgs e)
        {
            SqlFile sqlFile = (SqlFile)_tabPage.Tag;

            sqlFile.IsChanged = true;
            _tabPage.Text     = sqlFile.DisplayName;
            if (!_undo)
            {
                _lastData.RemoveRange(_lastData.Count - _undoCount, _undoCount);
                _lastData.Add(new Item(Text, SelectionStart));
                _undoCount = 0;
                if (UndoLimit != 0 && UndoLimit + 1 < _lastData.Count)
                {
                    _lastData.RemoveAt(0);
                }
            }
        }
        /// <summary>
        /// Add an Tab to the tab control and load the Element Template as default. The Text box is unchanged because it's just a template.
        /// </summary>
        /// <returns></returns>
        public TabPage AddTab()
        {
            // create a new TabPage in TabControl
            TabPage tabPage = new TabPage();

            // Create a new empty file
            SqlFile sqlFile = new SqlFile(this, $"{DefaultTabName}{_tabControl.Controls.Count}.sql", isChanged:false);
            tabPage.Tag = sqlFile;
            tabPage.Text = sqlFile.DisplayName;
            tabPage.ToolTipText = sqlFile.FullName;


            // add tab to TabControl
            _tabControl.Controls.Add(tabPage);
            _tabControl.SelectTab(tabPage);




            //-----------------------------------------------------------------
            // Tab with ContextMenuStrip
            // Create a text box in TabPage for the SQL string
            var sqlTextBox = new TextBoxUndo(tabPage)
            {
                Text = SqlTemplates.GetTemplateText(SqlTemplates.SqlTemplateId.ElementTemplate)
            };
            // load element template
            sqlFile.IsChanged = false;
            tabPage.Text = sqlFile.DisplayName;  // after isChanged = false

            // register CTRL+S (store SQL) and CTRL+R (run SQL)
            //sqlTextBox.KeyUp += sqlTextBox_KeyUp;
            //ToolTip toolTip = new ToolTip
            //{
            //    IsBalloon = false,
            //    InitialDelay = 0,
            //    ShowAlways = true
            //};
            //toolTip.SetToolTip(sqlTextBox, SqlTextBoxTooltip);
            tabPage.Controls.Add(sqlTextBox);



            // Load sql File into TabPage
            ToolStripMenuItem loadWithFileDialogMenuItem = new ToolStripMenuItem
            {
                Text = MenuLoadTabFileText,
                ToolTipText = MenuLoadTabFileTooltip
            };
            loadWithFileDialogMenuItem.Click += fileLoadMenuItem_Click;

            // ReLoad sql File
            ToolStripMenuItem reLoadTabMenuItem = new ToolStripMenuItem
            {
                Text = MenuReLoadTabText,
                ToolTipText = MenuReLoadTabTooltip
            };
            reLoadTabMenuItem.Click += reLoadTabMenuItem_Click;


            

            // New TabPage
            ToolStripMenuItem newTabMenuItem = new ToolStripMenuItem
            {
                Text = MenuNewTabText,
                ToolTipText = MenuNewTabTooltip
            };
            newTabMenuItem.Click += addTabMenuItem_Click;

            // New Tab and Load File via Dialog
            ToolStripMenuItem newTabWithFileDialogMenuItem = new ToolStripMenuItem
            {
                Text = MenuNewTabWithFileDialogText,
                ToolTipText = MenuNewTabWithFileDialogTooltip
            };
            newTabWithFileDialogMenuItem.Click += addTabFileDialogMenuItem_Click;


            // Save sql File from TabPage
            ToolStripMenuItem fileSaveMenuItem = new ToolStripMenuItem
            {
                Text = @"Save File (CTRL+S)"
            };
            fileSaveMenuItem.Click += fileSaveMenuItem_Click;

            // Save all sql files 
            ToolStripMenuItem fileSaveAllMenuItem = new ToolStripMenuItem { Text = @"Save All File (CTRL+SHFT+S)" };
            fileSaveAllMenuItem.Click += fileSaveAllMenuItem_Click;

            // Save As sql File from TabPage
            ToolStripMenuItem fileSaveAsMenuItem = new ToolStripMenuItem { Text = @"Save File As.." };
            fileSaveAsMenuItem.Click += fileSaveAsMenuItem_Click;



            // Close TabPage
            ToolStripMenuItem closeMenuItem = new ToolStripMenuItem {Text = @"Close Tab"};
            closeMenuItem.Click += closeMenuItem_Click;

            // Run sql File 
            ToolStripMenuItem fileRunMenuItem = new ToolStripMenuItem {Text = @"Run sql (CTRL+R)"};
            fileRunMenuItem.Click += fileRunMenuItem_Click;


            // Run selected sql 
            ToolStripMenuItem fileRunSelectedMenuItem = new ToolStripMenuItem
            {
                Text = @"Run selected string as sql",
                ToolTipText = @"Run selected string as SQL
1.  Select SQL part you want to run
2.  Run selected string as SQL

Useful to quickly test:
- Select of a UNION
- Only the a part of a long SQL 
"
            };

            fileRunSelectedMenuItem.Click += fileRunSelectedMenuItem_Click;


            //------------------------------------------------------------------------------------------------------------------
            // Insert Template
            ToolStripMenuItem insertTemplateMenuItem = GetTemplateMenuItems();


            //-----------------------------------------------------------------------------------------------------------------
            // Insert Macro
            ToolStripMenuItem insertMacroMenuItem = GetMacroMenuItems();

            // Load recent files into toolStrip menu
            LoadRecentFilesIntoToolStripItems();


            //----------------------------------------------------------------------------------------------------------
            // ToolStripItem for
            // - TabPage
            // - SQL TextBox
            var toolStripItems = new ToolStripItem[] {
                loadWithFileDialogMenuItem,                   // load Tab from file
                _tabLoadTabFromRecentFileMenuItem,              // load Tab from recent file  
                reLoadTabMenuItem,                 // ReLoad Tab
                new ToolStripSeparator(),
                newTabMenuItem,                     // new Tab
                newTabWithFileDialogMenuItem,       // New Tab with File Dialog
                _tabNewTabFromRecentFileMenuItem,      // new Tab from recent file

                new ToolStripSeparator(),
                insertTemplateMenuItem,             // insert template
                insertMacroMenuItem,                // insert macro
                new ToolStripSeparator(),
                fileRunMenuItem,                    // run sql
                fileRunSelectedMenuItem,            // run sql for selected area
                // run query
                new ToolStripSeparator(),
                fileSaveMenuItem,                   // save query
                fileSaveAsMenuItem,                 // save query as..
                closeMenuItem
                };

            // Context Menu
            ContextMenuStrip contextMenuStrip = new ContextMenuStrip(_components);
            contextMenuStrip.Items.AddRange(toolStripItems);




            // Add ContextMenuStrip to TabControl an TextBox
            sqlTextBox.ContextMenuStrip = contextMenuStrip;
            _tabControl.ContextMenuStrip = contextMenuStrip;
            return tabPage;
        }
        /// <summary>
        /// Load sql string from *.sql File into active TabPage with TextBox inside. 
        /// <para/>- Update and save the list of sql files 
        /// </summary>
        public void LoadTabPagePerFileDialog()
        {
            TabPage tabPage;
            // no tab page exists
            if (_tabControl.TabPages.Count == 0)
            {
                tabPage = AddTab();
            }
            else
            {
                // get TabPage
                if (_tabControl.SelectedIndex < 0) return;
                tabPage = _tabControl.TabPages[_tabControl.SelectedIndex];

                // If contend changed: Store first
                SqlFile sqlFile = (SqlFile)tabPage.Tag;
                if (sqlFile.IsChanged)
                {
                    DialogResult result = MessageBox.Show($"Old File: '{sqlFile.FullName}'",
                        @"First store old File? ", MessageBoxButtons.YesNoCancel);
                    if (result == DialogResult.Cancel) return;
                    if (result == DialogResult.Yes)
                    {
                        Save(tabPage);
                        sqlFile.IsChanged = false;
                        tabPage.ToolTipText = ((SqlFile)tabPage.Tag).FullName;
                    }
                }


            }
            OpenFileDialog openFileDialog = new OpenFileDialog
            {
                InitialDirectory = @"c:\temp\sql",
                RestoreDirectory = true,
                Filter = @"sql files (*.sql)|*.sql|All files (*.*)|*.*",
                FilterIndex = 1
            };


            if (openFileDialog.ShowDialog() == DialogResult.OK)

            {
                string fileName = openFileDialog.FileName;
                if (!string.IsNullOrEmpty(fileName))
                {
                    // get TextBox
                    var textBox = (TextBoxUndo)tabPage.Controls[0];
                    SqlFile sqlFile = new SqlFile(this, fileName, isChanged: false);
                    tabPage.Tag = sqlFile;
                    sqlFile.IsChanged = false;
                    tabPage.Text = sqlFile.DisplayName;
                    textBox.Text = sqlFile.Load();
                    sqlFile.IsChanged = false;

                    // store the complete filename in settings
                    InsertRecentFileLists(openFileDialog.FileName);
                    Settings.Save();

                    // Load recent files into ToolStripMenu
                    LoadRecentFilesIntoToolStripItems();

                }
            }
            // update Tab Caption
            tabPage.ToolTipText = ((SqlFile)tabPage.Tag).FullName;
            tabPage.Text = ((SqlFile)tabPage.Tag).DisplayName;

        }