【问题标题】:Hook to Save Action in Eclipse plugin [closed]在 Eclipse 插件中保存操作的挂钩 [关闭]
【发布时间】:2012-06-06 18:05:36
【问题描述】:

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

是否有一个插件的工作示例可以执行“相同类型的事情”,以便我可以使用它或教程来编写这样的东西?

根据下面的答案,我搜索了使用IResourceChangeListener 的项目并想出了以下代码:

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

plugin.xml:http://codepaste.net/qek3rw

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

虚拟启动:http://codepaste.net/rkub82

MinifiedJavascriptUpdater:http://codepaste.net/koweuh

在包含IResourceChangeListener 代码的MinifiedJavascriptUpdater.java 中,永远无法访问resourceChanged() 函数。

【问题讨论】:

标签: java eclipse plugins save


【解决方案1】:

从这里回答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;
    }
}

【讨论】:

    【解决方案2】:

    你需要一个建造者来做这件事。 Eclipse 为您想要做的事情提供了广泛的支持,即随着事情的变化需要维护生成的工件的概念。 This Paper 将帮助您入门(尽管它很旧,但完全准确)。

    所有语言插件(JDT、CDT 等)在编译代码时都会做这种事情。

    【讨论】:

    • 我已经读过那篇文章,虽然里面有“试试这个”的例子。在我的经验阶段,我没有设法让它发挥作用。不幸的是,它缺少一个完整的工作示例。
    • 你可能想用谷歌搜索IResourceChangeListener,它会告诉你使用它的插件。我已经能够让它在我的 RCP 应用程序中工作,我相信你会在开源世界中找到类似的东西。
    • 您在哪里可以通过 Google 找到它?我没有。
    • 谢谢,我还以为他们下线了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    相关资源
    最近更新 更多