markersPanel.ts 7.2 KB
Newer Older
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import 'vs/css!./media/markers';
S
Sandeep Somavarapu 已提交
7

S
Sandeep Somavarapu 已提交
8 9
import * as errors from 'vs/base/common/errors';
import * as Set from 'vs/base/common/set';
S
Sandeep Somavarapu 已提交
10
import URI from 'vs/base/common/uri';
11
import { TPromise } from 'vs/base/common/winjs.base';
12
import dom = require('vs/base/browser/dom');
13 14
import lifecycle = require('vs/base/common/lifecycle');
import builder = require('vs/base/browser/builder');
S
Sandeep Somavarapu 已提交
15
import {Action} from 'vs/base/common/actions';
16
import {IActionItem} from 'vs/base/browser/ui/actionbar/actionbar';
17 18
import { IMarkerService } from 'vs/platform/markers/common/markers';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
19
import { IEventService } from 'vs/platform/event/common/event';
20
import { Panel } from 'vs/workbench/browser/panel';
S
Sandeep Somavarapu 已提交
21
import {IAction} from 'vs/base/common/actions';
22
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
23 24 25
import Constants from 'vs/workbench/parts/markers/common/constants';
import { MarkersModel } from 'vs/workbench/parts/markers/common/markersModel';
import {Controller} from 'vs/workbench/parts/markers/browser/markersTreeController';
26
import Tree = require('vs/base/parts/tree/browser/tree');
S
Sandeep Somavarapu 已提交
27
import {CollapseAllAction} from 'vs/base/parts/tree/browser/treeDefaults';
28
import TreeImpl = require('vs/base/parts/tree/browser/treeImpl');
29
import * as Viewer from 'vs/workbench/parts/markers/browser/markersTreeViewer';
30
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
31
import { ActionProvider } from 'vs/workbench/parts/markers/browser/markersActionProvider';
32
import { FilterAction, FilterInputBoxActionItem } from 'vs/workbench/parts/markers/browser/markersPanelActions';
33 34 35

export class MarkersPanel extends Panel {

36
	public markersModel: MarkersModel;
37
	private tree: Tree.ITree;
S
Sandeep Somavarapu 已提交
38
	private toDispose: lifecycle.IDisposable[];
39
	private autoExpanded: Set.ArraySet<string>;
40

S
Sandeep Somavarapu 已提交
41
	private actions: IAction[];
42
	private filterAction: FilterAction;
S
Sandeep Somavarapu 已提交
43 44 45 46 47
	private collapseAllAction: IAction;

	private messageBoxContainer: HTMLElement;
	private messageBox: HTMLElement;

48 49 50 51
	constructor(
		@IInstantiationService private instantiationService: IInstantiationService,
		@IMarkerService private markerService: IMarkerService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
52
		@IEventService private eventService: IEventService,
53 54 55
		@ITelemetryService telemetryService: ITelemetryService
	) {
		super(Constants.MARKERS_PANEL_ID, telemetryService);
S
Sandeep Somavarapu 已提交
56
		this.markersModel= new MarkersModel();
S
Sandeep Somavarapu 已提交
57
		this.toDispose = [];
58
		this.autoExpanded= new Set.ArraySet<string>();
59 60 61 62
	}

	public create(parent: builder.Builder): TPromise<void> {
		super.create(parent);
63
		dom.addClass(parent.getHTMLElement(), 'markers-panel');
64

S
Sandeep Somavarapu 已提交
65 66
		let container= dom.append(parent.getHTMLElement(), dom.emmet('.markers-panel-container'));

67
		this.createMessageBox(container);
S
Sandeep Somavarapu 已提交
68 69
		this.createTree(container);

S
Sandeep Somavarapu 已提交
70
		this.createActions();
71
		this.createListeners();
72 73

		this.render();
S
Sandeep Somavarapu 已提交
74

75 76 77
		return TPromise.as(null);
	}

78
	public getTitle():string {
S
Sandeep Somavarapu 已提交
79
		let markerStatistics= this.markerService.getStatistics();
S
Sandeep Somavarapu 已提交
80
		return this.markersModel.getTitle(markerStatistics);
S
Sandeep Somavarapu 已提交
81 82
	}

83 84 85 86
	public layout(dimension: builder.Dimension): void {
		this.tree.layout(dimension.height);
	}

S
Sandeep Somavarapu 已提交
87
	public focus(): void {
S
Sandeep Somavarapu 已提交
88
		if (this.markersModel.hasFilteredResources()) {
89 90 91 92
			this.tree.DOMFocus();
			if (!this.tree.getFocus()) {
				this.tree.focusFirst();
			}
S
Sandeep Somavarapu 已提交
93
		}
S
Sandeep Somavarapu 已提交
94 95 96
	}

	public getActions(): IAction[] {
97
		this.collapseAllAction.enabled= this.markersModel.hasFilteredResources();
S
Sandeep Somavarapu 已提交
98 99 100
		return this.actions;
	}

101
	public refreshPanel(updateTitleArea: boolean= false):TPromise<any> {
102
		this.collapseAllAction.enabled= this.markersModel.hasFilteredResources();
103 104 105 106 107 108 109 110 111 112
		this.refreshAutoExpanded();
		if (updateTitleArea) {
			this.updateTitleArea();
		}
		return this.tree.refresh().then(() => {
			this.autoExpand();
			this.renderMessage();
		});
	}

S
Sandeep Somavarapu 已提交
113 114 115 116 117 118
	private createMessageBox(parent: HTMLElement): void {
		this.messageBoxContainer= dom.append(parent, dom.emmet('.message-box-container'));
		this.messageBox= dom.append(this.messageBoxContainer, dom.emmet('p'));
	}

	private createTree(parent: HTMLElement):void {
119
		var treeContainer= dom.append(parent, dom.emmet('.tree-container'));
S
Sandeep Somavarapu 已提交
120 121 122
		var actionProvider = this.instantiationService.createInstance(ActionProvider);
		var renderer = this.instantiationService.createInstance(Viewer.Renderer, this.getActionRunner(), actionProvider);
		var controller = this.instantiationService.createInstance(Controller);
123
		this.tree= new TreeImpl.Tree(treeContainer, {
S
Sandeep Somavarapu 已提交
124 125 126 127 128 129 130 131 132 133 134
			dataSource: new Viewer.DataSource(),
			renderer: renderer,
			controller: controller
		}, {
			indentPixels: 0,
			twistiePixels: 20,
		});
	}

	private createActions():void {
		this.collapseAllAction= this.instantiationService.createInstance(CollapseAllAction, this.tree, true);
135
		this.filterAction= new FilterAction(this);
S
Sandeep Somavarapu 已提交
136
		this.actions= [
137
					this.filterAction,
S
Sandeep Somavarapu 已提交
138 139 140 141 142 143 144
					this.collapseAllAction
				];
		this.actions.forEach(a => {
			this.toDispose.push(a);
		});
	}

145 146 147 148
	private createListeners(): void {
		this.toDispose.push(this.markerService.onMarkerChanged(this.onMarkerChanged.bind(this)));
	}

S
Sandeep Somavarapu 已提交
149 150
	private onMarkerChanged(changedResources: URI[]) {
		this.updateResources(changedResources);
151
		this.refreshPanel(true);
152 153
	}

S
Sandeep Somavarapu 已提交
154 155
	private updateResources(resources: URI[]) {
		resources.forEach((resource) => {
S
Sandeep Somavarapu 已提交
156
			let markers= this.markerService.read({resource: resource}).slice(0);
157 158 159 160
			this.markersModel.update(resource, markers);
			if (!this.markersModel.hasResource(resource)) {
				this.autoExpanded.unset(resource.toString());
			}
S
Sandeep Somavarapu 已提交
161
		});
S
Sandeep Somavarapu 已提交
162 163
	}

164 165
	private render(): void {
		let allMarkers = this.markerService.read().slice(0);
166
		this.markersModel.update(allMarkers);
S
Sandeep Somavarapu 已提交
167
		this.tree.setInput(this.markersModel).then(this.autoExpand.bind(this));
S
Sandeep Somavarapu 已提交
168 169 170 171 172 173 174
		this.renderMessage();
	}

	private renderMessage():void {
		let message= this.markersModel.getMessage();
		this.messageBox.textContent= message;
		dom.toggleClass(this.messageBoxContainer, 'visible', !this.markersModel.hasFilteredResources());
S
Sandeep Somavarapu 已提交
175 176
	}

177 178 179 180 181 182 183 184
	private refreshAutoExpanded(): void {
		this.markersModel.nonFilteredResources.forEach((resource) => {
			if (this.tree.isExpanded(resource)) {
				this.autoExpanded.unset(resource.uri.toString());
			}
		});
	}

S
Sandeep Somavarapu 已提交
185
	private autoExpand(): void {
186 187
		this.markersModel.filteredResources.forEach((resource) => {
			if (this.autoExpanded.contains(resource.uri.toString())) {
S
Sandeep Somavarapu 已提交
188 189
				return;
			}
190
			if (resource.markers.length > 0 && resource.markers.length < 10) {
S
Sandeep Somavarapu 已提交
191 192 193 194
				this.tree.expand(resource).done(null, errors.onUnexpectedError);
			} else {
				this.tree.collapse(resource).done(null, errors.onUnexpectedError);
			}
195
			this.autoExpanded.set(resource.uri.toString());
S
Sandeep Somavarapu 已提交
196
		});
197 198
	}

199 200 201 202 203
	public getActionItem(action: Action): IActionItem {
		if (action.id === 'workbench.markers.panel.action.filter') {
			return this.instantiationService.createInstance(FilterInputBoxActionItem, this, action);
		}
		return super.getActionItem(action);
S
Sandeep Somavarapu 已提交
204 205
	}

206
	public dispose(): void {
S
Sandeep Somavarapu 已提交
207
		this.toDispose = lifecycle.dispose(this.toDispose);
208 209 210
		super.dispose();
	}
}