Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Custom property page does not work for compilation units in Package Explorer
Custom property page does not work for compilation units in Package Explorer [message #534392] Tue, 18 May 2010 20:59 Go to next message
Eclipse UserFriend
I'm developing a plugin that adds a custom property page to all Java files. My extension code looks like this:

   <extension point="org.eclipse.ui.propertyPages"> 
        <page 
            id="com.example.MyPropertyPage" 
            name="My Property Page" 
            class="com.example.MyPropertyPage" 
            nameFilter="*.java"> 
            <enabledWhen>
             <instanceof
                  value="org.eclipse.core.resources.IFile">
             </instanceof>
          </enabledWhen>
        </page> 
    </extension>


It works for *.java files in the Package Explorer but only if they're located at the root of the project. It does not work for files in the source directory. Eclipse simply does not show my property page for those files.

"The Java Developer's Guide to Eclipse" (Chapter 16) offers a possible explanation:

Quote:

The Package Explorer view does not display files but displays objects (IJavaElement) as defined in JDT's Java model. For example, what appears to be a simple .java file (an instance of IFile) in the Package Explorer view is actually part of the JDT model, an instance of ICompilationUnit.



Indeed, my extension is enabled for instances of IFile, not ICompilationUnit, which explains the behavior I am seeing.

The book goes on to say:

Quote:

If you only wanted your page to be added to the Properties dialog opened from the Package Explorer view, the objectClass attribute would identify org.eclipse.jdt.core.ICompilationUnit.



Sure enough, when I change my extension's instanceof value from IFile to ICompilationUnit, the property page is suddenly enabled for files in the source directory (but of course no longer for those in the root directory).

However, when I click on my property page, an exception is thrown because my property page class calls getElement(), which now returns an instance of type ICompilationUnit, instead of the expected IResource.

So... what now? How can I get my property page to work for compilation units in the Package Explorer?

(The book goes on to mention something about setting the extension page's "adaptable" attribute to true, but this had no effect for me.)
Re: Custom property page does not work for compilation units in Package Explorer [message #534954 is a reply to message #534392] Thu, 20 May 2010 14:56 Go to previous messageGo to next message
Eclipse UserFriend
On 5/19/2010 6:29 AM, Trevor Harmon wrote:

> However, when I click on my property page, an exception is thrown
> because my property page class calls getElement(), which now returns an
> instance of type ICompilationUnit, instead of the expected IResource.
>

Can you try calling ICompilationUnit.getResource() ?
Re: Custom property page does not work for compilation units in Package Explorer [message #534973 is a reply to message #534954] Thu, 20 May 2010 16:51 Go to previous message
Eclipse UserFriend
Deepak Azad wrote on Thu, 20 May 2010 14:56
Can you try calling ICompilationUnit.getResource() ?


Hmm, no, that would be too easy... Rolling Eyes

Seriously, thanks for the suggestion; it works perfectly! I now have the following helper function to get the resource:

private IResource getResource() {
    if (getElement() instanceof ICompilationUnit) {
        ICompilationUnit compilationUnit = (ICompilationUnit) getElement();
        return compilationUnit.getResource();
    } else {
        return (IResource) getElement();
    }
}


I also split up the property page extension so that both cases (IFile and ICompilationUnit) are handled:

<extension point="org.eclipse.ui.propertyPages"> 
    <page 
        id="com.example.CompilationUnitPropertyPage" 
        name="My Property Page" 
        class="com.example.ModulePropertyPage" 
        nameFilter="*.java"> 
        <enabledWhen>
            <instanceof
                value="org.eclipse.jdt.core.ICompilationUnit">
            </instanceof>
        </enabledWhen>
    </page>
    <page 
        id="com.example.FilePropertyPage" 
        name="My Property Page" 
        class="com.example.ModulePropertyPage" 
        nameFilter="*.java"> 
        <enabledWhen>
            <instanceof
                value="org.eclipse.core.resources.IFile">
            </instanceof>
        </enabledWhen>
    </page>
</extension>


At least, I assume this is the right way to support both files and compilation units. If there's a better/easier way, I'm all ears. Thanks again for your help!
Previous Topic:[Formatter] Function calls don't honor indentation settings?
Next Topic:Running Java app - howto show java command line which is excecuted.
Goto Forum:
  


Current Time: Fri Mar 21 20:31:06 EDT 2025

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

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

Back to the top