/// <summary> /// Creates the <see cref="EditConnectionInfo"/> or restores the saved one. /// </summary> /// <param name="mySqlTable">The <see cref="MySqlDataTable"/> used for the Edit Data session.</param> /// <param name="currentWorksheet">The current worksheet.</param> /// <returns>A new or restored <see cref="EditConnectionInfo"/> object.</returns> private EditConnectionInfo GetEditConnectionInfo(MySqlDataTable mySqlTable, ExcelInterop.Worksheet currentWorksheet) { if (mySqlTable == null || currentWorksheet == null) { return(null); } var atCell = currentWorksheet.Range["A1", Type.Missing]; var editingRange = mySqlTable.ImportDataIntoExcelRange(atCell); EditConnectionInfo connectionInfo = null; var workbookEditConnectionInfos = WorkbookConnectionInfos.GetWorkbookEditConnectionInfos(Globals.ThisAddIn.ActiveWorkbook); if (workbookEditConnectionInfos.Count > 0) { connectionInfo = workbookEditConnectionInfos.GetActiveEditConnectionInfo(mySqlTable.TableName); } // The EditConnectionInfo is new and has to be created from scratch. if (connectionInfo == null) { var activeWorkbook = Globals.ThisAddIn.ActiveWorkbook; connectionInfo = new EditConnectionInfo(activeWorkbook.GetOrCreateId(), activeWorkbook.FullName, WbConnection.Id, WbConnection.Schema, mySqlTable.TableName); } if (connectionInfo.EditDialog != null) { return(connectionInfo); } // The EditConnectionInfo is being either restored from the settings file or created for the newborn object. connectionInfo.EditDialog = new EditDataDialog(this, new NativeWindowWrapper(Globals.ThisAddIn.Application.Hwnd), WbConnection, editingRange, mySqlTable, currentWorksheet); currentWorksheet.StoreProtectionKey(connectionInfo.EditDialog.WorksheetProtectionKey); return(connectionInfo); }
/// <summary> /// Checks if there is an Editing Operation active for a table with the given name. /// </summary> /// <param name="tableName">Name of the table.</param> /// <returns><c>true</c> if the table has is in editing mode, <c>false</c> otherwise.</returns> public bool TableHasEditOnGoing(string tableName) { var activeWorkbook = Globals.ThisAddIn.ActiveWorkbook; var activeWorkbookEditConnectionInfos = WorkbookConnectionInfos.GetWorkbookEditConnectionInfos(activeWorkbook); if (activeWorkbookEditConnectionInfos.Count == 0) { return(false); } var editContainer = activeWorkbookEditConnectionInfos.FirstOrDefault(ac => ac.EditDialog != null && ac.TableName == tableName); if (editContainer == null) { return(false); } // Means has an edit ongoing we need to make sure the edit has a valid sheet otherwise we need to release it if (Globals.ThisAddIn.Application.Worksheets.Cast <ExcelInterop.Worksheet>().Contains(editContainer.EditDialog.EditingWorksheet)) { return(true); } editContainer.EditDialog.Close(); return(false); }
/// <summary> /// Closes the current connection, editing dialogs and puts the welcome panel in focus. /// </summary> /// <param name="givePanelFocus">Flag indicating whether the <see cref="WelcomePanel"/> is given focus.</param> public void CloseConnection(bool givePanelFocus) { WbConnection = null; if (givePanelFocus) { WelcomePanel1.BringToFront(); } // Free up open Edit Dialogs WorkbookConnectionInfos.CloseWorkbookEditConnectionInfos(Globals.ThisAddIn.ActiveWorkbook); }
/// <summary> /// Raises the Closing event. /// </summary> /// <param name="e">A <see cref="CancelEventArgs"/> that contains the event data.</param> protected override void OnClosing(CancelEventArgs e) { base.OnClosing(e); _parentTaskPane.RefreshDbObjectPanelActionLabelsEnabledStatus(EditingTableName, false); if (EditingWorksheetExists) { UnprotectWorksheet(); EditingWorksheet.UsedRange.Interior.ColorIndex = ExcelInterop.XlColorIndex.xlColorIndexNone; if (!string.IsNullOrEmpty(EditingWorksheet.GetProtectionKey())) { EditingWorksheet.RemoveProtectionKey(); } } WorkbookConnectionInfos.RemoveEditConnectionInfoWithEditDialog(this); Dispose(); }
/// <summary> /// Closes the current connection, editing tables and puts the schema panel in focus. /// </summary> /// <param name="askToCloseConnections">Flag indicating whether users are asked for confirmation before closing active <see cref="EditConnectionInfo"/>.</param> /// <param name="givePanelFocus">Flag indicating whether the <see cref="SchemaSelectionPanel"/> is given focus.</param> /// <returns><c>true</c> if the schema and its open <see cref="EditConnectionInfo"/> objects are closed, <c>false</c> otherwise.</returns> public bool CloseSchema(bool askToCloseConnections, bool givePanelFocus) { if (askToCloseConnections && WorkbookConnectionInfos.GetWorkbookEditConnectionInfos(Globals.ThisAddIn.ActiveWorkbook).Count > 0) { // If there are Active OldStoredEditConnectionInfos warn the users that by closing the schema the active EditConnectionInfos will be closed. var dr = MiscUtilities.ShowCustomizedWarningDialog(Resources.ActiveEditConnectionInfosCloseWarningTitle, Resources.ActiveEditConnectionInfosCloseWarningDetail); if (dr == DialogResult.No) { return(false); } } WorkbookConnectionInfos.CloseWorkbookEditConnectionInfos(Globals.ThisAddIn.ActiveWorkbook); if (givePanelFocus) { SchemaSelectionPanel2.BringToFront(); } return(true); }
/// <summary> /// Opens an <see cref="EditConnectionInfo"/> for a MySQL table. /// </summary> /// <param name="tableObject">Table to start an editing for.</param> /// <param name="fromSavedConnectionInfo">Flag indicating whether the <see cref="EditConnectionInfo"/> to be opened corresponds.</param> /// <param name="workbook">The workbook.</param> /// <returns><c>true</c> If the export/append action was executed, <c>false</c> otherwise.</returns> public bool EditTableData(DbTable tableObject, bool fromSavedConnectionInfo, ExcelInterop.Workbook workbook) { if (tableObject == null) { return(false); } var schemaAndTableNames = WbConnection.Schema + "." + tableObject.Name; // Check if the current DB object has an edit ongoing if (TableHasEditOnGoing(tableObject.Name)) { // Display an error since there is an ongoing Editing operation and return InfoDialog.ShowDialog(InfoDialogProperties.GetErrorDialogProperties(Resources.TaskPaneEditingNotPossibleTitleText, string.Format(Resources.TableWithOperationOngoingError, schemaAndTableNames))); return(false); } // Preview the table's data in case the user option for that is on if (!fromSavedConnectionInfo && Settings.Default.EditPreviewMySqlData) { using (var previewDataDialog = new PreviewTableViewDialog(tableObject, true)) { if (previewDataDialog.ShowDialog() == DialogResult.Cancel) { return(false); } } } // Check if selected Table has a Primary Key, it it does not we prompt an error and exit since Editing on such table is not permitted if (!WbConnection.TableHasPrimaryKey(tableObject.Name)) { InfoDialog.ShowDialog(InfoDialogProperties.GetErrorDialogProperties(Resources.EditOpenSatusError, Resources.EditOpenSummaryError, Resources.EditOpenDetailsError)); return(false); } // Attempt to Import Data unless the user cancels the import operation var proposedWorksheetName = fromSavedConnectionInfo ? tableObject.Name : Globals.ThisAddIn.ActiveWorkbook.GetWorksheetNameAvoidingDuplicates(tableObject.Name); tableObject.ImportParameters.ForEditDataOperation = true; MySqlDataTable mySqlTable; using (var importForm = new ImportTableViewForm(tableObject, proposedWorksheetName)) { if (importForm.ImportHidingDialog() == DialogResult.Cancel) { return(false); } mySqlTable = importForm.MySqlTable; } if (mySqlTable == null || mySqlTable.Columns.Count == 0) { MiscUtilities.ShowCustomizedErrorDialog(string.Format(Resources.UnableToRetrieveData, tableObject.Name)); return(false); } var activeWorkbookEditConnectionInfos = WorkbookConnectionInfos.GetWorkbookEditConnectionInfos(Globals.ThisAddIn.ActiveWorkbook); // Hide all other open EditDataDialog forms before opening a new one. if (!fromSavedConnectionInfo) { foreach (var connectionInfo in activeWorkbookEditConnectionInfos.Where(connectionInfo => connectionInfo.EditDialog != null && connectionInfo.EditDialog.Visible)) { connectionInfo.EditDialog.Hide(); } } // Create the new Excel Worksheet and import the editing data there var editWorkbook = fromSavedConnectionInfo && workbook != null ? workbook : Globals.ThisAddIn.ActiveWorkbook; var currentWorksheet = fromSavedConnectionInfo && Settings.Default.EditSessionsReuseWorksheets ? editWorkbook.GetOrCreateWorksheet(proposedWorksheetName, true) : editWorkbook.CreateWorksheet(proposedWorksheetName, true); if (currentWorksheet == null) { return(false); } // Clear the contents of the worksheet if we are restoring a saved <see cref="EditConnectionInfo"/> since the user may have input data into it. if (fromSavedConnectionInfo) { currentWorksheet.UsedRange.Clear(); } // Create and show the Edit Data Dialog var editConnectionInfo = GetEditConnectionInfo(mySqlTable, currentWorksheet); ActiveEditDialog = editConnectionInfo.EditDialog; if (fromSavedConnectionInfo) { // If restoring EditConnectionInfo objects we need to create and link their corresponding EditDialog to it. var editConnectionInfoBeingRestored = activeWorkbookEditConnectionInfos.FirstOrDefault(connectionInfo => connectionInfo.TableName.Equals(editConnectionInfo.TableName)); if (editConnectionInfoBeingRestored != null) { editConnectionInfoBeingRestored.EditDialog = editConnectionInfo.EditDialog; } } else { ActiveEditDialog.ShowDialog(); // If not restoring EditConnectionInfo objects we need to add the manually triggered EditConnectionInfo to the list of the active workbook. activeWorkbookEditConnectionInfos.Add(editConnectionInfo); } return(true); }