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:
parent
2c2e4c5b96
commit
b6ef18973d
2 changed files with 102 additions and 126 deletions
|
@ -11,40 +11,34 @@
|
|||
*******************************************************************************/
|
||||
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.GUIUtils;
|
||||
import org.eclipse.core.resources.IMarker;
|
||||
import org.eclipse.swt.graphics.Color;
|
||||
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.
|
||||
*/
|
||||
public class BarGraphicObject extends GraphicObject {
|
||||
public class BarGraphicObject extends VirtualBoundsGraphicObject {
|
||||
|
||||
/* 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 boolean m_outline;
|
||||
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);
|
||||
m_outline = outline;
|
||||
public BarGraphicObject(int severity, int barPercent) {
|
||||
m_barPercent = barPercent;
|
||||
|
||||
Color color = getColor(severity);
|
||||
if (m_outline) {
|
||||
setForeground(color);
|
||||
} else {
|
||||
setBackground(color);
|
||||
}
|
||||
}
|
||||
|
||||
public void setLabel(String label) {
|
||||
m_label = label;
|
||||
|
@ -52,19 +46,19 @@ public class BarGraphicObject extends GraphicObject {
|
|||
|
||||
@Override
|
||||
public void paintContent(GC gc) {
|
||||
if (m_outline) {
|
||||
// draw outline of bar
|
||||
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
|
||||
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;
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Invoked to allow element to paint decorations on top of anything drawn on it */
|
||||
|
@ -83,14 +77,11 @@ public class BarGraphicObject extends GraphicObject {
|
|||
private Color getColor(int severity) {
|
||||
switch (severity) {
|
||||
case IMarker.SEVERITY_ERROR:
|
||||
if (m_outline) return ERROR_OUTLINE_COLOR;
|
||||
return ERROR_INSIDE_COLOR;
|
||||
return ERROR_OUTLINE_COLOR;
|
||||
case IMarker.SEVERITY_WARNING:
|
||||
if (m_outline) return WARNING_OUTLINE_COLOR;
|
||||
return WARNING_INSIDE_COLOR;
|
||||
return WARNING_OUTLINE_COLOR;
|
||||
case IMarker.SEVERITY_INFO:
|
||||
if (m_outline) return INFO_OUTLINE_COLOR;
|
||||
return INFO_INSIDE_COLOR;
|
||||
return INFO_OUTLINE_COLOR;
|
||||
}
|
||||
return Colors.ORANGE;
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
*
|
||||
* Contributors:
|
||||
* Marc Khouzam (Ericsson) - initial API and implementation
|
||||
* Marc Dumais (Ericsson) - Re-factored (bug 432908)
|
||||
*******************************************************************************/
|
||||
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.GraphicCanvasVisualizer;
|
||||
import org.eclipse.cdt.visualizer.ui.canvas.VirtualBoundsGraphicObject;
|
||||
import org.eclipse.cdt.visualizer.ui.util.Colors;
|
||||
import org.eclipse.cdt.visualizer.ui.util.SelectionManager;
|
||||
import org.eclipse.cdt.visualizer.ui.util.SelectionUtils;
|
||||
|
@ -22,25 +24,30 @@ 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.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;
|
||||
|
||||
private static final Color MAIN_BACKGROUND_COLOR = Colors.WHITE;
|
||||
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 */
|
||||
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.
|
||||
|
@ -49,6 +56,13 @@ public class ProblemVisualizer extends GraphicCanvasVisualizer {
|
|||
*/
|
||||
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() {
|
||||
super(Messages.ProblemCountVisualizer_Name,
|
||||
Messages.ProblemCountVisualizer_DisplayName,
|
||||
|
@ -89,59 +103,38 @@ public class ProblemVisualizer extends GraphicCanvasVisualizer {
|
|||
* @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];
|
||||
private void createBars() {
|
||||
BarGraphicObject bar;
|
||||
// Graphic container that will contain the bars
|
||||
m_container = new VirtualBoundsGraphicObject();
|
||||
m_container.setVirtualBounds(BAR_CONTAINER_BOUNDS);
|
||||
// no need to draw the ontainer's bounds
|
||||
m_container.setDrawContainerBounds(false);
|
||||
|
||||
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);
|
||||
// go from high severity to low
|
||||
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$
|
||||
}
|
||||
|
||||
return bars;
|
||||
// set real bounds on parent "container" object - real bounds of
|
||||
// bars will be recursively computed in proportion of their virtual
|
||||
// bounds, relative to their container
|
||||
m_container.setBounds(m_canvas.getBounds());
|
||||
// Add container object to canvas - when canvas draws the container,
|
||||
// the bars will automatically be drawn too, so no need to add them
|
||||
// to canvas.
|
||||
m_canvas.add(m_container);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -184,16 +177,8 @@ public class ProblemVisualizer extends GraphicCanvasVisualizer {
|
|||
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);
|
||||
}
|
||||
createBars();
|
||||
m_canvas.add(m_container);
|
||||
|
||||
m_canvas.redraw();
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue