view src/de/codedo/java/editor/CodedoJavaElementImageProvider.java @ 9:935df68696c0 default tip

Reflection Code in eigene Klasse verschoben.
author Dirk Olmes <dirk.olmes@codedo.de>
date Thu, 15 Oct 2020 12:05:06 +0200
parents 778c251baa66
children
line wrap: on
line source

package de.codedo.java.editor;

import java.io.IOException;
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();
		}
	}
	
	private Image getCustomTitleImage()
	{
		if (_customTitleImage == null)
		{
			Image image = PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_DEF_VIEW);
			Device device = image.getDevice();

			try (InputStream input = getClass().getClassLoader().getResourceAsStream("icons/icon.png")) 
			{
				_customTitleImage = new Image(device, input);
			}
			catch (IOException ex)
			{
				throw new IllegalStateException(ex);
			}
		}
		return _customTitleImage;
	}

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