private initializeObjectExplorer()

in src/controllers/mainController.ts [295:442]


	private initializeObjectExplorer(): void {
		const self = this;
		// Register the object explorer tree provider
		this._objectExplorerProvider = new ObjectExplorerProvider(this._connectionMgr);
		const treeView = vscode.window.createTreeView('objectExplorer', {
			treeDataProvider: this._objectExplorerProvider,
			canSelectMany: false
		});
		this._context.subscriptions.push(treeView);

		// Sets the correct current node on any node selection
		this._context.subscriptions.push(treeView.onDidChangeSelection((e: vscode.TreeViewSelectionChangeEvent<TreeNodeInfo>) => {
			const selections: TreeNodeInfo[] = e.selection;
			if (selections && selections.length > 0) {
				self._objectExplorerProvider.currentNode = selections[0];
			}
		}));

		// Add Object Explorer Node
		this.registerCommand(Constants.cmdAddObjectExplorer);
		this._event.on(Constants.cmdAddObjectExplorer, async () => {
			if (!self._objectExplorerProvider.objectExplorerExists) {
				self._objectExplorerProvider.objectExplorerExists = true;
			}
			await self.createObjectExplorerSession();
		});

		// Object Explorer New Query
		this._context.subscriptions.push(
			vscode.commands.registerCommand(
				Constants.cmdObjectExplorerNewQuery, async (treeNodeInfo: TreeNodeInfo) => {
					const connectionCredentials = Object.assign({}, treeNodeInfo.connectionInfo);
					const databaseName = ObjectExplorerUtils.getDatabaseName(treeNodeInfo);
					if (databaseName !== connectionCredentials.database &&
						databaseName !== LocalizedConstants.defaultDatabaseLabel) {
						connectionCredentials.database = databaseName;
					} else if (databaseName === LocalizedConstants.defaultDatabaseLabel) {
						connectionCredentials.database = '';
					}
					treeNodeInfo.connectionInfo = connectionCredentials;
					await self.onNewQuery(treeNodeInfo);
				}));

		// Remove Object Explorer Node
		this._context.subscriptions.push(
			vscode.commands.registerCommand(
				Constants.cmdRemoveObjectExplorerNode, async (treeNodeInfo: TreeNodeInfo) => {
					await this._objectExplorerProvider.removeObjectExplorerNode(treeNodeInfo);
					let profile = <IConnectionProfile>treeNodeInfo.connectionInfo;
					await this._connectionMgr.connectionStore.removeProfile(profile, false);
					return this._objectExplorerProvider.refresh(undefined);
				}));

		// Refresh Object Explorer Node
		this._context.subscriptions.push(
			vscode.commands.registerCommand(
				Constants.cmdRefreshObjectExplorerNode, async (treeNodeInfo: TreeNodeInfo) => {
					await this._objectExplorerProvider.refreshNode(treeNodeInfo);
				}));

		// Sign In into Object Explorer Node
		this._context.subscriptions.push(
			vscode.commands.registerCommand(
				Constants.cmdObjectExplorerNodeSignIn, async (node: AccountSignInTreeNode) => {
					let profile = <IConnectionProfile>node.parentNode.connectionInfo;
					profile = await self.connectionManager.connectionUI.promptForRetryCreateProfile(profile);
					if (profile) {
						node.parentNode.connectionInfo = <IConnectionInfo>profile;
						self._objectExplorerProvider.updateNode(node.parentNode);
						self._objectExplorerProvider.signInNodeServer(node.parentNode);
						return self._objectExplorerProvider.refresh(undefined);
					}
				}));

		// Connect to Object Explorer Node
		this._context.subscriptions.push(
			vscode.commands.registerCommand(
				Constants.cmdConnectObjectExplorerNode, async (node: ConnectTreeNode) => {
					await self.createObjectExplorerSession(node.parentNode.connectionInfo);
				}));

		// Disconnect Object Explorer Node
		this._context.subscriptions.push(
			vscode.commands.registerCommand(
				Constants.cmdDisconnectObjectExplorerNode, async (node: TreeNodeInfo) => {
					await this._objectExplorerProvider.removeObjectExplorerNode(node, true);
					return this._objectExplorerProvider.refresh(undefined);
				}));

		// Initiate the scripting service
		this._scriptingService = new ScriptingService(this._connectionMgr);

		// Script as Select
		this._context.subscriptions.push(
			vscode.commands.registerCommand(
				Constants.cmdScriptSelect, async (node: TreeNodeInfo) => {
					await this.scriptNode(node, ScriptOperation.Select, true);
				}));

		// Script as Create
		this._context.subscriptions.push(
			vscode.commands.registerCommand(
				Constants.cmdScriptCreate, async (node: TreeNodeInfo) =>
				await this.scriptNode(node, ScriptOperation.Create)));

		// Script as Drop
		this._context.subscriptions.push(
			vscode.commands.registerCommand(
				Constants.cmdScriptDelete, async (node: TreeNodeInfo) =>
				await this.scriptNode(node, ScriptOperation.Delete)));

		// Script as Execute
		this._context.subscriptions.push(
			vscode.commands.registerCommand(
				Constants.cmdScriptExecute, async (node: TreeNodeInfo) =>
				await this.scriptNode(node, ScriptOperation.Execute)));

		// Script as Alter
		this._context.subscriptions.push(
			vscode.commands.registerCommand(
				Constants.cmdScriptAlter, async (node: TreeNodeInfo) =>
				await this.scriptNode(node, ScriptOperation.Alter)));

		// Copy object name command
		this._context.subscriptions.push(
			vscode.commands.registerCommand(Constants.cmdCopyObjectName, async () => {
				let node = this._objectExplorerProvider.currentNode;
				// Folder node
				if (node.contextValue === Constants.folderLabel) {
					return;
				} else if (node.contextValue === Constants.serverLabel ||
					node.contextValue === Constants.disconnectedServerLabel) {
					const label = typeof node.label === 'string' ? node.label : node.label.label;
					await this._vscodeWrapper.clipboardWriteText(label);
				} else {
					let scriptingObject = this._scriptingService.getObjectFromNode(node);
					const escapedName = Utils.escapeClosingBrackets(scriptingObject.name);
					if (scriptingObject.schema) {
						let database = ObjectExplorerUtils.getDatabaseName(node);
						const databaseName = Utils.escapeClosingBrackets(database);
						const escapedSchema = Utils.escapeClosingBrackets(scriptingObject.schema);
						await this._vscodeWrapper.clipboardWriteText(`[${databaseName}].${escapedSchema}.[${escapedName}]`);
					} else {
						await this._vscodeWrapper.clipboardWriteText(`[${escapedName}]`);
					}
				}
			}));
	}