在Eclipse中以编程方式工作集选择 [英] Working Set Selection Programatically in Eclipse

查看:110
本文介绍了在Eclipse中以编程方式工作集选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


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


I want to achieve the functionality of selecting a working set programatically. 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);

但上述代码不起作用,Project Explorer不显示工作集。

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?

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

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

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

上述代码显示了ProjectExplorer中工作集的内容,但不会持久化。我的意思是,一旦Eclipse重新启动,而不是工作集,所有项目都会显示。

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.

推荐答案

这是一个我创建的示例处理程序它可以在Project Explorer中以编程方式设置工作集,并且如果尚未开启顶级的工作台,则开启工作集:

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.

这篇关于在Eclipse中以编程方式工作集选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆