diff --git a/build/org.eclipse.cdt.make.ui.tests/src/org/eclipse/cdt/make/ui/tests/AllMakeUITests.java b/build/org.eclipse.cdt.make.ui.tests/src/org/eclipse/cdt/make/ui/tests/AllMakeUITests.java index 4f6dbf54a9d..711060c5b1e 100644 --- a/build/org.eclipse.cdt.make.ui.tests/src/org/eclipse/cdt/make/ui/tests/AllMakeUITests.java +++ b/build/org.eclipse.cdt.make.ui.tests/src/org/eclipse/cdt/make/ui/tests/AllMakeUITests.java @@ -10,21 +10,11 @@ *******************************************************************************/ package org.eclipse.cdt.make.ui.tests; -import static org.junit.Assert.fail; - -import org.eclipse.cdt.make.ui.tests.AllMakeUITests.ToDo; -import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) -@SuiteClasses({ ToDo.class }) +@SuiteClasses({ MakeUISharedImagesTest.class }) public class AllMakeUITests { - public static class ToDo { - @Test - public void test() { - fail("Not yet implemented"); - } - } } diff --git a/build/org.eclipse.cdt.make.ui.tests/src/org/eclipse/cdt/make/ui/tests/MakeUISharedImagesTest.java b/build/org.eclipse.cdt.make.ui.tests/src/org/eclipse/cdt/make/ui/tests/MakeUISharedImagesTest.java new file mode 100644 index 00000000000..08da9f426f4 --- /dev/null +++ b/build/org.eclipse.cdt.make.ui.tests/src/org/eclipse/cdt/make/ui/tests/MakeUISharedImagesTest.java @@ -0,0 +1,118 @@ +/******************************************************************************* + * Copyright (c) 2013, 2013 Andrew Gvozdev and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Andrew Gvozdev - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.make.ui.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.net.URL; + +import org.eclipse.cdt.make.internal.ui.MakeUIPlugin; +import org.eclipse.cdt.make.internal.ui.MakeUISharedImages; +import org.eclipse.core.runtime.FileLocator; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Path; +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.jface.viewers.IDecoration; +import org.eclipse.swt.graphics.Image; +import org.junit.Test; + +public class MakeUISharedImagesTest { + private static final ImageDescriptor MISSING_IMAGE_DESCRIPTOR = ImageDescriptor.getMissingImageDescriptor(); + + // sample image (IMG_ETOOL_MAKEFILE) from MakeUIPlugin bundle + private static final String KEY_ETOOL_MAKEFILE = MakeUISharedImages.IMG_ETOOL_MAKEFILE; + private static final IPath PATH_ETOOL_MAKEFILE = new Path("icons/etool16/makefile.gif"); + private static final URL URL_ETOOL_MAKEFILE= FileLocator.find(MakeUIPlugin.getDefault().getBundle(), PATH_ETOOL_MAKEFILE, null); + + /** + * Handling of missing keys. + */ + @Test + public void testNoImage() throws Exception { + ImageDescriptor descriptor = MakeUISharedImages.getImageDescriptor("missing key 1"); + assertSame(MISSING_IMAGE_DESCRIPTOR, descriptor); + + Image image1 = MakeUISharedImages.getImage("missing key 1"); + Image image2 = MakeUISharedImages.getImage("missing key 2"); + assertSame(image1, image2); + } + + /** + * Test regular images. + */ + @Test + public void testImage() throws Exception { + // create descriptor from MakeUISharedImages key + ImageDescriptor descriptorFromKey = MakeUISharedImages.getImageDescriptor(KEY_ETOOL_MAKEFILE); + assertNotSame(MISSING_IMAGE_DESCRIPTOR, descriptorFromKey); + + // create descriptor from registered bundle URL as a key + MakeUISharedImages.register(URL_ETOOL_MAKEFILE); + ImageDescriptor descriptorFromUrl = MakeUISharedImages.getImageDescriptor(URL_ETOOL_MAKEFILE.toString()); + assertNotSame(MISSING_IMAGE_DESCRIPTOR, descriptorFromUrl); + assertSame(descriptorFromKey, descriptorFromUrl); + + // verify that it is the same image + Image imageFromKey = MakeUISharedImages.getImage(KEY_ETOOL_MAKEFILE); + Image imageFromUrl = MakeUISharedImages.getImage(URL_ETOOL_MAKEFILE.toString()); + assertSame(imageFromKey, imageFromUrl); + + // verify that no leaks on second access + Image imageFromKey2 = MakeUISharedImages.getImage(KEY_ETOOL_MAKEFILE); + assertSame(imageFromKey, imageFromKey2); + } + + /** + * Test images with overlays. + */ + @Test + public void testOverlays() throws Exception { + { + Image image1 = MakeUISharedImages.getImageOverlaid(KEY_ETOOL_MAKEFILE, new String[5]); + Image image2 = MakeUISharedImages.getImage(KEY_ETOOL_MAKEFILE); + assertSame(image1, image2); + } + + { + String[] overlayKeys = new String[5]; + overlayKeys[IDecoration.BOTTOM_LEFT] = MakeUISharedImages.IMG_OVR_AUTOMATIC; + Image imageOver1 = MakeUISharedImages.getImageOverlaid(KEY_ETOOL_MAKEFILE, overlayKeys); + Image imageOver2 = MakeUISharedImages.getImageOverlaid(KEY_ETOOL_MAKEFILE, MakeUISharedImages.IMG_OVR_AUTOMATIC, IDecoration.BOTTOM_LEFT); + assertSame(imageOver1, imageOver2); + } + } + + /** + * Verify that MakeUISharedImages constants define existing images. + */ + @Test + public void testVerifyFields() throws Exception { + Class clazz = MakeUISharedImages.class; + + Field[] fields = clazz.getDeclaredFields(); + for (Field field : fields) { + String name = field.getName(); + if (name.startsWith("IMG_")) { + assertEquals("MakeUISharedImages."+name+" is not a String", String.class, field.getType()); + assertTrue("MakeUISharedImages."+name+" is not a static field", (field.getModifiers() & Modifier.STATIC) != 0); + assertTrue("MakeUISharedImages."+name+" is not a public field", (field.getModifiers() & Modifier.PUBLIC) != 0); + String imageKey = (String) field.get(null); + ImageDescriptor descriptor = MakeUISharedImages.getImageDescriptor(imageKey); + assertTrue("Missing image MakeUISharedImages."+name+"=\""+imageKey+"\"", descriptor!=MISSING_IMAGE_DESCRIPTOR); + } + } + } +} diff --git a/build/org.eclipse.cdt.make.ui/icons/ovr16/auto_co.gif b/build/org.eclipse.cdt.make.ui/icons/ovr16/auto_co.gif new file mode 100644 index 00000000000..aa7712d4f90 Binary files /dev/null and b/build/org.eclipse.cdt.make.ui/icons/ovr16/auto_co.gif differ diff --git a/build/org.eclipse.cdt.make.ui/icons/ovr16/special_co.gif b/build/org.eclipse.cdt.make.ui/icons/ovr16/special_co.gif new file mode 100644 index 00000000000..9b2d21f808f Binary files /dev/null and b/build/org.eclipse.cdt.make.ui/icons/ovr16/special_co.gif differ diff --git a/build/org.eclipse.cdt.make.ui/src/org/eclipse/cdt/make/internal/ui/MakeUISharedImages.java b/build/org.eclipse.cdt.make.ui/src/org/eclipse/cdt/make/internal/ui/MakeUISharedImages.java new file mode 100644 index 00000000000..f94dd8f0495 --- /dev/null +++ b/build/org.eclipse.cdt.make.ui/src/org/eclipse/cdt/make/internal/ui/MakeUISharedImages.java @@ -0,0 +1,135 @@ +/******************************************************************************* + * Copyright (c) 2013, 2013 Andrew Gvozdev and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Andrew Gvozdev - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.make.internal.ui; + +import java.net.URL; + +import org.eclipse.cdt.ui.CDTSharedImages; +import org.eclipse.cdt.ui.SharedImagesFactory; +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.jface.viewers.IDecoration; +import org.eclipse.swt.graphics.Image; + +/** + * A repository for common images used by org.eclipse.cdt.make.ui plugin. + *

+ * This class provides {@link Image} and {@link ImageDescriptor} + * for each named image in the interface. All {@code Image} objects provided + * by this class are managed by this class and must never be disposed + * by other clients. + *

+ *

+ * For common CDT images see {@link CDTSharedImages}. + *

+ * For common platform images see {@link org.eclipse.ui.ISharedImages} + * ({@code org.eclipse.ui.PlatformUI.getWorkbench().getSharedImages()}) + *
+ * and {@link org.eclipse.ui.ide.IDE.SharedImages}. + *

+ *

+ * Note that org.eclipse.cdt.ui.tests.misc.MakeUISharedImagesTest will verify + * existence of the images defined here. + *

+ * + * @noextend This class is not intended to be subclassed by clients. + * @noinstantiate This class is not intended to be instantiated by clients. + * + * @since 5.3 + */ +public class MakeUISharedImages { + public static final String IMG_ETOOL_MAKEFILE = "icons/etool16/makefile.gif"; //$NON-NLS-1$ + public static final String IMG_OBJS_TARGET = "icons/obj16/target_obj.gif"; //$NON-NLS-1$ + + // overlays + public static final String IMG_OVR_AUTOMATIC = "icons/ovr16/auto_co.gif"; //$NON-NLS-1$ + public static final String IMG_OVR_SPECIAL = "icons/ovr16/special_co.gif"; //$NON-NLS-1$ + + private static SharedImagesFactory imagesFactory = new SharedImagesFactory(MakeUIPlugin.getDefault()); + + /** + * Internal method. It lets register image URL from a bundle directly to the map. + * It is user responsibility to ensure that a valid URL is passed. + * + * @param url - URL of the image pointing to its location in a bundle (bundle entry). + * + * @noreference This is internal method which is not intended to be referenced by clients. + */ + public static void register(URL url) { + imagesFactory.register(url); + } + + /** + * The method retrieves an image from the internal repository according to the given key. + * The image is managed by image registry and the caller must not dispose it. + * + * @param key - one of {@code MakeUISharedImages.IMG_} constants. + *

+ * Reserved for internal usage: the key could be a string representation of URL pointing to location + * of the image in the bundle. Such URL key must be registered first with {@code register(URL url)}. + *

+ * @return the image from the repository or the default image for missing image descriptor. + */ + public static Image getImage(String key) { + return imagesFactory.getImage(key); + } + + /** + * The method retrieves an image descriptor from the internal repository according to the given key. + * See also {@link #getImage(String)}. + * + * @param key - one of {@code MakeUISharedImages.IMG_} constants. + * @return the image from the repository or {@link ImageDescriptor#getMissingImageDescriptor()}. + */ + public static ImageDescriptor getImageDescriptor(String key) { + return imagesFactory.getImageDescriptor(key); + } + + /** + * Retrieves an overlaid image from the internal repository of images. + * If there is no image one will be created. + * + * The decoration overlay for the base image will use the array of + * provided overlays. The indices of the array correspond to the values + * of the 5 overlay constants defined on {@link IDecoration}, i.e. + * {@link IDecoration#TOP_LEFT}, + * {@link IDecoration#TOP_RIGHT}, + * {@link IDecoration#BOTTOM_LEFT}, + * {@link IDecoration#BOTTOM_RIGHT} or + * {@link IDecoration#UNDERLAY}. + * + * @param baseKey the base image key. + * @param overlayKeys the keys for the overlay images. Must be + * String[5], i.e. string array of 5 elements. Put {@code null} as + * an element to the array if no overlay should be added in given quadrant. + */ + public static Image getImageOverlaid(String baseKey, String[] overlayKeys) { + return imagesFactory.getImageOverlaid(baseKey, overlayKeys); + } + + /** + * Retrieves an overlaid image descriptor from the repository of images. + * If there is no image one will be created. + * + * @param baseKey - key of the base image. Expected to be in repository. + * @param overlayKey - key of overlay image. Expected to be in repository as well. + * @param quadrant - location of overlay, one of those: + * {@link IDecoration#TOP_LEFT}, + * {@link IDecoration#TOP_RIGHT}, + * {@link IDecoration#BOTTOM_LEFT}, + * {@link IDecoration#BOTTOM_RIGHT} + * + * @return image overlaid with smaller image in the specified quadrant. + */ + public static Image getImageOverlaid(String baseKey, String overlayKey, int quadrant) { + return imagesFactory.getImageOverlaid(baseKey, overlayKey, quadrant); + } +} +