下面列出了怎么用org.eclipse.ui.IPathEditorInput的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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;
}
/**
*
* @param fileName
* the fileName
* @return the editor with the given fileName, or null if not found.
*/
public static IEditorPart findOpenedEditor( String fileName )
{
IWorkbenchPage page = PlatformUI.getWorkbench( )
.getActiveWorkbenchWindow( )
.getActivePage( );
IEditorReference[] editors = page.getEditorReferences( );
for ( int i = 0; i < editors.length; i++ )
{
IEditorPart part = editors[i].getEditor( true );
IPath location = ( (IPathEditorInput) part.getEditorInput( ) ).getPath( );
if ( fileName.equalsIgnoreCase( location.toOSString( ) ) )
{
return part;
}
}
return null;
}
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;
}
/**
* @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;
}
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 testExecuteWithPathEditorInput() throws IOException {
File file = tempFolder.newFile( "foo.txt" );
IPathEditorInput editorInput = mockPathEditorInput( 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 );
}
}
/**
* 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;
}
public void saveReport( ModuleHandle moduleHandle, Object element,
IPath origReportPath, IProgressMonitor monitor )
{
if ( element instanceof IPathEditorInput )
{
IPathEditorInput input = (IPathEditorInput) element;
saveFile( moduleHandle,
input.getPath( ).toFile( ),
origReportPath,
monitor );
}
}
public IPath getSaveAsPath( Object element )
{
if ( element instanceof IPathEditorInput )
{
IEditorInput input = (IEditorInput) element;
SaveReportAsWizardDialog dialog = new SaveReportAsWizardDialog( UIUtil.getDefaultShell( ),
new SaveReportAsWizard( model, input ) );
if ( dialog.open( ) == Window.OK )
{
return dialog.getResult( );
}
}
return null;
}
public IPath getInputPath( IEditorInput input )
{
if ( input instanceof IPathEditorInput )
{
return ( (IPathEditorInput) input ).getPath( );
}
return null;
}
private File getInputForlder( )
{
IEditorPart editor = UIUtil.getActiveEditor( true );
if ( editor != null )
{
IEditorInput input = editor.getEditorInput( );
if ( input instanceof IPathEditorInput )
{
return ( (IPathEditorInput) input ).getPath( )
.toFile( )
.getParentFile( );
}
}
return null;
}
public boolean isReadOnly( Object element )
{
if ( element instanceof IPathEditorInput )
{
File file = ( (IPathEditorInput) element ).getPath( ).toFile( );
return !file.canWrite( );
}
return super.isReadOnly( element );
}
public boolean equals( Object obj )
{
if ( this == obj )
return true;
if ( obj instanceof IPathEditorInput )
{
obj = new ReportEditorInput( (IPathEditorInput) obj );
}
if ( !( obj instanceof ReportEditorInput ) )
return false;
return file.equals( ( (ReportEditorInput) obj ).file );
}
public void setOriginalFile( IEditorInput input )
{
String container = ( (IPathEditorInput) input ).getPath( )
.removeLastSegments( 1 )
.toOSString( );
support.setInitialFileLocation( container );
support.setInitialFileName( input.getName( ) );
}
public IEditorInput findFromOpenedPyEdits() {
Object ret = OpenEditors.iterOpenEditorsUntilFirstReturn(new ICallback<Object, IPyEdit>() {
@Override
public Object call(IPyEdit pyEdit) {
IEditorInput editorInput = (IEditorInput) pyEdit.getEditorInput();
if (editorInput instanceof IPathEditorInput) {
IPathEditorInput pathEditorInput = (IPathEditorInput) editorInput;
IPath localPath = pathEditorInput.getPath();
if (localPath != null) {
if (matchesPath(matchName, editorInput, localPath)) {
return editorInput;
}
}
} else {
File editorFile = pyEdit.getEditorFile();
if (editorFile != null) {
if (matchesFile(matchName, editorInput, editorFile)) {
return editorInput;
}
}
}
return null;
}
});
return (IEditorInput) ret;
}
private static IPathEditorInput mockPathEditorInput( File file ) throws IOException {
IPathEditorInput editorInput = mock( IPathEditorInput.class );
when( editorInput.getPath() ).thenReturn( new Path( file.getCanonicalPath() ) );
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;
}
/**
* Constructor
*
* @param input
*/
public ReportEditorInput( IPathEditorInput input )
{
this( input.getPath( ).toFile( ) );
}