]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/RedGreenBar.java
Ignore multiple modelled tests via context menu action
[simantics/platform.git] / bundles / org.simantics.tests.modelled.ui / src / org / simantics / tests / modelled / ui / RedGreenBar.java
1 package org.simantics.tests.modelled.ui;
2
3 import java.text.DecimalFormat;
4
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;
9
10 public final class RedGreenBar {
11
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;
16
17     private static final String MAX_PERCENTAGE_STRING = new DecimalFormat("0.0 %").format(1.0);
18     
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);
23     
24     private RedGreenBar() {
25     }
26
27 //    public static void draw(Event event, int columnWith, ICounter counter) {
28 //        draw(event, columnWith, counter, counter.getTotalCount());
29 //    }
30
31     public static void draw(Event event, int columnWith, Coverage coverage) {
32         int maxTotal = coverage.getTotalCodeSize();
33         if (maxTotal == 0)
34             return;
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);
40     }
41
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);
46     }
47
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);
52     }
53
54 }