Eclipse Search

Loading

May 16, 2009

Creating a custom Property View

In an RCP application, you might need a Properties View, which shows only the properties of a specific view or a set of views. But the generic Properties View will show from all the other views that support it. Armed up with the knowledge of how a PageBookView works, lets see how to hack the Properties View to listen only to a specific view.

The isImportant() method is the one which decides whether to create an IPage for the specific IWorkbenchPart or not. The idea is to override that method and return false for all the workbenchPart that we are not interested in. Lets create the view first:

<view
            class="com.eclipse_tips.views.CustomPropertiesView"
            icon="icons/sample.gif"
            id="com.eclipse-tips.views.customePropertiesView"
            name="My Properties View">
</view>

The CustomPropertiesView should extend PropertySheet and override the isImportant():

public class CustomPropertiesView extends PropertySheet {

 @Override
 protected boolean isImportant(IWorkbenchPart part) {
  if (part.getSite().getId().equals(IPageLayout.ID_PROJECT_EXPLORER))
   return true;
  return false;
 }
}

In this case, I'm making the view only to respond to Project Explorer and ignore other views. Here is the CustomPropertyView in action:
 
 

If you are on 3.5 or above, you would see the Pin Action in your Custom Properties View. If you don't want the Pin action in your properties view, there is no way to prevent the PropertySheet to adding the action. The action is added to both tool bar and menu in the createControl() method. Only way to get rid of the action is to remove it after the PropertySheet adds it:

@Override
 public void createPartControl(Composite parent) {
  
  super.createPartControl(parent);
  IMenuManager menuManager = getViewSite().getActionBars().getMenuManager();
  IContributionItem[] items = menuManager.getItems();
  for (IContributionItem iContributionItem : items) {
   if(iContributionItem instanceof ActionContributionItem) {
    if(((ActionContributionItem) iContributionItem).getAction() instanceof PinPropertySheetAction) {
     menuManager.remove(iContributionItem);
     break;
    }
   }
  }

  IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager();
  items = toolBarManager.getItems();
  for (IContributionItem iContributionItem : items) {
   if(iContributionItem instanceof ActionContributionItem) {
    if(((ActionContributionItem) iContributionItem).getAction() instanceof PinPropertySheetAction)) {
     toolBarManager.remove(iContributionItem);
     break;
    }
   }
  }
 }

And don't forget to override the isPinned() method:

@Override
 public boolean isPinned() {
  return false;
 }

There you go:

11 comments:

  1. Great, but how do you make it show properties for a specific view even if that view is not selected? (Provided it's active and visible, ideally)
    ReplyDelete
  2. You have to override the methods and make sure that your specific view is the one that supplies PropertySource
    ReplyDelete
  3. Thanks. This was very helpful
    ReplyDelete
  4. Hi Prakash,

    This is a very informational topic. I'm trying to add a custom action to the properties view. I'm extending the viewActions, but couldn't see the icon on the Properties tool bar. In debug mode, I see that the class that I'm using for viewActions is being initiated. Not sure where I'm going wrong. Can you throw some light in here?

    Thanks and Regards,
    Murali
    ReplyDelete
  5. @Murali,
    You might be missing something. It works fine for me:

    <viewContribution
    id="com.eclipse-tips.openProject.viewContribution"
    targetID="org.eclipse.ui.views.PropertySheet">
    <action
    class="viewactiondelegate.ViewActionDelegate1"
    icon="icons/sample.gif"
    id="com.eclipse-tips.openProject.action1"
    label="My Action"
    menubarPath="additions"
    style="push"
    toolbarPath="additions">
    </action>
    </viewContribution>
    </extension>

    Probably the menubarPath & toolbarPath?
    ReplyDelete
  6. Thanks for the quick reply, Prakash. I'm not sure what the problem was, but I am able to see the icon now and the action is working pretty well. Its not the issue with menubarpath and toolbarPath, for sure. Well, do you have any suggestion to read the entries in the properties view? I see that it invokes the getPropertyDescriptors from the IPropertySource, and populates the view. But can we access those details once it is populated?

    Thanks again!
    Murali
    ReplyDelete
  7. In my application I have multiple editors. One editor uses the GEF palette. However when focus moves to the GEF editor I do not want selection to change in the properties view. Is there a way to tell the properties view to 'ignore GEF palette?.. Basically when focus is on the palette just show the properties for the currently selected item in the editor.
    Thanks
    ReplyDelete
  8. @Dan,
    I suppose you need this: http://blog.eclipse-tips.com/2009/09/excluding-view-from-properties-view.html
    ReplyDelete
  9. @Prakash
    This would be ideal, but my product is running on 3.4.2 and I do not see us upgrading from this within the next few months.
    How would the same be accomplished on 3.4.x?

    Thanks
    ReplyDelete
  10. @Dan,as a policy, no new apis will be added to the service releases. moreover, 3.4 is out of support. you can either port the change yourself and use that custom version of workbench or try to have your own properties view.
    ReplyDelete
  11. Could you please tell me, how to show the "Value" in Properties View in multi-line table format.
    ReplyDelete