Java Code Examples for org.eclipse.core.runtime.content.IContentType

The following examples show how to use org.eclipse.core.runtime.content.IContentType. These examples are extracted from open source projects. 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
public String getContentType()
{
	try
	{
		IContentType contentType = ((TextFileDocumentProvider) getDocumentProvider())
				.getContentType(getEditorInput());
		if (contentType != null)
		{
			return contentType.getId();
		}
	}
	catch (Exception e)
	{
		IdeLog.logError(CommonEditorPlugin.getDefault(), e);
	}
	return null;
}
 
Example 2
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
@Override
public IReconciler getReconciler(ISourceViewer sourceViewer) {
	if (!EditorsUI.getPreferenceStore().getBoolean(SpellingService.PREFERENCE_SPELLING_ENABLED))
		return null;

	IReconcilingStrategy strategy = new SpellingReconcileStrategy(sourceViewer, EditorsUI.getSpellingService()) {
		@Override
		protected IContentType getContentType() {
			return EditorConfigTextTools.EDITORCONFIG_CONTENT_TYPE;
		}
	};

	MonoReconciler reconciler = new MonoReconciler(strategy, false);
	reconciler.setDelay(500);
	return reconciler;
}
 
Example 4
@Override
public IReconciler getReconciler(ISourceViewer sourceViewer) {
	if (!EditorsUI.getPreferenceStore().getBoolean(SpellingService.PREFERENCE_SPELLING_ENABLED))
		return null;

	IReconcilingStrategy strategy= new SpellingReconcileStrategy(sourceViewer, EditorsUI.getSpellingService()) {
		@Override
		protected IContentType getContentType() {
			return PROPERTIES_CONTENT_TYPE;
		}
	};

	MonoReconciler reconciler= new MonoReconciler(strategy, false);
	reconciler.setDelay(500);
	return reconciler;
}
 
Example 5
@Override
protected void updateStatus() {

	super.updateStatus();

	if (jarResource == null) {
		jarResource = new JarResource();
	}
	jarResource.setPackageFragmentRoot(classFile.getClass(), classFile);

	IContentType contentType = Platform.getContentTypeManager().findContentTypeFor(getFileName());
	if (contentType != null) {
		contentTypeEncoding = contentType.getDefaultCharset();
	}
	String content = getContentString();
	if (content != null) {
		lineSeparator = LineSeparators.ofContent(new StringReader(content));
	} else {
		// Non source code, don't show.
		currentEncoding = null;
	}
}
 
Example 6
@Override
protected void updateStatus() {

	super.updateStatus();

	if (storage != null && storage.getClass().getName().equals("org.eclipse.jdt.internal.core.JarEntryFile")) {
		if (jarResource == null) {
			jarResource = new JarResource();
		}
		jarResource.setPackageFragmentRoot(storage.getClass().getSuperclass(), storage);
	}

	detectedCharset = Charsets.detect(getInputStream());
	IContentType contentType = Platform.getContentTypeManager().findContentTypeFor(getFileName());
	if (contentType != null) {
		contentTypeEncoding = contentType.getDefaultCharset();
	}
	lineSeparator = LineSeparators.ofContent(getInputStream(), getCurrentEncoding());
}
 
Example 7
Source Project: birt   Source File: PreviewToolbarMenuAction.java    License: Eclipse Public License 1.0 6 votes vote down vote up
private boolean isEnable( )
{
	IEditorPart editor = UIUtil.getActiveEditor( true );
	if ( editor != null )
	{
		IContentType[] contentTypes = Platform.getContentTypeManager( )
				.findContentTypesFor( editor.getEditorInput( ).getName( ) );
		for ( IContentType type : contentTypes )
		{
			if ( type.getId( )
					.equals( "org.eclipse.birt.report.designer.ui.editors.reportdesign" ) //$NON-NLS-1$
					|| type.getId( )
							.equals( "org.eclipse.birt.report.designer.ui.editors.reporttemplate" ) ) //$NON-NLS-1$
				return true;
		}
	}
	return false;
}
 
Example 8
Source Project: APICloud-Studio   Source File: XMLEditor.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public String getContentType()
{
	try
	{
		IContentType contentType = ((TextFileDocumentProvider) getDocumentProvider())
				.getContentType(getEditorInput());
		if (contentType != null)
		{
			IContentType baseType = contentType.getBaseType();
			if (baseType != null && IXMLConstants.CONTENT_TYPE_XML.equals(baseType.getId()))
			{
				return contentType.getId();
			}
		}
	}
	catch (Exception e)
	{
	}
	return IXMLConstants.CONTENT_TYPE_XML;
}
 
Example 9
/**
 * Determines if a given content type is associated with the filename.
 * 
 * @param filename
 * @param types
 * @return
 */
private boolean hasType(String contentTypeId, Set<IContentType> types)
{
	if (CollectionsUtil.isEmpty(types))
	{
		// FIXME this means if no content type binding is specified then we assume the build participant is valid
		// for all types!
		return true;
	}
	for (IContentType type : types)
	{
		if (type == null)
		{
			continue;
		}
		if (type.getId().equals(contentTypeId))
		{
			return true;
		}
	}
	return false;
}
 
Example 10
/**
 * Returns true if the given file can be evaluated for template-variables.<br>
 * There is no good way of detecting what is binary and what is not, so we decide what is supported by checking if
 * the content type is a sub-type of text.
 * 
 * @param file
 * @return true if the file can be processed; false, otherwise.
 */
private static boolean isSupportedFile(IFile file)
{
	IContentTypeManager manager = Platform.getContentTypeManager();
	if (manager == null)
	{
		return false;
	}
	IContentType contentType = manager.findContentTypeFor(file.getName());
	if (contentType == null)
	{
		return false;
	}
	IContentType text = manager.getContentType("org.eclipse.core.runtime.text"); //$NON-NLS-1$
	return contentType.isKindOf(text);
}
 
Example 11
Source Project: APICloud-Studio   Source File: IndexManager.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Determines if a given content type is associated with the filename.
 * 
 * @param filename
 * @param types
 * @return
 */
private boolean hasType(String filename, Set<IContentType> types)
{
	if (types == null || types.isEmpty())
	{
		return false;
	}
	for (IContentType type : types)
	{
		if (type == null)
		{
			continue;
		}
		if (type.isAssociatedWith(filename))
		{
			return true;
		}
	}
	return false;
}
 
Example 12
/**
 * Exports all the profiles to a file.
 * 
 * @since 3.6
 */
private void exportAllButtonPressed() {
	final FileDialog dialog= new FileDialog(fComposite.getShell(), SWT.SAVE);
	dialog.setText(FormatterMessages.CodingStyleConfigurationBlock_export_profiles_dialog_title);
	dialog.setFilterExtensions(new String [] {"*.xml"}); //$NON-NLS-1$
	final String lastPath= JavaPlugin.getDefault().getDialogSettings().get(fLastSaveLoadPathKey + ".loadpath"); //$NON-NLS-1$
	if (lastPath != null) {
		dialog.setFilterPath(lastPath);
	}
	final String path= dialog.open();
	if (path == null)
		return;

	JavaPlugin.getDefault().getDialogSettings().put(fLastSaveLoadPathKey + ".savepath", dialog.getFilterPath()); //$NON-NLS-1$

	final File file= new File(path);
	if (file.exists() && !MessageDialog.openQuestion(fComposite.getShell(), FormatterMessages.CodingStyleConfigurationBlock_export_profiles_overwrite_title, Messages.format(FormatterMessages.CodingStyleConfigurationBlock_export_profiles_overwrite_message, BasicElementLabels.getPathLabel(file)))) {
		return;
	}
	String encoding= ProfileStore.ENCODING;
	final IContentType type= Platform.getContentTypeManager().getContentType("org.eclipse.core.runtime.xml"); //$NON-NLS-1$
	if (type != null)
		encoding= type.getDefaultCharset();
	final Collection<Profile> profiles= new ArrayList<Profile>();
	profiles.addAll(fProfileManager.getSortedProfiles());
	try {
		fProfileStore.writeProfilesToFile(profiles, file, encoding);
	} catch (CoreException e) {
		final String title= FormatterMessages.CodingStyleConfigurationBlock_export_profiles_error_title;
		final String message= FormatterMessages.CodingStyleConfigurationBlock_export_profiles_error_message;
		ExceptionHandler.handle(e, fComposite.getShell(), title, message);
	}

}
 
Example 13
private void initializeEncoding() {
	if (fEncoding == null) {
		fExplicitEncoding= false;
		IFile file= ResourcesPlugin.getWorkspace().getRoot().getFile(fPath);
		if (file != null) {
			try {
				if (file.exists()) {
					fEncoding= file.getCharset(false);
					if (fEncoding == null) {
						fEncoding= file.getCharset(true);
					} else {
						fExplicitEncoding= true;
					}
				} else {
					IContentType contentType= Platform.getContentTypeManager().findContentTypeFor(file.getName());
					if (contentType != null)
						fEncoding= contentType.getDefaultCharset();
					if (fEncoding == null)
						fEncoding= file.getCharset(true);
				}
			} catch (CoreException e) {
				fEncoding= ResourcesPlugin.getEncoding();
				fExplicitEncoding= true;
			}
		} else {
			fEncoding= ResourcesPlugin.getEncoding();
			fExplicitEncoding= true;
		}
	}
	Assert.isNotNull(fEncoding);
}
 
Example 14
Source Project: APICloud-Studio   Source File: BuildContext.java    License: GNU General Public License v3.0 5 votes vote down vote up
protected IContentType[] getContentTypes() throws CoreException
{
	// TODO Cache this?
	IProject theProject = getProject();
	if (theProject != null)
	{
		IContentTypeMatcher matcher = theProject.getContentTypeMatcher();
		return matcher.findContentTypesFor(getName());
	}

	IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
	if (ArrayUtil.isEmpty(projects))
	{
		return Platform.getContentTypeManager().findContentTypesFor(getName());
	}

	for (IProject project : projects)
	{
		try
		{
			IContentType[] type = project.getContentTypeMatcher().findContentTypesFor(getName());
			if (type != null)
			{
				return type;
			}
		}
		catch (CoreException e)
		{
			IdeLog.logError(IndexPlugin.getDefault(), e);
		}
	}
	return NO_CONTENT_TYPES;
}
 
Example 15
Source Project: tm4e   Source File: ContentTypeLabelProvider.java    License: Eclipse Public License 1.0 5 votes vote down vote up
@Override
public String getColumnText(Object element, int columnIndex) {
	String contentTypeId = (String) element;
	switch (columnIndex) {
	case 0:
		IContentType contentType = Platform.getContentTypeManager().getContentType(contentTypeId);
		if (contentType == null) {
			return contentTypeId;
		}
		return contentType.getName() + " (" + contentType.getId() + ")";
	default:
		return ""; //$NON-NLS-1$
	}
}
 
Example 16
Source Project: tm4e   Source File: ToggleLineCommentHandler.java    License: Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the comment support from the given list of content types and null
 * otherwise.
 * 
 * @param contentTypes
 * @return the comment support from the given list of content types and null
 *         otherwise.
 */
private CommentSupport getCommentSupport(IContentType[] contentTypes) {
	LanguageConfigurationRegistryManager registry = LanguageConfigurationRegistryManager.getInstance();
	for (IContentType contentType : contentTypes) {
		if (!registry.shouldComment(contentType)) {
			continue;
		}
		CommentSupport commentSupport = registry.getCommentSupport(contentType);
		if (commentSupport != null) {
			return commentSupport;
		}
	}
	return null;
}
 
Example 17
/**
 * Constructor for user preferences (loaded from Json with Gson).
 */
public LanguageConfigurationDefinition(IContentType contentType, String path, String pluginId,
		boolean onEnterEnabled, boolean bracketAutoClosingEnabled, boolean matchingPairsEnabled) {
	super(path, pluginId);
	this.contentType = contentType;
	this.onEnterEnabled = onEnterEnabled;
	this.bracketAutoClosingEnabled = bracketAutoClosingEnabled;
	this.matchingPairsEnabled = matchingPairsEnabled;
}
 
Example 18
@Override
public ILanguageConfiguration getLanguageConfigurationFor(IContentType[] contentTypes) {
	for (IContentType contentType : contentTypes) {
		if (userDefinitions.containsKey(contentType)) {
			return userDefinitions.get(contentType).getLanguageConfiguration();
		}
		if (pluginDefinitions.containsKey(contentType)) {
			return pluginDefinitions.get(contentType).getLanguageConfiguration();
		}
	}
	return null;
}
 
Example 19
private void createContentTypeTreeViewer(Composite composite) {
	TreeViewer contentTypesViewer = new TreeViewer(composite,
			SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
	contentTypesViewer.getControl().setFont(composite.getFont());
	contentTypesViewer.setContentProvider(new ContentTypesContentProvider());
	contentTypesViewer.setLabelProvider(new ContentTypesLabelProvider());
	contentTypesViewer.setComparator(new ViewerComparator());
	contentTypesViewer.setInput(Platform.getContentTypeManager());
	GridData data = new GridData(GridData.FILL_HORIZONTAL);
	data.horizontalSpan = 2;
	contentTypesViewer.getControl().setLayoutData(data);

	contentTypesViewer.addSelectionChangedListener(event -> contentTypeText
			.setText(((IContentType) event.getStructuredSelection().getFirstElement()).toString()));
}
 
Example 20
@Override
public Object[] getChildren(Object parentElement) {
	List<Object> elements = new ArrayList<>();
	IContentType baseType = (IContentType) parentElement;
	for (IContentType contentType : manager.getAllContentTypes()) {
		if ((contentType.getBaseType() == null && baseType == null)
				|| ((contentType.getBaseType() != null && contentType.getBaseType().equals(baseType)))) {
			elements.add(contentType);
		}
	}
	return elements.toArray();
}
 
Example 21
protected IStatus validatePage(Event event) {
	infoWidget.refresh(null);
	String path = fileText.getText();
	if (path.length() == 0) {
		return new Status(IStatus.ERROR, LanguageConfigurationPlugin.PLUGIN_ID,
				LanguageConfigurationMessages.SelectLanguageConfigurationWizardPage_fileError_noSelection);
	}
	IPath p = new Path(path);
	if (!p.isAbsolute()) {
		p = ResourcesPlugin.getWorkspace().getRoot().getFile(p).getLocation();
	}
	try {
		ILanguageConfiguration configuration = LanguageConfiguration.load(new FileReader(p.toFile()));
		if (configuration == null) {
			return new Status(IStatus.ERROR, LanguageConfigurationPlugin.PLUGIN_ID,
					LanguageConfigurationMessages.SelectLanguageConfigurationWizardPage_fileError_invalid);
		}
		infoWidget.refresh(configuration);
	} catch (Exception e) {
		return new Status(IStatus.ERROR, LanguageConfigurationPlugin.PLUGIN_ID,
				LanguageConfigurationMessages.SelectLanguageConfigurationWizardPage_fileError_error
						+ e.getLocalizedMessage());
	}
	if (contentTypeText.getText().isEmpty()) {
		return new Status(IStatus.ERROR, LanguageConfigurationPlugin.PLUGIN_ID,
				LanguageConfigurationMessages.SelectLanguageConfigurationWizardPage_contentTypeError_noSelection);
	}
	IContentType contentType = ContentTypeHelper.getContentTypeById(contentTypeText.getText());
	if (contentType == null) {
		return new Status(IStatus.ERROR, LanguageConfigurationPlugin.PLUGIN_ID,
				LanguageConfigurationMessages.SelectLanguageConfigurationWizardPage_contentTypeError_invalid);
	}
	if (registryManager.getLanguageConfigurationFor(new IContentType[] { contentType }) != null) {
		return new Status(IStatus.WARNING, LanguageConfigurationPlugin.PLUGIN_ID,
				LanguageConfigurationMessages.SelectLanguageConfigurationWizardPage_contentTypeWarning_duplicate);
	}
	return null;
}
 
Example 22
private LanguageConfigurationDefinition getDefinition(IContentType contentType) {
	LanguageConfigurationDefinition bestFit = null;
	for (ILanguageConfigurationDefinition iDefinition : getDefinitions()) {
		if (iDefinition instanceof LanguageConfigurationDefinition) {
			LanguageConfigurationDefinition definition = (LanguageConfigurationDefinition) iDefinition;
			if (contentType.isKindOf(definition.getContentType())
					&& (bestFit == null || definition.getContentType().isKindOf(bestFit.getContentType()))) {
				bestFit = definition;
			}
		}

	}
	return bestFit;
}
 
Example 23
public CharacterPair getAutoClosePair(String text, Integer offset, String newCharacter, IContentType contentType) {
	LanguageConfigurationDefinition definition = getDefinition(contentType);
	if (definition == null || !definition.isBracketAutoClosingEnabled()) {
		return null;
	}
	CharacterPairSupport characterPairSupport = this._getCharacterPairSupport(contentType);
	return characterPairSupport == null ? null : characterPairSupport.getAutoClosePair(text, offset, newCharacter);
}
 
Example 24
public boolean shouldSurroundingPairs(IDocument document, int offset, IContentType contentType) {
	LanguageConfigurationDefinition definition = getDefinition(contentType);
	if (definition == null || !definition.isMatchingPairsEnabled()) {
		return false;
	}
	CharacterPairSupport characterPairSupport = this._getCharacterPairSupport(contentType);
	return characterPairSupport != null;
}
 
Example 25
public boolean shouldEnterAction(IDocument document, int offset, IContentType contentType) {
	LanguageConfigurationDefinition definition = getDefinition(contentType);
	if (definition == null || !definition.isOnEnterEnabled()) {
		return false;
	}
	OnEnterSupport onEnterSupport = this._getOnEnterSupport(contentType);
	return onEnterSupport != null;
}
 
Example 26
private void saveButtonPressed() {
	Profile selected= new CustomProfile(fProfileNameField.getText(), new HashMap<String, String>(fWorkingValues), fProfile.getVersion(), fProfileManager.getProfileVersioner().getProfileKind());

	final FileDialog dialog= new FileDialog(getShell(), SWT.SAVE);
	dialog.setText(FormatterMessages.CodingStyleConfigurationBlock_save_profile_dialog_title);
	dialog.setFilterExtensions(new String [] {"*.xml"}); //$NON-NLS-1$

	final String lastPath= JavaPlugin.getDefault().getDialogSettings().get(fLastSaveLoadPathKey + ".savepath"); //$NON-NLS-1$
	if (lastPath != null) {
		dialog.setFilterPath(lastPath);
	}
	final String path= dialog.open();
	if (path == null)
		return;

	JavaPlugin.getDefault().getDialogSettings().put(fLastSaveLoadPathKey + ".savepath", dialog.getFilterPath()); //$NON-NLS-1$

	final File file= new File(path);
	if (file.exists() && !MessageDialog.openQuestion(getShell(), FormatterMessages.CodingStyleConfigurationBlock_save_profile_overwrite_title, Messages.format(FormatterMessages.CodingStyleConfigurationBlock_save_profile_overwrite_message, BasicElementLabels.getPathLabel(file)))) {
		return;
	}
	String encoding= ProfileStore.ENCODING;
	final IContentType type= Platform.getContentTypeManager().getContentType("org.eclipse.core.runtime.xml"); //$NON-NLS-1$
	if (type != null)
		encoding= type.getDefaultCharset();
	final Collection<Profile> profiles= new ArrayList<Profile>();
	profiles.add(selected);
	try {
		fProfileStore.writeProfilesToFile(profiles, file, encoding);
	} catch (CoreException e) {
		final String title= FormatterMessages.CodingStyleConfigurationBlock_save_profile_error_title;
		final String message= FormatterMessages.CodingStyleConfigurationBlock_save_profile_error_message;
		ExceptionHandler.handle(e, getShell(), title, message);
	}
}
 
Example 27
public List<CharacterPair> getEnabledAutoClosingPairs(IContentType contentType) {
	LanguageConfigurationDefinition definition = getDefinition(contentType);
	if (definition == null || !definition.isBracketAutoClosingEnabled()) {
		return Collections.emptyList();
	}
	CharacterPairSupport characterPairSupport = this._getCharacterPairSupport(contentType);
	if (characterPairSupport == null) {
		return Collections.emptyList();
	}
	return characterPairSupport.getAutoClosingPairs();
}
 
Example 28
public List<CharacterPair> getSurroundingPairs(IContentType contentType) {
	CharacterPairSupport characterPairSupport = this._getCharacterPairSupport(contentType);
	if (characterPairSupport == null) {
		return Collections.emptyList();
	}
	return characterPairSupport.getSurroundingPairs();
}
 
Example 29
public CommentSupport getCommentSupport(IContentType contentType) {
	LanguageConfigurationDefinition value = this.getDefinition(contentType);
	if (value == null) {
		return null;
	}
	return value.getCommentSupport();
}
 
Example 30
private OnEnterSupport _getOnEnterSupport(IContentType contentType) {
	LanguageConfigurationDefinition value = this.getDefinition(contentType);
	if (value == null) {
		return null;
	}
	return value.getOnEnter();

}