Working Set Selection Programmatically in Eclipse

懵懂的女人 提交于 2019-12-05 02:30:24

问题


I want to achieve the functionality of selecting a working set programmatically. I tried with the below code:

IWorkingSetManager wsMgr = PlatformUI.getWorkbench().getWorkingSetManager();
IWorkingSet ws = wsMgr.getWorkingSet("custom");

IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IWorkingSet[] windowset = new IWorkingSet[]{ws};
page.setWorkingSets(windowset);

But the above code does not work and the Project Explorer does not show the working set.

Why does not the above code work and what is the solution for the above?

For updating the ProjectExplorer view with a working set, I tried the below code

IWorkingSetManager wsMgr = PlatformUI.getWorkbench().getWorkingSetManager();
IWorkingSet ws = wsMgr.getWorkingSet("custom");

ProjectExplorer pView = (ProjectExplorer)page.findView(IPageLayout.ID_PROJECT_EXPLORER); pView.getCommonViewer().setInput(ws);

The above code displays the content of the working set in ProjectExplorer, but that is not persisted. I mean once Eclipse is restarted, instead of the working set, all projects are getting displayed.


回答1:


Here's an example handler I created that can set working sets programmatically in a Project Explorer and turn on top level workingsets if it's not already on:

public Object execute(ExecutionEvent event) throws ExecutionException {
    IWorkbenchWindow workbenchWindow = HandlerUtil
            .getActiveWorkbenchWindowChecked(event);
    IWorkbenchPage page = workbenchWindow.getActivePage();
    IWorkingSetManager manager = workbenchWindow.getWorkbench()
            .getWorkingSetManager();
    ProjectExplorer projExplorer = (ProjectExplorer) page
            .findView(IPageLayout.ID_PROJECT_EXPLORER);

    // This is just a test, to ensure we got hold on the correct object for
    // Project Explorer.
    // The project explorer will get focus now.
    projExplorer.setFocus();

    // Obtain list of all existing working sets.
    // This assumes that the debug workspace used have some working sets
    // prepared.
    IWorkingSet[] allExistingSets = manager.getWorkingSets();
    IWorkingSet workingSet = null;

    // The prints information about all working sets.
    for (IWorkingSet myset : allExistingSets) {
        workingSet = myset;
        IAdaptable[] elems = myset.getElements();
        System.out.println("Working set " + myset.getName() + " has "
                + elems.length + " projects.");
        for (IAdaptable elem : elems) {
            System.out.println("Working set " + myset.getName()
                    + " contains " + elem.toString());
        }
    }

    page.setWorkingSets(allExistingSets);
    NavigatorActionService actionService = projExplorer
            .getNavigatorActionService();
    CommonViewer viewer = (CommonViewer) projExplorer
            .getAdapter(CommonViewer.class);
    INavigatorContentService contentService = viewer
            .getNavigatorContentService();
    try {
        IExtensionStateModel extensionStateModel = contentService
                .findStateModel(WorkingSetsContentProvider.EXTENSION_ID);
        extensionStateModel.setBooleanProperty(
                WorkingSetsContentProvider.SHOW_TOP_LEVEL_WORKING_SETS,
                true);
        projExplorer.setRootMode(ProjectExplorer.WORKING_SETS);

        WorkingSetActionProvider provider = (WorkingSetActionProvider) getActionProvider(
                contentService, actionService,
                WorkingSetActionProvider.class);
        IPropertyChangeListener l = provider.getFilterChangeListener();
        PropertyChangeEvent pevent = new PropertyChangeEvent(this,
                WorkingSetFilterActionGroup.CHANGE_WORKING_SET, null,
                page.getAggregateWorkingSet());
        l.propertyChange(pevent);

        viewer.refresh();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

public static CommonActionProvider getActionProvider(
        INavigatorContentService contentService,
        NavigatorActionService actionService, Class cls) throws Exception {

    CommonActionProvider provider = null;
    CommonActionProviderDescriptor[] providerDescriptors = CommonActionDescriptorManager
            .getInstance().findRelevantActionDescriptors(contentService,
                    new ActionContext(new StructuredSelection()));
    if (providerDescriptors.length > 0) {
        for (int i = 0; i < providerDescriptors.length; i++) {
            provider = actionService
                    .getActionProviderInstance(providerDescriptors[i]);
            if (provider.getClass() == cls)
                return provider;
        }
    }
    return null;
}

It doesn't reset the radio button in the view menu for Top Level Elements though. It also has to use internals to work.




回答2:


After setting the new working sets into the active page. Did you change your project explorer view into working sets mode?

Please find the project explorer view and do set the mode.

ProjectExplorer projExplorer = (ProjectExplorer) page.findView(IPageLayout.ID_PROJECT_EXPLORER);
projExplorer.setRootMode(ProjectExplorer.WORKING_SETS);


来源:https://stackoverflow.com/questions/10512093/working-set-selection-programmatically-in-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!