下面列出了怎么用org.eclipse.ui.IFileEditorInput的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* This method will get the DesignerProject for the current XPage and return it.
* @param compEditor
* @return
*/
private DesignerProject getDesignerProjectForEditor(CompositeEditor compEditor){
IWorkbenchPart part = super.getWorkBenchPart();
if(part instanceof EditorPart){
EditorPart editor = (EditorPart)part;
IEditorInput input = editor.getEditorInput();
if(input instanceof IFileEditorInput){
IFileEditorInput fileInput = (IFileEditorInput)input;
IFile xpageFile = fileInput.getFile();
if(null != xpageFile){
IProject project = xpageFile.getProject();
if(null != project){
DesignerProject designerProj = DesignerResource.getDesignerProject(project);
if(null != designerProj){
return designerProj;
}
}
}
}
}
return null;
}
public boolean isShownInEditor(Match match, IEditorPart editor) {
Object element= match.getElement();
IJavaElement je= ((JavaElementLine) element).getJavaElement();
IEditorInput editorInput= editor.getEditorInput();
if (editorInput instanceof IFileEditorInput) {
try {
return ((IFileEditorInput)editorInput).getFile().equals(je.getCorrespondingResource());
} catch (JavaModelException e) {
return false;
}
} else if (editorInput instanceof IClassFileEditorInput) {
return ((IClassFileEditorInput)editorInput).getClassFile().equals(je);
}
return false;
}
/**
* Returns the URI for the specific editor input.
*
* @param input
* the editor input
* @return the URI, or null if none could be determined
*/
public static URI getURI(IEditorInput input)
{
if (input instanceof IFileEditorInput)
{
return ((IFileEditorInput) input).getFile().getLocationURI();
}
if (input instanceof IURIEditorInput)
{
return ((IURIEditorInput) input).getURI();
}
if (input instanceof IPathEditorInput)
{
return URIUtil.toURI(((IPathEditorInput) input).getPath());
}
return null;
}
@Override
public void setInput(Object input) {
if (input instanceof Model) {
Model model = (Model) input;
if (model.getPath() == null) {
IFile currentFile = null;
IEditorInput editorInput = editor.getEditorInput();
if (editorInput instanceof IFileEditorInput) {
currentFile = ((IFileEditorInput) editorInput).getFile();
}
if (currentFile != null) {
model.setPath(currentFile.getFullPath());
}
}
}
treeViewer.setInput(input);
treeViewer.setSelection(null, true);
}
@Override
public void init(IEditorSite site, IEditorInput input) throws PartInitException {
setSite(site);
setInput(input);
//to avoid errors, close editor, when project is not open
//use case : project deleted
if(!getProjectFromIEditorPart(this).isOpen()) {
this.getEditorSite().getPage().closeEditor(this, false);
}
// initialize whole project
unloadDocumentResources();
initializeWholeProject(getProjectFromIEditorPart(this));
// set filename as displayed tab name
activeFilePath = ((IFileEditorInput) input).getFile().getLocation();
String pathString = activeFilePath.toOSString();
this.setPartName(pathString.substring(pathString.lastIndexOf(File.separator) + 1));
activeFilePath = ((IFileEditorInput) input).getFile().getLocation();
}
public IReportProvider getProvider( IEditorInput input )
{
if ( input instanceof IFileEditorInput )
{
return new IDEFileReportProvider( );
}
else if ( input instanceof IPathEditorInput )
{
return super.getProvider( input );
}
// else
// {
// return FileReportProvider.getInstance( );
// }
return null;
}
protected void setInput( IEditorInput input )
{
// The workspace never changes for an editor. So, removing and re-adding
// the
// resourceListener is not necessary. But it is being done here for the
// sake
// of proper implementation. Plus, the resourceListener needs to be
// added
// to the workspace the first time around.
if ( isWorkspaceResource )
{
getFile( getEditorInput( ) ).getWorkspace( )
.removeResourceChangeListener( resourceListener );
}
isWorkspaceResource = input instanceof IFileEditorInput;
super.setInput( input );
if ( isWorkspaceResource )
{
getFile( getEditorInput( ) ).getWorkspace( )
.addResourceChangeListener( resourceListener );
}
}
/**
* Returns File underlying this editor input.
*
* This method should be modified, whenever new editor input is supported
* for some editor requiring access to File.
*
* @param input the editor input to operate on.
*
* @return file underlying given editor input.
*/
public static File editorInputToFile(IEditorInput input) {
if (input == null)
return null;
if (input instanceof IFileEditorInput) {
IFile file = editorInputToIFile(input);
if (file != null) {
return ResourceUtils.getAbsoluteFile(file);
}
}
else if (input instanceof IURIEditorInput) {
IURIEditorInput uriEditorInput = (IURIEditorInput) input;
return new File(uriEditorInput.getURI());
}
else if (input instanceof CommonSourceNotFoundEditorInput || input instanceof CompareEditorInput || input instanceof IStorageEditorInput) {
// do nothing
}
else{
LogHelper.logError("Unknown editor input");
}
return null;
}
/**
* Returns the project that the file for the editor belongs.
*
* @param editor
* a file editor
* @return the project the editor belongs
*/
public static IProject getProject(AbstractThemeableEditor editor)
{
if (editor != null)
{
IEditorInput editorInput = editor.getEditorInput();
if (editorInput instanceof IFileEditorInput)
{
IFileEditorInput fileEditorInput = (IFileEditorInput) editorInput;
return fileEditorInput.getFile().getProject();
}
}
return null;
}
public Match[] computeContainedMatches(AbstractTextSearchResult result, IEditorPart editor) {
//TODO same code in JavaSearchResult
IEditorInput editorInput= editor.getEditorInput();
if (editorInput instanceof IFileEditorInput) {
IFileEditorInput fileEditorInput= (IFileEditorInput) editorInput;
return computeContainedMatches(result, fileEditorInput.getFile());
} else if (editorInput instanceof IClassFileEditorInput) {
IClassFileEditorInput classFileEditorInput= (IClassFileEditorInput) editorInput;
IClassFile classFile= classFileEditorInput.getClassFile();
Object[] elements= getElements();
if (elements.length == 0)
return NO_MATCHES;
//all matches from same file:
JavaElementLine jel= (JavaElementLine) elements[0];
if (jel.getJavaElement().equals(classFile))
return collectMatches(elements);
}
return NO_MATCHES;
}
private boolean matchInput(IEditorInput input) {
if (file != null && (input instanceof IFileEditorInput)) {
return file.equals(((IFileEditorInput) input).getFile());
}
if (javaElt != null && input != null) {
IJavaElement javaElement = JavaUI.getEditorInputJavaElement(input);
if (javaElt.equals(javaElement)) {
return true;
}
IJavaElement parent = javaElt.getParent();
while (parent != null && !parent.equals(javaElement)) {
parent = parent.getParent();
}
if (parent != null && parent.equals(javaElement)) {
return true;
}
}
return false;
}
private static IPath getSourceFilePathFromEditorInput(IEditorInput editorInput) {
if (editorInput instanceof IURIEditorInput) {
URI uri = ((IURIEditorInput) editorInput).getURI();
if (uri != null) {
IPath path = URIUtil.toPath(uri);
if (path != null) {
return path;
}
}
}
if (editorInput instanceof IFileEditorInput) {
IFile file = ((IFileEditorInput) editorInput).getFile();
if (file != null) {
return file.getLocation();
}
}
if (editorInput instanceof ILocationProvider) {
return ((ILocationProvider) editorInput).getPath(editorInput);
}
return null;
}
/**
* Initializes this folding structure provider for the given editor.
*
* @param editor
*/
public TLAProofFoldingStructureProvider(TLAEditor editor)
{
canPerformFoldingCommands = true;
this.editor = editor;
this.document = editor.getDocumentProvider().getDocument(editor.getEditorInput());
foldPositions = new Vector<TLAProofPosition>();
// add this as listener to document to listen for changes
document.addDocumentListener(this);
// get a parse result if the parse result broadcaster has already stored one
if (editor.getEditorInput() instanceof IFileEditorInput) {
IFileEditorInput editorInput = (IFileEditorInput) editor.getEditorInput();
IPath location = editorInput.getFile().getLocation();
ParseResult parseResult = ParseResultBroadcaster.getParseResultBroadcaster().getParseResult(location);
if (parseResult != null)
{
newParseResult(parseResult);
}
}
// now listen to any new parse results
ParseResultBroadcaster.getParseResultBroadcaster().addParseResultListener(this);
}
private static IFile getEditorFile( IEditorReference fileRef )
{
if ( fileRef != null )
{
IEditorPart part = (IEditorPart) fileRef.getPart( false );
if ( part != null )
{
IEditorInput input = part.getEditorInput( );
if ( input != null && input instanceof IFileEditorInput )
{
return ( (IFileEditorInput) input ).getFile( );
}
}
}
return null;
}
@Override
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
if (IS_EXPERIMENT_EDITOR_INPUT.equals(property)) {
if (receiver instanceof IFileEditorInput) {
IFileEditorInput editorInput = (IFileEditorInput) receiver;
IFile file = editorInput.getFile();
if (file != null) {
try {
final String traceTypeId = file.getPersistentProperty(TmfCommonConstants.TRACETYPE);
if (traceTypeId != null && ITmfEventsEditorConstants.EXPERIMENT_INPUT_TYPE_CONSTANTS.contains(traceTypeId)) {
return true;
}
} catch (CoreException e) {
// Ignore
}
}
}
}
return false;
}
@Override
public void run(IAction action) {
IFile file = null;
IEditorInput input = targetEditor.getEditorInput();
if(input instanceof IFileEditorInput) {
file = ((IFileEditorInput) input).getFile();
}
String url = file.getLocation().toOSString();
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if(window == null) return;
IWorkbenchPage page = window.getActivePage();
if(page == null) return;
WebBrowserView part = (WebBrowserView)page.findView("com.aptana.browser.views.webbrowser");
if(part != null) {
page.bringToTop(part);
part.setURL(url);
return;
}
try {
WebBrowserView view = (WebBrowserView)page.showView("com.aptana.browser.views.webbrowser");
view.setURL(url);
} catch (PartInitException e) {
e.printStackTrace();
}
}
/**
* @return The project which contains the file that is open in the last
* active editor in the current workbench page.
*/
public static IProject getLastActiveEditorProject() {
final IEditorPart editor = getLastActiveEditor();
if (editor == null)
return null;
final IEditorInput editorInput = editor.getEditorInput();
if (editorInput instanceof IFileEditorInput) {
final IFileEditorInput input = (IFileEditorInput) editorInput;
return input.getFile().getProject();
} else if (editorInput instanceof URIEditorInput) {
final URI uri = ((URIEditorInput) editorInput).getURI();
if (uri.isPlatformResource()) {
final String platformString = uri.toPlatformString(true);
return ResourcesPlugin.getWorkspace().getRoot().findMember(platformString).getProject();
}
}
return null;
}
/**
* Creates a new properties key hyperlink.
*
* @param region the region
* @param key the properties key
* @param editor the text editor
*/
public PropertyKeyHyperlink(IRegion region, String key, ITextEditor editor) {
Assert.isNotNull(region);
Assert.isNotNull(key);
Assert.isNotNull(editor);
fRegion= region;
fPropertiesKey= key;
fEditor= editor;
fIsFileEditorInput= fEditor.getEditorInput() instanceof IFileEditorInput;
IStorageEditorInput storageEditorInput= (IStorageEditorInput)fEditor.getEditorInput();
fShell= fEditor.getEditorSite().getShell();
try {
fStorage= storageEditorInput.getStorage();
} catch (CoreException e) {
fStorage= null;
}
}
/**
* Checks whether the passed file editor input defines a Java properties file.
*
* @param element the file editor input
* @return <code>true</code> if element defines a Java properties file, <code>false</code>
* otherwise
* @throws CoreException
*
* @since 3.7
*/
public static boolean isJavaPropertiesFile(Object element) throws CoreException {
if (JAVA_PROPERTIES_FILE_CONTENT_TYPE == null || !(element instanceof IFileEditorInput))
return false;
IFileEditorInput input= (IFileEditorInput)element;
IFile file= input.getFile();
if (file == null || !file.isAccessible())
return false;
IContentDescription description= file.getContentDescription();
if (description == null || description.getContentType() == null || !description.getContentType().isKindOf(JAVA_PROPERTIES_FILE_CONTENT_TYPE))
return false;
return true;
}
private void initFromInput(IEditorInput input) throws PartInitException {
if (!(input instanceof IFileEditorInput)) {
throw new PartInitException("Invalid Input: Must be IFileEditorInput");
}
// load the graph
file = ((IFileEditorInput) input).getFile();
GraphDocLogger.LOG.info("Reading {}", file.getRawLocationURI());
graph = ResourceCache.fetchGraphDocument(file);
GraphDocLogger.LOG.info(" DONE");
DependencyModel model = graph.getDependencyModel();
graphResources = GraphResourceBuilder.forModel(model);
// set the title to the filename, excepted the file extension
String title = file.getName();
title = title.substring(0, title.lastIndexOf('.'));
this.setPartName(title);
}
@Override
public void launch(IEditorPart editor, String mode) {
IProject project = null;
IEditorInput editorInput = editor.getEditorInput();
if (editorInput instanceof IFileEditorInput) {
project = ((IFileEditorInput) editorInput).getFile().getProject();
}
launchProject(project, mode);
}
@Override
public void launch(IEditorPart editor, String mode) {
try {
IEditorInput editorInput = editor.getEditorInput();
if (editorInput instanceof IFileEditorInput) {
IFile selectObj = ((IFileEditorInput) editorInput).getFile();
launchFile(selectObj, mode);
} else {
showDialogNotImplemented(editor.getClass().getName());
}
} catch (CoreException e) {
System.out.println(e.getLocalizedMessage() + "\n");
}
}
@Override
public void launch(IEditorPart editor, String mode) {
IEditorInput editorInput = editor.getEditorInput();
if (editorInput instanceof IFileEditorInput) {
IFile selectObj = ((IFileEditorInput) editorInput).getFile();
generateBug(selectObj);
} else {
showDialogNotImplemented(editor.getClass().getName());
}
}
protected IProject getCurrentProject(ITextEditor editor) {
if (editor != null) {
IEditorInput input = editor.getEditorInput();
if (input instanceof IFileEditorInput) {
return ((IFileEditorInput) input).getFile().getProject();
}
}
return null;
}
private IFile tryGetFileFromActiveEditor() {
final Optional<IEditorPart> editor = getActiveEditor();
if (editor.isPresent() && editor.get().getEditorInput() instanceof IFileEditorInput) {
return ((IFileEditorInput) editor.get().getEditorInput()).getFile();
}
return null;
}
protected void refreshProject() {
final IFile iFile = ((IFileEditorInput) getEditorPart().getEditorInput()).getFile();
final IProject project = iFile.getProject();
try {
project.refreshLocal(IResource.DEPTH_INFINITE, null);
} catch (final CoreException e) {
Activator.showExceptionDialog(e);
}
}
@Override
public boolean equals(final Object obj) {
if (obj == null) {
return false;
}
if (obj instanceof IFileEditorInput) {
return getFile().equals(((IFileEditorInput) obj).getFile());
}
return false;
}
@Override
public void launch(IEditorPart editor, String mode) {
IEditorInput editorInput = editor.getEditorInput();
if (editorInput instanceof IFileEditorInput) {
IFile selectObj = ((IFileEditorInput) editorInput).getFile();
launchFile(selectObj, mode);
} else {
showDialogNotImplemented(editor.getClass().getName());
}
}
/**
* Obtains a file existing on an editor.
* @param editor the editor
* @return the file existing on the editor, or <code>null</code> if none
*/
public static IFile getInputFile(IEditorPart editor) {
IEditorInput input = editor.getEditorInput();
if (input instanceof IFileEditorInput) {
IFile file = ((IFileEditorInput)input).getFile();
return file;
}
return null;
}
private IResource getResource() {
IEditorInput input= fTextEditor.getEditorInput();
if (input instanceof IFileEditorInput) {
IFileEditorInput fileInput= (IFileEditorInput) input;
return fileInput.getFile();
}
return null;
}