欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

RCP(二)

程序员文章站 2022-06-14 15:31:52
...

1.在RCP应用中,我们或多或少会依赖一些eclipse官方或第三方的插件,而这些插件或者它们依赖的插件很有可能有自己的UI。这样就会造成在某些位置会出现一些不该出现的组件,为了删除这些组件,我们通常会这样做:

        getWindowConfigurer().getActionBarConfigurer().getCoolBarManager().removeAll();
        getWindowConfigurer().getActionBarConfigurer().getCoolBarManager().update(true);
        getWindowConfigurer().getActionBarConfigurer().getMenuManager().removeAll();
        getWindowConfigurer().getActionBarConfigurer().getMenuManager().update(true);
 在GsbpWorkbenchWindowAdvisor类中的postWindowOpen方法里面对菜单或者工具栏进行删除,这样做比较麻烦的是,我们通常不是希望删除所有。

 

2.无意中在网上发现了这样一个思路,就是删除某些UI扩展点的扩展,这样也可以把不该出现的组件删掉。本人比较赞同这种方式,感觉删除扩展点会删除整块UI而不是某个菜单,这样就不容易漏掉,包括像向导,view,Editor,透视图等都可以移除。如果有必要,还可以通过删除扩展点的方式删除不必要的功能。

虽然网上提供了这种思路,但是具体的实现并不详细,所有自己写了一个类似删除的工具类,此类只提供了删除,是因为增加一般都是通过直接扩展的方式实现的。

 

public class ExtensionUtil
{
    public static final String Registry_Type_Perspective = "Perspective";

    public static final String Registry_Type_ActionSet = "ActionSet";

    public static final String Registry_Type_Editor = "Editor";

    public static final String Registry_Type_ExportWizard = "ExportWizard";

    public static final String Registry_Type_ImportWizard = "ImportWizard";

    public static final String Registry_Type_NewWizard = "NewWizard";

    public static final String Registry_Type_View = "View";

    /**
     * 暂时没有必要实现
     */
    public static final String Registry_Type_Theme = "Theme";

    public static final String Registry_Type_WorkingSet = "WorkingSet";
    
    public static final String Registry_Type_Intro = "Intro";
    
    
    

    /**
     * 删除ActionSet扩展,根据其id
     * 
     * @param id
     * @return
     */
    public static boolean removeActionSetRegistrys(List<String> list)
    {
        ActionSetRegistry registry = WorkbenchPlugin.getDefault()
                .getActionSetRegistry();
        IActionSetDescriptor[] descriptors = registry.getActionSets();
        for (IActionSetDescriptor descriptor : descriptors)
        {
            if (StringUtil.existInList(list, descriptor.getId()))
            {
                IExtension extension = descriptor.getConfigurationElement()
                        .getDeclaringExtension();
                registry.removeExtension(extension, new Object[]
                { descriptor });
                list.remove(descriptor.getId());
            }
        }

        return true;
    }

    /**
     * 删除View扩展,根据其id
     * 
     * @param id
     * @return
     */
    public static boolean removeViewRegistrys(List<String> list)
    {
        ViewRegistry registry = (ViewRegistry) WorkbenchPlugin.getDefault()
                .getViewRegistry();
        IViewDescriptor[] descriptors = registry.getViews();
        for (IViewDescriptor descriptor : descriptors)
        {
            ViewDescriptor vDescriptor = (ViewDescriptor) descriptor;
            if (StringUtil.existInList(list, vDescriptor.getId()))
            {
                IExtension extension = vDescriptor.getConfigurationElement()
                        .getDeclaringExtension();
                registry.removeExtension(extension, new Object[]
                { descriptor });
            }
        }

        return true;
    }

    /**
     * 删除Perspective扩展,根据其id
     * 
     * @param id
     * @return
     */
    public static boolean removePerspectiveRegistrys(List<String> list)
    {
        PerspectiveRegistry registry = (PerspectiveRegistry) WorkbenchPlugin
                .getDefault().getPerspectiveRegistry();
        IPerspectiveDescriptor[] descriptors = registry.getPerspectives();// PerspectiveDescriptor
        for (IPerspectiveDescriptor descriptor : descriptors)
        {
            PerspectiveDescriptor pDescriptor = (PerspectiveDescriptor) descriptor;
            if (StringUtil.existInList(list, pDescriptor.getId()))
            {
                IExtension extension = pDescriptor.getConfigElement()
                        .getDeclaringExtension();
                registry.removeExtension(extension, new Object[]
                { descriptor });
            }
        }

        return true;
    }
    
    /**
     * 删除Editor扩展,根据其list中的id
     * 
     * @param id
     * @return
     */
    public static boolean removeEditorRegistrys(List<String> list)//String id
    {
        EditorRegistry registry = (EditorRegistry)WorkbenchPlugin.getDefault().getEditorRegistry();
        IEditorDescriptor[] descriptors = registry.getSortedEditorsFromPlugins();
        for(IEditorDescriptor descriptor: descriptors){
            EditorDescriptor eDescriptor = (EditorDescriptor)descriptor;
            if (StringUtil.existInList(list, eDescriptor.getId()))
            {
            IExtension extension = eDescriptor.getConfigurationElement().getDeclaringExtension();
            registry.removeExtension(extension, new Object[]
                                                         { descriptor });
            }
        }
        
        return true;
    }

    /**
     * 删除ImportWizard扩展,根据其id
     * 
     * @param id
     * @return
     */
    public static boolean removeWizardRegistrys(String type,List<String> list)// String id
    {
        WorkbenchPlugin wPlugin = WorkbenchPlugin.getDefault();
        IWizardRegistry registry = null;

        if (Registry_Type_ExportWizard.equals(type))
        {
            registry = wPlugin.getExportWizardRegistry();
        } else if (Registry_Type_ImportWizard.equals(type))
        {
            registry = wPlugin.getImportWizardRegistry();
        } else if (Registry_Type_NewWizard.equals(type))
        {
            registry = wPlugin.getNewWizardRegistry();
        }

        if (wPlugin == null)
        {
            return false;
        }

        IWizardCategory root = registry.getRootCategory();

        segmentRemoveWizardRegistrys(registry, root,list);

        return true;
    }

    private static void segmentRemoveWizardRegistrys(
            IWizardRegistry registry, IWizardCategory root,List<String> list)
    {
        IWizardDescriptor[] descriptors = root.getWizards();
        for (IWizardDescriptor descriptor : descriptors)
        {
            if (StringUtil.existInList(list, descriptor.getId()))
            {
                WorkbenchWizardElement wizard = (WorkbenchWizardElement) descriptor;
                IExtension extension = wizard.getConfigurationElement()
                        .getDeclaringExtension();

                if (registry instanceof ImportWizardRegistry)
                {
                    ((ImportWizardRegistry) registry).removeExtension(extension,
                            new Object[]
                            { descriptor });
                } else if (registry instanceof ExportWizardRegistry)
                {
                    ((ExportWizardRegistry) registry).removeExtension(extension,
                            new Object[]
                            { descriptor });
                } else if (registry instanceof NewWizardRegistry)
                {
                    ((NewWizardRegistry) registry).removeExtension(extension,
                            new Object[]
                            { descriptor });
                }
            }
        }

        IWizardCategory[] categorys = root.getCategories();
        for (IWizardCategory category : categorys)
        {
            segmentRemoveWizardRegistrys(registry, category,list);
        }
    }

}