Reputation: 7403
I need to implement a plugin to eclipse that logs something to a server everytime the use saves a file. The file can be of any type (html, js, css, java, py). I have the following:
package tstsave;
import org.eclipse.ui.IStartup;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
public class Activator extends AbstractUIPlugin implements IStartup {
public static final String PLUGIN_ID = "tstsave"; //$NON-NLS-1$
private static Activator plugin;
public Activator() {
Log.log("Activator()");
}
public void start(BundleContext context) throws Exception {
super.start(context);
ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
commandService.addExecutionListener(new SaveListener());
plugin = this;
}
public void stop(BundleContext context) throws Exception {
plugin = null;
super.stop(context);
}
public static Activator getDefault() {
return plugin;
}
@Override
public void earlyStartup() {
Log.log("earlyStartup()");
}
}
My listener
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IExecutionListener;
import org.eclipse.core.commands.NotHandledException;
public class SaveListener implements IExecutionListener {
@Override
public void notHandled(String arg0, NotHandledException arg1) {
}
@Override
public void postExecuteFailure(String arg0, ExecutionException arg1) {
}
@Override
public void postExecuteSuccess(String arg0, Object arg1) {
System.out.println("test");
Log.log("postExecuteSuccess");
}
@Override
public void preExecute(String arg0, ExecutionEvent arg1) {
}
}
the simple logger
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
public class Log {
public static void log(String message){
Writer writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:\\temp\\filename.txt"), "utf-8"));
writer.write(message);
} catch (IOException ex) {
// report
} finally {
try {writer.close();} catch (Exception ex) {/*ignore*/}
}
}
}
The plugin doesn't seem to work, it is never called. Any tips?
Upvotes: 1
Views: 255
Reputation: 7403
I had to create another class and also add the org.eclipse.ui.startup extension point.
import org.eclipse.ui.IStartup;
public class StartupClass implements IStartup {
@Override
public void earlyStartup()
{
}
}
and the plugin.xml code
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension point="org.eclipse.ui.startup">
<startup class="myplugin.StartupClass" />
</extension>
</plugin>
Thanks @greg-449
Upvotes: 1