resource change events with jobs [message #252036] |
Sat, 15 March 2008 11:05 |
Eclipse User |
|
|
|
Originally posted by: tschappi.web.de
Hi,
i want to react to resource change events in the following way: if you
move a file, for example abc.txt inside the package explorer view, all
files with the same name (but other file-extension) should be moved
automatically to the
same destination. But there is the problem that the workspace is locked
inside the resourceChanged() method. So i think i have to use
"org.eclipse.core.runtime.jobs" for
the concurrency move commands.
But unfortunately it doesn't work.
If i try to move the file abc.txt to Folder01, the job should move
abc.uml also to Folder01. But i get the following exception:
org.eclipse.core.internal.resources.ResourceException: Resource
'/TestProject/Folder01/abc.txt' does not exist
at line: "child.move(movedTo.append("/" + fName), true, null);"
I do not understand it because it should be clear that the file cannot
exist while moving. And fName has the value: abc.uml and not abc.txt!
The destination of the move-method is "movedTo.append("/" + "abc.uml")"
and not .../abc.txt.
/*
TestProject
Folder01
Folder02
- abc.txt
- abc.uml
*/
I have read the article "On the Job: The Eclipse Jobs API" and "How
You've Changed!" but i still have problems with jobs. Do i need
Sceduling Rules for my purpose, or should it work in this way ?
Thanks a lot for any help !
- Silvio
Here is the code:
protected void registerResourceChangeListener(){
final IWorkspace workspace = ResourcesPlugin.getWorkspace();
workspace.addResourceChangeListener(new IResourceChangeListener(){
public void resourceChanged(IResourceChangeEvent event){
IResourceDelta rootDelta = event.getDelta();
if (rootDelta != null) try {
rootDelta.accept(new IResourceDeltaVisitor(){
public boolean visit(IResourceDelta delta){
int flags = delta.getFlags();
IResource resource = delta.getResource();
...
if(resource.getType() == IResource.FILE){
switch(delta.getKind()){
...
case IResourceDelta.REMOVED:
if ((flags & IResourceDelta.MOVED_TO) != 0) {
final IPath movedTo = delta.getMovedToPath();
//also move all resources with the same prefix
Job job = new Job("Move-Job"){
IPath wspath =workspace.getRoot().getLocation();
IPath absolut = wspath.append(movedTo);
IFile destFile =
workspace.getRoot().getFileForLocation(absolut);
IContainer sourceFolder = file.getParent();
@Override
protected IStatus run(
IProgressMonitor monitor) {
try {
IResource[] children = sourceFolder.members();
for(IResource child : children){
if(child.getType() == IResource.FILE){
String fName = child.getName();
String fileNameWithoutExtension =
file.getName().substring(0,
file.getName().lastIndexOf("."));
boolean sameName =
fName.startsWith(fileNameWithoutExtension);
if(sameName){
// !!!! move -> here i get the exception :(
child.move(movedTo.append("/" +
fName),true, null);
}
}
}
return Status.OK_STATUS;
} catch (CoreException e) {
e.printStackTrace();
return Status.CANCEL_STATUS; }
}
};
job.setSystem(true);
job.setPriority(Job.SHORT);
job.schedule(); // start as soon as possible
}
}
}
}
};
}
|
|
|
Powered by
FUDForum. Page generated in 0.05986 seconds