Change text on button in eclipse header on the fly

I am creating a view in eclipse to display a stack trace, and I want the view to be accessible using the button in the header. Therefore, I create the extension using the plugin.xml framework, as it 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 text. However, I want to increase the number in the button with each trace that I collect in the listener. Basically, I just need to be able to edit the button in the header in the Java file, and not in the plugin.xml file. But I do not know how to do it! I looked at the eclipse SDK help but found nothing. Please, help!

I am also open to any other way to do this in an eclipse-based application, as long as the text changes and clicking on the text opens the view.

+3
source share
1 answer

I believe headlines use icons, not sure if you can use text. You can add an entry to the context menu (popup), in which the number of traces will be displayed, and when the element is clicked, start your viewing.

I based this on the Hello, World Command template that comes with Eclipse, and added input to the menu.

I expanded CompoundContributionItem to act as a menu tab as follows.


/**
 * Menu contribution which will update it 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();
    }
}

, .

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>
+1

Source: https://habr.com/ru/post/1711132/


All Articles