Add Data Explorer Context and apply to disconnect (#4265)

* adding context

* apply extension changes

* shimming disconnect

* add data explorer context menu and add disconnect to it

* clean up shim code; better handle errors

* remove tpromise

* simplify code

* add node context on data explorer

* formatting

* fix various errors with how the context menus work
This commit is contained in:
Anthony Dresser
2019-03-05 17:09:00 -08:00
committed by GitHub
parent 7eaf8cfd2f
commit da06a96630
7 changed files with 153 additions and 58 deletions

View File

@@ -0,0 +1,73 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { ConnectionContextKey } from 'sql/parts/connection/common/connectionContextKey';
import { IOEShimService } from 'sql/parts/objectExplorer/common/objectExplorerViewTreeShim';
import { ITreeItem } from 'sql/workbench/common/views';
import { Disposable } from 'vs/base/common/lifecycle';
import { IContextKey, IContextKeyService, RawContextKey } from 'vs/platform/contextkey/common/contextkey';
export interface INodeContextValue {
node: ITreeItem;
viewId: string;
}
export class NodeContextKey extends Disposable implements IContextKey<INodeContextValue> {
static IsConnectable = new RawContextKey<boolean>('isConnectable', false);
static IsConnected = new RawContextKey<boolean>('isConnected', false);
static ViewId = new RawContextKey<string>('view', undefined);
static ViewItem = new RawContextKey<string>('viewItem', undefined);
static Node = new RawContextKey<INodeContextValue>('node', undefined);
private readonly _connectionContextKey: ConnectionContextKey;
private readonly _connectableKey: IContextKey<boolean>;
private readonly _connectedKey: IContextKey<boolean>;
private readonly _viewIdKey: IContextKey<string>;
private readonly _viewItemKey: IContextKey<string>;
private readonly _nodeContextKey: IContextKey<INodeContextValue>;
constructor(
@IContextKeyService contextKeyService: IContextKeyService,
@IOEShimService private oeService: IOEShimService
) {
super();
this._connectableKey = NodeContextKey.IsConnectable.bindTo(contextKeyService);
this._connectedKey = NodeContextKey.IsConnected.bindTo(contextKeyService);
this._viewIdKey = NodeContextKey.ViewId.bindTo(contextKeyService);
this._viewItemKey = NodeContextKey.ViewItem.bindTo(contextKeyService);
this._nodeContextKey = NodeContextKey.Node.bindTo(contextKeyService);
this._connectionContextKey = new ConnectionContextKey(contextKeyService);
}
set(value: INodeContextValue) {
if (value.node && value.node.payload) {
this._connectableKey.set(true);
this._connectedKey.set(this.oeService.isNodeConnected(value.viewId, value.node));
this._connectionContextKey.set(value.node.payload);
} else {
this._connectableKey.set(false);
this._connectedKey.set(false);
this._connectionContextKey.reset();
}
this._nodeContextKey.set(value);
this._viewIdKey.set(value.viewId);
this._viewItemKey.set(value.node.contextValue);
}
reset(): void {
this._viewIdKey.reset();
this._viewItemKey.reset();
this._connectableKey.reset();
this._connectedKey.reset();
this._connectionContextKey.reset();
this._nodeContextKey.reset();
}
get(): INodeContextValue | undefined {
return this._nodeContextKey.get();
}
}

View File

@@ -0,0 +1,19 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { NodeContextKey } from 'sql/workbench/parts/dataExplorer/common/nodeContext';
import { localize } from 'vs/nls';
import { MenuId, MenuRegistry } from 'vs/platform/actions/common/actions';
import { DISCONNECT_COMMAND_ID } from './nodeCommands';
MenuRegistry.appendMenuItem(MenuId.DataExplorerContext, {
group: 'connection',
order: 4,
command: {
id: DISCONNECT_COMMAND_ID,
title: localize('disconnect', 'Disconnect')
},
when: NodeContextKey.IsConnected
});

View File

@@ -0,0 +1,26 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { IOEShimService } from 'sql/parts/objectExplorer/common/objectExplorerViewTreeShim';
import { ICustomViewDescriptor, TreeViewItemHandleArg } from 'sql/workbench/common/views';
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
import { ViewsRegistry } from 'vs/workbench/common/views';
export const DISCONNECT_COMMAND_ID = 'dataExplorer.disconnect';
CommandsRegistry.registerCommand({
id: DISCONNECT_COMMAND_ID,
handler: (accessor, args: TreeViewItemHandleArg) => {
if (args.$treeItem) {
const oeService = accessor.get(IOEShimService);
return oeService.disconnectNode(args.$treeViewId, args.$treeItem).then(() => {
const { treeView } = (<ICustomViewDescriptor>ViewsRegistry.getView(args.$treeViewId));
// we need to collapse it then refresh it so that the tree doesn't try and use it's cache next time the user expands the node
return treeView.collapse(args.$treeItem).then(() => treeView.refresh([args.$treeItem]).then(() => true));
});
}
return Promise.resolve(true);
}
});