Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Getting A Handle to a ViewPart
Getting A Handle to a ViewPart [message #464824] Fri, 16 March 2007 02:40 Go to next message
Troy Bourdon is currently offline Troy BourdonFriend
Messages: 12
Registered: July 2009
Junior Member
Hello -

I'm having a problem getting a handle to a ViewPart. I need handle so I
can add the View as a listener to a component I've defined. However,
there doesn't seem to be an easy way to do this. I've grokked the api
and all I can come up with is the following:

IViewDescriptor descriptor =
getSite().getWorkbenchWindow().getWorkbench().getViewRegistr y().find(View.ID);


But this just gives me an IViewDescriptor when I really need a handle to
the view.

I've hacked my way around this by assuming there will only be one
instance of the View in the application so in the no-arg constructor I
set a static instance variable and provide a getInstance() method. Sort
of turning the class into a singleton but without the private
constructor. Because obviously the constructor can't be private or the
workbench framework would never be able to create the instance in the
first place.

This is a BIG hack though. There must be a way to get a handle to a View
but I just can't find it.

Thanks for the help in advance.

- Troy
Re: Getting A Handle to a ViewPart [message #464837 is a reply to message #464824] Fri, 16 March 2007 07:46 Go to previous messageGo to next message
Dani Megert is currently offline Dani MegertFriend
Messages: 3802
Registered: July 2009
Senior Member
Troy Bourdon wrote:

> Hello -
>
> I'm having a problem getting a handle to a ViewPart. I need handle so
> I can add the View as a listener to a component I've defined. However,
> there doesn't seem to be an easy way to do this. I've grokked the api
> and all I can come up with is the following:
>
> IViewDescriptor descriptor =
> getSite().getWorkbenchWindow().getWorkbench().getViewRegistr y().find(View.ID);
>
>
> But this just gives me an IViewDescriptor when I really need a handle
> to the view.
>
> I've hacked my way around this by assuming there will only be one
> instance of the View in the application so in the no-arg constructor I
> set a static instance variable and provide a getInstance() method.
> Sort of turning the class into a singleton but without the private
> constructor. Because obviously the constructor can't be private or the
> workbench framework would never be able to create the instance in the
> first place.
>
> This is a BIG hack though. There must be a way to get a handle to a
> View but I just can't find it.

See IWorkbenchPage.getViewReferences(). You can get the page(s) from the
workbench window.

Dani

>
> Thanks for the help in advance.
>
> - Troy
Re: Getting A Handle to a ViewPart [message #464863 is a reply to message #464837] Fri, 16 March 2007 13:12 Go to previous messageGo to next message
Troy Bourdon is currently offline Troy BourdonFriend
Messages: 12
Registered: July 2009
Junior Member
Thanks Dani, that seems to be the trick, although I don't have direct
access to an IWorkbenchPage from where I need the handle. However ...

PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage().
findViewReference(View.ID).getView(false);

with a cast to my View class gets me the handle.

Quite the path to get what I need. One would think that something like
PlatformUI.getWorkbench().getViewRegistry() would give me access to actual
view handles and not just IViewDescriptors.

- Troy
Re: Getting A Handle to a ViewPart [message #464894 is a reply to message #464863] Mon, 19 March 2007 09:59 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
Troy Bourdon wrote:
> Thanks Dani, that seems to be the trick, although I don't have direct
> access to an IWorkbenchPage from where I need the handle. However ...
>
> PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage().
> findViewReference(View.ID).getView(false);
>
> with a cast to my View class gets me the handle.
>
> Quite the path to get what I need. One would think that something like
> PlatformUI.getWorkbench().getViewRegistry() would give me access to actual
> view handles and not just IViewDescriptors.

Your original code uses a site and I wonder why you do not properly use
the site to get access to the page, like this:

getSite().getPage().findViewReference(View.ID).getView(false );

This way you prevent unnecessary access to global variables
and possible null pointer exceptions.

Just my personal 2 Cent,

Daniel Krügler
Re: Getting A Handle to a ViewPart [message #464896 is a reply to message #464894] Mon, 19 March 2007 13:00 Go to previous message
Troy Bourdon is currently offline Troy BourdonFriend
Messages: 12
Registered: July 2009
Junior Member
Daniel -

Yes that looks to be better way to get the handle.

Thanks;

- Troy
Previous Topic:Use Of Developped Plugins in a Workbench
Next Topic:AvantSoft Eclipse Plugin-RCP consultants, onsite-offsite available from April 1
Goto Forum:
  


Current Time: Wed Jan 15 09:34:44 GMT 2025

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

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

Back to the top