From b6ef18973d38aaf2be02786e30b627c1da07c86b Mon Sep 17 00:00:00 2001 From: Marc Dumais Date: Mon, 28 Apr 2014 13:29:55 -0400 Subject: [PATCH] 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 Tested-by: Hudson CI Reviewed-by: Marc Dumais --- .../problemvisualizer/BarGraphicObject.java | 65 +++---- .../problemvisualizer/ProblemVisualizer.java | 163 ++++++++---------- 2 files changed, 102 insertions(+), 126 deletions(-) diff --git a/visualizer/org.eclipse.cdt.visualizer.examples/src/org/eclipse/cdt/visualizer/examples/problemvisualizer/BarGraphicObject.java b/visualizer/org.eclipse.cdt.visualizer.examples/src/org/eclipse/cdt/visualizer/examples/problemvisualizer/BarGraphicObject.java index a8d98c06a63..4f3d8b85769 100644 --- a/visualizer/org.eclipse.cdt.visualizer.examples/src/org/eclipse/cdt/visualizer/examples/problemvisualizer/BarGraphicObject.java +++ b/visualizer/org.eclipse.cdt.visualizer.examples/src/org/eclipse/cdt/visualizer/examples/problemvisualizer/BarGraphicObject.java @@ -11,68 +11,62 @@ *******************************************************************************/ 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; - - public BarGraphicObject(int severity, int x, int y, int w, int h, boolean outline) { - super(x, y, w, h); - m_outline = outline; - + private int m_barPercent; + + public BarGraphicObject(int severity, int barPercent) { + m_barPercent = barPercent; + Color color = getColor(severity); - if (m_outline) { - setForeground(color); - } else { - setBackground(color); - } + setForeground(color); + 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); - } + // draw outline of bar + gc.drawRectangle(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 */ @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; @@ -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; } diff --git a/visualizer/org.eclipse.cdt.visualizer.examples/src/org/eclipse/cdt/visualizer/examples/problemvisualizer/ProblemVisualizer.java b/visualizer/org.eclipse.cdt.visualizer.examples/src/org/eclipse/cdt/visualizer/examples/problemvisualizer/ProblemVisualizer.java index e5989c28cdd..7fe53b4ce56 100644 --- a/visualizer/org.eclipse.cdt.visualizer.examples/src/org/eclipse/cdt/visualizer/examples/problemvisualizer/ProblemVisualizer.java +++ b/visualizer/org.eclipse.cdt.visualizer.examples/src/org/eclipse/cdt/visualizer/examples/problemvisualizer/ProblemVisualizer.java @@ -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,36 +24,48 @@ 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. * In this case, it is the number of the three * different types of problem markers. */ 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, + super(Messages.ProblemCountVisualizer_Name, + Messages.ProblemCountVisualizer_DisplayName, Messages.ProblemCountVisualizer_Description); } @@ -60,13 +74,13 @@ public class ProblemVisualizer extends GraphicCanvasVisualizer { m_canvas = new ResizableGraphicCanvas(this, parent); return m_canvas; } - + @Override - protected void initializeCanvas(GraphicCanvas canvas) { + protected void initializeCanvas(GraphicCanvas canvas) { m_canvas.setBackground(MAIN_BACKGROUND_COLOR); m_canvas.setForeground(MAIN_FOREGROUND_COLOR); } - + @Override public void disposeCanvas() { @@ -75,75 +89,54 @@ public class ProblemVisualizer extends GraphicCanvasVisualizer { 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(); + 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); - 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; + // 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. + + // 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$ } - 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; + // 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); } - + /** * Clear the marker count array. */ @@ -152,7 +145,7 @@ public class ProblemVisualizer extends GraphicCanvasVisualizer { 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. @@ -164,7 +157,7 @@ public class ProblemVisualizer extends GraphicCanvasVisualizer { } catch (CoreException e) { return; } - + for (IMarker problem : problems) { try { Object attrValue = problem.getAttribute(IMarker.SEVERITY); @@ -182,19 +175,11 @@ public class ProblemVisualizer extends GraphicCanvasVisualizer { */ 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); - } - + createBars(); + m_canvas.add(m_container); + m_canvas.redraw(); } @@ -209,27 +194,27 @@ public class ProblemVisualizer extends GraphicCanvasVisualizer { return 2; } } - + return 0; } @Override public void workbenchSelectionChanged(ISelection selection) { clearMarkerCount(); - + List 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; - } + } }