view src/de/codedo/java/editor/CodedoJavaElementImageProvider.java @ 6:6287f5e469f1

Reset the title image when an editor is closed.
author Dirk Olmes <dirk@xanthippe.ping.de>
date Mon, 07 Sep 2015 14:42:44 +0200
parents f7d908568cfc
children 778c251baa66
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;
	}

	public void resetTitleImage(IJavaElement element)
	{
		_editorsWithCustomTitleImage.remove(element);
	}
}