Eclipse插件保存命令sucess [英] Eclipse plugin save command sucess
本文介绍了Eclipse插件保存命令sucess的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要实现一个插件,eclipse每次使用保存文件时,都会向服务器记录一些东西。
该文件可以是任何类型(html,js,css,java,py)。
我有以下内容:
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()");
}
}
我的收听者
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*/}
}
}
}
插件似乎不工作,它从来没有被称为。任何提示?
The plugin doesn't seem to work, it is never called. Any tips?
推荐答案
我不得不创建另一个类,并添加org.eclipse.ui.startup扩展点。 p>
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()
{
}
}
和plugin.xml代码
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>
感谢@ greg-449
Thanks @greg-449
这篇关于Eclipse插件保存命令sucess的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文