Skip to content

Commit

Permalink
Improve selection of cluster
Browse files Browse the repository at this point in the history
Fixes #62

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Jan 14, 2021
1 parent 0f5b732 commit 7c9439c
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,12 @@
},
{
"command": "vscode-kafka.explorer.deleteselected",
"when": "view == kafkaExplorer && viewItem =~ /^cluster$|^topic$/ && !listMultiSelection",
"when": "view == kafkaExplorer && viewItem =~ /^cluster$|^selectedCluster$|^topic$/ && !listMultiSelection",
"group": "inline"
},
{
"command": "vscode-kafka.explorer.deleteselected",
"when": "view == kafkaExplorer && viewItem =~ /^cluster$|^topic$/ && !listMultiSelection",
"when": "view == kafkaExplorer && viewItem =~ /^cluster$|^selectedCluster$|^topic$/ && !listMultiSelection",
"group": "3_modification"
}
],
Expand Down
5 changes: 5 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ export class Icons {
}
}


export enum GlyphChars {
Check = '\u2713'
}

export class CommonMessages {
public static showNoSelectedCluster(): void {
vscode.window.showInformationMessage("No cluster selected");
Expand Down
17 changes: 16 additions & 1 deletion src/explorer/kafkaExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { KafkaModel } from "./models/kafka";
import { ClusterItem } from "./models/cluster";
import { EOL } from 'os';
import { TopicItem } from "./models/topics";
import { SelectedClusterChangedEvent } from "../settings/clusters";

const TREEVIEW_ID = 'kafkaExplorer';

Expand Down Expand Up @@ -39,8 +40,10 @@ export class KafkaExplorer implements vscode.Disposable, vscode.TreeDataProvider
treeDataProvider: this,
canSelectMany: true
});
this.clusterSettings.onDidChangeSelected((e) => {
this.updateClusterSelection(e);
});
}

public refresh(): void {
// reset the kafka model
this.root = null;
Expand Down Expand Up @@ -155,4 +158,16 @@ export class KafkaExplorer implements vscode.Disposable, vscode.TreeDataProvider
}
}
}

private async updateClusterSelection(e: SelectedClusterChangedEvent): Promise<void> {
const oldSelection = e.oldClusterId ? (await this.root?.findClusterItemById(e.oldClusterId)) : undefined;
if (oldSelection != undefined) {
this.onDidChangeTreeDataEvent.fire(oldSelection);
}
const newSelection = e.newClusterId ? (await this.root?.findClusterItemById(e.newClusterId)) : undefined;
if (newSelection != undefined) {
this.onDidChangeTreeDataEvent.fire(newSelection);
}
}

}
25 changes: 21 additions & 4 deletions src/explorer/models/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import { TopicGroupItem, TopicItem } from "./topics";
import { ConsumerGroupsItem } from "./consumerGroups";
import { KafkaModel } from "./kafka";
import { Disposable } from "vscode";
import { GlyphChars } from "../../constants";

const TOPIC_INDEX = 1;

export class ClusterItem extends NodeBase implements Disposable {
public contextValue = "cluster";
public collapsibleState = vscode.TreeItemCollapsibleState.Collapsed;

constructor(public client: Client, public cluster: Cluster, parent: KafkaModel) {
constructor(public client: Client, public readonly cluster: Cluster, parent: KafkaModel) {
super(parent);
this.label = cluster.name;
this.description = cluster.bootstrap;
Expand All @@ -32,6 +33,22 @@ export class ClusterItem extends NodeBase implements Disposable {
return <KafkaModel>super.getParent();
}

getTreeItem(): vscode.TreeItem {
const treeItem = super.getTreeItem();
treeItem.id = this.cluster.name;
if (this.selected) {
treeItem.contextValue = 'selectedCluster';
treeItem.label = GlyphChars.Check + ' ' + treeItem.label;
} else {
treeItem.contextValue = 'cluster';
}
return treeItem;
}

public get selected(): boolean {
return (this.getParent().clusterSettings.selected?.name === this.cluster.name);
}

public dispose(): void {
this.client.dispose();
}
Expand Down Expand Up @@ -60,14 +77,14 @@ export class NoClusterItem extends NodeBase {
super(parent);
this.label = 'Click here to add a cluster';
}
getTreeItem():vscode.TreeItem {
getTreeItem(): vscode.TreeItem {
return {
label: this.label,
contextValue: this.contextValue,
description: this.description,
command : {
command: {
title: 'Add a cluster',
command:"vscode-kafka.explorer.addcluster"
command: "vscode-kafka.explorer.addcluster"
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/explorer/models/kafka.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class KafkaModel extends NodeBase implements Disposable {
public collapsibleState = TreeItemCollapsibleState.Collapsed;

constructor(
protected clusterSettings: ClusterSettings,
public readonly clusterSettings: ClusterSettings,
protected clientAccessor: ClientAccessor) {
super(undefined);
}
Expand Down Expand Up @@ -42,4 +42,11 @@ export class KafkaModel extends NodeBase implements Disposable {
clusters.find(child => (<ClusterItem>child).cluster.name === clusterName)
);
}

async findClusterItemById(clusterId: string): Promise<NodeBase | ClusterItem | undefined> {
return this.getChildren()
.then(clusters =>
clusters.find(child => (<ClusterItem>child).cluster.id === clusterId)
);
}
}
8 changes: 5 additions & 3 deletions src/settings/clusters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import * as vscode from "vscode";
import { Cluster } from "../client";
import { Context } from "../context";

interface SelectedClusterChangedEvent {
export interface SelectedClusterChangedEvent {
oldClusterId?: string,
newClusterId?: string;
}

Expand Down Expand Up @@ -72,8 +73,9 @@ class MementoClusterSettings implements ClusterSettings {
}

set selected(value: Cluster | undefined) {
const oldClusterId = this.selected?.id;
this.storage.update(this.selectedClusterIdStorageKey, value?.id);
this.onDidChangeSelectedEmitter.fire({ newClusterId: value?.id });
this.onDidChangeSelectedEmitter.fire({ oldClusterId : oldClusterId, newClusterId: value?.id });
}

getAll(): Cluster[] {
Expand Down Expand Up @@ -122,4 +124,4 @@ class MementoClusterSettings implements ClusterSettings {
}
}

export const getClusterSettings = (): ClusterSettings => MementoClusterSettings.getInstance();
export const getClusterSettings = (): ClusterSettings => MementoClusterSettings.getInstance();

0 comments on commit 7c9439c

Please sign in to comment.