Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » need help eliminating hack
need help eliminating hack [message #108915] Fri, 08 August 2003 09:48 Go to next message
Eclipse UserFriend
Hi all,

I have been doing some development on the AspectJ Development Tools plugin
to automatically set some preferences when the plugin is first used. In
order to get it working, I had to do a nasty hack in a couple of places:
casting an interface to its underlying concrete type. This is because the
API to save or set the preferences is not made public on the interface
class. If anyone knows of a better way to do this, or a public API I'm
missing, I'd appreciate hearing. Here are the specifics:

The following code is used to set the default editor for .java files to
the AspectJ editor. The very first cast (from IEditorRegistry to
EditorRegistry) is done to allow the save method to be called. Note that
this particular cast is also done in the Eclipse code
org.eclipse.internal.ui.dialogs.FileEditorsPreferencePage.pe rformOk(),
where it's done for the same reason (in order to call save). Maybe this
one should be exposed publicly, since even Eclipse needs it?

EditorRegistry editorRegistry =
(EditorRegistry)WorkbenchPlugin.getDefault().getEditorRegist ry(); // HACK:
cast to allow save to be called
IFileEditorMapping[] array =
WorkbenchPlugin.getDefault().getEditorRegistry().getFileEdit orMappings();
editorRegistry.setFileEditorMappings((FileEditorMapping[])ar ray); // HACK:
cast to allow set to be called
editorRegistry.setDefaultEditor("*.java", "CompilationUnitEditor");
editorRegistry.saveAssociations();

The following code is used to make our plugin's "New Aspect" and "New
AspectJ Project" wizards visible in the File->New menu. I had to cast the
IWorkbenchPage to a WorkbenchPage in order to get access to the
Perspective object.

IWorkbench wkbnch =
org.eclipse.ui.internal.WorkbenchPlugin.getDefault().getWork bench();
IWorkbenchWindow wdw = wkbnch.getActiveWorkbenchWindow();
IWorkbenchPage page = wdw.getActivePage();
IPerspectiveDescriptor pdesc = page.getPerspective();
Perspective persp =
((org.eclipse.ui.internal.WorkbenchPage)page).findPerspectiv e(pdesc);
java.util.ArrayList wizardActions = persp.getNewWizardActionIds();

Thanks in advance for any pointers.

Julie Waterhouse
AJDT Development
Re: need help eliminating hack [message #109017 is a reply to message #108915] Fri, 08 August 2003 13:57 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: simon.ibm.oti.lab

"Julie Waterhouse" <juliew@ca.ibm.com> wrote in message
news:bh09n6$idh$1@eclipse.org...
> Hi all,
>
> I have been doing some development on the AspectJ Development Tools plugin
> to automatically set some preferences when the plugin is first used. In
> order to get it working, I had to do a nasty hack in a couple of places:
> casting an interface to its underlying concrete type. This is because the
> API to save or set the preferences is not made public on the interface
> class. If anyone knows of a better way to do this, or a public API I'm
> missing, I'd appreciate hearing. Here are the specifics:
>
> The following code is used to set the default editor for .java files to
> the AspectJ editor. The very first cast (from IEditorRegistry to
> EditorRegistry) is done to allow the save method to be called. Note that
> this particular cast is also done in the Eclipse code
> org.eclipse.internal.ui.dialogs.FileEditorsPreferencePage.pe rformOk(),
> where it's done for the same reason (in order to call save). Maybe this
> one should be exposed publicly, since even Eclipse needs it?

If you want to set the default editor by code, use
IEditorRegistry.setDefaultEditor.

>
> EditorRegistry editorRegistry =
> (EditorRegistry)WorkbenchPlugin.getDefault().getEditorRegist ry(); // HACK:
> cast to allow save to be called
> IFileEditorMapping[] array =
> WorkbenchPlugin.getDefault().getEditorRegistry().getFileEdit orMappings();
> editorRegistry.setFileEditorMappings((FileEditorMapping[])ar ray); // HACK:
> cast to allow set to be called
> editorRegistry.setDefaultEditor("*.java", "CompilationUnitEditor");
> editorRegistry.saveAssociations();
>
> The following code is used to make our plugin's "New Aspect" and "New
> AspectJ Project" wizards visible in the File->New menu. I had to cast the
> IWorkbenchPage to a WorkbenchPage in order to get access to the
> Perspective object.

You should look at the extension point org.eclipse.ui.perspectiveExtensions

>
> IWorkbench wkbnch =
> org.eclipse.ui.internal.WorkbenchPlugin.getDefault().getWork bench();
> IWorkbenchWindow wdw = wkbnch.getActiveWorkbenchWindow();
> IWorkbenchPage page = wdw.getActivePage();
> IPerspectiveDescriptor pdesc = page.getPerspective();
> Perspective persp =
> ((org.eclipse.ui.internal.WorkbenchPage)page).findPerspectiv e(pdesc);
> java.util.ArrayList wizardActions = persp.getNewWizardActionIds();
>
> Thanks in advance for any pointers.
>
> Julie Waterhouse
> AJDT Development
>
Re: need help eliminating hack [message #110820 is a reply to message #109017] Wed, 13 August 2003 11:33 Go to previous message
Eclipse UserFriend
Thanks for your help. I still have one problem - see below.

Simon Arsenault wrote:

> "Julie Waterhouse" <juliew@ca.ibm.com> wrote in message
> news:bh09n6$idh$1@eclipse.org...
> > Hi all,
> >
> > I have been doing some development on the AspectJ Development Tools plugin
> > to automatically set some preferences when the plugin is first used. In
> > order to get it working, I had to do a nasty hack in a couple of places:
> > casting an interface to its underlying concrete type. This is because the
> > API to save or set the preferences is not made public on the interface
> > class. If anyone knows of a better way to do this, or a public API I'm
> > missing, I'd appreciate hearing. Here are the specifics:
> >
> > The following code is used to set the default editor for .java files to
> > the AspectJ editor. The very first cast (from IEditorRegistry to
> > EditorRegistry) is done to allow the save method to be called. Note that
> > this particular cast is also done in the Eclipse code
> > org.eclipse.internal.ui.dialogs.FileEditorsPreferencePage.pe rformOk(),
> > where it's done for the same reason (in order to call save). Maybe this
> > one should be exposed publicly, since even Eclipse needs it?

> If you want to set the default editor by code, use
> IEditorRegistry.setDefaultEditor.

This is what I first tried using. However, I found that with only this
call, the setting only takes effect for the duration of that session, and
doesn't persist once I close the workbench and restart it. It seems that
setDefaultEditor() only acts in memory. That's why I attempted to follow
it with editorRegistry.saveAssociations() (only available on the
EditorRegistry type). In fact, even setDefaultEditor() followed by
saveAssociations() wasn't sufficient. I also had to call
setFileEditorMappings().

This works, but results in the need for the casting hacks.

As I mentioned,
org.eclipse.internal.ui.dialogs.FileEditorsPreferencePage.pe rformOk() is
also doing the cast and setFileEditorMappings and saveAssociations calls.
If they need it ...will there be a better way for me? ...should it be
added to the interface? Am I still missing something?

> > EditorRegistry editorRegistry =
> > (EditorRegistry)WorkbenchPlugin.getDefault().getEditorRegist ry(); // HACK:
> > cast to allow save to be called
> > IFileEditorMapping[] array =
> > WorkbenchPlugin.getDefault().getEditorRegistry().getFileEdit orMappings();
> > editorRegistry.setFileEditorMappings((FileEditorMapping[])ar ray); // HACK:
> > cast to allow set to be called
> > editorRegistry.setDefaultEditor("*.java", "CompilationUnitEditor");
> > editorRegistry.saveAssociations();
> >
> > The following code is used to make our plugin's "New Aspect" and "New
> > AspectJ Project" wizards visible in the File->New menu. I had to cast the
> > IWorkbenchPage to a WorkbenchPage in order to get access to the
> > Perspective object.

> You should look at the extension point org.eclipse.ui.perspectiveExtensions

Ah. Thank you. What I was missing was "newWizardShortcut" under that
extension point. Much nicer.

> > IWorkbench wkbnch =
> > org.eclipse.ui.internal.WorkbenchPlugin.getDefault().getWork bench();
> > IWorkbenchWindow wdw = wkbnch.getActiveWorkbenchWindow();
> > IWorkbenchPage page = wdw.getActivePage();
> > IPerspectiveDescriptor pdesc = page.getPerspective();
> > Perspective persp =
> > ((org.eclipse.ui.internal.WorkbenchPage)page).findPerspectiv e(pdesc);
> > java.util.ArrayList wizardActions = persp.getNewWizardActionIds();
> >
> > Thanks in advance for any pointers.
> >
> > Julie Waterhouse
> > AJDT Development
> >
Previous Topic:MultiPageEditor & widget disposed
Next Topic:Text Editing, ability to join lines
Goto Forum:
  


Current Time: Wed Jul 23 10:41:30 EDT 2025

Powered by FUDForum. Page generated in 0.07050 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top