view src/de/codedo/java/editor/CodedoJavaEditor.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 ca07a15b6cc8
children 6287f5e469f1
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()
	{
		IEditorInput input = getEditorInput();
		
		// this class is registered as Java editor so we should always have a java element here
		IJavaElement javaElement = (IJavaElement)input.getAdapter(IJavaElement.class);

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