我需要实现一个eclipse插件,每次使用保存文件时都会将某些内容记录到服务器上。 该文件可以是任何类型(html,js,css,java,py)。 我有以下内容:
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()");
}
}
我的倾听者
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) {
}
}
简单记录器
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*/}
}
}
}
该插件似乎无法正常工作,它永远不会被调用。有什么提示吗?
答案 0 :(得分:1)
我必须创建另一个类,并添加org.eclipse.ui.startup扩展点。
import org.eclipse.ui.IStartup;
public class StartupClass implements IStartup {
@Override
public void earlyStartup()
{
}
}
和plugin.xml代码
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension point="org.eclipse.ui.startup">
<startup class="myplugin.StartupClass" />
</extension>
</plugin>
谢谢@ greg-449