new class to manage icons. Showing failures,etc on a test suite.
[phpeclipse.git] / net.sourceforge.phpeclipse.phpunit / src / net / sourceforge / phpeclipse / phpunit / PHPUnitImages.java
1 package net.sourceforge.phpeclipse.phpunit;
2
3 import java.net.MalformedURLException;
4 import java.net.URL;
5
6 import org.eclipse.jface.resource.ImageDescriptor;
7 import org.eclipse.jface.resource.ImageRegistry;
8 import org.eclipse.swt.graphics.Image;
9
10 public class PHPUnitImages {
11
12         protected static final String NAME_PREFIX =
13                 "net.sourceforge.phpeclipse.phpunit";
14         protected static final int NAME_PREFIX_LENGTH = NAME_PREFIX.length();
15
16         protected static URL iconBaseURL;
17
18         static {
19                 String pathSuffix = "icons/";
20                 try {
21                         iconBaseURL =
22                                 new URL(
23                                         PHPUnitPlugin.getDefault().getDescriptor().getInstallURL(),
24                                         pathSuffix);
25                 } catch (MalformedURLException e) {
26                         //PHPUnitPlugin.log(e);
27                         e.printStackTrace();
28                 }
29         }
30
31         protected static final ImageRegistry IMAGE_REGISTRY = new ImageRegistry();
32
33         /*
34          * Available cached Images in the Java plugin image registry.
35          */
36
37         public static final String IMG_SELECT_TEST_SUITE =
38                 NAME_PREFIX + "tsuite.gif";
39         public static final String IMG_RUN_TEST_SUITE = NAME_PREFIX + "start.gif";
40         public static final String IMG_TEST_ERROR = NAME_PREFIX + "testerr.gif";
41         public static final String IMG_TEST_FAILURE = NAME_PREFIX + "testfail.gif";
42         public static final String IMG_TEST_PASS = NAME_PREFIX + "testok.gif";
43         public static final String IMG_TEST_SUITE_ERROR = NAME_PREFIX + "tsuiteerror.gif";
44         public static final String IMG_TEST_SUITE_PASS = NAME_PREFIX + "tsuiteok.gif";
45         public static final String IMG_TEST_SUITE_FAILURE = NAME_PREFIX + "tsuitefail.gif";
46         
47         
48
49
50         public static final ImageDescriptor DESC_SELECT_TEST_SUITE = createManaged(IMG_SELECT_TEST_SUITE);
51         public static final ImageDescriptor DESC_RUN_TEST_SUITE =createManaged(IMG_RUN_TEST_SUITE);
52         public static final ImageDescriptor DESC_TEST_ERROR = createManaged(IMG_TEST_ERROR);
53         public static final ImageDescriptor DESC_TEST_FAILURE = createManaged(IMG_TEST_FAILURE);
54         public static final ImageDescriptor DESC_TEST_PASS = createManaged(IMG_TEST_PASS);
55         public static final ImageDescriptor DESC_TEST_SUITE_ERROR = createManaged(IMG_TEST_SUITE_ERROR);
56         public static final ImageDescriptor DESC_TEST_SUITE_PASS = createManaged(IMG_TEST_SUITE_PASS);
57         public static final ImageDescriptor DESC_TEST_SUITE_FAILURE = createManaged(IMG_TEST_SUITE_FAILURE);
58         
59         /**
60          * Returns the image managed under the given key in this registry.
61          * 
62          * @param key the image's key
63          * @return the image managed under the given key
64          */
65         public static Image get(String key) {
66                 return IMAGE_REGISTRY.get(key);
67         }
68
69         public static ImageRegistry getImageRegistry() {
70                 return IMAGE_REGISTRY;
71         }
72
73         //---- Helper methods to access icons on the file system --------------------------------------
74
75         protected static ImageDescriptor createManaged(String name) {
76                 try {
77                         ImageDescriptor result =
78                                 ImageDescriptor.createFromURL(
79                                         makeIconFileURL(name.substring(NAME_PREFIX_LENGTH)));
80                         IMAGE_REGISTRY.put(name, result);
81                         return result;
82                 } catch (MalformedURLException e) {
83                         return ImageDescriptor.getMissingImageDescriptor();
84                 }
85         }
86
87         protected static URL makeIconFileURL(String name)
88                 throws MalformedURLException {
89                 if (iconBaseURL == null)
90                         throw new MalformedURLException();
91
92                 return new URL(iconBaseURL, name);
93         }
94 }