org.gradle.eclipse.ImageDescriptorRegistry.java Source code

Java tutorial

Introduction

Here is the source code for org.gradle.eclipse.ImageDescriptorRegistry.java

Source

/**
 * Copyright 2010 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gradle.eclipse;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;

/**
 * @author Rene Groeschke
 * 
 * A registry that maps <code>ImageDescriptors</code> to <code>Image</code>.
 */
public class ImageDescriptorRegistry {

    private Map<ImageDescriptor, Image> fRegistry = new HashMap<ImageDescriptor, Image>(10);
    private Display fDisplay;

    /**
     * Creates a new image descriptor registry for the current or default display,
     * respectively.
     */
    public ImageDescriptorRegistry() {
        this(GradlePlugin.getStandardDisplay());
    }

    /**
     * Creates a new image descriptor registry for the given display. All images
     * managed by this registry will be disposed when the display gets disposed.
     * 
     * @param display the display the images managed by this registry are allocated for 
     */
    public ImageDescriptorRegistry(Display display) {
        fDisplay = display;
        Assert.isNotNull(fDisplay);
        hookDisplay();
    }

    /**
     * Returns the image associated with the given image descriptor.
     * 
     * @param descriptor the image descriptor for which the registry manages an image
     * @return the image associated with the image descriptor or <code>null</code>
     *  if the image descriptor can't create the requested image.
     */
    public Image get(ImageDescriptor descriptor) {
        if (descriptor == null)
            descriptor = ImageDescriptor.getMissingImageDescriptor();

        Image result = (Image) fRegistry.get(descriptor);
        if (result != null)
            return result;

        Assert.isTrue(fDisplay == GradlePlugin.getStandardDisplay(),
                GradleMessages.ImageDescriptorRegistryAllocatingImageForWrongDisplay1);
        result = descriptor.createImage();
        if (result != null)
            fRegistry.put(descriptor, result);
        return result;
    }

    /**
     * Disposes all images managed by this registry.
     */
    public void dispose() {
        for (Iterator<Image> iter = fRegistry.values().iterator(); iter.hasNext();) {
            Image image = iter.next();
            image.dispose();
        }
        fRegistry.clear();
    }

    private void hookDisplay() {
        fDisplay.disposeExec(new Runnable() {
            public void run() {
                dispose();
            }
        });
    }
}