在Eclipse中以编程方式工作集选择

时间:2012-05-09 08:01:35

标签: eclipse working-set

我想实现以编程方式选择工作集的功能。我尝试使用以下代码:

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

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

但是上面的代码不起作用,Project Explorer也没有显示工作集。

为什么上面的代码不起作用,上面的解决方案是什么?

为了使用工作集更新ProjectExplorer视图,我尝试了下面的代码

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

ProjectExplorer pView =(ProjectExplorer)page.findView(IPageLayout.ID_PROJECT_EXPLORER); 。pView.getCommonViewer()setInput设置(WS);

上面的代码显示了ProjectExplorer中工作集的内容,但是没有保留。我的意思是一旦Eclipse重新启动,而不是工作集,所有项目都会显示出来。

2 个答案:

答案 0 :(得分:2)

这是我创建的一个示例处理程序,它可以在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;
}

但它不会重置顶级元素的视图菜单中的单选按钮。它还必须使用内部工作。

答案 1 :(得分:0)

将新工作集设置为活动页面后。您是否将项目浏览器视图更改为工作集模式?

请找到项目浏览器视图并设置模式。

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