Fixed #4567 adding open connectionDialog when no connection is available (#5581)

* Fixed #4567 adding open connectionDialog when no connection is available
This commit is contained in:
Yurong He
2019-05-23 15:11:10 -07:00
committed by GitHub
parent 34ca0e8671
commit 776e2cf6e7
5 changed files with 65 additions and 44 deletions

View File

@@ -217,8 +217,10 @@ export class CellModel implements ICellModel {
} else {
// TODO update source based on editor component contents
if (kernel.requiresConnection && !this.notebookModel.activeConnection) {
this.sendNotification(notificationService, Severity.Error, localize('kernelRequiresConnection', "Please select a connection to run cells for this kernel"));
return false;
let connected = await this.notebookModel.requestConnection();
if (!connected) {
return false;
}
}
let content = this.source;
if (content) {

View File

@@ -16,7 +16,7 @@ import * as notebookUtils from '../notebookUtils';
import { INotebookManager, SQL_NOTEBOOK_PROVIDER, DEFAULT_NOTEBOOK_PROVIDER } from 'sql/workbench/services/notebook/common/notebookService';
import { NotebookContexts } from 'sql/workbench/parts/notebook/models/notebookContexts';
import { IConnectionProfile } from 'sql/platform/connection/common/interfaces';
import { INotification, Severity } from 'vs/platform/notification/common/notification';
import { INotification, Severity, INotificationService } from 'vs/platform/notification/common/notification';
import { URI } from 'vs/base/common/uri';
import { ISingleNotebookEditOperation } from 'sql/workbench/api/common/sqlExtHostTypes';
import { ConnectionProfile } from 'sql/platform/connection/common/connectionProfile';
@@ -72,11 +72,13 @@ export class NotebookModel extends Disposable implements INotebookModel {
private _oldKernel: nb.IKernel;
private _clientSessionListeners: IDisposable[] = [];
private _connectionUrisToDispose: string[] = [];
public requestConnectionHandler: () => Promise<boolean>;
constructor(
private _notebookOptions: INotebookModelOptions,
public connectionProfile: IConnectionProfile | undefined,
@ILogService private readonly logService: ILogService
@ILogService private readonly logService: ILogService,
@INotificationService private readonly notificationService: INotificationService
) {
super();
if (!_notebookOptions || !_notebookOptions.notebookUri || !_notebookOptions.notebookManagers) {
@@ -301,6 +303,15 @@ export class NotebookModel extends Disposable implements INotebookModel {
}
}
public async requestConnection(): Promise<boolean> {
if (this.requestConnectionHandler) {
return this.requestConnectionHandler();
} else if (this.notificationService) {
this.notificationService.notify({ severity: Severity.Error, message: localize('kernelRequiresConnection', "Please select a connection to run cells for this kernel") });
}
return false;
}
public findCellIndex(cellModel: ICellModel): number {
return this._cells.findIndex((cell) => cell.equals(cellModel));
}

View File

@@ -279,7 +279,7 @@ export class NotebookComponent extends AngularDisposable implements OnInit, OnDe
defaultKernel: this._notebookParams.input.defaultKernel,
layoutChanged: this._notebookParams.input.layoutChanged,
capabilitiesService: this.capabilitiesService
}, this.profile, this.logService);
}, this.profile, this.logService, this.notificationService);
model.onError((errInfo: INotification) => this.handleModelError(errInfo));
let trusted = await this.notebookService.isNotebookTrustCached(this._notebookParams.notebookUri, this.isDirty());
await model.requestModelLoad(trusted);

View File

@@ -350,6 +350,7 @@ export class AttachToDropdown extends SelectBox {
this._register(this.model.contextsLoading(() => {
this.setOptions([msgLoadingContexts], 0);
}));
this.model.requestConnectionHandler = () => this.openConnectionDialog(true);
this.handleContextsChanged();
}
@@ -489,46 +490,53 @@ export class AttachToDropdown extends SelectBox {
* Bind the server value to 'Attach To' drop down
* Connected server is displayed at the top of drop down
**/
public async openConnectionDialog(useProfile: boolean = false): Promise<void> {
public async openConnectionDialog(useProfile: boolean = false): Promise<boolean> {
try {
await this._connectionDialogService.openDialogAndWait(this._connectionManagementService, { connectionType: 1, providers: this.model.getApplicableConnectionProviderIds(this.model.clientSession.kernel.name) }, useProfile ? this.model.connectionProfile : undefined).then(connection => {
let attachToConnections = this.values;
if (!connection) {
this.loadAttachToDropdown(this.model, this.getKernelDisplayName());
this.doChangeContext(undefined, true);
return;
}
let connectionUri = this._connectionManagementService.getConnectionUri(connection);
let connectionProfile = new ConnectionProfile(this._capabilitiesService, connection);
let connectedServer = connectionProfile.title ? connectionProfile.title : connectionProfile.serverName;
//Check to see if the same server is already there in dropdown. We only have server names in dropdown
if (attachToConnections.some(val => val === connectedServer)) {
this.loadAttachToDropdown(this.model, this.getKernelDisplayName());
this.doChangeContext();
return;
}
else {
attachToConnections.unshift(connectedServer);
}
//To ignore n/a after we have at least one valid connection
attachToConnections = attachToConnections.filter(val => val !== msgSelectConnection);
let connection = await this._connectionDialogService.openDialogAndWait(this._connectionManagementService,
{
connectionType: 1,
providers: this.model.getApplicableConnectionProviderIds(this.model.clientSession.kernel.name)
},
useProfile ? this.model.connectionProfile : undefined);
let index = attachToConnections.findIndex((connection => connection === connectedServer));
this.setOptions([]);
this.setOptions(attachToConnections);
if (!index || index < 0 || index >= attachToConnections.length) {
index = 0;
}
this.select(index);
let attachToConnections = this.values;
if (!connection) {
this.loadAttachToDropdown(this.model, this.getKernelDisplayName());
this.doChangeContext(undefined, true);
return false;
}
let connectionUri = this._connectionManagementService.getConnectionUri(connection);
let connectionProfile = new ConnectionProfile(this._capabilitiesService, connection);
let connectedServer = connectionProfile.title ? connectionProfile.title : connectionProfile.serverName;
//Check to see if the same server is already there in dropdown. We only have server names in dropdown
if (attachToConnections.some(val => val === connectedServer)) {
this.loadAttachToDropdown(this.model, this.getKernelDisplayName());
this.doChangeContext();
return true;
}
else {
attachToConnections.unshift(connectedServer);
}
//To ignore n/a after we have at least one valid connection
attachToConnections = attachToConnections.filter(val => val !== msgSelectConnection);
this.model.addAttachToConnectionsToBeDisposed(connectionUri);
// Call doChangeContext to set the newly chosen connection in the model
this.doChangeContext(connectionProfile);
});
let index = attachToConnections.findIndex((connection => connection === connectedServer));
this.setOptions([]);
this.setOptions(attachToConnections);
if (!index || index < 0 || index >= attachToConnections.length) {
index = 0;
}
this.select(index);
this.model.addAttachToConnectionsToBeDisposed(connectionUri);
// Call doChangeContext to set the newly chosen connection in the model
this.doChangeContext(connectionProfile);
return true;
}
catch (error) {
const actions: INotificationActions = { primary: [] };
this._notificationService.notify({ severity: Severity.Error, message: getErrorMessage(error), actions });
return false;
}
}
}