1 package org.simantics.tests.modelled.ui;
3 import java.text.DecimalFormat;
5 import org.eclipse.jface.resource.ImageDescriptor;
6 import org.eclipse.swt.graphics.Image;
7 import org.eclipse.swt.widgets.Event;
8 import org.simantics.scl.compiler.module.coverage.Coverage;
10 public final class RedGreenBar {
12 private static final int BORDER_LEFT = 2;
13 private static final int BORDER_RIGHT = 10;
14 private static final int BORDER_TOP = 3;
15 private static final int BORDER_BOTTOM = 4;
17 private static final String MAX_PERCENTAGE_STRING = new DecimalFormat("0.0 %").format(1.0);
19 private static final ImageDescriptor redbar = Activator.getImageDescriptor("icons/redbar.gif"); //$NON-NLS-1$
20 private static final ImageDescriptor greenbar = Activator.getImageDescriptor("icons/greenbar.gif"); //$NON-NLS-1$
21 private static Image redbarIcon = Activator.createManagedImage(redbar);
22 private static Image greenbarIcon = Activator.createManagedImage(greenbar);
24 private RedGreenBar() {
27 // public static void draw(Event event, int columnWith, ICounter counter) {
28 // draw(event, columnWith, counter, counter.getTotalCount());
31 public static void draw(Event event, int columnWith, Coverage coverage) {
32 int maxTotal = coverage.getTotalCodeSize();
35 final int maxWidth = getMaxWidth(event, columnWith);
36 final int redLength = maxWidth * coverage.getMissedCodeSize() / maxTotal;
37 bar(event, redbarIcon, 0, redLength);
38 final int greenLength = maxWidth * coverage.getCoveredCodeSize() / maxTotal;
39 bar(event, greenbarIcon, redLength, greenLength);
42 private static void bar(Event event, Image image, int xOffset, int width) {
43 final int height = event.getBounds().height - BORDER_TOP - BORDER_BOTTOM;
44 event.gc.drawImage(image, 0, 0, 1, 10, event.x + xOffset + BORDER_LEFT,
45 event.y + BORDER_TOP, width, height);
48 private static int getMaxWidth(Event event, int columnWith) {
49 final int textWidth = event.gc.textExtent(MAX_PERCENTAGE_STRING).x;
50 final int max = columnWith - BORDER_LEFT - BORDER_RIGHT - textWidth;
51 return Math.max(0, max);