-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #61 Signed-off-by: azerr <[email protected]>
- Loading branch information
1 parent
d814bfa
commit 50def4b
Showing
11 changed files
with
266 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,119 @@ | ||
import * as vscode from "vscode"; | ||
|
||
import { Cluster, ClientAccessor } from "../client"; | ||
import { ClientAccessor } from "../client"; | ||
import { WorkspaceSettings, ClusterSettings } from "../settings"; | ||
import { InformationItem } from "./models/common"; | ||
import { NodeBase } from "./models/nodeBase"; | ||
import { TreeView } from "vscode"; | ||
import { KafkaModel } from "./models/kafka"; | ||
import { ClusterItem } from "./models/cluster"; | ||
|
||
const TREEVIEW_ID = 'kafkaExplorer'; | ||
|
||
/** | ||
* Kafka explorer to show in a tree clusters, topics. | ||
*/ | ||
export class KafkaExplorer implements vscode.Disposable, vscode.TreeDataProvider<NodeBase> { | ||
|
||
private onDidChangeTreeDataEvent: vscode.EventEmitter<NodeBase | undefined> | ||
= new vscode.EventEmitter<NodeBase | undefined>(); | ||
public onDidChangeTreeData?: vscode.Event<NodeBase | null | undefined> | undefined | ||
readonly onDidChangeTreeData?: vscode.Event<NodeBase | null | undefined> | undefined | ||
= this.onDidChangeTreeDataEvent.event; | ||
|
||
private readonly clusterSettings: ClusterSettings; | ||
private readonly clientAccessor: ClientAccessor; | ||
|
||
protected tree: TreeView<NodeBase> | undefined; | ||
|
||
private root: KafkaModel | null; | ||
|
||
constructor( | ||
settings: WorkspaceSettings, | ||
settings: WorkspaceSettings, | ||
clusterSettings: ClusterSettings, | ||
clientAccessor: ClientAccessor) { | ||
this.clusterSettings = clusterSettings; | ||
this.clientAccessor = clientAccessor; | ||
this.root = null; | ||
this.tree = vscode.window.createTreeView(TREEVIEW_ID, { | ||
treeDataProvider: this | ||
}); | ||
} | ||
|
||
public refresh(): void { | ||
// reset the kafka model | ||
this.root = null; | ||
// refresh the treeview | ||
this.onDidChangeTreeDataEvent.fire(undefined); | ||
this.show(); | ||
} | ||
|
||
private show(): void { | ||
vscode.commands.executeCommand(`${TREEVIEW_ID}.focus`); | ||
} | ||
|
||
public getTreeItem(element: NodeBase): vscode.TreeItem | Thenable<vscode.TreeItem> { | ||
return element.getTreeItem(); | ||
} | ||
|
||
public getChildren(element?: NodeBase): vscode.ProviderResult<NodeBase[]> { | ||
const clusters = this.clusterSettings.getAll(); | ||
async getChildren(element?: NodeBase): Promise<NodeBase[]> { | ||
if (!element) { | ||
if (!this.root) { | ||
this.root = new KafkaModel(this.clusterSettings, this.clientAccessor); | ||
} | ||
element = this.root; | ||
} | ||
return element.getChildren(); | ||
} | ||
|
||
if (clusters.length === 0) { | ||
return [new InformationItem("No clusters added")]; | ||
public getParent(element: NodeBase): NodeBase | undefined { | ||
if (element instanceof ClusterItem) { | ||
return undefined; | ||
} | ||
return element.getParent(); | ||
} | ||
|
||
if (!element) { | ||
return this.getGroupChildren(clusters); | ||
public dispose(): void { | ||
if (this.root) { | ||
this.root.dispose(); | ||
} | ||
} | ||
|
||
return element.getChildren(element); | ||
/** | ||
* Select the given cluster name in the tree. | ||
* | ||
* @param clusterName the cluster name to select. | ||
*/ | ||
async selectClusterByName(clusterName: string): Promise<void> { | ||
const clusterItem = await this.root?.findClusterItemByName(clusterName); | ||
if (!clusterItem) { | ||
return; | ||
} | ||
this.selectItem(clusterItem); | ||
} | ||
|
||
public dispose(): void { | ||
// noop | ||
/** | ||
* Select the given topic name which belongs to the given cluster in the tree. | ||
* | ||
* @param clusterName the owner cluster name | ||
* @param topicName the topic name | ||
*/ | ||
async selectTopic(clusterName: string, topicName: string): Promise<void> { | ||
const clusterItem = await this.root?.findClusterItemByName(clusterName); | ||
if (!clusterItem) { | ||
return; | ||
} | ||
const topicItem = await (<ClusterItem>clusterItem).findTopictemByName(topicName); | ||
if (!topicItem) { | ||
return; | ||
} | ||
this.selectItem(topicItem); | ||
} | ||
|
||
private getGroupChildren(clusters: Cluster[]): NodeBase[] { | ||
return clusters.map((c) => { | ||
return new ClusterItem(this.clientAccessor.get(c.id), c); | ||
private selectItem(item: NodeBase): void { | ||
this.show(); | ||
this.tree?.reveal(item, { | ||
select: true, | ||
expand: true, | ||
focus: true | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.