在Eclipse插件中保存动作的钩子

时间:2012-06-01 13:33:17

标签: java eclipse plugins save

我想为Eclipse创建一个Google Closure Compiler插件。我已经有一个弹出菜单项,可以将JavaScript文件编译为其缩小版本。但是,如果每次保存*.js,那么自动生成缩小版本将会非常有用。我读/听过自然和建设者,延伸点和IResourceChangeListener。但我没有设法弄清楚我应该使用什么,特别是如何让它发挥作用。

是否有一个插件的工作示例可以执行“相同类型的操作”,因此我可以使用该插件或教程来编写这样的插件?

通过以下答案,我搜索了使用IResourceChangeListener的项目,并提出了以下代码:

清单:http://codepaste.net/3yahwe

plugin.xmlhttp://codepaste.net/qek3rw

激活器:http://codepaste.net/s7xowm

DummyStartup:http://codepaste.net/rkub82

MinifiedJavascriptUpdater:http://codepaste.net/koweuh

MinifiedJavascriptUpdater.java中包含IResourceChangeListener的代码,resourceChanged()函数永远不会到达。

2 个答案:

答案 0 :(得分:5)

从这里回答http://www.eclipse.org/forums/index.php/t/362425/

解决方案是将代码放入激活器并删除MinifiedJavascriptUpdater

package closure_compiler_save;

import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin {

    // The plug-in ID
    public static final String PLUGIN_ID = "closure-compiler-save"; //$NON-NLS-1$

    // The shared instance
    private static Activator plugin;

    /**
     * The constructor
     */
    public Activator() {
    } //gets here

    @Override
    public void start(BundleContext context) throws Exception {
        super.start(context);
        Activator.plugin = this;

        ResourcesPlugin.getWorkspace().addResourceChangeListener(new IResourceChangeListener() {
            public void resourceChanged(IResourceChangeEvent event) {
                System.out.println("Something changed!");
            }
        });
    }

    @Override
    public void stop(BundleContext context) throws Exception {
        Activator.plugin = null;
        super.stop(context);
    }

    /**
     * Returns the shared instance
     *
     * @return the shared instance
     */
    public static Activator getDefault() {
        return plugin;
    }
}

答案 1 :(得分:1)

你想要一个建设者。 Eclipse广泛支持您想要做的事情,生成的工件的概念需要在事情发生变化时进行维护。 This Paper会让你开始(虽然它很老,但它完全准确)。

所有语言插件(JDT,CDT等)在编译代码时都会这样做。

相关问题