下面列出了怎么用org.eclipse.ui.IURIEditorInput的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public String getEncoding(Object element) {
String encoding = super.getEncoding(element);
if (encoding == null && element instanceof IStorageEditorInput) {
try {
IStorage storage = ((IStorageEditorInput) element).getStorage();
URI uri = storage2UriMapper.getUri(storage);
if (uri != null) {
encoding = encodingProvider.getEncoding(uri);
} else if (storage instanceof IEncodedStorage) {
encoding = ((IEncodedStorage)storage).getCharset();
}
} catch (CoreException e) {
throw new WrappedException(e);
}
}
if (encoding == null) {
if (isWorkspaceExternalEditorInput(element))
encoding = getWorkspaceExternalEncoding((IURIEditorInput)element);
else
encoding = getWorkspaceOrDefaultEncoding();
}
return encoding;
}
@Override
public boolean isModifiable(Object element) {
if (isWorkspaceExternalEditorInput(element)) {
URIInfo info= (URIInfo) getElementInfo(element);
if (info != null) {
if (info.updateCache) {
try {
updateCache((IURIEditorInput) element);
} catch (CoreException x) {
handleCoreException(x, "XtextDocumentProvider.isModifiable");
}
}
return info.isModifiable;
}
}
return super.isModifiable(element);
}
@Override
public boolean isReadOnly(Object element) {
if (isWorkspaceExternalEditorInput(element)) {
URIInfo info= (URIInfo) getElementInfo(element);
if (info != null) {
if (info.updateCache) {
try {
updateCache((IURIEditorInput) element);
} catch (CoreException x) {
handleCoreException(x, "XtextDocumentProvider.isReadOnly");
}
}
return info.isReadOnly;
}
}
return super.isReadOnly(element);
}
/**
* @since 2.3
*/
protected void updateCache(IURIEditorInput input) throws CoreException {
URIInfo info= (URIInfo) getElementInfo(input);
if (info != null) {
URI emfURI = toEmfUri(input.getURI());
if (emfURI != null) {
boolean readOnly = true;
if (emfURI.isFile() && !emfURI.isArchive()) {
// TODO: Should we use the ResourceSet somehow to obtain the URIConverter for the file protocol?
// see also todo below, but don't run into a stackoverflow ;-)
Map<String, ?> attributes = URIConverter.INSTANCE.getAttributes(emfURI, null);
readOnly = Boolean.TRUE.equals(attributes.get(URIConverter.ATTRIBUTE_READ_ONLY));
}
info.isReadOnly= readOnly;
info.isModifiable= !readOnly;
}
info.updateCache= false;
}
}
/**
* 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;
}
/**
* TODO : common with CoreEditorUtils
* @param input
* @return
*/
private static URI toURI(IEditorInput input) {
URI uri = null;
if (input instanceof IStorageEditorInput) {
IStorageEditorInput storageEditorInput = (IStorageEditorInput)input;
IFileRevision state = AdapterUtilities.getAdapter(storageEditorInput, IFileRevision.class);
if (state != null) {
uri = HistoryFs.toURI(state);
}
}
else if (input instanceof IURIEditorInput) {
IURIEditorInput uriEditorInput = (IURIEditorInput) input;
uri = uriEditorInput.getURI();
}
return uri;
}
/**
* 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;
}
private IPath getPath(IEditorPart otherEditor)
{
IEditorInput input = otherEditor.getEditorInput();
try
{
if (input instanceof IPathEditorInput)
{
return ((IPathEditorInput) input).getPath();
}
URI uri = (URI) input.getAdapter(URI.class);
if (uri != null)
{
return new Path(uri.getHost() + Path.SEPARATOR + uri.getPath());
}
if (input instanceof IURIEditorInput)
{
return URIUtil.toPath(((IURIEditorInput) input).getURI());
}
}
catch (Exception e)
{
}
return null;
}
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;
}
@Override
public void partActivated(IWorkbenchPartReference partRef) {
IEditorPart part = partRef.getPage().getActiveEditor();
if (!(part instanceof AbstractTextEditor))
return;
// log new active file
IEditorInput input = part.getEditorInput();
if (input instanceof IURIEditorInput) {
URI uri = ((IURIEditorInput)input).getURI();
if (uri != null && uri.getPath() != null) {
String currentFile = uri.getPath();
long currentTime = System.currentTimeMillis() / 1000;
if (!currentFile.equals(WakaTime.getDefault().lastFile) || WakaTime.getDefault().lastTime + WakaTime.FREQUENCY * 60 < currentTime) {
WakaTime.sendHeartbeat(currentFile, WakaTime.getActiveProject(), false);
WakaTime.getDefault().lastFile = currentFile;
WakaTime.getDefault().lastTime = currentTime;
}
}
}
}
public IPath getInputPath( IEditorInput input )
{
if ( input instanceof IURIEditorInput )
{
//return new Path( ( (IURIEditorInput) input ).getURI( ).getPath( ) );
URI uri = ( (IURIEditorInput) input ).getURI( );
if(uri == null && input instanceof IFileEditorInput)
return ((IFileEditorInput)input).getFile( ).getFullPath( );
IPath localPath = URIUtil.toPath( uri );
String host = uri.getHost( );
if ( host != null && localPath == null )
{
return new Path( host + uri.getPath( ) ).makeUNC( true );
}
return localPath;
}
if ( input instanceof IFileEditorInput )
{
return ( (IFileEditorInput) input ).getFile( ).getLocation( );
}
return null;
}
/**
* Returns the element contained in the EditorInput
*/
Object getElementOfInput(IEditorInput input) {
if (input instanceof IFileEditorInput) {
return ((IFileEditorInput) input).getFile();
}
if (input instanceof IURIEditorInput) {
IURIEditorInput iuriEditorInput = (IURIEditorInput) input;
URI uri = iuriEditorInput.getURI();
return new File(uri);
}
if (input instanceof PydevZipFileEditorInput) {
PydevZipFileEditorInput pydevZipFileEditorInput = (PydevZipFileEditorInput) input;
try {
IStorage storage = pydevZipFileEditorInput.getStorage();
if (storage instanceof PydevZipFileStorage) {
PydevZipFileStorage pydevZipFileStorage = (PydevZipFileStorage) storage;
return pydevZipFileStorage;
}
} catch (CoreException e) {
Log.log(e);
}
}
return null;
}
/**
* @return the IEditorInput if we're dealing with an external file (or null otherwise)
*/
public static IEditorInput getExternalFileEditorInput(ITextEditor editor) {
IEditorInput input = editor.getEditorInput();
//only return not null if it's an external file (IFileEditorInput marks a workspace file, not external file)
if (input instanceof IFileEditorInput) {
return null;
}
if (input instanceof IPathEditorInput) { //PydevFileEditorInput would enter here
return input;
}
try {
if (input instanceof IURIEditorInput) {
return input;
}
} catch (Throwable e) {
//IURIEditorInput not added until eclipse 3.3
}
//Note that IStorageEditorInput is not handled for external files (files from zip)
return input;
}
@Override
protected IAnnotationModel createAnnotationModel(Object element) throws CoreException {
if (element instanceof IFileEditorInput) {
IFileEditorInput input = (IFileEditorInput) element;
return new XtextResourceMarkerAnnotationModel(input.getFile(), issueResolutionProvider, issueUtil);
} else if (element instanceof IURIEditorInput) {
return new AnnotationModel();
}
return super.createAnnotationModel(element);
}
public static IFileStore editorInputToFileStore(IEditorInput input) {
if (input == null)
return null;
IFileStore result = null;
if (input instanceof IFileEditorInput) {
IFile file = editorInputToIFile(input);
if (file != null) {
result = ResourceUtils.toFileStore(file);
}
}
else if (input instanceof IURIEditorInput) {
IURIEditorInput uriEditorInput = (IURIEditorInput) input;
result = ResourceUtils.toFileStore(uriEditorInput.getURI());
}
else if (input instanceof CommonSourceNotFoundEditorInput || input instanceof CompareEditorInput) {
// do nothing
return null;
}
else if (input instanceof IStorageEditorInput) {
IStorageEditorInput storageEditorInput = (IStorageEditorInput)input;
IFileRevision state = AdapterUtilities.getAdapter(storageEditorInput, IFileRevision.class);
if (state != null) {
result = ResourceUtils.toFileStore(HistoryFs.toURI(state));
}
}
if (result == null){
LogHelper.logError("Unknown editor input");
}
return result;
}
private static File getFile( IEditorInput editorInput ) {
File result = null;
if( editorInput instanceof IPathEditorInput ) {
IPathEditorInput pathEditorInput = ( IPathEditorInput )editorInput;
result = pathEditorInput.getPath().toFile();
} else if( editorInput instanceof IURIEditorInput ) {
IURIEditorInput uriEditorInput = ( IURIEditorInput )editorInput;
result = URIUtil.toFile( uriEditorInput.getURI() );
}
return result;
}
@Test
public void testExecuteWithUriEditorInput() throws IOException {
File file = tempFolder.newFile( "foo.txt" );
IURIEditorInput editorInput = mockUriEditorInput( file );
executeHandler( editorInput );
assertThat( file ).doesNotExist();
}
private static void checkEditorInput( IEditorInput input ) {
if( !( input instanceof IStorageEditorInput )
&& !( input instanceof IPathEditorInput )
&& !( input instanceof IURIEditorInput ) )
{
throw new IllegalArgumentException( "Invalid input: " + input );
}
}
protected URI getURI()
{
// Now try and resolve the value as a URI...
IEditorInput input = getEditorInput();
if (input instanceof IURIEditorInput)
{
return ((IURIEditorInput) input).getURI();
}
if (input instanceof IFileEditorInput)
{
IFile file = ((IFileEditorInput) input).getFile();
return file.getLocationURI();
}
return null;
}
/**
* Gets the indexing associated with the editor.
*
* @param editor
* @return
*/
public static Index getIndex(AbstractThemeableEditor editor)
{
// NOTE: Moved from CommonContentAssistProcessor
if (editor != null)
{
IEditorInput editorInput = editor.getEditorInput();
if (editorInput instanceof IFileEditorInput)
{
IFileEditorInput fileEditorInput = (IFileEditorInput) editorInput;
IFile file = fileEditorInput.getFile();
return getIndexManager().getIndex(file.getProject().getLocationURI());
}
if (editorInput instanceof IURIEditorInput)
{
IURIEditorInput uriEditorInput = (IURIEditorInput) editorInput;
// FIXME This file may be a child, we need to check to see if there's an index with a parent URI.
return getIndexManager().getIndex(uriEditorInput.getURI());
}
if (editorInput instanceof IPathEditorInput)
{
IPathEditorInput pathEditorInput = (IPathEditorInput) editorInput;
// FIXME This file may be a child, we need to check to see if there's an index with a parent URI.
return getIndexManager().getIndex(URIUtil.toURI(pathEditorInput.getPath()));
}
}
return null;
}
/**
* Creates a fake compilation unit.
*
* @param editorInput the URI editor input
* @return the fake compilation unit
* @since 3.3
*/
private ICompilationUnit createFakeCompiltationUnit(IURIEditorInput editorInput) {
try {
final URI uri= editorInput.getURI();
final IFileStore fileStore= EFS.getStore(uri);
final IPath path= URIUtil.toPath(uri);
String fileStoreName= fileStore.getName();
if (fileStoreName == null || path == null)
return null;
WorkingCopyOwner woc= new WorkingCopyOwner() {
/*
* @see org.eclipse.jdt.core.WorkingCopyOwner#createBuffer(org.eclipse.jdt.core.ICompilationUnit)
* @since 3.2
*/
@Override
public IBuffer createBuffer(ICompilationUnit workingCopy) {
return new DocumentAdapter(workingCopy, fileStore, path);
}
};
IClasspathEntry[] cpEntries= null;
IJavaProject jp= findJavaProject(path);
if (jp != null)
cpEntries= jp.getResolvedClasspath(true);
if (cpEntries == null || cpEntries.length == 0)
cpEntries= new IClasspathEntry[] { JavaRuntime.getDefaultJREContainerEntry() };
final ICompilationUnit cu= woc.newWorkingCopy(fileStoreName, cpEntries, getProgressMonitor());
if (!isModifiable(editorInput))
JavaModelUtil.reconcile(cu);
return cu;
} catch (CoreException ex) {
return null;
}
}
/**
* Get the file path from a FileStoreEditorInput. Removes any "file:"
* from the URI to the file path if it exists.
*
* @param fileInput
* @return String
*/
public String getFilePath(IEditorInput fileInput) {
final IFile file = (IFile)fileInput.getAdapter(IFile.class);
if (file!=null) return file.getLocation().toOSString();
if (fileInput instanceof IURIEditorInput) {
String path = ((IURIEditorInput)fileInput).getURI().toString();
if (path.startsWith("file:")) path = path.substring(5);
path = path.replace("%20", " ");
return path;
}
return null;
}
@Override
public void caretMoved(CaretEvent event) {
IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
if (window == null) return;
if (window.getPartService() == null) return;
if (window.getPartService().getActivePart() == null) return;
if (window.getPartService().getActivePart().getSite() == null) return;
if (window.getPartService().getActivePart().getSite().getPage() == null) return;
if (window.getPartService().getActivePart().getSite().getPage().getActiveEditor() == null) return;
if (window.getPartService().getActivePart().getSite().getPage().getActiveEditor().getEditorInput() == null) return;
// log file if one is opened by default
IEditorInput input = window.getPartService().getActivePart().getSite().getPage().getActiveEditor().getEditorInput();
if (input instanceof IURIEditorInput) {
URI uri = ((IURIEditorInput)input).getURI();
if (uri != null && uri.getPath() != null) {
String currentFile = uri.getPath();
long currentTime = System.currentTimeMillis() / 1000;
if (!currentFile.equals(WakaTime.getDefault().lastFile) || WakaTime.getDefault().lastTime + WakaTime.FREQUENCY * 60 < currentTime) {
WakaTime.sendHeartbeat(currentFile, WakaTime.getActiveProject(), false);
WakaTime.getDefault().lastFile = currentFile;
WakaTime.getDefault().lastTime = currentTime;
}
}
}
}
@Override
public void postExecuteSuccess(String commandId, Object returnValue) {
if (commandId.equals("org.eclipse.ui.file.save")) {
IWorkbench workbench = PlatformUI.getWorkbench();
if (workbench == null) return;
IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
if (window == null) return;
if (window.getPartService() == null) return;
if (window.getPartService().getActivePart() == null) return;
if (window.getPartService().getActivePart().getSite() == null) return;
if (window.getPartService().getActivePart().getSite().getPage() == null) return;
if (window.getPartService().getActivePart().getSite().getPage().getActiveEditor() == null) return;
if (window.getPartService().getActivePart().getSite().getPage().getActiveEditor().getEditorInput() == null) return;
// log file save event
IEditorInput input = window.getPartService().getActivePart().getSite().getPage().getActiveEditor().getEditorInput();
if (input instanceof IURIEditorInput) {
URI uri = ((IURIEditorInput)input).getURI();
if (uri != null && uri.getPath() != null) {
String currentFile = uri.getPath();
long currentTime = System.currentTimeMillis() / 1000;
// always log writes
WakaTime.sendHeartbeat(currentFile, WakaTime.getActiveProject(), true);
WakaTime.getDefault().lastFile = currentFile;
WakaTime.getDefault().lastTime = currentTime;
}
}
}
}
public void init( IEditorSite site, IEditorInput input )
throws PartInitException
{
cachedSite = site;
if ( instance != null )
{
getSite( ).getWorkbenchWindow( )
.getPartService( )
.removePartListener( instance );
instance.dispose( );
}
if ( input instanceof IFileEditorInput
|| input instanceof IURIEditorInput )
{
instance = new IDEMultiPageReportEditor( );
}
else
{
instance = new MultiPageReportEditor( );
}
// must add property listener before init.
instance.addPropertyListener( this );
instance.init( site, input );
getSite( ).getWorkbenchWindow( )
.getPartService( )
.addPartListener( this );
}
@Override
public URI getURI(Object element) {
if (element instanceof IURIEditorInput) {
IURIEditorInput editorInput = (IURIEditorInput) element;
return editorInput.getURI();
}
return null;
}
/**
* @since 2.5
*/
protected String getWorkspaceExternalEncoding(IURIEditorInput element) {
URI emfURI = toEmfUri(element.getURI());
return encodingProvider.getEncoding(emfURI);
}
/**
* @since 2.3
*/
protected boolean isWorkspaceExternalEditorInput(Object element) {
return element instanceof IURIEditorInput && !(element instanceof IFileEditorInput);
}
private static IURIEditorInput mockUriEditorInput( File file ) {
IURIEditorInput editorInput = mock( IURIEditorInput.class );
when( editorInput.getURI() ).thenReturn( file.toURI() );
return editorInput;
}
/**
* Gets the URI associated with the editor.
*
* @param editor
* @return
*/
public static URI getURI(IEditorPart editor)
{
// NOTE: Moved from CommonContentAssistProcessor
if (editor != null)
{
IEditorInput editorInput = editor.getEditorInput();
if (editorInput instanceof IURIEditorInput)
{
IURIEditorInput uriEditorInput = (IURIEditorInput) editorInput;
return uriEditorInput.getURI();
}
if (editorInput instanceof IPathEditorInput)
{
IPathEditorInput pathEditorInput = (IPathEditorInput) editorInput;
return URIUtil.toURI(pathEditorInput.getPath());
}
if (editorInput instanceof IFileEditorInput)
{
IFileEditorInput fileEditorInput = (IFileEditorInput) editorInput;
return fileEditorInput.getFile().getLocationURI();
}
try
{
if (editorInput instanceof IStorageEditorInput)
{
IStorageEditorInput storageEditorInput = (IStorageEditorInput) editorInput;
IStorage storage = storageEditorInput.getStorage();
if (storage != null)
{
IPath path = storage.getFullPath();
if (path != null)
{
return URIUtil.toURI(path);
}
}
}
}
catch (CoreException e)
{
IdeLog.logError(CommonEditorPlugin.getDefault(), e);
}
if (editorInput instanceof ILocationProviderExtension)
{
ILocationProviderExtension lpe = (ILocationProviderExtension) editorInput;
return lpe.getURI(null);
}
if (editorInput instanceof ILocationProvider)
{
ILocationProvider lp = (ILocationProvider) editorInput;
return URIUtil.toURI(lp.getPath(null));
}
}
return null;
}