Choosing a working set programmatically in Eclipse

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

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 code above does not work, and Project Explorer does not show the working set.

Why is the above code not working and what is the solution for the above?

To update the ProjectExplorer view with the working set, I tried the following 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 contents of the working set in ProjectExplorer, but it is not saved. I mean, after restarting Eclipse, all projects are displayed instead of the working set.

+3
source share
2 answers

, , Project Explorer , :

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;
}

reset . .

+2

. ?

, .

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

All Articles