Hook to Save Action in Eclipse plugin [closed]

邮差的信 提交于 2019-12-19 03:11:24

问题


I want to create a Google Closure Compiler plugin for Eclipse. I already have a popup menu entry to compile a JavaScript file to its minified version. But it would be more than helpful if every time you save a *.js that minified version would be generated automatically. I read/heard about natures and builders, extension points and IResourceChangeListener. But I did not manage to figure out what I should use and especially how to get it to work.

Is there a working example of a plugin that does "the same kind of thing" so I can work from that or a tutorial to write such?

With the answer below I searched for projects that use the IResourceChangeListener and came up with this code:

Manifest: http://codepaste.net/3yahwe

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

Activator: http://codepaste.net/s7xowm

DummyStartup: http://codepaste.net/rkub82

MinifiedJavascriptUpdater: http://codepaste.net/koweuh

There in the MinifiedJavascriptUpdater.java which holds the code for the IResourceChangeListener the resourceChanged() function is never reached.


回答1:


Answer from here http://www.eclipse.org/forums/index.php/t/362425/

Solution is to get the code into the activator and get rid of the 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:


You want a builder for this. Eclipse has extensive support for just what you want to do, the notion of generated artifacts that need to be maintained as things change. This Paper will get you started (even though it's very old, it's completely accurate).

All of the language plugins (JDT, CDT, etc) do this sort of thing when they compile code.



来源:https://stackoverflow.com/questions/10851280/hook-to-save-action-in-eclipse-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!