Java Code Examples for org.eclipse.core.resources.IFile#getContentDescription()

The following examples show how to use org.eclipse.core.resources.IFile#getContentDescription() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PropertiesFileDocumentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: EditorUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean isClassFile(IFile file) {
	IContentDescription contentDescription;
	try {
		contentDescription= file.getContentDescription();
	} catch (CoreException e) {
		contentDescription= null;
	}
	if (contentDescription == null)
		return false;

	IContentType contentType= contentDescription.getContentType();
	if (contentType == null)
		return false;

	return "org.eclipse.jdt.core.javaClass".equals(contentType.getId()); //$NON-NLS-1$
}
 
Example 3
Source File: ProfilesContribution.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
private void addFromFileResource ( final List<IContributionItem> defs, final ISelection sel )
{
    final ModelLoader<Definition> loader = new ModelLoader<> ( Definition.class );

    for ( final IFile res : SelectionHelper.iterable ( sel, IFile.class ) )
    {
        try
        {
            if ( res == null || res.getContentDescription () == null || res.getContentDescription ().getContentType () == null )
            {
                continue;
            }
            if ( !CONTENT_TYPE_ID.equals ( res.getContentDescription ().getContentType ().getId () ) )
            {
                continue;
            }
        }
        catch ( final CoreException e1 )
        {
            continue;
        }
        try
        {
            final Definition def = loader.load ( res.getLocationURI () );
            if ( def != null )
            {
                addDefinition ( defs, res.getParent (), def );
            }

        }
        catch ( final Exception e )
        {
            // ignore
            StatusManager.getManager ().handle ( StatusHelper.convertStatus ( Activator.PLUGIN_ID, e ), StatusManager.LOG );
        }
    }
}
 
Example 4
Source File: TextSearchVisitor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Returns a map from IFile to IDocument for all open, dirty editors. After creation this map
 * is not modified, so returning a non-synchronized map is ok.
 *
 * @return a map from IFile to IDocument for all open, dirty editors
 */
//	private Map<IFile, IDocument> evalNonFileBufferDocuments() {
//		Map<IFile, IDocument> result= new HashMap<>();
//		IWorkbench workbench= SearchPlugin.getDefault().getWorkbench();
//		IWorkbenchWindow[] windows= workbench.getWorkbenchWindows();
//		for (IWorkbenchWindow window : windows) {
//			IWorkbenchPage[] pages= window.getPages();
//			for (IWorkbenchPage page : pages) {
//				IEditorReference[] editorRefs= page.getEditorReferences();
//				for (IEditorReference editorRef : editorRefs) {
//					IEditorPart ep= editorRef.getEditor(false);
//					if (ep instanceof ITextEditor && ep.isDirty()) { // only dirty editors
//						evaluateTextEditor(result, ep);
//					}
//				}
//			}
//		}
//		return result;
//	}

//	private void evaluateTextEditor(Map<IFile, IDocument> result, IEditorPart ep) {
//		IEditorInput input= ep.getEditorInput();
//		if (input instanceof IFileEditorInput) {
//			IFile file= ((IFileEditorInput) input).getFile();
//			if (!result.containsKey(file)) { // take the first editor found
//				ITextFileBufferManager bufferManager= FileBuffers.getTextFileBufferManager();
//				ITextFileBuffer textFileBuffer= bufferManager.getTextFileBuffer(file.getFullPath(), LocationKind.IFILE);
//				if (textFileBuffer != null) {
//					// file buffer has precedence
//					result.put(file, textFileBuffer.getDocument());
//				} else {
//					// use document provider
//					IDocument document= ((ITextEditor) ep).getDocumentProvider().getDocument(input);
//					if (document != null) {
//						result.put(file, document);
//					}
//				}
//			}
//		}
//	}

private boolean hasBinaryContent(CharSequence seq, IFile file) throws CoreException {
	IContentDescription desc= file.getContentDescription();
	if (desc != null) {
		IContentType contentType= desc.getContentType();
		if (contentType != null && contentType.isKindOf(Platform.getContentTypeManager().getContentType(IContentTypeManager.CT_TEXT))) {
			return false;
		}
	}

	// avoid calling seq.length() at it runs through the complete file,
	// thus it would do so for all binary files.
	try {
		int limit= FileCharSequenceProvider.BUFFER_SIZE;
		for (int i= 0; i < limit; i++) {
			if (seq.charAt(i) == '\0') {
				return true;
			}
		}
	} catch (IndexOutOfBoundsException e) {
	} catch (FileCharSequenceException ex) {
		if (ex.getCause() instanceof CharConversionException) {
			return true;
		}
		throw ex;
	}
	return false;
}