1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-22 06:02:11 +02:00

Bug 432908 - [visualizer] Update Problem Visualizer example to use

VirtualBoundsGraphicObject

Change-Id: I5c15ac2bfa4bfc09faee70f5b12f73360af47f86
Reviewed-on: https://git.eclipse.org/r/25685
Reviewed-by: Marc Khouzam <marc.khouzam@ericsson.com>
Tested-by: Hudson CI
Reviewed-by: Marc Dumais <marc.dumais@ericsson.com>
This commit is contained in:
Marc Dumais 2014-04-28 13:29:55 -04:00
parent 2c2e4c5b96
commit b6ef18973d
2 changed files with 102 additions and 126 deletions

View file

@ -11,68 +11,62 @@
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.visualizer.examples.problemvisualizer; package org.eclipse.cdt.visualizer.examples.problemvisualizer;
import org.eclipse.cdt.visualizer.ui.canvas.GraphicObject; import org.eclipse.cdt.visualizer.ui.canvas.VirtualBoundsGraphicObject;
import org.eclipse.cdt.visualizer.ui.util.Colors; import org.eclipse.cdt.visualizer.ui.util.Colors;
import org.eclipse.cdt.visualizer.ui.util.GUIUtils; import org.eclipse.cdt.visualizer.ui.util.GUIUtils;
import org.eclipse.core.resources.IMarker; import org.eclipse.core.resources.IMarker;
import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
/** /**
* A class that draws a bar or a bar outline in the specified color. * A class that draws a bar or a bar outline in the specified color.
*/ */
public class BarGraphicObject extends GraphicObject { public class BarGraphicObject extends VirtualBoundsGraphicObject {
/* The different colors to use for the different severities */ /* The different colors to use for the different severities */
private static final Color ERROR_OUTLINE_COLOR = Colors.DARK_RED; 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_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_OUTLINE_COLOR = Colors.DARK_BLUE;
private static final Color INFO_INSIDE_COLOR = Colors.DARK_BLUE;
private boolean m_outline;
private String m_label; private String m_label;
private int m_barPercent;
public BarGraphicObject(int severity, int x, int y, int w, int h, boolean outline) {
super(x, y, w, h); public BarGraphicObject(int severity, int barPercent) {
m_outline = outline; m_barPercent = barPercent;
Color color = getColor(severity); Color color = getColor(severity);
if (m_outline) { setForeground(color);
setForeground(color); setBackground(color);
} else {
setBackground(color);
}
} }
public void setLabel(String label) { public void setLabel(String label) {
m_label = label; m_label = label;
} }
@Override @Override
public void paintContent(GC gc) { public void paintContent(GC gc) {
if (m_outline) { // draw outline of bar
gc.drawRectangle(m_bounds); gc.drawRectangle(m_bounds);
} else {
gc.fillRectangle(m_bounds); // figure-out the width that needs to be filled-in for this bar
} int barWidth = m_bounds.width * m_barPercent / 100;
Rectangle fillIn = new Rectangle(m_bounds.x, m_bounds.y, barWidth, m_bounds.height);
// fill-in bar
gc.fillRectangle(fillIn);
} }
@Override @Override
public boolean hasDecorations() { public boolean hasDecorations() {
// Only the outline bar has a label decoration. return true;
// 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 */ /** Invoked to allow element to paint decorations on top of anything drawn on it */
@Override @Override
public void paintDecorations(GC gc) { public void paintDecorations(GC gc) {
if (m_bounds.height > 20) { if (m_bounds.height > 20) {
gc.setForeground(Colors.BLACK); gc.setForeground(Colors.BLACK);
int text_indent = 6; int text_indent = 6;
int tx = m_bounds.x + m_bounds.width - text_indent; int tx = m_bounds.x + m_bounds.width - text_indent;
int ty = m_bounds.y + m_bounds.height - text_indent; int ty = m_bounds.y + m_bounds.height - text_indent;
@ -83,14 +77,11 @@ public class BarGraphicObject extends GraphicObject {
private Color getColor(int severity) { private Color getColor(int severity) {
switch (severity) { switch (severity) {
case IMarker.SEVERITY_ERROR: case IMarker.SEVERITY_ERROR:
if (m_outline) return ERROR_OUTLINE_COLOR; return ERROR_OUTLINE_COLOR;
return ERROR_INSIDE_COLOR;
case IMarker.SEVERITY_WARNING: case IMarker.SEVERITY_WARNING:
if (m_outline) return WARNING_OUTLINE_COLOR; return WARNING_OUTLINE_COLOR;
return WARNING_INSIDE_COLOR;
case IMarker.SEVERITY_INFO: case IMarker.SEVERITY_INFO:
if (m_outline) return INFO_OUTLINE_COLOR; return INFO_OUTLINE_COLOR;
return INFO_INSIDE_COLOR;
} }
return Colors.ORANGE; return Colors.ORANGE;
} }

View file

@ -7,6 +7,7 @@
* *
* Contributors: * Contributors:
* Marc Khouzam (Ericsson) - initial API and implementation * Marc Khouzam (Ericsson) - initial API and implementation
* Marc Dumais (Ericsson) - Re-factored (bug 432908)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.visualizer.examples.problemvisualizer; package org.eclipse.cdt.visualizer.examples.problemvisualizer;
@ -14,6 +15,7 @@ import java.util.List;
import org.eclipse.cdt.visualizer.ui.canvas.GraphicCanvas; import org.eclipse.cdt.visualizer.ui.canvas.GraphicCanvas;
import org.eclipse.cdt.visualizer.ui.canvas.GraphicCanvasVisualizer; import org.eclipse.cdt.visualizer.ui.canvas.GraphicCanvasVisualizer;
import org.eclipse.cdt.visualizer.ui.canvas.VirtualBoundsGraphicObject;
import org.eclipse.cdt.visualizer.ui.util.Colors; import org.eclipse.cdt.visualizer.ui.util.Colors;
import org.eclipse.cdt.visualizer.ui.util.SelectionManager; import org.eclipse.cdt.visualizer.ui.util.SelectionManager;
import org.eclipse.cdt.visualizer.ui.util.SelectionUtils; import org.eclipse.cdt.visualizer.ui.util.SelectionUtils;
@ -22,36 +24,48 @@ import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Composite;
public class ProblemVisualizer extends GraphicCanvasVisualizer { 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 */ /** The predefined number of severities */
private static final int NUM_SEVERITY = 3; private static final int NUM_SEVERITY = 3;
private static final Color MAIN_BACKGROUND_COLOR = Colors.WHITE; private static final Color MAIN_BACKGROUND_COLOR = Colors.WHITE;
private static final Color MAIN_FOREGROUND_COLOR = Colors.BLACK; private static final Color MAIN_FOREGROUND_COLOR = Colors.BLACK;
/** Virtual bounds of the "box" that will contains the bars */
private static final int[] BAR_CONTAINER_BOUNDS = { 0, 0, 1, 18 };
private static final int BAR_VIRTUAL_WIDTH = 1;
private static final int BAR_VIRTUAL_HEIGHT = 4;
/** Virtual bounds of each of the bars, relative to their container */
private static final int[][] BARS_VIRTUAL_BOUNDS = {
{ 0, 13, BAR_VIRTUAL_WIDTH, BAR_VIRTUAL_HEIGHT }, // infos
{ 0, 7, BAR_VIRTUAL_WIDTH, BAR_VIRTUAL_HEIGHT }, // warnings
{ 0, 1, BAR_VIRTUAL_WIDTH, BAR_VIRTUAL_HEIGHT } // errors
};
/** The canvas on which we'll draw our bars */ /** The canvas on which we'll draw our bars */
private GraphicCanvas m_canvas; private GraphicCanvas m_canvas;
/** Graphic container object - will hold the 3 bars */
private VirtualBoundsGraphicObject m_container = null;
/** /**
* The model containing the data to be displayed. * The model containing the data to be displayed.
* In this case, it is the number of the three * In this case, it is the number of the three
* different types of problem markers. * different types of problem markers.
*/ */
private int[] m_markerCount = new int[NUM_SEVERITY]; private int[] m_markerCount = new int[NUM_SEVERITY];
/** Labels for the different marker severity levels*/
private String[] m_markerSeverityLabels = {
Messages.ProblemCountVisualizer_Infos,
Messages.ProblemCountVisualizer_Warnings,
Messages.ProblemCountVisualizer_Errors,
};
public ProblemVisualizer() { public ProblemVisualizer() {
super(Messages.ProblemCountVisualizer_Name, super(Messages.ProblemCountVisualizer_Name,
Messages.ProblemCountVisualizer_DisplayName, Messages.ProblemCountVisualizer_DisplayName,
Messages.ProblemCountVisualizer_Description); Messages.ProblemCountVisualizer_Description);
} }
@ -60,13 +74,13 @@ public class ProblemVisualizer extends GraphicCanvasVisualizer {
m_canvas = new ResizableGraphicCanvas(this, parent); m_canvas = new ResizableGraphicCanvas(this, parent);
return m_canvas; return m_canvas;
} }
@Override @Override
protected void initializeCanvas(GraphicCanvas canvas) { protected void initializeCanvas(GraphicCanvas canvas) {
m_canvas.setBackground(MAIN_BACKGROUND_COLOR); m_canvas.setBackground(MAIN_BACKGROUND_COLOR);
m_canvas.setForeground(MAIN_FOREGROUND_COLOR); m_canvas.setForeground(MAIN_FOREGROUND_COLOR);
} }
@Override @Override
public void disposeCanvas() public void disposeCanvas()
{ {
@ -75,75 +89,54 @@ public class ProblemVisualizer extends GraphicCanvasVisualizer {
m_canvas = null; m_canvas = null;
} }
} }
@Override @Override
public void visualizerDeselected() { public void visualizerDeselected() {
} }
@Override @Override
public void visualizerSelected() { public void visualizerSelected() {
} }
/** /**
* Actually create the graphics bars for the different severities. * Actually create the graphics bars for the different severities.
* @param outline Should the bars be created, or the bar outline * @param outline Should the bars be created, or the bar outline
* @return The bars to be drawn. * @return The bars to be drawn.
*/ */
private BarGraphicObject[] getBars(boolean outline) { private void createBars() {
BarGraphicObject[] bars = new BarGraphicObject[3]; BarGraphicObject bar;
// Graphic container that will contain the bars
Rectangle bounds = m_canvas.getBounds(); m_container = new VirtualBoundsGraphicObject();
m_container.setVirtualBounds(BAR_CONTAINER_BOUNDS);
// no need to draw the ontainer's bounds
m_container.setDrawContainerBounds(false);
int x = bounds.x + MARGIN_WIDTH; // The inside of the bars use a proportional width with the maximum width and
int y = bounds.y + MARGIN_HEIGHT; // the largest amount of markers for one severity.
// Find the maximum marker count to dictate the width
int spacing = SPACING_HEIGHT; int maxCount = Math.max(m_markerCount[0], m_markerCount[1]);
int height = (bounds.height - 2 * MARGIN_HEIGHT - 2 * SPACING_HEIGHT) / 3; maxCount = Math.max(maxCount, m_markerCount[2]);
if (height <= 0) { if (maxCount == 0) maxCount = 1; // Set to anything but 0. It will be multiplied by 0 and not matter.
spacing = 0;
y = bounds.y; // go from high severity to low
height = bounds.height / 3; for (int severity = IMarker.SEVERITY_ERROR; severity >= IMarker.SEVERITY_INFO; severity--) {
float barPercent = m_markerCount[severity] / (float) maxCount * 100.0f;
bar = new BarGraphicObject(severity, Math.round(barPercent));
bar.setVirtualBounds(BARS_VIRTUAL_BOUNDS[severity]);
bar.setLabel(m_markerSeverityLabels[severity] + " " + m_markerCount[severity]); //$NON-NLS-1$
m_container.addChildObject("bar" + severity, bar); //$NON-NLS-1$
} }
int maxWidth = bounds.width - 2 * MARGIN_WIDTH; // set real bounds on parent "container" object - real bounds of
// bars will be recursively computed in proportion of their virtual
if (outline) { // bounds, relative to their container
// The bar outlines take the entire width of the view m_container.setBounds(m_canvas.getBounds());
bars[0] = new BarGraphicObject(IMarker.SEVERITY_ERROR, x, y, maxWidth, height, outline); // Add container object to canvas - when canvas draws the container,
bars[0].setLabel(Messages.ProblemCountVisualizer_Errors + m_markerCount[IMarker.SEVERITY_ERROR]); // the bars will automatically be drawn too, so no need to add them
// to canvas.
y = y + height + spacing; m_canvas.add(m_container);
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. * Clear the marker count array.
*/ */
@ -152,7 +145,7 @@ public class ProblemVisualizer extends GraphicCanvasVisualizer {
m_markerCount[IMarker.SEVERITY_WARNING] = 0; m_markerCount[IMarker.SEVERITY_WARNING] = 0;
m_markerCount[IMarker.SEVERITY_INFO] = 0; m_markerCount[IMarker.SEVERITY_INFO] = 0;
} }
/** /**
* Add the count of problem markers for each severity for the * Add the count of problem markers for each severity for the
* specified resource. * specified resource.
@ -164,7 +157,7 @@ public class ProblemVisualizer extends GraphicCanvasVisualizer {
} catch (CoreException e) { } catch (CoreException e) {
return; return;
} }
for (IMarker problem : problems) { for (IMarker problem : problems) {
try { try {
Object attrValue = problem.getAttribute(IMarker.SEVERITY); Object attrValue = problem.getAttribute(IMarker.SEVERITY);
@ -182,19 +175,11 @@ public class ProblemVisualizer extends GraphicCanvasVisualizer {
*/ */
public void refresh() { public void refresh() {
m_canvas.clear(); m_canvas.clear();
// First create the outline bars // First create the outline bars
BarGraphicObject[] bars = getBars(true); createBars();
for (BarGraphicObject bar : bars) { m_canvas.add(m_container);
m_canvas.add(bar);
}
// Now, create the inside bars
bars = getBars(false);
for (BarGraphicObject bar : bars) {
m_canvas.add(bar);
}
m_canvas.redraw(); m_canvas.redraw();
} }
@ -209,27 +194,27 @@ public class ProblemVisualizer extends GraphicCanvasVisualizer {
return 2; return 2;
} }
} }
return 0; return 0;
} }
@Override @Override
public void workbenchSelectionChanged(ISelection selection) { public void workbenchSelectionChanged(ISelection selection) {
clearMarkerCount(); clearMarkerCount();
List<Object> selections = SelectionUtils.getSelectedObjects(selection); List<Object> selections = SelectionUtils.getSelectedObjects(selection);
for (Object sel : selections) { for (Object sel : selections) {
if (sel instanceof IResource) { if (sel instanceof IResource) {
// Update the data // Update the data
addToMarkerCount((IResource)sel); addToMarkerCount((IResource)sel);
} }
} }
refresh(); refresh();
} }
public SelectionManager getSelectionManager() { public SelectionManager getSelectionManager() {
return m_selectionManager; return m_selectionManager;
} }
} }