X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.tests.modelled.ui%2Fsrc%2Forg%2Fsimantics%2Ftests%2Fmodelled%2Fui%2FRedGreenBar.java;fp=bundles%2Forg.simantics.tests.modelled.ui%2Fsrc%2Forg%2Fsimantics%2Ftests%2Fmodelled%2Fui%2FRedGreenBar.java;h=9812549501bd5396c4e7d3cdf6b63b82b19081b2;hp=0000000000000000000000000000000000000000;hb=3b5069d0d30e7de27f73d88d5e89d29052291a34;hpb=bf75fd9740858140eac90c18f0bca0aea3893248 diff --git a/bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/RedGreenBar.java b/bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/RedGreenBar.java new file mode 100644 index 000000000..981254950 --- /dev/null +++ b/bundles/org.simantics.tests.modelled.ui/src/org/simantics/tests/modelled/ui/RedGreenBar.java @@ -0,0 +1,54 @@ +package org.simantics.tests.modelled.ui; + +import java.text.DecimalFormat; + +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.widgets.Event; +import org.simantics.scl.compiler.module.coverage.Coverage; + +public final class RedGreenBar { + + private static final int BORDER_LEFT = 2; + private static final int BORDER_RIGHT = 10; + private static final int BORDER_TOP = 3; + private static final int BORDER_BOTTOM = 4; + + private static final String MAX_PERCENTAGE_STRING = new DecimalFormat("0.0 %").format(1.0); + + private static final ImageDescriptor redbar = Activator.getImageDescriptor("icons/redbar.gif"); //$NON-NLS-1$ + private static final ImageDescriptor greenbar = Activator.getImageDescriptor("icons/greenbar.gif"); //$NON-NLS-1$ + private static Image redbarIcon = Activator.createManagedImage(redbar); + private static Image greenbarIcon = Activator.createManagedImage(greenbar); + + private RedGreenBar() { + } + +// public static void draw(Event event, int columnWith, ICounter counter) { +// draw(event, columnWith, counter, counter.getTotalCount()); +// } + + public static void draw(Event event, int columnWith, Coverage coverage) { + int maxTotal = coverage.getTotalCodeSize(); + if (maxTotal == 0) + return; + final int maxWidth = getMaxWidth(event, columnWith); + final int redLength = maxWidth * coverage.getMissedCodeSize() / maxTotal; + bar(event, redbarIcon, 0, redLength); + final int greenLength = maxWidth * coverage.getCoveredCodeSize() / maxTotal; + bar(event, greenbarIcon, redLength, greenLength); + } + + private static void bar(Event event, Image image, int xOffset, int width) { + final int height = event.getBounds().height - BORDER_TOP - BORDER_BOTTOM; + event.gc.drawImage(image, 0, 0, 1, 10, event.x + xOffset + BORDER_LEFT, + event.y + BORDER_TOP, width, height); + } + + private static int getMaxWidth(Event event, int columnWith) { + final int textWidth = event.gc.textExtent(MAX_PERCENTAGE_STRING).x; + final int max = columnWith - BORDER_LEFT - BORDER_RIGHT - textWidth; + return Math.max(0, max); + } + +} \ No newline at end of file