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

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

问题描述

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

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

但是上面的代码不起作用,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天全站免登陆