1.在写Eclipse插件的时候,使插件取得监听资源改变事件的能力的主要方法是
安装resouce change listener.
这个监听器必须实现IResourceChangeListener接口,并且在IWorkspace
上用addResourceChangeListener()方法注册.
如下小段代码所示:
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IResourceChangeListener listener = new IResourceChangeListener() {
public void resourceChanged(IResourceChangeEvent event) {
System.out.println("Something changed!");
}
};
workspace.addResourceChangeListener(listener);
//... some time later one ...
workspace.removeResourceChangeListener(listener);
当多个文件同时需要改变时,我们要避免让他们每个都引发IResourceChangeEvent,
强烈建议用嵌套方法,把它们放到一个方法里面,然后放到IWorkspaceRunnable的
run()方法里.这样只会引发一次事件.
如下小段代码所示:
IWorkspace workspace = ResourcesPlugin.getWorkspace();
final IProject project = workspace.getRoot().getProject("My Project");
IWorkspaceRunnable operation = new IWorkspaceRunnable() {
public void run(IProgressMonitor monitor) throws CoreException {
int fileCount = 10;
project.create(null);
project.open(null);
for (int i = 0; i < fileCount; i++) {
IFile file = project.getFile("File" + i);
file.create(null, IResource.NONE, null);
}
}
};
workspace.run(operation, null);
可以看到上面的代码里虽然改变了10个文件,但是只广播了一次事件.
只要把操作放到一起,IWorkspaceRunnable是强烈推荐的.
2.一个大型的复杂系统里,保持对用户的响应是非常重要的任务.
对于扩展性的大型程序里更加有挑战性,因为各个子系统不是用来设计和其它系统共享同一个资源的.
感谢eclipse为我们带来了一个非常好的工具:
org.eclipse.core.runtime.jobs
它可以对要同时运行的操作自动分配、执行、管理.
比如说,现在同一时刻需要运行很多任务,只需要分别产生job1,job2,job3...然后分别调用schedule()
方法就全部ok了,api会自己把它们分别运行完.
在我的财务管理程序里,当很大很大的文件要保存时,如果电脑太慢,就不能响应用户了,有了jobs工具,就可以轻松
搞定,它会把可能需要长时间运行的任务放到后台运行.
系统调用到某个job,调用它的run()方法:
例子:
class TrivialJob extends Job {
public TrivialJob() {
super("Trivial Job");
}
public IStatus run(IProgressMonitor monitor) {
System.out.println("This is a job");
return Status.OK_STATUS;
}
}
job的创建和计划如下所示:
TrivialJob job = new TrivialJob();
System.out.println("About to schedule a job");
job.schedule();
System.out.println("Finished scheduling a job");
他们的执行是和时间没关系的,输出可能如下:
About to schedule a job
This is a job
Finished scheduling a job
也可能是:
About to schedule a job
Finished scheduling a job
This is a job
如果希望某个job运行完成后在继续时,可以使用join()方法.
join()会一直阻塞到该job运行完.
例子:
TrivialJob job = new TrivialJob();
System.out.println("About to schedule a job");
job.schedule();
job.join();
if (job.getResult().isOk())
System.out.println("Job completed with success");
else
System.out.println("Job did not complete successfully");
上面的代码执行后,输出应该就是这样:
About to schedule a job
This is a job
Job completed with success
在我的程序里,我的openFile()和saveFile()两个方法用到了Job.
如下所示:
// 保存数据
public void saveData(){
saveDialog = new FileDialog(getShell(), SWT.SAVE);
saveDialog.setFilterNames(FILTER_NAMES);
saveDialog.setFilterExtensions(FILTER_EXTS);
String fileName = saveDialog.open();
final String s = Departments.getInstance().toXml();
final File file;
if (fileName != null) {
file = new File(fileName);
Job saveFile = new Job("SaveFile"){
public IStatus run(IProgressMonitor monitor) {
try {
if(!file.exists())
file.createNewFile();
else {
file.delete();
file.createNewFile();
}
StringReader in = new StringReader(s);
OutputStreamWriter out =
new OutputStreamWriter(
new BufferedOutputStream(
new FileOutputStream(file)),"UTF8");
int c;
while((c = in.read()) != -1)
out.write((char)c);
out.close();
}catch(Exception e){
e.printStackTrace();
}
return Status.OK_STATUS;
}
};
//因为无论保存数据要多少时间,我都要响应用户操作,所以schedule()后不管了.
saveFile.schedule();
setTableViewSave();
}
}
// 打开文件
public void openFile() {
openDialog = new FileDialog(getShell(), SWT.OPEN);
openDialog.setFilterNames(FILTER_NAMES);
openDialog.setFilterExtensions(FILTER_EXTS);
final String fileName = openDialog.open();
Job openFile = new Job("OpenFile"){
public IStatus run(IProgressMonitor monitor) {
if (fileName != null) {
// Open the file
//File file = new File(fileName);
Departments.getInstance().initSelf();
XmlHandler.getHandler().hand(fileName);
}
return Status.OK_STATUS;
}
};
openFile.schedule();
try {
//因为是打开文件,我必须等该操作完后才继续
//所以用了join()方法
openFile.join();
if (openFile.getResult().isOK())
ProjectList.getInstance().updateProject();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
上面两点只是我的一点点看法,一定有不对的地方,还望指点.
参考资料:
http://www.eclipse.org/articles/Article-Resource-deltas/resource-deltas.html
http://help.eclipse.org/help30/index.jsp?topic=/org.eclipse.platform.doc.isv/guide/runtime_jobs.htm