view src/de/codedo/java/editor/CodedoJavaElementImageProvider.java @ 1:f7d908568cfc

Install a custom JavaElementImageProvider subclass so that the custom title image stays in place even if the editor icon is decorated e.g. by error markers.
author Dirk Olmes <dirk@xanthippe.ping.de>
date Sat, 13 Dec 2014 13:54:38 +0100
parents
children 6287f5e469f1
line wrap: on
line source

package de.codedo.java.editor;

import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

public class CodedoJavaElementImageProvider extends JavaElementImageProvider 
{
	private Set<IJavaElement> _editorsWithCustomTitleImage;
	private Image _customTitleImage;

	public CodedoJavaElementImageProvider()
	{
		super();
		_editorsWithCustomTitleImage = new HashSet<>();
	}

	@Override
	public Image getImageLabel(Object element, int flags)
	{
		if (element instanceof IJavaElement)
		{
			IJavaElement javaElement = (IJavaElement)element;
			if (_editorsWithCustomTitleImage.contains(javaElement))
			{
				return getCustomTitleImage();
			}
		}
		
		return super.getImageLabel(element, flags);
	}

	public Image toggleTitleImage(IJavaElement element)
	{
		if (_editorsWithCustomTitleImage.contains(element))
		{
			_editorsWithCustomTitleImage.remove(element);
			return super.getImageLabel(element, 0);
		}
		else
		{
			_editorsWithCustomTitleImage.add(element);
			return getCustomTitleImage();
		}
	}
	
	protected Image getCustomTitleImage()
	{
		if (_customTitleImage == null)
		{
			Image image = PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_DEF_VIEW);
			Device device = image.getDevice();

			InputStream input = getClass().getClassLoader().getResourceAsStream("icons/icon.png");
			_customTitleImage = new Image(device, input);
		}
		return _customTitleImage;
	}
}