output folder when exporting rcp plugin - eclipse-rcp

In my rcp plugin i want when i export my plugin to create a folder, lets say images in the exported folder. eg my exported folder now has configuration, p2, plugins and workspace folders. I want a default folder there to store images and can referee from inside the plugin.

You are maybe going about this slightly incorrectly; you can package your images inside your plugin and then easily access them from code. Just create your image folder inside your plugin project and then select that folder on the "Binary Build" pane of the Build Configuration page of the plugin.xml editor.
You can then manage your images via a cache like this:
public class ImageCache
{
private static ImageCache s_instance = new ImageCache();
private ImageRegistry m_imageRegistry = Activator.getDefault().getImageRegistry();
public static enum AppImage
{
EXAMPLE("images/example.gif")
;
public String location;
private AppImage(String location)
{
this.location = location;
}
}
private ImageCache()
{
for(AppImage image : AppImage.values())
{
imageRegistry.put(image.name(),Activator.getImageDescriptor(image.location));
}
}
public static ImageCache instance()
{
return s_instance;
}
public final static Image get(AppImage key)
{
return instance().m_imageRegistry.get(key.name());
}
public final static ImageDescriptor getDescriptor(AppImage key)
{
return instance().m_imageRegistry.getDescriptor(key.name());
}
}
Where example.gif is in an images folder under the root of your plugin project.
See also: http://www.vogella.com/articles/EclipseRCP/article.html#tips_loadimages
and: http://obscuredclarity.blogspot.co.uk/2009/10/add-image-to-eclipse-rcp-application.html

Related

Unable to fetch OSGi Configuration Values

I have created an event handler and used OSGi configuration as below.
#Component(immediate = true,
service=EventHandler.class,
property= {
EventConstants.EVENT_TOPIC + "=" + ReplicationAction.EVENT_TOPIC
}
)
#Designate(ocd = PagePublishEventHandler.Configuration.class)
public class PagePublishEventHandler implements EventHandler {
private static String rootPage = "";
#Override
public void handleEvent(final Event event) {
}
#Activate
#Modified
public void activate(Configuration config) {
String rootPage = config.getPath();
logger.info("********ConfigurationPropertyInterface**********activate**********************");
logger.info("********rootPage********",rootPage);
}
#ObjectClassDefinition(name="AEM Plugin OSGi Configuration")
public #interface Configuration {
#AttributeDefinition(
name = "Root Page For Web Site",
description = "Configurable paths for root page",
type = AttributeType.STRING
)
String getPath() default "/content";
}
}
Inside the activate method, Value of rootPage is always blank. Do anyone has the solution on this.
I
Thanks
I ran your code in my machine and I don't see any issues with it except for the following things which I noticed.
The rootPage is defined as a static variable. Though this is not the cause of the issue in question, it might cause issues during runtime.
You are not printing the value of the rootPage in your log (probably that is why you think that the value is null?). In order to print it, use format specifiers as shown below.
logger.info("********rootPage******** {}",rootPage);

How to destroy unity objects created in an static class?

Please consider the following code. It creates a material and shader from a certain path. It's a utility to use in an editor extensions.
public static class GpuImageProcessing
{
private static readonly string matPath = Application.dataPath + "/Uplus/Zcommon/Material/ImageProcessing/";
private static Shader Gaussian2D5Shader;
private static Material Gaussian2D5Mat;
static GpuImageProcessing()
{
Gaussian2D5Shader = (Shader) AssetDatabase.LoadAssetAtPath(matPath
+ "Gaussian2D5.shader", typeof(Shader));
Gaussian2D5Mat = new Material(Gaussian2D5Shader);
}
}
Now the problem is how can I destroy this material before editor recompiles each time some script is changed? I mean after some code is changed the editor needs to recompile scripts and create a new execution context and a new version of this GpuImageProcessing will be created. I want to destroy the materials created in previous runtime.
PS: This is included in a DLL file so I can't make it a ScriptableObject and listen to the event callbacks and also because it's a utility class I really like it being static.
Thanks to #Programmer Figured out a workaround to ensure the destruction of materials. In order for this to work, the class should become a singleton and inherit from ScriptableObject. Then we can implement OnDisable() and it will be called when editor is recompiling. Here is the working example:
public class GpuImageProcessing:ScriptableObject
{
private static readonly string matPath = "Assets/Uplus/Zcommon/Material/ImageProcessing/";
private Material Gaussian2D5Mat;
private Material Gaussian1DVariableMat;
private static GpuImageProcessing _instance;
private bool destroyedAlready;
public static GpuImageProcessing Instance
{
get
{
if (_instance != null) return _instance;
_instance= CreateInstance<GpuImageProcessing>();
_instance.Init();
return _instance;
}
}
private void Init()
{
var Gaussian2D5Shader = (Shader)AssetDatabase.LoadAssetAtPath(matPath + "Gaussian2D5.shader", typeof(Shader));
Gaussian2D5Mat = new Material(Gaussian2D5Shader);
var gaus1dVarShader = (Shader)AssetDatabase.LoadAssetAtPath(matPath + "Gaussian1DVariable.shader", typeof(Shader));
Gaussian1DVariableMat = new Material(gaus1dVarShader);
}
private void OnDisable()
{
Debug.Log("On disable GpuImgProc");
if(destroyedAlready) return;
DestroyImmediate(Gaussian2D5Mat);
DestroyImmediate(Gaussian1DVariableMat);
destroyedAlready = true;
DestroyImmediate(this);
}
}
Important: Please notice the destroyedAlready field. It ensures materials are destroyed only once since the OnDisable is called two times (one when unity editor invokes it and one when we DestroyImmediate(this)). Please set destroyedAlready before calling DestroyImmediate(this).
While not the exact solution for original question and not a nice solution, it does work and prevents the memory leak while preserving the static nature of the class.

Deleting values from Project Preference store

In my custom plugin I am creating some folders on a new Wizard and adding paths of those created folders in project preferences. Now while deleting the folder I need to remove that path from the store. For that I extended org.eclipse.ltk.core.refactoring.deleteParticipants extension. I also created a class extending Change class and inside it in the perform method I wrote following to remove the key-value from preferences.
public Change perform(IProgressMonitor pm) throws CoreException {
System.out.println("called");
IPath deletedFolderPath = deletedFolder.getProjectRelativePath().makeAbsolute();
IResource[] roots = {deletedFolder.getProject()};
String[] fileSearchPatterns = {"*.properties", "*.prefs"};
FileTextSearchScope searchScope = FileTextSearchScope.newSearchScope(roots, fileSearchPatterns, false);
String regex = deletedFolderPath.addTrailingSeparator().toString();
Pattern searchPattern = Pattern.compile(regex);
TextSearchRequestor requestor = new TextSearchRequestor() {
public boolean acceptPatternMatch(TextSearchMatchAccess matchAccess) throws CoreException {
IFile matchedFile = matchAccess.getFile();
if("prefs".equals(matchedFile.getFileExtension())){
String prefKey = "";
try {
prefKey = ProjectPreferences.getKeyForValue(regex, deletedFolder.getProject());
ProjectPreferences.removeKeyFromStore(prefKey, deletedFolder.getProject());
System.out.println("after");
}
catch (BackingStoreException e) {
throw new CoreException(null);
}
}
else if(".properties".equals(matchedFile.getFileExtension())) {
}
return true;
}
};
TextSearchEngine.create().search(searchScope, requestor, searchPattern, pm);
return null;
}
but it get stuck on the line after pressing "OK" on the delete wizard.
ProjectPreferences.removeKeyFromStore(prefKey, deletedFolder.getProject())
or more specifically on the flush statement written in the removeKeyFromStore method
public static void removeKeyFromStore(String key, IProject project) throws BackingStoreException {
IEclipsePreferences projPref = getPreferences(project);
projPref.remove(key);
projPref.flush();
}
What am I doing wrong here or any other better way to achieve the same functionality.
Thanks!!

Filtering Project explorer context menu

I have different kind of projects in the eclipse project explorer, say a normal Java project and custom project. I have menu contributions for custom project. On right click of the custom project I want to display the project related menus only, right now its showing all the menu options. I tried with removing menus by attaching IMenuListener but I don't think its the proper way. Using activities seems to be good one but I am not getting how to use it for filtering menus on context base :
This how I removing unwanted menus , I have attached this listener into the IMenumanager. But on first right click all the menus are appearing
private final class MenuListener implements IMenuListener2
{
#Override
public void menuAboutToShow(IMenuManager manager)
{
ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection(IPageLayout.ID_PROJECT_EXPLORER);
if (selection != null && !selection.isEmpty())
{
if (selection instanceof IStructuredSelection)
{
Object obj = ((IStructuredSelection) selection).getFirstElement();
if (applContainerPorjectNatureChecker.apply(obj))
{
IContributionItem[] items = manager.getItems();
for (IContributionItem item : items)
{
if (ids.contains(item.getId()))
{
item.setVisible(false);
}
}
}
}
}
}
#Override
public void menuAboutToHide(IMenuManager manager)
{
// TODO:Do Nothing
}
}
In method menuAboutToShow
public void menuAboutToShow(IMenuManager manager) {
// Get the selection object
// if selectedObj instanceof CustumProject {
// get the list of customProject actions which you want to add
// Add it like below:
for (Action action : actionList)
menuManager.add(action(action.getId()));
}
}

Running External Apps on save in Eclipse

Since we cannot setup Eclipse's RSE to use at the tool for remote editing, I have installed Unison. But how can I get Eclipse to automatically run unison on every file save? Is there an eclipse plugin available for this?
TIA
You can setup it to be run on every build. Any external tool can be run on every build, just open project's preferences, go to Builders page, click “New…”.
Depending on the importance, I would write a simple plugin to handle this.
EDIT:
All you really need to do is this:
1) Create the plugin from the templates with the RCP\PDE Eclipse install
2) Add the following code to your activator...
#Override
public void start( final BundleContext context ) throws Exception {
super.start( context );
plugin = this;
ICommandService commandService = (ICommandService)plugin.getWorkbench().getService( ICommandService.class );
commandService.addExecutionListener( new IExecutionListener() {
public void notHandled( final String commandId, final NotHandledException exception ) {}
public void postExecuteFailure( final String commandId, final ExecutionException exception ) {}
public void postExecuteSuccess( final String commandId, final Object returnValue ) {
if ( commandId.equals( "org.eclipse.ui.file.save" ) ) {
// add in your action here...
// personally, I would use a custom preference page,
// but hard coding would work ok too
}
}
public void preExecute( final String commandId, final ExecutionEvent event ) {}
} );
}