站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Eclipse 专题Eclipse资源改变通知机制

Eclipse资源改变通知机制

摘要: Eclipse资源改变通知机制

eclipse资源改变通知机制

请参见原文:

http://www.eclipse.org/articles/Article-Resource-deltas/resource-deltas.html<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Resource Change Listener

接口:IresourceChangeListener

<table style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt" cellSpacing=0 cellPadding=0 border=1> <tr> <td style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 426.1pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent" vAlign=top width=568>

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);

</td></tr></table>

在资源改变的通知期间,workspace会locked来避免产生更多的通知。

Resource API

Resource API在执行creating,copying,moving和deleting操作时会向外广播资源改变事件。

资源操作会嵌套,如Ifile.move操作会触发一个Ifile.create操作创建新的文件,然后再触发一个Ifile.delete操作删除老文件。所以一个Ifile.move操作嵌套了一个Ifile.create操作和一个Ifile.delete操作,但是以上操作只会通知一次

Batching Changes

当有一批资源改变事件需要通知时,需要采用Batching的方式来提高性能。这样能保证只有一个资源改变事件被广播了出去,而不是多个。

<table style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt" cellSpacing=0 cellPadding=0 border=1> <tr> <td style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 426.1pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent" vAlign=top width=568>

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);

</td></tr></table>

eclipse 3.0不再保证IworkspaceRunnable它在执行期间会阻止其他的通知发生。这样做的原因是为了保证能够在通知发生期间能够使UI相应用户事件。

eclipse 3.0中引入了一个新类WorkspaceJob,可以将大量的Workspace资源改变操作放到一个Job中在后台执行。

一个更强大的特点当资源在Workspace外部被更改ResourceChangeListener也会被通知到,比如直接对文件系统的文件进行更改。由于大多数的操作系统并没有这种机制,所以需要执行Iresource.refreshLocal操作后,资源改变会通知所有的Listener.

IResourceChangeEvent

l Event Type

PRE_CHANGE,POST_CHANGE,PRE_BUILD,POST_BUILD,PRE_CLOSE,POST_CLOSE,PRE_DELETE,POST_DELETE

l IresourceDelta

IresourceDelta.getKind()

IResourceDelta.ADDED

IResourceDelta.REMOVED

IResourceDelta.CHANGED

IresourceDelta.getFlags()

IResourceDelta.CONTENT

IResourceDelta.REPLACED

IResourceDelta.REMOVED

IResourceDelta.MOVED_FROM

IresourceDelta.getMovedFromPath()

IResourceDelta.MOVED_TO

IresourceDelta.getMovedToPath()

IResourceDelta.MARKERS

(IMarkerDelta[] markers = delta.getMarkerDeltas())

Listener的性能

Resource Change Listener的实现应该是轻量级的,并且能够快速执行。资源改变的通知会被经常的发出,如果Listener的性能不高,将会影响整个平台的性能。如果在Listener中有大量的工作需要做,最好将它放到后台线程中执行。

使用IResourceDelta.findMember(IPath)可以快速定位到我们关注的IresourceDelta,而不必按树状结构依次向下通知。

IresourceDelta.accept(IResourceDeltaVistor)来访问我们关心的ResourceDeltaVistor树的分支。

线程安全问题:我们不能控制我们的Listener会在哪一个线程中执行。Workspace操作会出现在任何的线程中,Resource Change Listener也会运行在触发该操作的任何线程中。如果我们想让我们的更新操作出现在一个特定的线程中,我们不得不确保代码被post到那个线程。通常我们要更新用户界面,就先得到UI的Display对象,使用Display.syncExec()或者Display.asyncExec()方法。

如果采用异步方式执行,需要注意ResourceDelta都有一个“expire date”.如果传递一个IresourceDelta到另外一个线程,如果Listener的ResourceChanged()方法已经在另外一个线程中返回,对IresourceDelta的引用将会发生错误。请确保Listener不会一直保存Resource Delta的引用,因为这些Resource Delta的数量是很大的,如果一直保持对其引用,会导致memory leak.

Sample

<table style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt" cellSpacing=0 cellPadding=0 border=1> <tr> <td style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 426.1pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent" vAlign=top width=568>

public class DocIndexUpdater implements IResourceChangeListener {

private TableViewer table; //assume this gets initialized somewhere

private static final IPath DOC_PATH = new Path("MyProject/doc");

public void resourceChanged(IResourceChangeEvent event) {

//we are only interested in POST_CHANGE events

if (event.getType() != IResourceChangeEvent.POST_CHANGE)

return;

IResourceDelta rootDelta = event.getDelta();

//get the delta, if any, for the documentation directory

IResourceDelta docDelta = rootDelta.findMember(DOC_PATH);

if (docDelta == null)

return;

final ArrayList changed = new ArrayList();

IResourceDeltaVisitor visitor = new IResourceDeltaVisitor() {

public boolean visit(IResourceDelta delta) {

//only interested in changed resources (not added or removed)

if (delta.getKind() != IResourceDelta.CHANGED)

return true;

//only interested in content changes

if ((delta.getFlags() & IResourceDelta.CONTENT) == 0)

return true;

IResource resource = delta.getResource();

//only interested in files with the "txt" extension

if (resource.getType() == IResource.FILE &&

"txt".equalsIgnoreCase(resource.getFileExtension())) {

changed.add(resource);

}

return true;

}

};

try {

docDelta.accept(visitor);

} catch (CoreException e) {

//open error dialog with syncExec or print to plugin log file

}

//nothing more to do if there were no changed text files

if (changed.size() == 0)

return;

//post this update to the table

Display display = table.getControl().getDisplay();

if (!display.isDisposed()) {

display.asyncExec(new Runnable() {

public void run() {

//make sure the table still exists

if (table.getControl().isDisposed())

return;

table.update(changed.toArray(), null);

}

});

}

}

}

</td></tr></table>



↑返回目录
前一篇: XSWT for Eclipse form layout
后一篇: [Eclipse图解]利用Manage Configurationn轻松安装eclipse 插件