view src/de/codedo/java/editor/CodedoJavaEditor.java @ 8:778c251baa66

Anpassungen fuer aktuelles JDK. Input Stream, aus dem das Icon geladen wird, ordentlich schliessen.
author Dirk Olmes <dirk.olmes@codedo.de>
date Thu, 15 Oct 2020 09:43:34 +0200
parents 6287f5e469f1
children 935df68696c0
line wrap: on
line source

package de.codedo.java.editor;

import java.lang.reflect.Field;

import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;
import org.eclipse.jdt.internal.ui.javaeditor.JavaEditorErrorTickUpdater;
import org.eclipse.jdt.internal.ui.viewsupport.JavaUILabelProvider;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IEditorInput;

public class CodedoJavaEditor extends CompilationUnitEditor 
{
	public CodedoJavaEditor() 
	{
		super();
		installJavaElementImageProvider();
	}

	private void installJavaElementImageProvider()
	{
		try
		{
			JavaUILabelProvider labelProvider = getJavaUILabelProvider();
			setupJavaElementImageProvider(labelProvider);
		}
		catch (ReflectiveOperationException e) 
		{
			throw new RuntimeException(e);
		}
	}

	private JavaUILabelProvider getJavaUILabelProvider() throws ReflectiveOperationException
	{
		JavaEditorErrorTickUpdater tickUpdater = getJavaEditorErrorTickUpdater();

		Field field = tickUpdater.getClass().getDeclaredField("fLabelProvider");
		field.setAccessible(true);
		return (JavaUILabelProvider)field.get(tickUpdater);
	}
	
	private JavaEditorErrorTickUpdater getJavaEditorErrorTickUpdater() throws ReflectiveOperationException 
	{
		Field field = getClass().getSuperclass().getDeclaredField("fJavaEditorErrorTickUpdater");
		field.setAccessible(true);
		return (JavaEditorErrorTickUpdater)field.get(this);
	}	
	
	private void setupJavaElementImageProvider(JavaUILabelProvider labelProvider) throws ReflectiveOperationException
	{
		CodedoJavaElementImageProvider imageProvider = Activator.getDefault().getJavaElementImageProvider();
		
		Field field = labelProvider.getClass().getDeclaredField("fImageLabelProvider");
		field.setAccessible(true);
		field.set(labelProvider, imageProvider);
	}

	public void toggleTitleImage()
	{
		IJavaElement javaElement = getJavaElement();

		CodedoJavaElementImageProvider imageProvider = Activator.getDefault().getJavaElementImageProvider();
		Image newTitleImage =  imageProvider.toggleTitleImage(javaElement);
		setTitleImage(newTitleImage);
	}

	@Override
	public void dispose()
	{
		CodedoJavaElementImageProvider imageProvider = Activator.getDefault().getJavaElementImageProvider();

		IJavaElement javaElement = getJavaElement();
		imageProvider.resetTitleImage(javaElement);
		
		super.dispose();
	}
	
	private IJavaElement getJavaElement()
	{
		IEditorInput input = getEditorInput();
		
		// this class is registered as Java editor so we should always have a java element here
		return (IJavaElement)input.getAdapter(IJavaElement.class);
	}
}