gt: 		IEditorPart editorPart= p.openEditor(input, editorID, activate);
context: /*******************************************************************************
   * Copyright (c) 2000, 2017 IBM Corporation and others.
   * All rights reserved. This program and the accompanying materials
   * are made available under the terms of the Eclipse Public License v1.0
   * which accompanies this distribution, and is available at
   * http://www.eclipse.org/legal/epl-v10.html
   *
   * Contributors:
   *     IBM Corporation - initial API and implementation
   *******************************************************************************/
  package org.eclipse.jdt.internal.ui.javaeditor;
  
  import java.lang.reflect.InvocationTargetException;
  import java.util.ArrayList;
  import java.util.HashSet;
  import java.util.List;
  import java.util.Set;
  
  import org.osgi.framework.Bundle;
  
  import org.eclipse.swt.SWT;
  
  import org.eclipse.core.runtime.Assert;
  import org.eclipse.core.runtime.CoreException;
  import org.eclipse.core.runtime.IPath;
  import org.eclipse.core.runtime.IProgressMonitor;
  import org.eclipse.core.runtime.ISafeRunnable;
  import org.eclipse.core.runtime.IStatus;
  import org.eclipse.core.runtime.NullProgressMonitor;
  import org.eclipse.core.runtime.Platform;
  import org.eclipse.core.runtime.SafeRunner;
  import org.eclipse.core.runtime.Status;
  import org.eclipse.core.runtime.SubProgressMonitor;
  import org.eclipse.core.runtime.content.IContentDescription;
  import org.eclipse.core.runtime.content.IContentType;
  
  import org.eclipse.core.resources.IFile;
  import org.eclipse.core.resources.IMarker;
  import org.eclipse.core.resources.IProject;
  import org.eclipse.core.resources.IResource;
  import org.eclipse.core.resources.IStorage;
  
  import org.eclipse.core.filebuffers.FileBuffers;
  import org.eclipse.core.filebuffers.ITextFileBuffer;
  import org.eclipse.core.filebuffers.ITextFileBufferManager;
  import org.eclipse.core.filebuffers.LocationKind;
  
  import org.eclipse.jface.action.Action;
  import org.eclipse.jface.action.IAction;
  import org.eclipse.jface.viewers.ISelectionProvider;
  
  import org.eclipse.jface.text.BadLocationException;
  import org.eclipse.jface.text.IDocument;
  import org.eclipse.jface.text.IRegion;
  import org.eclipse.jface.text.Region;
  import org.eclipse.jface.text.TextSelection;
  
  import org.eclipse.ui.IEditorInput;
  import org.eclipse.ui.IEditorPart;
  import org.eclipse.ui.IEditorSite;
  import org.eclipse.ui.IFileEditorInput;
  import org.eclipse.ui.IWorkbench;
  import org.eclipse.ui.IWorkbenchPage;
  import org.eclipse.ui.IWorkbenchWindow;
  import org.eclipse.ui.PartInitException;
  import org.eclipse.ui.PlatformUI;
  import org.eclipse.ui.actions.WorkspaceModifyOperation;
  import org.eclipse.ui.ide.IDE;
  import org.eclipse.ui.ide.IGotoMarker;
  import org.eclipse.ui.part.FileEditorInput;
  import org.eclipse.ui.part.MultiEditorInput;
  
  import org.eclipse.ui.texteditor.IDocumentProvider;
  import org.eclipse.ui.texteditor.ITextEditor;
  import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
  import org.eclipse.ui.texteditor.TextEditorAction;
  
  import org.eclipse.ui.editors.text.TextFileDocumentProvider;
  
  import org.eclipse.compare.rangedifferencer.IRangeComparator;
  import org.eclipse.compare.rangedifferencer.RangeDifference;
  import org.eclipse.compare.rangedifferencer.RangeDifferencer;
  
  import org.eclipse.jdt.core.IClassFile;
  import org.eclipse.jdt.core.ICompilationUnit;
  import org.eclipse.jdt.core.IJavaElement;
  import org.eclipse.jdt.core.IJavaProject;
  import org.eclipse.jdt.core.ISourceRange;
  import org.eclipse.jdt.core.ISourceReference;
  import org.eclipse.jdt.core.ITypeRoot;
  import org.eclipse.jdt.core.JavaCore;
  import org.eclipse.jdt.core.JavaModelException;
  
  import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
  import org.eclipse.jdt.internal.corext.util.Messages;
  
  import org.eclipse.jdt.ui.JavaUI;
  import org.eclipse.jdt.ui.PreferenceConstants;
  
  import org.eclipse.jdt.internal.ui.IJavaStatusConstants;
  import org.eclipse.jdt.internal.ui.JavaPlugin;
  import org.eclipse.jdt.internal.ui.compare.JavaCompareUtilities;
  import org.eclipse.jdt.internal.ui.text.LineComparator;
  
  
  /**
   * A number of routines for working with JavaElements in editors.
   *
   * Use 'isOpenInEditor' to test if an element is already open in a editor
   * Use 'openInEditor' to force opening an element in a editor
   * With 'getWorkingCopy' you get the working copy (element in the editor) of an element
   */
  public class EditorUtility {
  
  	/**
  	 * Tests if a CU is currently shown in an editor
  	 *
  	 * @param inputElement the input element
  	 * @return the IEditorPart if shown, null if element is not open in an editor
  	 */
  	public static IEditorPart isOpenInEditor(Object inputElement) {
  		IEditorPart editor= findEditor(inputElement, false);
  		if (editor != null)
  			return editor;
  
  		IEditorInput input= getEditorInput(inputElement);
  
  		if (input != null) {
  			IWorkbenchPage p= JavaPlugin.getActivePage();
  			if (p != null) {
  				return p.findEditor(input);
  			}
  		}
  
  		return null;
  	}
  
  	/**
  	 * Opens a Java editor for an element such as <code>IJavaElement</code>, <code>IFile</code>, or <code>IStorage</code>.
  	 * The editor is activated by default.
  	 *
  	 * @param inputElement the input element
  	 * @return an open editor or <code>null</code> if an external editor was opened
  	 * @throws PartInitException if the editor could not be opened or the input element is not valid.
  	 * Status code {@link IJavaStatusConstants#EDITOR_NO_EDITOR_INPUT} if opening the editor failed as
  	 * no editor input could be created for the given element.
  	 */
  	public static IEditorPart openInEditor(Object inputElement) throws PartInitException {
  		return openInEditor(inputElement, true);
  	}
  
  	/**
  	 * Opens the editor currently associated with the given element (IJavaElement, IFile, IStorage...)
  	 *
  	 * @param inputElement the input element
  	 * @param activate <code>true</code> if the editor should be activated
  	 * @return an open editor or <code>null</code> if an external editor was opened
  	 * @throws PartInitException if the editor could not be opened or the input element is not valid
  	 * Status code {@link IJavaStatusConstants#EDITOR_NO_EDITOR_INPUT} if opening the editor failed as
  	 * no editor input could be created for the given element.
  	 */
  	public static IEditorPart openInEditor(Object inputElement, boolean activate) throws PartInitException {
  
  		if (inputElement instanceof IFile) {
  			IFile file= (IFile) inputElement;
  			if (!isClassFile(file))
  				return openInEditor(file, activate);
  			inputElement= JavaCore.createClassFileFrom(file);
  		}
  
  		IEditorPart editor= findEditor(inputElement, activate);
  		if (editor != null)
  			return editor;
  
  		IEditorInput input= getEditorInput(inputElement);
  		if (input == null)
  			throwPartInitException(JavaEditorMessages.EditorUtility_no_editorInput, IJavaStatusConstants.EDITOR_NO_EDITOR_INPUT);
  
  		return openInEditor(input, getEditorID(input), activate);
  	}
  	
  	/**
  	 * Tries to find the editor for the given input element.
  	 * 
  	 * @param inputElement the input element
  	 * @param activate <code>true</code> if the found editor should be activated
  	 * @return the editor or <code>null</code>
  	 * @since 3.5
  	 */
  	private static IEditorPart findEditor(Object inputElement, boolean activate) {
  		/*
  		 * Support to navigate inside non-primary working copy.
  		 * For now we only support to navigate inside the currently
  		 * active editor.
  		 *
  		 * XXX: once we have FileStoreEditorInput as API,
  		 * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=111887
  		 * we can fix this code by creating the correct editor input
  		 * in getEditorInput(Object)
  		 */
  		if (inputElement instanceof IJavaElement) {
  			ICompilationUnit cu= (ICompilationUnit)((IJavaElement)inputElement).getAncestor(IJavaElement.COMPILATION_UNIT);
  			if (cu != null) {
  				IWorkbenchPage page= JavaPlugin.getActivePage();
  				IEditorPart editor= page != null ? editor= page.getActiveEditor() : null;
  				if (editor != null) {
  					boolean isCompareEditorInput= isCompareEditorInput(editor.getEditorInput());
  					if (isCompareEditorInput || !JavaModelUtil.isPrimary(cu)) {
  						IEditorInput editorInput;
  						if (isCompareEditorInput)
  							editorInput= editor.getAdapter(IEditorInput.class);
  						else
  							editorInput= editor.getEditorInput();
  						IJavaElement editorCU= getEditorInputJavaElement(editorInput, false);
  						if (cu.equals(editorCU)) {
  							if (activate && page.getActivePart() != editor)
  								page.activate(editor);
  							return editor;
  						}
  					}
  				}
  			}
  		}
  		return null;
  	}
  
  	/*
  	 * Avoid unnecessary loading of Compare plug-in
  	 */
  	private static boolean isComparePlugInActivated() {
  		return Platform.getBundle("org.eclipse.compare").getState() == Bundle.ACTIVE; //$NON-NLS-1$
  	}
  
  	/*
  	 * Avoid unnecessary loading of Compare plug-in
  	 */
  	public static boolean isCompareEditorInput(IEditorInput input) {
  		return isComparePlugInActivated() && JavaCompareUtilities.isCompareEditorInput(input);
  	}
  
  	/**
  	 * Selects a Java Element in an editor.
  	 *
  	 * @param part the editor part
  	 * @param element the Java element to reveal
  	 */
  	public static void revealInEditor(IEditorPart part, IJavaElement element) {
  		if (element == null)
  			return;
  
  		if (part instanceof JavaEditor) {
  			((JavaEditor) part).setSelection(element);
  			return;
  		}
  
  		// Support for non-Java editor
  		try {
  			ISourceRange range= null;
  			if (element instanceof ICompilationUnit)
  				return;
  			else if (element instanceof IClassFile)
  				return;
  
  			if (element instanceof ISourceReference)
  				range= ((ISourceReference)element).getNameRange();
  
  			if (range != null)
  				revealInEditor(part, range.getOffset(), range.getLength());
  		} catch (JavaModelException e) {
  			// don't reveal
  		}
  	}
  
  	/**
  	 * Selects and reveals the given region in the given editor part.
  	 *
  	 * @param part the editor part
  	 * @param region the region
  	 */
  	public static void revealInEditor(IEditorPart part, IRegion region) {
  		if (part != null && region != null)
  			revealInEditor(part, region.getOffset(), region.getLength());
  	}
  
  	/**
  	 * Selects and reveals the given offset and length in the given editor part.
  	 * @param editor the editor part
  	 * @param offset the offset
  	 * @param length the length
  	 */
  	public static void revealInEditor(IEditorPart editor, final int offset, final int length) {
  		if (editor instanceof ITextEditor) {
  			((ITextEditor)editor).selectAndReveal(offset, length);
  			return;
  		}
  
  		// Support for non-text editor - try IGotoMarker interface
  		final IGotoMarker gotoMarkerTarget;
  		if (editor instanceof IGotoMarker)
  			gotoMarkerTarget= (IGotoMarker)editor;
  		else
  			gotoMarkerTarget= editor != null ? (IGotoMarker)editor.getAdapter(IGotoMarker.class) : null;
  		if (gotoMarkerTarget != null) {
  			final IEditorInput input= editor.getEditorInput();
  			if (input instanceof IFileEditorInput) {
  				WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
  					@Override
  					protected void execute(IProgressMonitor monitor) throws CoreException {
  						IMarker marker= null;
  						try {
  							marker= ((IFileEditorInput)input).getFile().createMarker(IMarker.TEXT);
  							marker.setAttribute(IMarker.CHAR_START, offset);
  							marker.setAttribute(IMarker.CHAR_END, offset + length);
  
  							gotoMarkerTarget.gotoMarker(marker);
  
  						} finally {
  							if (marker != null)
  								marker.delete();
  						}
  					}
  				};
  
  				try {
  					op.run(null);
  				} catch (InvocationTargetException ex) {
  					// reveal failed
  				} catch (InterruptedException e) {
  					Assert.isTrue(false, "this operation can not be canceled"); //$NON-NLS-1$
  				}
  			}
  			return;
  		}
  
  		/*
  		 * Workaround: send out a text selection
  		 * XXX: Needs to be improved, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=32214
  		 */
  		if (editor != null && editor.getEditorSite().getSelectionProvider() != null) {
  			IEditorSite site= editor.getEditorSite();
  			if (site == null)
  				return;
  
  			ISelectionProvider provider= editor.getEditorSite().getSelectionProvider();
  			if (provider == null)
  				return;
  
  			provider.setSelection(new TextSelection(offset, length));
  		}
  	}
  
  	private static IEditorPart openInEditor(IFile file, boolean activate) throws PartInitException {
  		if (file == null)
  			throwPartInitException(JavaEditorMessages.EditorUtility_file_must_not_be_null);
  
  		IWorkbenchPage p= JavaPlugin.getActivePage();
  		if (p == null)
  			throwPartInitException(JavaEditorMessages.EditorUtility_no_active_WorkbenchPage);
  
  		IEditorPart editorPart= IDE.openEditor(p, file, activate);
  		initializeHighlightRange(editorPart);
  		return editorPart;
  	}
  
  	private static IEditorPart openInEditor(IEditorInput input, String editorID, boolean activate) throws PartInitException {
  		Assert.isNotNull(input);
  		Assert.isNotNull(editorID);
  
  		IWorkbenchPage p= JavaPlugin.getActivePage();
  		if (p == null)
  			throwPartInitException(JavaEditorMessages.EditorUtility_no_active_WorkbenchPage);
  
  