Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 47a2a69e6ed517abc383b7ec2b404ef2d5e51cdc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*******************************************************************************
 * Copyright (c) 2012 Ericsson and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Marc Khouzam (Ericsson) - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.visualizer.examples.ProblemVisualizer;

import java.util.List;

import org.eclipse.cdt.visualizer.ui.canvas.GraphicCanvas;
import org.eclipse.cdt.visualizer.ui.canvas.GraphicCanvasVisualizer;
import org.eclipse.cdt.visualizer.ui.canvas.GraphicObject;
import org.eclipse.cdt.visualizer.ui.util.Colors;
import org.eclipse.cdt.visualizer.ui.util.GUIUtils;
import org.eclipse.cdt.visualizer.ui.util.SelectionManager;
import org.eclipse.cdt.visualizer.ui.util.SelectionUtils;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;

public class ProblemVisualizer extends GraphicCanvasVisualizer {

	/** The width of the side margins */
	private static final int MARGIN_WIDTH = 10;
	/** The height of the top and bottom margins */
	private static final int MARGIN_HEIGHT = 10;
	/** The default space between bars in the chart */
	private static final int SPACING_HEIGHT = 40;
	/** The predefined number of severities */
	private static final int NUM_SEVERITY = 3;

	/* The different colors to use for the different severities */
	private static final Color ERROR_OUTLINE_COLOR = Colors.DARK_RED;
	private static final Color ERROR_INSIDE_COLOR = Colors.DARK_RED;
	private static final Color WARNING_OUTLINE_COLOR = Colors.DARK_YELLOW;
	private static final Color WARNING_INSIDE_COLOR = Colors.DARK_YELLOW;
	private static final Color INFO_OUTLINE_COLOR = Colors.DARK_BLUE;
	private static final Color INFO_INSIDE_COLOR = Colors.DARK_BLUE;
	
	private static final Color MAIN_BACKGROUND_COLOR = Colors.WHITE;
	private static final Color MAIN_FOREGROUND_COLOR = Colors.BLACK;

	/**
	 * A class that draws a bar or a bar outline in the specified color.
	 */
	private class BarGraphicObject extends GraphicObject {
		private boolean m_outline;
		private String m_label;
		
		public BarGraphicObject(int severity, int x, int y, int w, int h, boolean outline) {
			super(x, y, w, h);
			m_outline = outline;
			
			Color color = getColor(severity);
			if (m_outline) {
				setForeground(color);
			} else {
				setBackground(color);
			}
		}
		
		public void setLabel(String label) {
			m_label = label;
		}
		
		@Override
		public void paintContent(GC gc) {
			if (m_outline) {
				gc.drawRectangle(m_bounds);
			} else {
				gc.fillRectangle(m_bounds);
			}
		}
		
		@Override
		public boolean hasDecorations() {
			// Only the outline bar has a label decoration.
			// We muse the the outline bar and not the inside one because
			// the inside bar may be too small
			return m_outline;
		}
		
		/** Invoked to allow element to paint decorations on top of anything drawn on it */
		@Override
		public void paintDecorations(GC gc) {
			if (m_bounds.height > 20) {
				gc.setForeground(Colors.BLACK);
				
				int text_indent = 6;
				int tx = m_bounds.x + m_bounds.width  - text_indent;
				int ty = m_bounds.y + m_bounds.height - text_indent;
				GUIUtils.drawTextAligned(gc, m_label, tx, ty, false, false);
			}
		}

		private Color getColor(int severity) {
			switch (severity) {
			case IMarker.SEVERITY_ERROR:
				if (m_outline) return ERROR_OUTLINE_COLOR;
				return ERROR_INSIDE_COLOR;
			case IMarker.SEVERITY_WARNING:
				if (m_outline) return WARNING_OUTLINE_COLOR;
				return WARNING_INSIDE_COLOR;
			case IMarker.SEVERITY_INFO:
				if (m_outline) return INFO_OUTLINE_COLOR;
				return INFO_INSIDE_COLOR;
			}
			return Colors.ORANGE;
		}
	}
	
	private class ResizableGraphicCanvas extends GraphicCanvas {
		public ResizableGraphicCanvas(Composite parent) {
			super(parent);
		}
		
		@Override
		public void resized(Rectangle bounds) {
			ProblemVisualizer.this.refresh();
		}
	}
	/** The canvas on which we'll draw our bars */
	private GraphicCanvas m_canvas;
	
	/**
	 * The model containing the data to be displayed.
	 * In this case, it is the number of the three
	 * different types of problem markers.
	 */
	private int[] m_markerCount = new int[NUM_SEVERITY];
	
	public ProblemVisualizer() {
		super();
	}

	@Override
	public String getName() {
		return Messages.ProblemCountVisualizer_Name;
	}
	
	@Override
	public String getDisplayName() {
		return Messages.ProblemCountVisualizer_DisplayName;
	}
	
	@Override
	public String getDescription() {
		return Messages.ProblemCountVisualizer_Description;
	}
	
	@Override
	public GraphicCanvas createCanvas(Composite parent) {
		m_canvas = new ResizableGraphicCanvas(parent);
		return m_canvas;
	}
	
	@Override
	protected void initializeCanvas(GraphicCanvas canvas) {		
		m_canvas.setBackground(MAIN_BACKGROUND_COLOR);
		m_canvas.setForeground(MAIN_FOREGROUND_COLOR);
	}
	
	@Override
	public void disposeCanvas()
	{
		if (m_canvas != null) {
			m_canvas.dispose();
			m_canvas = null;
		}
	}
	
	@Override
	public void visualizerDeselected() {
	}
	
	@Override
	public void visualizerSelected() {
	}
	
	/**
	 * Actually create the graphics bars for the different severities.
	 * @param outline Should the bars be created, or the bar outline
	 * @return The bars to be drawn.
	 */
	private BarGraphicObject[] getBars(boolean outline) {
		BarGraphicObject[] bars = new BarGraphicObject[3];
		
		Rectangle bounds = m_canvas.getBounds();

		int x = bounds.x + MARGIN_WIDTH;
		int y = bounds.y + MARGIN_HEIGHT;
		
		int spacing = SPACING_HEIGHT;
		int height = (bounds.height - 2 * MARGIN_HEIGHT - 2 * SPACING_HEIGHT) / 3;
		if (height <= 0) {
			spacing = 0;
			y = bounds.y;
			height = bounds.height / 3;
		}

		int maxWidth = bounds.width - 2 * MARGIN_WIDTH;
		
		if (outline) {
			// The bar outlines take the entire width of the view
			bars[0] = new BarGraphicObject(IMarker.SEVERITY_ERROR, x, y, maxWidth, height, outline);
			bars[0].setLabel(Messages.ProblemCountVisualizer_Errors + m_markerCount[IMarker.SEVERITY_ERROR]);
			
			y = y + height + spacing;
			bars[1] = new BarGraphicObject(IMarker.SEVERITY_WARNING, x, y, maxWidth, height, outline);
			bars[1].setLabel(Messages.ProblemCountVisualizer_Warnings + m_markerCount[IMarker.SEVERITY_WARNING]);

			y = y + height + spacing;
			bars[2] = new BarGraphicObject(IMarker.SEVERITY_INFO, x, y, maxWidth, height, outline);
			bars[2].setLabel(Messages.ProblemCountVisualizer_Infos + m_markerCount[IMarker.SEVERITY_INFO]);

		} else {
			// The inside of the bars use a proportional width with the maximum width and
			// the largest amount of markers for one severity.
			
			// Find the maximum marker count to dictate the width
			int maxCount = Math.max(m_markerCount[0], m_markerCount[1]);
			maxCount = Math.max(maxCount, m_markerCount[2]);
			if (maxCount == 0) maxCount = 1; // Set to anything but 0.  It will be multiplied by 0 and not matter.

			int width = maxWidth * m_markerCount[IMarker.SEVERITY_ERROR] / maxCount;
			bars[0] = new BarGraphicObject(IMarker.SEVERITY_ERROR, x, y, width, height, outline);

			y = y + height + spacing;
			width = maxWidth * m_markerCount[IMarker.SEVERITY_WARNING] / maxCount;
			bars[1] = new BarGraphicObject(IMarker.SEVERITY_WARNING, x, y, width, height, outline);

			y = y + height + spacing;
			width = maxWidth * m_markerCount[IMarker.SEVERITY_INFO] / maxCount;
			bars[2] = new BarGraphicObject(IMarker.SEVERITY_INFO, x, y, width, height, outline);
		}
		
		return bars;
	}
	
	/**
	 * Clear the marker count array.
	 */
	private void clearMarkerCount() {
		m_markerCount[IMarker.SEVERITY_ERROR] = 0;
		m_markerCount[IMarker.SEVERITY_WARNING] = 0;
		m_markerCount[IMarker.SEVERITY_INFO] = 0;
	}
	
	/**
	 * Add the count of problem markers for each severity for the
	 * specified resource.
	 */
	private void addToMarkerCount(IResource resource) {
		IMarker[] problems = null;
		try {
			problems = resource.findMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE);
		} catch (CoreException e) {
			return;
		}
		
		for (IMarker problem : problems) {
			try {
				Object attrValue = problem.getAttribute(IMarker.SEVERITY);
				if (attrValue != null && attrValue instanceof Integer) {
					int severity = (Integer)attrValue;
					m_markerCount[severity]++;
				}
			} catch (CoreException e) {
			}
		}
	}

	/**
	 * Refresh the visualizer display based on the existing data.
	 */
	public void refresh() {
		m_canvas.clear();
		
		// First create the outline bars
		BarGraphicObject[] bars = getBars(true);
		for (BarGraphicObject bar : bars) {
			m_canvas.add(bar);
		}
		
		// Now, create the inside bars
		bars = getBars(false);
		for (BarGraphicObject bar : bars) {
			m_canvas.add(bar);
		}
		
		m_canvas.redraw();
	}

	@Override
	public int handlesSelection(ISelection selection) {
		List<Object> selections = SelectionUtils.getSelectedObjects(selection);

		// As long as we support at least one element of the selection
		// that is good enough
		for (Object sel : selections) {
			if (sel instanceof IResource) {
				return 2;
			}
		}
		
		return 0;
	}

	@Override
	public void workbenchSelectionChanged(ISelection selection) {
		clearMarkerCount();
		
		List<Object> selections = SelectionUtils.getSelectedObjects(selection);
		
		for (Object sel : selections) {
			if (sel instanceof IResource) {
				// Update the data
				addToMarkerCount((IResource)sel);
			}
		}
		
		refresh();
	}
	
	public SelectionManager getSelectionManager() {
		return m_selectionManager;
	}	
}

Back to the top