Monday, July 23, 2007

QuickFix: Converting an Eclipse Preference Page to a View

One of the handy, time-saving and beutiful features of eclipse is the templates. You use CTL-Space (Cmd-Space on Mac) for invoking content assist that shows the currently defined templates. But most users do not bother to update the templates because the option is buried in preferences. For adding new templates to the java editor or modifying existing templates - you need to go through Preferences -> Java -> Editor -> Templates.;div>
Having the templates handy as a view will help in heavier use of templates. If you are interested in this update please vote for this enhancement at Eclipse Bugzilla.

Now for the quick fix.
Fortunately, both the viewpart and preferencepage are similar. A small plugin can convert the preference page to a view. What I have done is to take eclipse example view plugin (called SampleView). Remove all the code leaving the definition for the createPartControl() and setFocus(). Add this code in the createPartControl():

public void createPartControl(Composite parent) {
PreferenceDialog dialog = PreferencesUtil.createPreferenceDialogOn(
parent.getShell(),
"org.eclipse.jdt.ui.preferences.JavaTemplatePreferencePage",
new String[] {}, null);
PreferencePage selectedPage =
(PreferencePage) dialog.getSelectedPage();
selectedPage.createControl(parent);
}

Create the plugin and deploy it. You have the view that shows the template preference page (see the image below).


From this view, you can add/edit the templates.

Incidentally, this trick should work for any preference page that is derived from PreferencePage class.

2 comments:

Jonhnny Weslley said...

Very good!!! I liked a lot this tip. :)

KD said...

Thanks.