Apr 24, 2008

Save hours of EMF coding ...

Questions:

  • How would you perform a deep copy an EObject with all it references?
  • How would you resolve all the proxies of an EObject?
  • How would you find whether one EObject is a child of other (directly/indirectly)?
  • How would you get the root containder of a given EObject?

These are common problems which you would face in your EMF code. Before you try to write the generic code using powerful EMF reflections, resist yourself. Somehow in between posting 5 millionth news group message and shooting 100k photographs (both the numbers are my guess, and true values should be more than that :-P ), Ed Merks finds time to do all that work for us. This nice solution, which is available in EMF itself: ECoreUtil.

Take a look at the other methods in the class. Will surely come handy sometimes.

Apr 23, 2008

Window Title in an RCP app

In Eclipse IDE, you can see the window title in the format: perspective - editPart name - Eclipse SDK - workspace. But in an RCP app, the title is usually the Product Name defined in the *.product file. This tip explains how to change it dynamically like IM clients - where the window title should be product name - user name.

Window title is *not* set on the IWorkbenchWindow. You need to do it on the IWorkbenchWindowConfigurer.

Whenever the user performs a login/logout, you need to set the window title. The details are sketchy, as its left to you whether to pass the instance of the windowConfigurer to the Login/LogoutAction or implement some sort of event listener for login/logout and do the title change there. But in general:

IWorkbenchWindowConfigurer windowConfigurer = ...;
String title = Platform.getProduct().getName();
if(loggedIn)
title = title + " - " + userName;
windowConfigurer.setTitle(title);



In case you were wondering where to get the instance of the windowConfigurer, it will be passed to your application thru the WorkbenchAdvisor.createWorkbenchWindowAdvisor() method.

Apr 1, 2008

Content type Specific File icons

Eclipse 3.4 M6 is out. It has lots of wonderful features like p2 and the installer. But one small change that is going to save a lot of developers time is the content type specific file icons. Earlier if I had to contribute an icon for a specific file, I've to write my own label decorator. Now Eclipse comes with a label decorator that does the same.

All you have to do is to define the content type and associate an editor with that content type. If you were wondering writing an editor is not a trivial task, don't worry, you can reuse the existing ones.

Assume that in my app, I have some files with extension 'config'. They are nothing but normal properties files. I would like to invoke the Properties editor on this extension and give a different icon. Here is how I create a new content type for the extension associated the Properties editor for that.

<extension point="org.eclipse.core.contenttype.contentTypes">
<content-type base-type="org.eclipse.core.runtime.properties"
file-extensions="config"

id="in.cypal.eclipse.myConfig"
name="My Config File"

priority="normal">

</content-type>

</extension
>
<extension point="org.eclipse.ui.editors">
<editor
class="org.eclipse.jdt.internal.ui.propertiesfileeditor.PropertiesFileEditor"
default="false"

extensions="config"

icon="icons/sample.gif"

id="in.cypal.eclipse.editors.myConfigEditor"

name="My Config Editor">
<contentTypeBinding contentTypeId="in.cypal.eclipse.myConfig">
</contentTypeBinding>
</editor>
</extension>





Content types are not just based on file names/extensions. If you have an xml file, you can use the XMLContentDescriber to find the content type based on the root element; create an editor extension; specify the Text Editor/XML Editor class; associate the content id and add the icon to the editor. You are done. All of this without writing a single line of code!