Enable basic wizard API (#1450)

This commit is contained in:
Matt Irvine
2018-05-21 15:19:21 -07:00
committed by GitHub
parent 70819252a9
commit 8e234d9b2d
16 changed files with 1247 additions and 157 deletions

View File

@@ -15,13 +15,24 @@ import * as sqlops from 'sqlops';
import { SqlMainContext, ExtHostModelViewDialogShape, MainThreadModelViewDialogShape, ExtHostModelViewShape } from 'sql/workbench/api/node/sqlExtHost.protocol';
import { IItemConfig, ModelComponentTypes, IComponentShape } from 'sql/workbench/api/common/sqlExtHostTypes';
const DONE_LABEL = nls.localize('dialogDoneLabel', 'Done');
const CANCEL_LABEL = nls.localize('dialogCancelLabel', 'Cancel');
const GENERATE_SCRIPT_LABEL = nls.localize('generateScriptLabel', 'Generate script');
const NEXT_LABEL = nls.localize('dialogNextLabel', 'Next');
const PREVIOUS_LABEL = nls.localize('dialogPreviousLabel', 'Previous');
class ModelViewPanelImpl implements sqlops.window.modelviewdialog.ModelViewPanel {
private _modelView: sqlops.ModelView;
private _handle: number;
protected _modelViewId: string;
protected _valid: boolean = true;
protected _onValidityChanged: vscode.Event<boolean>;
constructor(private _viewType: string,
protected _extHostModelViewDialog: ExtHostModelViewDialog,
protected _extHostModelView: ExtHostModelViewShape) {
this._onValidityChanged = this._extHostModelViewDialog.getValidityChangedEvent(this);
this._onValidityChanged(valid => this._valid = valid);
}
public registerContent(handler: (view: sqlops.ModelView) => void): void {
@@ -50,15 +61,24 @@ class ModelViewPanelImpl implements sqlops.window.modelviewdialog.ModelViewPanel
public set modelView(value: sqlops.ModelView) {
this._modelView = value;
}
public get valid(): boolean {
return this._valid;
}
public get onValidityChanged(): Event<boolean> {
return this._onValidityChanged;
}
}
class ModelViewEditorImpl extends ModelViewPanelImpl implements sqlops.workspace.ModelViewEditor {
constructor(
extHostModelViewDialog: ExtHostModelViewDialog,
extHostModelView: ExtHostModelViewShape,
private _proxy: MainThreadModelViewDialogShape,
private _title: string
) {
super('modelViewEditor', extHostModelView);
super('modelViewEditor', extHostModelViewDialog, extHostModelView);
}
public openEditor(position?: vscode.ViewColumn): Thenable<void> {
@@ -72,21 +92,12 @@ class DialogImpl extends ModelViewPanelImpl implements sqlops.window.modelviewdi
public okButton: sqlops.window.modelviewdialog.Button;
public cancelButton: sqlops.window.modelviewdialog.Button;
public customButtons: sqlops.window.modelviewdialog.Button[];
public readonly onValidityChanged: vscode.Event<boolean>;
private _valid: boolean = true;
constructor(private _extHostModelViewDialog: ExtHostModelViewDialog,
constructor(extHostModelViewDialog: ExtHostModelViewDialog,
extHostModelView: ExtHostModelViewShape) {
super('modelViewDialog', extHostModelView);
this.okButton = this._extHostModelViewDialog.createButton(nls.localize('dialogOkLabel', 'Done'));
this.cancelButton = this._extHostModelViewDialog.createButton(nls.localize('dialogCancelLabel', 'Cancel'));
this.onValidityChanged = this._extHostModelViewDialog.getValidityChangedEvent(this);
this.onValidityChanged(valid => this._valid = valid);
}
public get valid(): boolean {
return this._valid;
super('modelViewDialog', extHostModelViewDialog, extHostModelView);
this.okButton = this._extHostModelViewDialog.createButton(DONE_LABEL);
this.cancelButton = this._extHostModelViewDialog.createButton(CANCEL_LABEL);
}
public setModelViewId(value: string) {
@@ -97,9 +108,9 @@ class DialogImpl extends ModelViewPanelImpl implements sqlops.window.modelviewdi
class TabImpl extends ModelViewPanelImpl implements sqlops.window.modelviewdialog.DialogTab {
constructor(
private _extHostModelViewDialog: ExtHostModelViewDialog,
extHostModelViewDialog: ExtHostModelViewDialog,
extHostModelView: ExtHostModelViewShape) {
super('modelViewDialogTab', extHostModelView);
super('modelViewDialogTab', extHostModelViewDialog, extHostModelView);
}
public title: string;
@@ -157,17 +168,110 @@ class ButtonImpl implements sqlops.window.modelviewdialog.Button {
}
}
class WizardPageImpl extends ModelViewPanelImpl implements sqlops.window.modelviewdialog.WizardPage {
public customButtons: sqlops.window.modelviewdialog.Button[];
private _enabled: boolean = true;
constructor(public title: string, _extHostModelViewDialog: ExtHostModelViewDialog, _extHostModelView: ExtHostModelViewShape) {
super('modelViewWizardPage', _extHostModelViewDialog, _extHostModelView);
}
public get enabled(): boolean {
return this._enabled;
}
public set enabled(enabled: boolean) {
this._enabled = enabled;
this._extHostModelViewDialog.updateWizardPage(this);
}
public get content(): string {
return this._modelViewId;
}
public set content(content: string) {
this._modelViewId = content;
}
}
export enum WizardPageInfoEventType {
PageChanged,
PageAddedOrRemoved
}
export interface WizardPageEventInfo {
eventType: WizardPageInfoEventType;
pageChangeInfo: sqlops.window.modelviewdialog.WizardPageChangeInfo;
pages?: sqlops.window.modelviewdialog.WizardPage[];
}
class WizardImpl implements sqlops.window.modelviewdialog.Wizard {
private _currentPage: number = undefined;
public pages: sqlops.window.modelviewdialog.WizardPage[] = [];
public doneButton: sqlops.window.modelviewdialog.Button;
public cancelButton: sqlops.window.modelviewdialog.Button;
public generateScriptButton: sqlops.window.modelviewdialog.Button;
public nextButton: sqlops.window.modelviewdialog.Button;
public backButton: sqlops.window.modelviewdialog.Button;
public customButtons: sqlops.window.modelviewdialog.Button[];
private _pageChangedEmitter = new Emitter<sqlops.window.modelviewdialog.WizardPageChangeInfo>();
public readonly onPageChanged = this._pageChangedEmitter.event;
constructor(public title: string, private _extHostModelViewDialog: ExtHostModelViewDialog) {
this.doneButton = this._extHostModelViewDialog.createButton(DONE_LABEL);
this.cancelButton = this._extHostModelViewDialog.createButton(CANCEL_LABEL);
this.generateScriptButton = this._extHostModelViewDialog.createButton(GENERATE_SCRIPT_LABEL);
this.nextButton = this._extHostModelViewDialog.createButton(NEXT_LABEL);
this.backButton = this._extHostModelViewDialog.createButton(PREVIOUS_LABEL);
this._extHostModelViewDialog.registerWizardPageInfoChangedCallback(this, info => this.handlePageInfoChanged(info));
this.onPageChanged(info => this._currentPage = info.newPage);
}
public get currentPage(): number {
return this._currentPage;
}
public addPage(page: sqlops.window.modelviewdialog.WizardPage, index?: number): Thenable<void> {
return this._extHostModelViewDialog.updateWizardPage(page).then(() => {
this._extHostModelViewDialog.addPage(this, page, index);
});
}
public removePage(index: number): Thenable<void> {
return this._extHostModelViewDialog.removePage(this, index);
}
public setCurrentPage(index: number): Thenable<void> {
return this._extHostModelViewDialog.setWizardPage(this, index);
}
public open(): Thenable<void> {
return this._extHostModelViewDialog.openWizard(this);
}
public close(): Thenable<void> {
return this._extHostModelViewDialog.closeWizard(this);
}
private handlePageInfoChanged(info: WizardPageEventInfo): void {
this._currentPage = info.pageChangeInfo.newPage;
if (info.eventType === WizardPageInfoEventType.PageAddedOrRemoved) {
this.pages = info.pages;
} else if (info.eventType === WizardPageInfoEventType.PageChanged) {
this._pageChangedEmitter.fire(info.pageChangeInfo);
}
}
}
export class ExtHostModelViewDialog implements ExtHostModelViewDialogShape {
private static _currentHandle = 0;
private readonly _proxy: MainThreadModelViewDialogShape;
private readonly _dialogHandles = new Map<sqlops.window.modelviewdialog.Dialog, number>();
private readonly _tabHandles = new Map<sqlops.window.modelviewdialog.DialogTab, number>();
private readonly _buttonHandles = new Map<sqlops.window.modelviewdialog.Button, number>();
private readonly _editorHandles = new Map<sqlops.workspace.ModelViewEditor, number>();
private readonly _objectHandles = new Map<object, number>();
private readonly _objectsByHandle = new Map<number, object>();
private readonly _validityEmitters = new Map<number, Emitter<boolean>>();
private readonly _pageInfoChangedCallbacks = new Map<number, (info: WizardPageEventInfo) => void>();
private readonly _onClickCallbacks = new Map<number, () => void>();
constructor(
@@ -183,38 +287,13 @@ export class ExtHostModelViewDialog implements ExtHostModelViewDialogShape {
return handle;
}
private getEditorHandle(editor: sqlops.workspace.ModelViewEditor) {
let handle = this._editorHandles.get(editor);
private getHandle(item: sqlops.window.modelviewdialog.Button | sqlops.window.modelviewdialog.Dialog | sqlops.window.modelviewdialog.DialogTab
| sqlops.window.modelviewdialog.ModelViewPanel | sqlops.window.modelviewdialog.Wizard | sqlops.window.modelviewdialog.WizardPage | sqlops.workspace.ModelViewEditor) {
let handle = this._objectHandles.get(item);
if (handle === undefined) {
handle = ExtHostModelViewDialog.getNewHandle();
this._editorHandles.set(editor, handle);
}
return handle;
}
private getDialogHandle(dialog: sqlops.window.modelviewdialog.Dialog) {
let handle = this._dialogHandles.get(dialog);
if (handle === undefined) {
handle = ExtHostModelViewDialog.getNewHandle();
this._dialogHandles.set(dialog, handle);
}
return handle;
}
private getTabHandle(tab: sqlops.window.modelviewdialog.DialogTab) {
let handle = this._tabHandles.get(tab);
if (handle === undefined) {
handle = ExtHostModelViewDialog.getNewHandle();
this._tabHandles.set(tab, handle);
}
return handle;
}
private getButtonHandle(button: sqlops.window.modelviewdialog.Button) {
let handle = this._buttonHandles.get(button);
if (handle === undefined) {
handle = ExtHostModelViewDialog.getNewHandle();
this._buttonHandles.set(button, handle);
this._objectHandles.set(item, handle);
this._objectsByHandle.set(handle, item);
}
return handle;
}
@@ -223,32 +302,57 @@ export class ExtHostModelViewDialog implements ExtHostModelViewDialogShape {
this._onClickCallbacks.get(handle)();
}
public $onDialogValidityChanged(handle: number, valid: boolean): void {
public $onPanelValidityChanged(handle: number, valid: boolean): void {
let emitter = this._validityEmitters.get(handle);
if (emitter) {
emitter.fire(valid);
}
}
public open(dialog: sqlops.window.modelviewdialog.Dialog): void {
let handle = this.getDialogHandle(dialog);
this.updateDialogContent(dialog);
this._proxy.$open(handle);
public $onWizardPageChanged(handle: number, info: sqlops.window.modelviewdialog.WizardPageChangeInfo): void {
let callback = this._pageInfoChangedCallbacks.get(handle);
if (callback) {
callback({
eventType: WizardPageInfoEventType.PageChanged,
pageChangeInfo: info
});
}
}
public close(dialog: sqlops.window.modelviewdialog.Dialog): void {
let handle = this.getDialogHandle(dialog);
this._proxy.$close(handle);
public $updateWizardPageInfo(handle: number, pageHandles: number[], currentPageIndex: number): void {
let callback = this._pageInfoChangedCallbacks.get(handle);
if (callback) {
let pages = pageHandles.map(pageHandle => this._objectsByHandle.get(handle) as sqlops.window.modelviewdialog.WizardPage);
callback({
eventType: WizardPageInfoEventType.PageAddedOrRemoved,
pageChangeInfo: {
lastPage: undefined,
newPage: currentPageIndex
},
pages: pages
});
}
}
public openDialog(dialog: sqlops.window.modelviewdialog.Dialog): void {
let handle = this.getHandle(dialog);
this.updateDialogContent(dialog);
this._proxy.$openDialog(handle);
}
public closeDialog(dialog: sqlops.window.modelviewdialog.Dialog): void {
let handle = this.getHandle(dialog);
this._proxy.$closeDialog(handle);
}
public createModelViewEditor(title: string): sqlops.workspace.ModelViewEditor {
let editor = new ModelViewEditorImpl(this._extHostModelView, this._proxy, title);
editor.handle = this.getEditorHandle(editor);
let editor = new ModelViewEditorImpl(this, this._extHostModelView, this._proxy, title);
editor.handle = this.getHandle(editor);
return editor;
}
public updateDialogContent(dialog: sqlops.window.modelviewdialog.Dialog): void {
let handle = this.getDialogHandle(dialog);
let handle = this.getHandle(dialog);
let tabs = dialog.content;
if (tabs && typeof tabs !== 'string') {
tabs.forEach(tab => this.updateTabContent(tab));
@@ -260,15 +364,15 @@ export class ExtHostModelViewDialog implements ExtHostModelViewDialogShape {
this.updateButton(dialog.cancelButton);
this._proxy.$setDialogDetails(handle, {
title: dialog.title,
okButton: this.getButtonHandle(dialog.okButton),
cancelButton: this.getButtonHandle(dialog.cancelButton),
content: dialog.content && typeof dialog.content !== 'string' ? dialog.content.map(tab => this.getTabHandle(tab)) : dialog.content as string,
customButtons: dialog.customButtons ? dialog.customButtons.map(button => this.getButtonHandle(button)) : undefined
okButton: this.getHandle(dialog.okButton),
cancelButton: this.getHandle(dialog.cancelButton),
content: dialog.content && typeof dialog.content !== 'string' ? dialog.content.map(tab => this.getHandle(tab)) : dialog.content as string,
customButtons: dialog.customButtons ? dialog.customButtons.map(button => this.getHandle(button)) : undefined
});
}
public updateTabContent(tab: sqlops.window.modelviewdialog.DialogTab): void {
let handle = this.getTabHandle(tab);
let handle = this.getHandle(tab);
this._proxy.$setTabDetails(handle, {
title: tab.title,
content: tab.content
@@ -276,7 +380,7 @@ export class ExtHostModelViewDialog implements ExtHostModelViewDialogShape {
}
public updateButton(button: sqlops.window.modelviewdialog.Button): void {
let handle = this.getButtonHandle(button);
let handle = this.getHandle(button);
this._proxy.$setButtonDetails(handle, {
label: button.label,
enabled: button.enabled,
@@ -285,34 +389,34 @@ export class ExtHostModelViewDialog implements ExtHostModelViewDialogShape {
}
public registerOnClickCallback(button: sqlops.window.modelviewdialog.Button, callback: () => void) {
let handle = this.getButtonHandle(button);
let handle = this.getHandle(button);
this._onClickCallbacks.set(handle, callback);
}
public createDialog(title: string): sqlops.window.modelviewdialog.Dialog {
let dialog = new DialogImpl(this, this._extHostModelView);
dialog.title = title;
dialog.handle = this.getDialogHandle(dialog);
dialog.handle = this.getHandle(dialog);
return dialog;
}
public createTab(title: string): sqlops.window.modelviewdialog.DialogTab {
let tab = new TabImpl(this, this._extHostModelView);
tab.title = title;
tab.handle = this.getTabHandle(tab);
tab.handle = this.getHandle(tab);
return tab;
}
public createButton(label: string): sqlops.window.modelviewdialog.Button {
let button = new ButtonImpl(this);
this.getButtonHandle(button);
this.getHandle(button);
this.registerOnClickCallback(button, button.getOnClickCallback());
button.label = label;
return button;
}
public getValidityChangedEvent(dialog: sqlops.window.modelviewdialog.Dialog) {
let handle = this.getDialogHandle(dialog);
public getValidityChangedEvent(panel: sqlops.window.modelviewdialog.ModelViewPanel) {
let handle = this.getHandle(panel);
let emitter = this._validityEmitters.get(handle);
if (!emitter) {
emitter = new Emitter<boolean>();
@@ -320,4 +424,81 @@ export class ExtHostModelViewDialog implements ExtHostModelViewDialogShape {
}
return emitter.event;
}
public registerWizardPageInfoChangedCallback(wizard: sqlops.window.modelviewdialog.Wizard, callback: (info: WizardPageEventInfo) => void): void {
let handle = this.getHandle(wizard);
this._pageInfoChangedCallbacks.set(handle, callback);
}
public createWizardPage(title: string): sqlops.window.modelviewdialog.WizardPage {
let page = new WizardPageImpl(title, this, this._extHostModelView);
page.handle = this.getHandle(page);
return page;
}
public createWizard(title: string): sqlops.window.modelviewdialog.Wizard {
let wizard = new WizardImpl(title, this);
this.getHandle(wizard);
return wizard;
}
public updateWizardPage(page: sqlops.window.modelviewdialog.WizardPage): Thenable<void> {
let handle = this.getHandle(page);
if (page.customButtons) {
page.customButtons.forEach(button => this.updateButton(button));
}
return this._proxy.$setWizardPageDetails(handle, {
content: page.content,
customButtons: page.customButtons ? page.customButtons.map(button => this.getHandle(button)) : undefined,
enabled: page.enabled,
title: page.title
});
}
public updateWizard(wizard: sqlops.window.modelviewdialog.Wizard): Thenable<void> {
let handle = this.getHandle(wizard);
wizard.pages.forEach(page => this.updateWizardPage(page));
this.updateButton(wizard.backButton);
this.updateButton(wizard.cancelButton);
this.updateButton(wizard.generateScriptButton);
this.updateButton(wizard.doneButton);
this.updateButton(wizard.nextButton);
if (wizard.customButtons) {
wizard.customButtons.forEach(button => this.updateButton(button));
}
return this._proxy.$setWizardDetails(handle, {
title: wizard.title,
pages: wizard.pages.map(page => this.getHandle(page)),
currentPage: wizard.currentPage,
backButton: this.getHandle(wizard.backButton),
cancelButton: this.getHandle(wizard.cancelButton),
generateScriptButton: this.getHandle(wizard.generateScriptButton),
doneButton: this.getHandle(wizard.doneButton),
nextButton: this.getHandle(wizard.nextButton),
customButtons: wizard.customButtons ? wizard.customButtons.map(button => this.getHandle(button)) : undefined
});
}
public addPage(wizard: sqlops.window.modelviewdialog.Wizard, page: sqlops.window.modelviewdialog.WizardPage, pageIndex?: number): Thenable<void> {
return this._proxy.$addWizardPage(this.getHandle(wizard), this.getHandle(page), pageIndex);
}
public removePage(wizard: sqlops.window.modelviewdialog.Wizard, pageIndex: number): Thenable<void> {
return this._proxy.$removeWizardPage(this.getHandle(wizard), pageIndex);
}
public setWizardPage(wizard: sqlops.window.modelviewdialog.Wizard, pageIndex: number): Thenable<void> {
return this._proxy.$setWizardPage(this.getHandle(wizard), pageIndex);
}
public openWizard(wizard: sqlops.window.modelviewdialog.Wizard): Thenable<void> {
let handle = this.getHandle(wizard);
this.updateWizard(wizard);
return this._proxy.$openWizard(handle);
}
public closeWizard(wizard: sqlops.window.modelviewdialog.Wizard): Thenable<void> {
let handle = this.getHandle(wizard);
return this._proxy.$closeWizard(handle);
}
}