Skip to content

Commit

Permalink
[eclipse-cdt#484] Add extension point for custom icons of C/C++ sourc…
Browse files Browse the repository at this point in the history
…e files

fixes eclipse-cdt#484
  • Loading branch information
ghentschke committed Aug 2, 2023
1 parent d6c4e73 commit ab1b558
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 2 deletions.
1 change: 1 addition & 0 deletions core/org.eclipse.cdt.ui/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<extension-point id="semanticHighlighting" name="%semanticHighlightingExtensionPoint" schema="schema/semanticHighlighting.exsd"/>
<extension-point id="newToolChainWizards" name="New ToolChain Wizards" schema="schema/newToolChainWizards.exsd"/>
<extension-point id="CCallHierarchy" name="The Call Hierarchy Tree Extension" schema="schema/CCallHierarchy.exsd"/>
<extension-point id="CFileImageProvider" name="Image Provider for C/C++ Source Files" schema="schema/CFileImageProvider.exsd"/>

<extension
point="org.eclipse.core.runtime.adapters">
Expand Down
102 changes: 102 additions & 0 deletions core/org.eclipse.cdt.ui/schema/CFileImageProvider.exsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?xml version='1.0' encoding='UTF-8'?>
<!-- Schema file written by PDE -->
<schema targetNamespace="org.eclipse.cdt.ui" xmlns="http://www.w3.org/2001/XMLSchema">
<annotation>
<appInfo>
<meta.schema plugin="org.eclipse.cdt.ui" id="CFileImageProvider" name="Image Provider for C/C++ Source Files"/>
</appInfo>
<documentation>
This extension point can be used to provide custom icons for C/C++ source files.
</documentation>
</annotation>

<element name="extension">
<annotation>
<appInfo>
<meta.element />
</appInfo>
</annotation>
<complexType>
<sequence>
<element ref="imageDescriptor"/>
</sequence>
<attribute name="point" type="string" use="required">
<annotation>
<documentation>

</documentation>
</annotation>
</attribute>
<attribute name="id" type="string">
<annotation>
<documentation>

</documentation>
</annotation>
</attribute>
<attribute name="name" type="string">
<annotation>
<documentation>

</documentation>
<appInfo>
<meta.attribute translatable="true"/>
</appInfo>
</annotation>
</attribute>
</complexType>
</element>

<element name="imageDescriptor">
<complexType>
<attribute name="class" type="string" use="required">
<annotation>
<documentation>
Class which implements ICFileImageDescriptor interface.
</documentation>
<appInfo>
<meta.attribute kind="java" basedOn=":org.eclipse.cdt.ui.ICFileImageDescriptor"/>
</appInfo>
</annotation>
</attribute>
</complexType>
</element>

<annotation>
<appInfo>
<meta.section type="since"/>
</appInfo>
<documentation>
[Enter the first release in which this extension point appears.]
</documentation>
</annotation>

<annotation>
<appInfo>
<meta.section type="examples"/>
</appInfo>
<documentation>
[Enter extension point usage example here.]
</documentation>
</annotation>

<annotation>
<appInfo>
<meta.section type="apiinfo"/>
</appInfo>
<documentation>
[Enter API information here.]
</documentation>
</annotation>

<annotation>
<appInfo>
<meta.section type="implementation"/>
</appInfo>
<documentation>
[Enter information about supplied implementation of this extension point.]
</documentation>
</annotation>


</schema>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*******************************************************************************/
package org.eclipse.cdt.internal.ui.viewsupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

Expand Down Expand Up @@ -48,11 +50,15 @@
import org.eclipse.cdt.ui.CDTSharedImages;
import org.eclipse.cdt.ui.CElementImageDescriptor;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.ICFileImageDescriptor;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
Expand All @@ -64,8 +70,12 @@
/**
* Default strategy of the C plugin for the construction of C element icons.
*/

public class CElementImageProvider {

private final ICFileImageDescriptor[] descriptors;
private final ICFileImageDescriptor defaultCFileImageDescriptor = new DefaultCFileImageDescriptor();

/**
* Flags for the CElementImageProvider:
* Generate images with overlays.
Expand Down Expand Up @@ -125,8 +135,52 @@ public class CElementImageProvider {
* the Eclipse platform, see Bug 563454
*/
private final Map<CElementImageDescriptor, CElementImageDescriptor> allDescriptors = new HashMap<>();
private static final String EXTENSION_POINT_ID = "org.eclipse.cdt.ui.CFileImageProvider"; //$NON-NLS-1$
public static final String ELEMENT_NAME = "imageDescriptor"; //$NON-NLS-1$
public static final String CLASS_NAME = "class"; //$NON-NLS-1$

public CElementImageProvider() {
descriptors = getCFileImageDescriptors();
}

private static ICFileImageDescriptor[] getCFileImageDescriptors() {
List<ICFileImageDescriptor> list = new ArrayList<>();

IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(EXTENSION_POINT_ID);
if (extensionPoint == null) {
return list.toArray(new ICFileImageDescriptor[0]);
}

for (var extension : extensionPoint.getExtensions()) {
var elements = extension.getConfigurationElements();
for (var element : elements) {
if (element.getName().equals(ELEMENT_NAME)) {
ICFileImageDescriptor descriptor = null;
try {
descriptor = (ICFileImageDescriptor) element.createExecutableExtension(CLASS_NAME);
} catch (CoreException e) {
}
if (descriptor != null) {
list.add(descriptor);
}
}
}
}
return list.toArray(new ICFileImageDescriptor[list.size()]);
}

private ICFileImageDescriptor getDescriptor(ITranslationUnit unit) {
if (descriptors.length > 0 && unit.getCProject() != null) {
var project = unit.getCProject().getProject();
if (project != null) {
for (var descriptor : descriptors) {
if (descriptor.isEnabled(project)) {
return descriptor;
}
}
}
}
return defaultCFileImageDescriptor;
}

/**
Expand Down Expand Up @@ -383,13 +437,15 @@ public ImageDescriptor getBaseImageDescriptor(ICElement celement, int renderFlag
case ICElement.C_UNIT: {
ITranslationUnit unit = (ITranslationUnit) celement;
if (unit.isHeaderUnit()) {
return CDTSharedImages.getImageDescriptor(CDTSharedImages.IMG_OBJS_TUNIT_HEADER);
return getDescriptor(unit).getHeaderImageDescriptor();
} else if (unit.isSourceUnit()) {
if (unit.isASMLanguage()) {
return CDTSharedImages.getImageDescriptor(CDTSharedImages.IMG_OBJS_TUNIT_ASM);
} else if (unit.isCXXLanguage()) {
return getDescriptor(unit).getCXXImageDescriptor();
}
}
return CDTSharedImages.getImageDescriptor(CDTSharedImages.IMG_OBJS_TUNIT);
return getDescriptor(unit).getCImageDescriptor();
}

case ICElement.C_CCONTAINER:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.eclipse.cdt.internal.ui.viewsupport;

import org.eclipse.cdt.ui.CDTSharedImages;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.ICFileImageDescriptor;
import org.eclipse.cdt.ui.SharedImagesFactory;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.resource.ImageDescriptor;

public class DefaultCFileImageDescriptor implements ICFileImageDescriptor {
private SharedImagesFactory imagesFactory = new SharedImagesFactory(CUIPlugin.getDefault());

@Override
public ImageDescriptor getCImageDescriptor() {
return imagesFactory.getImageDescriptor(CDTSharedImages.IMG_OBJS_TUNIT);
}

@Override
public ImageDescriptor getCXXImageDescriptor() {
return imagesFactory.getImageDescriptor(CDTSharedImages.IMG_OBJS_TUNIT);
}

@Override
public ImageDescriptor getHeaderImageDescriptor() {
return imagesFactory.getImageDescriptor(CDTSharedImages.IMG_OBJS_TUNIT_HEADER);
}

@Override
public boolean isEnabled(IProject project) {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.eclipse.cdt.ui;

import org.eclipse.core.resources.IProject;
import org.eclipse.jface.resource.ImageDescriptor;

/**
* C Image Descriptor Provider
* @since 8.1
*/
public interface ICFileImageDescriptor {

/**
* @return ImageDescriptor for a C source file
*/
public ImageDescriptor getCImageDescriptor();

/**
* @return ImageDescriptor for a C++ source file
*/
public ImageDescriptor getCXXImageDescriptor();

/**
* @return ImageDescriptor for a C/C++ header file
*/
public ImageDescriptor getHeaderImageDescriptor();

/**
* Checks whether the descriptor can be used for the given project.
* @param project
* @return true if the descriptor can be used for the given project.
*/
public boolean isEnabled(IProject project);

}

0 comments on commit ab1b558

Please sign in to comment.