KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > ui > ProgressImages


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.junit.ui;
12
13 import org.eclipse.swt.graphics.Image;
14
15 /**
16  * Manages a set of images that can show progress in the image itself.
17  */

18 public class ProgressImages {
19     private static final int PROGRESS_STEPS= 9;
20     
21     private static final String JavaDoc BASE= "prgss/"; //$NON-NLS-1$
22
private static final String JavaDoc FAILURE= "ff"; //$NON-NLS-1$
23
private static final String JavaDoc OK= "ss"; //$NON-NLS-1$
24

25     private Image[] fOKImages= new Image[PROGRESS_STEPS];
26     private Image[] fFailureImages= new Image[PROGRESS_STEPS];
27     
28     private void load() {
29         if (isLoaded())
30             return;
31             
32         for (int i= 0; i < PROGRESS_STEPS; i++) {
33             String JavaDoc okname= BASE+OK+Integer.toString(i+1)+".gif"; //$NON-NLS-1$
34
fOKImages[i]= createImage(okname);
35             String JavaDoc failurename= BASE+FAILURE+Integer.toString(i+1)+".gif"; //$NON-NLS-1$
36
fFailureImages[i]= createImage(failurename);
37         }
38     }
39
40     private Image createImage(String JavaDoc name) {
41         return JUnitPlugin.getImageDescriptor(name).createImage();
42     }
43     
44     public void dispose() {
45         if (!isLoaded())
46             return;
47                 
48         for (int i= 0; i < PROGRESS_STEPS; i++) {
49             fOKImages[i].dispose();
50             fOKImages[i]= null;
51             fFailureImages[i].dispose();
52             fFailureImages[i]= null;
53         }
54     }
55     
56     public Image getImage(int current, int total, int errors, int failures) {
57         if (!isLoaded())
58             load();
59             
60         if (total == 0)
61             return fOKImages[0];
62         int index= ((current*PROGRESS_STEPS)/total)-1;
63         index= Math.min(Math.max(0, index), PROGRESS_STEPS-1);
64
65         if (errors + failures == 0)
66             return fOKImages[index];
67         return fFailureImages[index];
68     }
69     
70     private boolean isLoaded() {
71         return fOKImages[0] != null;
72     }
73 }
74
Popular Tags