views:

318

answers:

1

I'm creating a view in eclipse to display stack traces, and I want to have the view accessible from a button in the header. So I'm creating an extension using the plugin.xml framework, like this basically:

<extension
    point="org.eclipse.ui.commands">
  <command
        categoryId="com.commands.category"
        id="commands.tracing"
        name="0 Traces">
  </command>
 </extension>

This will create a button with "0 traces" as the text. However, I want to increment the number in the button with each trace that I collect in a listener. Basically I just need to be able to edit the button in the header in a Java file, rather than in the plugin.xml file. But I have no idea how to do this! I've looked through the eclipse SDK help but haven't found anything. Please help!

I'm also open to any other way to make this happen in an application built on eclipse, so long as the text changes and clicking the text opens a view.

+1  A: 

I believe the headers use icons, not sure if you can use text. You could add an entry to the context (popup) menu which will display the number of traces and when the item is clicked launch your view.

I based this off of the Hello, World Command template that comes with Eclipse, and added a menu contribution.

I extended CompoundContributionItem to act as the menu contribution as follows.


/**
 * Menu contribution which will update it's label to show the number of current
 * traces.
 */
public class TraceWindowContributionItem extends CompoundContributionItem {
    private static int demoTraceCount = 0;
    /**
     * Implemented to create a label which displays the number of traces.
     */
    @Override
    protected IContributionItem[] getContributionItems() {

        //Here is your dynamic label.
     final String label = getTraceCount() + " traces";

        //Create other items needed for creation of contribution.
        final String id = "test.dynamicContribution";
     final IServiceLocator serviceLocator = findServiceLocator();
     final String commandId = "test.commands.sampleCommand";
     final Map parameters = new HashMap();

     //Here is the contribution which will be displayed in the menu.
     final CommandContributionItem contribution = new CommandContributionItem(
       serviceLocator, id, commandId, parameters, null, null, null,
       label, null, null, SWT.NONE);

     return new IContributionItem[] { contribution };
    }

    /**
     * Demo method simply increments by 1 each time the popup is shown. Your
     * code would store or query for the actual trace count each time.
     * @return
     */
    private int getTraceCount() {
     return demoTraceCount++;
    }

    /**
     * Find the service locator to give to the contribution.
     */
    private IServiceLocator findServiceLocator() {
     return PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    }
}

Now we need to wire this into the command and handler that will launch your view.

Here is my plugin.xml.


<!-- Wire up our dynamic menu contribution which will show the number of traces -->
<extension
     point="org.eclipse.ui.menus">
  <menuContribution
        locationURI="popup:org.eclipse.ui.popup.any?after=additions">
     <dynamic
           class="test.TraceWindowContributionItem"
           id="test.dynamicContribution">
     </dynamic>
  </menuContribution>
</extension>
<!-- Command and handler for launching the traces view -->
<extension
     point="org.eclipse.ui.commands">
  <category
        name="Sample Category"
        id="test.commands.category">
  </category>
  <command
        categoryId="test.commands.category"
        defaultHandler="test.handlers.SampleHandler"
        id="test.commands.sampleCommand"
        name="Sample Command">
  </command>
</extension>
<extension
     point="org.eclipse.ui.handlers">
  <handler
        commandId="test.commands.sampleCommand"
        class="test.handlers.SampleHandler">
  </handler>
</extension>
HonkHonkHonk