Refreshing the project

Tell us what you think about our book and report any errata. Sample chapters are available at http://www.qualityeclipse.com/

(Note: Previous Editions were called "Eclipse: Building Commercial Quality Plugins")

Moderators: Eric Clayberg, Dan Rubel

Refreshing the project

Postby Tony Weddle » Mon Sep 13, 2004 8:30 am

I can't find anything in the index about refreshing the project (apart from references to a few API descriptions). Although I'm working my way through the book, I'm also having to do some work on an existing plug-in and was hoping I could use the book as a kind of reference. In my particular case, I have a plug-in that generates new Java classes, from a wizard interface, and would like to have the project refresh, to pick up those new classes rather than require the user to do the refresh manually. Is there anything in the book about that?
Tony
Tony Weddle
 
Posts: 9
Joined: Mon Sep 13, 2004 8:22 am

Re: Refreshing the project

Postby Eric Clayberg » Mon Sep 13, 2004 3:38 pm

Tony Weddle wrote:I can't find anything in the index about refreshing the project (apart from references to a few API descriptions). Although I'm working my way through the book, I'm also having to do some work on an existing plug-in and was hoping I could use the book as a kind of reference. In my particular case, I have a plug-in that generates new Java classes, from a wizard interface, and would like to have the project refresh, to pick up those new classes rather than require the user to do the refresh manually. Is there anything in the book about that?

Have you tried the IResource refreshLocal(int depth, IProgressMonitor monitor) method? For example...

    myProject.refreshLocal(IResource.DEPTH_INFINITE, null)
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA

Re: Refreshing the project

Postby Tony Weddle » Mon Sep 13, 2004 11:25 pm

Eric Clayberg wrote:Have you tried the IResource refreshLocal(int depth, IProgressMonitor monitor) method?


Hi Eric,

Well, yes, I did try that. I think I spotted the API in the book but there was no discussion on how to use it. From my plug-in, I sometimes have a selected resouce (an org.eclipse.core.internal.resources.File). From this, I can get the project and tried the call, passing various depths and a null for the progress monitor (since I didn't know how to create a monitor and didn't think I needed one for this plug-in). However, the call appears to do nothing. Also, I can't always guarantee to have a selected resource, so how do I get to the project from my wizard (started from a popup menu in the Navigator or Package Explorer)?

Some of these questions will seem stupid, once I know what I'm doing, but are these issues addressed in the book? I would expect so, since needing the user to manually do a refresh seems the opposite of commercial quality.
Tony
Tony Weddle
 
Posts: 9
Joined: Mon Sep 13, 2004 8:22 am

Postby Tony Weddle » Tue Sep 14, 2004 1:25 am

An update.

I discovered, by drilling down though the eclipse code, that the depth parameter, for the refreshLocal call, is really a depth indicator that can only take a few values, it's not an arbitrary depth level. I set it to IResource.DEPTH_INFINITE and that worked, when I had a selected resource with which to track back to the project. I'm looking at how to get at the project without a selected resource.
Tony
Tony Weddle
 
Posts: 9
Joined: Mon Sep 13, 2004 8:22 am

Postby Eric Clayberg » Tue Sep 14, 2004 2:55 am

Tony Weddle wrote:I discovered, by drilling down though the eclipse code, that the depth parameter, for the refreshLocal call, is really a depth indicator that can only take a few values, it's not an arbitrary depth level. I set it to IResource.DEPTH_INFINITE and that worked, when I had a selected resource with which to track back to the project.

Isn't that exactly what I suggested? ;-)

Tony Weddle wrote:I'm looking at how to get at the project without a selected resource.

Use the IWorkspaceRoot getProject(String name) method. E.g.,
    ResourcesPlugin.getWorkspace().getProject("MyProject")
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA

Postby Guest » Tue Sep 14, 2004 3:45 am

Eric Clayberg wrote:Isn't that exactly what I suggested? ;-)
Oh my goodness, you did! I saw the API and knew I'd tried it, but the fact that you used the constant as the depth parameter didn't sink in. Sorry.
Eric Clayberg wrote:Use the IWorkspaceRoot getProject(String name) method. E.g.,
    ResourcesPlugin.getWorkspace().getProject("MyProject")
Thanks, I'll try that later; I'm trying a different tack, at the moment. But how do I find this information in the book? There's nothing in the index about ResourcesPlugin or IWorkspaceRoot, not that I would know to look at those anyway.
Guest
 

Postby Tony Weddle » Tue Sep 14, 2004 5:42 am

OK. I'm getting round the problem by working on the assumption that either a project needs to be selected or a particular type of file needs to be selected, to run the wizard. If it's a file, I can get the project from the selected resource (since it's an IResource). If it's a project, I've got the project right there.

My problem now is how to enforce that asssumption. For now, I have a viewerContribution for each of Navigator and Package Explorer, requiring a single selection. I'd prefer to have a single objectContribution that will filter for either a project or a particular type of file. Is this possible? Is this information in the book somewhere?

More generally, is there information in the book about how selections work? There is an objectClass parameter for the objectContribution but what classes can I choose from? In a related objectContribution, I have an objectClass of ICompilationUnit (this plug-in was inherited, so I don't know the reasoning behind it) but the code using the selection is using IFile as the class of the selected item. And StructuredSelection figures in there somewhere but I'm not sure how this is handled, though I can guess. So, is there a discussion of selections in the book?

Sorry for all the questions but, although I'm trying to work through the book sequentially, I still have to try and modify an existing plug-in and so need to try to use the book as a reference.
Tony
Tony Weddle
 
Posts: 9
Joined: Mon Sep 13, 2004 8:22 am

Postby Dan Rubel » Tue Sep 14, 2004 1:59 pm

Section 6.3 covers object contributions in general terms,
section 6.3.2.2 covers expressions specifying when that object contribution should be made visible, and
section 6.3.2.5 covers expressions specifying when that object contribution should be enabled.

Section 6.3.3 covers IObjectActionDelegate containing run(...) method that is called when the user selects your object contribution. You need to cache the selection in the seletionChanged(...) method (top of page 260) for later use in the run(...) method. (see sections 6.2.6.1 and 6.2.6.2 for more on these 2 methods)

All selections stem from the ISelection interface. In your run(...) method, you would then test to see if the selection cached by the selectionChanged(...) method is an instance of IStructuredSelection. If it is, then check that the objects contained in the selection are of the type you need to perform the operation and if so perform the operation using them.
Dan Rubel
Moderator
 
Posts: 27
Joined: Thu Oct 30, 2003 9:13 am

Postby Tony Weddle » Tue Sep 14, 2004 11:50 pm

Hi Dan,

That is excellent information, thanks. This is another example of what I was referring to in my post about the index and table of contents. I would have got to these sections eventually, as I work through the book sequentially, but it would have been difficult to find the sections with the index (which has no reference to "visibility") or the shallow table of contents (without yet knowing the full meaning of an "action").

But a quick glance at the sections you've noted tells me that they have a lot of the information I need. The thing I haven't spotted yet is information on the classes I can specify for an object contribution. What are the typical model classes that could be selected in, say, a package explorer or navigator? I've come across IFile, IResource, ICompilationUnit and JavaProject, so far. However, JavaProject is not an implementer of IProject, which caused me a problem until I found the getProject method on JavaProject, a method I didn't expect. I think adapters have a part to play here but what classes can be specified as the objectClass attribute on the various elements related to visibility?
Tony
Tony Weddle
 
Posts: 9
Joined: Mon Sep 13, 2004 8:22 am

Postby Dan Rubel » Wed Sep 15, 2004 8:13 am

Tony Weddle wrote: That is excellent information, thanks. This is another example of what I was referring to in my post about the index...


Glad it helped, and yes, I too would have liked a more comprehensive index with more cross references had I the choice. Hopefully we can improve it in an upcoming reprint. That said, it is still a good index once you know the basic terms such as "action". We have to assume that the reader has some level of familiarity with Eclipse and has had time to play with and explore the environment. Covering every detail of such a rich and powerful environment would take several books... hence the Eclipse series of which we are but one book.

Tony Weddle wrote: The thing I haven't spotted yet is information on the classes I can specify for an object contribution. What are the typical model classes that could be selected in, say, a package explorer or navigator? I've come across IFile, IResource, ICompilationUnit and JavaProject, so far. However, JavaProject is not an implementer of IProject, which caused me a problem until I found the getProject method on JavaProject, a method I didn't expect. I think adapters have a part to play here but what classes can be specified as the objectClass attribute on the various elements related to visibility?


You are correct that adapters play a key role here (see section 20.3). IResource subclasses (IFile, IProject, etc) can be adapted to their corresponding IJavaElement subclasses (ICompilationUnit, IJavaProject, etc) using the adapter interface. You probably want to make some "broad" visibility/enablement expression in the plugin manifest (e.g. visible and enabled when one or more IFile are selected) and then make a more specific enablement in the selectionChanged(...) method (e.g. check that the IFile objects in the selection can all be adapted to ICompilationUnit), calling action.setEnabled(true/false) based upon whether or not the selection is valid for your operation.
Dan Rubel
Moderator
 
Posts: 27
Joined: Thu Oct 30, 2003 9:13 am

Postby Tony Weddle » Wed Sep 15, 2004 11:02 pm

Thanks, Dan, I was wondering if I could enable/disable the action from within the code, and now I see I can. This is a good way of getting fine grained control.

Thanks again.
Tony
Tony Weddle
 
Posts: 9
Joined: Mon Sep 13, 2004 8:22 am


Return to Book: Eclipse Plug-Ins (3rd Edition)

Who is online

Users browsing this forum: No registered users and 1 guest