【问题标题】:How to perform an action after eclipse plugin is opened打开eclipse插件后如何执行操作
【发布时间】:2012-07-28 05:07:24
【问题描述】:

我有一个 eclipse 插件,我想在这个插件中执行某些操作,但是在 eclipse 应用程序打开之后。

我试图通过覆盖来做到这一点

public void postWindowCreate()

但启动应用程序时似乎无法进入此功能

有什么想法吗?

【问题讨论】:

    标签: eclipse eclipse-plugin eclipse-rcp


    【解决方案1】:

    你用e4吗?那么也许以下链接可能会有所帮助:http://www.eclipse.org/forums/index.php/m/886197/

    编辑:

    好的,你定义自己的应用程序吗?

    org.eclipse.ui.application.WorkbenchWindowAdvisor提供的方法是你需要的吗? (例如preWindowOpen()preWindowShellClose()postWindowRestore()postWindowCreate()、...)

    我也需要这个功能,所以我是这样做的:

    您需要 3 个类,一个实现 org.eclipse.equinox.app.IApplication 例如MyApp,一个扩展 org.eclipse.ui.application.WorkbenchAdvisor 例如MyAdvisor,一个扩展 org.eclipse.ui.application.WorkbenchWindowAdvisor 例如我的窗口顾问。

    然后在 MyApp 中你可能会调用类似

    PlatformUI.createAndRunWorkbench(display, new MyAdvisor());
    

    您实际启动工作台并提供您自己的WorkbenchWindowAdvisor 的位置。在 MyAdvisor 中,您必须覆盖:

    @Override    
    public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
        return new MyWindowAdvisor(configurer);
    }
    

    您在其中提供您的WorkbenchWindowAdvisor。在MyWindowAdvisor 类中,您最终可以覆盖适当的函数,例如

    @Override
    public void postWindowOpen() {
        //TODO
    }
    

    当然,您必须运行适当的应用程序才能使其工作;) 好的,现在,要提供任意插件来处理这些事件,您可以定义一个扩展点。

    首先你需要一个接口来定义你想听的“事件”,例如:

    public interface IWorkbenchWindowAdvisorHook
    {
        /**
         * Performs arbitrary actions before the window is opened.
         * <p>
         * This method is called before the window's controls have been created.
         * Clients must not call this method directly (although super calls are okay).
         * The default implementation does nothing. Subclasses may override.
         * Typical clients will use the window configurer to tweak the
         * workbench window in an application-specific way; however, filling the
         * window's menu bar, tool bar, and status line must be done in 
         * {@link ActionBarAdvisor#fillActionBars}, which is called immediately
         * after this method is called.
         * </p>
         */
        void preWindowOpen();
    
        /**
         * Performs arbitrary actions as the window's shell is being closed
         * directly, and possibly veto the close.
         * <p>
         * This method is called from a ShellListener associated with the window,
         * for example when the user clicks the window's close button. It is not
         * called when the window is being closed for other reasons, such as if the
         * user exits the workbench via the {@link ActionFactory#QUIT} action.
         * Clients must not call this method directly (although super calls are
         * okay). If this method returns <code>false</code>, then the user's
         * request to close the shell is ignored. This gives the workbench advisor
         * an opportunity to query the user and/or veto the closing of a window
         * under some circumstances.
         * </p>
         * 
         * @return <code>true</code> to allow the window to close, and
         *         <code>false</code> to prevent the window from closing
         * @see org.eclipse.ui.IWorkbenchWindow#close
         * @see WorkbenchAdvisor#preShutdown()
         */
        public boolean preWindowShellClose();
    
        /**
         * Performs arbitrary actions after the window has been restored, 
         * but before it is opened.
         * <p>
         * This method is called after a previously-saved window has been
         * recreated. This method is not called when a new window is created from
         * scratch. This method is never called when a workbench is started for the
         * very first time, or when workbench state is not saved or restored.
         * Clients must not call this method directly (although super calls are okay).
         * The default implementation does nothing. Subclasses may override.
         * It is okay to call <code>IWorkbench.close()</code> from this method.
         * </p>
         * 
         * @exception WorkbenchException thrown if there are any errors to report
         *   from post-restoration of the window
         */
        void postWindowRestore() throws WorkbenchException;
    
        /**
         * Performs arbitrary actions after the window has been created (possibly 
         * after being restored), but has not yet been opened.
         * <p>
         * This method is called after the window has been created from scratch, 
         * or when it has been restored from a previously-saved window.  In the latter case,
         * this method is called after <code>postWindowRestore</code>.
         * Clients must not call this method directly (although super calls are okay).
         * The default implementation does nothing. Subclasses may override.
         * </p>
         */
        void postWindowCreate();
    
        /**
         * Performs arbitrary actions after the window has been opened (possibly 
         * after being restored).
         * <p>
         * This method is called after the window has been opened. This method is 
         * called after the window has been created from scratch, or when
         * it has been restored from a previously-saved window.
         * Clients must not call this method directly (although super calls are okay).
         * The default implementation does nothing. Subclasses may override.
         * </p>
         */
        void postWindowOpen();
    
        /**
         * Performs arbitrary actions after the window is closed.
         * <p>
         * This method is called after the window's controls have been disposed.
         * Clients must not call this method directly (although super calls are
         * okay). The default implementation does nothing. Subclasses may override.
         * </p>
         */
        void postWindowClose();
    }
    

    然后是扩展点架构(将所有“YOUR-xxx”替换为您自己的包/插件名称和命名空间):

    <?xml version='1.0' encoding='UTF-8'?>
    <!-- Schema file written by PDE -->
    <schema targetNamespace="***YOUR-NAMESPACE***" xmlns="http://www.w3.org/2001/XMLSchema">
    <annotation>
          <appInfo>
             <meta.schema plugin="***YOUR-PLUGIN***" id="workbenchWindowHook" name="***YOUR-PACKAGE***.workbenchWindowHook"/>
          </appInfo>
          <documentation>
             An extension to actively hook into the WorkbenchWindowAdvisor&apos;s pre/post methods from other plug-ins.
    This is primarily intended for plug-ins that are optional or restricted to some specific products.
          </documentation>
       </annotation>
    
       <element name="extension">
          <annotation>
             <appInfo>
                <meta.element />
             </appInfo>
          </annotation>
          <complexType>
             <sequence>
                <element ref="class" minOccurs="1" maxOccurs="unbounded"/>
             </sequence>
             <attribute name="point" type="string" use="required">
                <annotation>
                   <documentation>
    
                   </documentation>
                </annotation>
             </attribute>
             <attribute name="id" type="string">
                <annotation>
                   <documentation>
    
                   </documentation>
                </annotation>
             </attribute>
             <attribute name="name" type="string">
                <annotation>
                   <documentation>
    
                   </documentation>
                   <appInfo>
                      <meta.attribute translatable="true"/>
                   </appInfo>
                </annotation>
             </attribute>
          </complexType>
       </element>
    
       <element name="class">
          <annotation>
             <documentation>
                The hook class implementing IWorkbenchWindowAdvisorHook.
             </documentation>
          </annotation>
          <complexType>
             <attribute name="name" type="string" use="required">
                <annotation>
                   <documentation>
                      The hook class implementing IWorkbenchWindowAdvisorHook.
                   </documentation>
                   <appInfo>
                      <meta.attribute kind="java" basedOn=":***YOUR-PACKAGE***.IWorkbenchWindowAdvisorHook"/>
                   </appInfo>
                </annotation>
             </attribute>
          </complexType>
       </element>
    
       <annotation>
          <appInfo>
             <meta.section type="since"/>
          </appInfo>
          <documentation>
          </documentation>
       </annotation>
    </schema>
    

    然后,在您的 MyWindowAdvisor 中,您需要保留对扩展的引用

    // the reference list
    private List<IWorkbenchWindowAdvisorHook> hooks = new ArrayList<IWorkbenchWindowAdvisorHook>();
    

    加载/初始化扩展

    //code for initializing the extensions, must be called in the constructor
    private void initExtensions()
    {
        IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(
                IWorkbenchWindowAdvisorHook.ID);
        for(IConfigurationElement element : config)
        {
            try
            {
                final Object o = element.createExecutableExtension("name"); //$NON-NLS-1$
                if(o instanceof IWorkbenchWindowAdvisorHook)
                {
                    hooks.add((IWorkbenchWindowAdvisorHook)o);
                }
            }
            catch(CoreException e)
            {
                e.printStackTrace();
            }
        }
    }
    

    并在每个“事件”函数中调用扩展的方法:

    // example method preWindowOpen()
    public void preWindowOpen()
    {
        for(IWorkbenchWindowAdvisorHook hook : hooks)
        {
            try
            {
                hook.preWindowOpen();
            }
            catch(Throwable t)
            {
                CorePlugin.logDefaultError(t);
            }
        }
    }
    

    最后一步是在您需要监听这些工作台窗口事件的每个插件中提供扩展和类。

    【讨论】:

    • 不,我使用靛蓝。所以这并没有解决我的问题无论如何谢谢
    猜你喜欢
    • 1970-01-01
    • 2014-01-06
    • 1970-01-01
    • 2011-09-26
    • 2011-03-19
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多